]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarObjects/templates/person/Person.php
Release 6.5.15
[Github/sugarcrm.git] / include / SugarObjects / templates / person / Person.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 require_once('include/SugarObjects/templates/basic/Basic.php');
39
40 class Person extends Basic
41 {
42     var $picture;
43     /**
44      * @var bool controls whether or not to invoke the getLocalFormatttedName method with title and salutation
45      */
46     var $createLocaleFormattedName = true;
47
48     /**
49      * @var Link2
50      */
51     public $email_addresses;
52
53         public function Person()
54         {
55                 parent::Basic();
56                 $this->emailAddress = new SugarEmailAddress();
57         }
58
59         /**
60          * need to override to have a name field created for this class
61          *
62          * @see parent::retrieve()
63          */
64     public function retrieve($id = -1, $encode=true, $deleted=true) {
65                 $ret_val = parent::retrieve($id, $encode, $deleted);
66                 $this->_create_proper_name_field();
67                 return $ret_val;
68         }
69
70         /**
71          * Populate email address fields here instead of retrieve() so that they are properly available for logic hooks
72          *
73          * @see parent::fill_in_relationship_fields()
74          */
75         public function fill_in_relationship_fields()
76         {
77             parent::fill_in_relationship_fields();
78             $this->emailAddress->handleLegacyRetrieve($this);
79         }
80
81         /**
82      * This function helps generate the name and full_name member field variables from the salutation, title, first_name and last_name fields.
83      * It takes into account the locale format settings as well as ACL settings if supported.
84          */
85         public function _create_proper_name_field()
86         {
87                 global $locale, $app_list_strings;
88
89         // Bug# 46125 - make first name, last name, salutation and title of Contacts respect field level ACLs
90         $first_name = ""; $last_name = ""; $salutation = ""; $title = "";
91
92            // first name has at least read access
93            $first_name = $this->first_name;
94
95             // last name has at least read access
96             $last_name = $this->last_name;
97
98
99             // salutation has at least read access
100             if(isset($this->field_defs['salutation']['options'])
101                           && isset($app_list_strings[$this->field_defs['salutation']['options']])
102                           && isset($app_list_strings[$this->field_defs['salutation']['options']][$this->salutation]) ) {
103
104                                 $salutation = $app_list_strings[$this->field_defs['salutation']['options']][$this->salutation];
105                         } // if
106
107             // last name has at least read access
108             $title = $this->title;
109
110         // Corner Case:
111         // Both first name and last name cannot be empty, at least one must be shown
112         // In that case, we can ignore field level ACL and just display last name...
113         // In the ACL field level access settings, last_name cannot be set to "none"
114         if (empty($first_name) && empty($last_name)) {
115             $full_name = $locale->getLocaleFormattedName("", $last_name, $salutation, $title);
116         } else {
117                         if($this->createLocaleFormattedName) {
118                                 $full_name = $locale->getLocaleFormattedName($first_name, $last_name, $salutation, $title);
119                         } else {
120                                 $full_name = $locale->getLocaleFormattedName($first_name, $last_name);
121                         }
122                 }
123
124                 $this->name = $full_name;
125                 $this->full_name = $full_name; //used by campaigns
126         }
127         
128
129         /**
130          * @see parent::save()
131          */
132         public function save($check_notify=false) 
133         {
134                 //If we are saving due to relationship changes, don't bother trying to update the emails
135         if(!empty($GLOBALS['resavingRelatedBeans']))
136         {
137             parent::save($check_notify);
138             return $this->id;
139         }
140         $this->add_address_streets('primary_address_street');
141         $this->add_address_streets('alt_address_street');
142         $ori_in_workflow = empty($this->in_workflow) ? false : true;
143         $this->emailAddress->handleLegacySave($this, $this->module_dir);
144         // bug #39188 - store emails state before workflow make any changes
145         $this->emailAddress->stash($this->id, $this->module_dir);
146         parent::save($check_notify);
147         // $this->emailAddress->evaluateWorkflowChanges($this->id, $this->module_dir);
148         $override_email = array();
149         if(!empty($this->email1_set_in_workflow)) {
150             $override_email['emailAddress0'] = $this->email1_set_in_workflow;
151         }
152         if(!empty($this->email2_set_in_workflow)) {
153             $override_email['emailAddress1'] = $this->email2_set_in_workflow;
154         }
155         if(!isset($this->in_workflow)) {
156             $this->in_workflow = false;
157         }
158         if($ori_in_workflow === false || !empty($override_email)){
159             $this->emailAddress->save($this->id, $this->module_dir, $override_email,'','','','',$this->in_workflow);
160             // $this->emailAddress->applyWorkflowChanges($this->id, $this->module_dir);
161         }
162                 return $this->id;
163         }
164
165         /**
166          * @see parent::get_summary_text()
167          */
168         public function get_summary_text() 
169         {
170                 $this->_create_proper_name_field();
171         return $this->name;
172         }
173
174         /**
175          * @see parent::get_list_view_data()
176          */
177         public function get_list_view_data() 
178         {
179                 global $system_config;
180                 global $current_user;
181
182                 $this->_create_proper_name_field();
183                 $temp_array = $this->get_list_view_array();
184
185         $temp_array['NAME'] = $this->name;
186         $temp_array["ENCODED_NAME"] = $this->full_name;
187         $temp_array["FULL_NAME"] = $this->full_name;
188
189         $temp_array['EMAIL1'] = $this->emailAddress->getPrimaryAddress($this);
190
191             $this->email1 = $temp_array['EMAIL1'];
192         $temp_array['EMAIL1_LINK'] = $current_user->getEmailLink('email1', $this, '', '', 'ListView');
193
194                 return $temp_array;
195         }
196
197     /**
198      * @see SugarBean::populateRelatedBean()
199          */
200     public function populateRelatedBean(
201         SugarBean $newbean
202         )
203     {
204         parent::populateRelatedBean($newbean);
205
206         if ( $newbean instanceOf Company ) {
207             $newbean->phone_fax = $this->phone_fax;
208             $newbean->phone_office = $this->phone_work;
209             $newbean->phone_alternate = $this->phone_other;
210             $newbean->email1 = $this->email1;
211             $this->add_address_streets('primary_address_street');
212             $newbean->billing_address_street = $this->primary_address_street;
213             $newbean->billing_address_city = $this->primary_address_city;
214             $newbean->billing_address_state = $this->primary_address_state;
215             $newbean->billing_address_postalcode = $this->primary_address_postalcode;
216             $newbean->billing_address_country = $this->primary_address_country;
217             $this->add_address_streets('alt_address_street');
218             $newbean->shipping_address_street = $this->alt_address_street;
219             $newbean->shipping_address_city = $this->alt_address_city;
220             $newbean->shipping_address_state = $this->alt_address_state;
221             $newbean->shipping_address_postalcode = $this->alt_address_postalcode;
222             $newbean->shipping_address_country = $this->alt_address_country;
223         }
224     }
225
226     /**
227      * Default export query for Person based modules
228      * used to pick all mails (primary and non-primary)
229      *
230      * @see SugarBean::create_export_query()
231      */
232     function create_export_query(&$order_by, &$where, $relate_link_join = '')
233     {
234         $custom_join = $this->custom_fields->getJOIN(true, true, $where);
235
236         // For easier code reading, reused plenty of time
237         $table = $this->table_name;
238
239         if($custom_join)
240         {
241             $custom_join['join'] .= $relate_link_join;
242         }
243         $query = "SELECT
244                                         $table.*,
245                                         email_addresses.email_address email_address,
246                                         '' email_addresses_non_primary, " . // email_addresses_non_primary needed for get_field_order_mapping()
247                                         "users.user_name as assigned_user_name ";
248         if($custom_join)
249         {
250             $query .= $custom_join['select'];
251         }
252
253         $query .= " FROM $table ";
254
255
256         $query .= "LEFT JOIN users
257                                         ON $table.assigned_user_id=users.id ";
258
259
260         //Join email address table too.
261         $query .=  " LEFT JOIN email_addr_bean_rel on $table.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module = '" . $this->module_dir . "' and email_addr_bean_rel.deleted = 0 and email_addr_bean_rel.primary_address = 1";
262         $query .=  " LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ";
263
264         if($custom_join)
265         {
266             $query .= $custom_join['join'];
267         }
268
269         $where_auto = " $table.deleted=0 ";
270
271         if($where != "")
272         {
273             $query .= "WHERE ($where) AND " . $where_auto;
274         }
275         else
276         {
277             $query .= "WHERE " . $where_auto;
278         }
279
280         $order_by = $this->process_order_by($order_by);
281         if (!empty($order_by)) {
282             $query .= ' ORDER BY ' . $order_by;
283         }
284
285         return $query;
286     }
287
288 }