]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Contacts/Contact.php
Release 6.5.1
[Github/sugarcrm.git] / modules / Contacts / Contact.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
41  * All Rights Reserved.
42  * Contributor(s): ______________________________________..
43  ********************************************************************************/
44
45 require_once('include/SugarObjects/templates/person/Person.php');
46 // Contact is used to store customer information.
47 class Contact extends Person {
48     var $field_name_map;
49         // Stored fields
50         var $id;
51         var $name = '';
52         var $lead_source;
53         var $date_entered;
54         var $date_modified;
55         var $modified_user_id;
56         var $assigned_user_id;
57         var $created_by;
58         var $created_by_name;
59         var $modified_by_name;
60
61         var $description;
62         var $salutation;
63         var $first_name;
64         var $last_name;
65         var $title;
66         var $department;
67         var $birthdate;
68         var $reports_to_id;
69         var $do_not_call;
70         var $phone_home;
71         var $phone_mobile;
72         var $phone_work;
73         var $phone_other;
74         var $phone_fax;
75         var $email1;
76         var $email_and_name1;
77         var $email_and_name2;
78         var $email2;
79         var $assistant;
80         var $assistant_phone;
81         var $email_opt_out;
82         var $primary_address_street;
83         var $primary_address_city;
84         var $primary_address_state;
85         var $primary_address_postalcode;
86         var $primary_address_country;
87         var $alt_address_street;
88         var $alt_address_city;
89         var $alt_address_state;
90         var $alt_address_postalcode;
91         var $alt_address_country;
92         var $portal_name;
93         var $portal_app;
94         var $portal_active;
95         var $contacts_users_id;
96         // These are for related fields
97         var $bug_id;
98         var $account_name;
99         var $account_id;
100         var $report_to_name;
101         var $opportunity_role;
102         var $opportunity_rel_id;
103         var $opportunity_id;
104         var $case_role;
105         var $case_rel_id;
106         var $case_id;
107         var $task_id;
108         var $note_id;
109         var $meeting_id;
110         var $call_id;
111         var $email_id;
112         var $assigned_user_name;
113         var $accept_status;
114     var $accept_status_id;
115     var $accept_status_name;
116     var $alt_address_street_2;
117     var $alt_address_street_3;
118     var $opportunity_role_id;
119     var $portal_password;
120     var $primary_address_street_2;
121     var $primary_address_street_3;
122     var $campaign_id;
123     var $sync_contact;
124         var $full_name; // l10n localized name
125         var $invalid_email;
126         var $table_name = "contacts";
127         var $rel_account_table = "accounts_contacts";
128         //This is needed for upgrade.  This table definition moved to Opportunity module.
129         var $rel_opportunity_table = "opportunities_contacts";
130
131         var $object_name = "Contact";
132         var $module_dir = 'Contacts';
133         var $emailAddress;
134         var $new_schema = true;
135         var $importable = true;
136
137         // This is used to retrieve related fields from form posts.
138         var $additional_column_fields = Array('bug_id', 'assigned_user_name', 'account_name', 'account_id', 'opportunity_id', 'case_id', 'task_id', 'note_id', 'meeting_id', 'call_id', 'email_id'
139         );
140
141         var $relationship_fields = Array('account_id'=> 'accounts','bug_id' => 'bugs', 'call_id'=>'calls','case_id'=>'cases','email_id'=>'emails',
142                                                                 'meeting_id'=>'meetings','note_id'=>'notes','task_id'=>'tasks', 'opportunity_id'=>'opportunities', 'contacts_users_id' => 'user_sync'
143                                                                 );
144
145         function Contact() {
146                 parent::Person();
147         }
148
149         function add_list_count_joins(&$query, $where)
150         {
151                 // accounts.name
152                 if(stristr($where, "accounts.name"))
153                 {
154                         // add a join to the accounts table.
155                         $query .= "
156                     LEFT JOIN accounts_contacts
157                     ON contacts.id=accounts_contacts.contact_id
158                     LEFT JOIN accounts
159                     ON accounts_contacts.account_id=accounts.id
160                         ";
161                 }
162                 $custom_join = $this->custom_fields->getJOIN();
163                 if($custom_join){
164                                 $query .= $custom_join['join'];
165                 }
166
167
168         }
169
170         function listviewACLHelper(){
171                 $array_assign = parent::listviewACLHelper();
172                 $is_owner = false;
173                 //MFH BUG 18281; JChi #15255
174                 $is_owner = !empty($this->assigned_user_id) && $this->assigned_user_id == $GLOBALS['current_user']->id;
175                         if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
176                                 $array_assign['ACCOUNT'] = 'a';
177                         }else{
178                                 $array_assign['ACCOUNT'] = 'span';
179
180                         }
181                 return $array_assign;
182         }
183
184         function create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false,$parentbean=null, $singleSelect = false)
185         {
186                 //if this is from "contact address popup" action, then process popup list query
187                 if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'ContactAddressPopup'){
188                         return $this->address_popup_create_new_list_query($order_by, $where, $filter, $params, $show_deleted, $join_type, $return_array, $parentbean, $singleSelect);
189
190                 }else{
191                         //any other action goes to parent function in sugarbean
192                         if(strpos($order_by,'sync_contact') !== false){
193                                 //we have found that the user is ordering by the sync_contact field, it would be troublesome to sort by this field
194                                 //and perhaps a performance issue, so just remove it
195                                 $order_by = '';
196                         }
197                         return parent::create_new_list_query($order_by, $where, $filter, $params, $show_deleted, $join_type, $return_array, $parentbean, $singleSelect);
198                 }
199
200
201         }
202
203
204
205         function address_popup_create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false,$parentbean=null, $singleSelect = false)
206         {
207                 //if this is any action that is not the contact address popup, then go to parent function in sugarbean
208                 if(isset($_REQUEST['action']) && $_REQUEST['action'] !== 'ContactAddressPopup'){
209                         return parent::create_new_list_query($order_by, $where, $filter, $params, $show_deleted, $join_type, $return_array, $parentbean, $singleSelect);
210                 }
211
212                 $custom_join = $this->custom_fields->getJOIN();
213                 // MFH - BUG #14208 creates alias name for select
214                 $select_query = "SELECT ";
215                 $select_query .= db_concat($this->table_name,array('first_name','last_name')) . " name, ";
216                 $select_query .= "
217                                 $this->table_name.*,
218                 accounts.name as account_name,
219                 accounts.id as account_id,
220                 accounts.assigned_user_id account_id_owner,
221                 users.user_name as assigned_user_name ";
222                 if($custom_join){
223                                 $select_query .= $custom_join['select'];
224                 }
225                 $ret_array['select'] = $select_query;
226
227                 $from_query = "
228                 FROM contacts ";
229
230                 $from_query .=          "LEFT JOIN users
231                             ON contacts.assigned_user_id=users.id
232                             LEFT JOIN accounts_contacts
233                             ON contacts.id=accounts_contacts.contact_id  and accounts_contacts.deleted = 0
234                             LEFT JOIN accounts
235                             ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 ";
236                 $from_query .= "LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = contacts.id AND eabl.bean_module = 'Contacts' and eabl.primary_address = 1 and eabl.deleted=0 ";
237         $from_query .= "LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) ";
238                 if($custom_join){
239                                 $from_query .= $custom_join['join'];
240                 }
241                 $ret_array['from'] = $from_query;
242                 $ret_array['from_min'] = 'from contacts';
243
244                 $where_auto = '1=1';
245                 if($show_deleted == 0){
246                 $where_auto = " $this->table_name.deleted=0 ";
247                 //$where_auto .= " AND accounts.deleted=0  ";
248                 }else if($show_deleted == 1){
249                                 $where_auto = " $this->table_name.deleted=1 ";
250                 }
251
252
253                 if($where != ""){
254                         $where_query = "where ($where) AND ".$where_auto;
255                 }else{
256                         $where_query = "where ".$where_auto;
257                 }
258
259
260                 $ret_array['where'] = $where_query;
261                 $orderby_query = '';
262                 if(!empty($order_by)){
263                     $orderby_query =  " ORDER BY ". $this->process_order_by($order_by, null);
264                 }
265                 $ret_array['order_by'] = $orderby_query ;
266
267                 if($return_array)
268         {
269                 return $ret_array;
270         }
271
272             return $ret_array['select'] . $ret_array['from'] . $ret_array['where']. $ret_array['order_by'];
273
274         }
275
276
277
278
279                 function create_export_query(&$order_by, &$where, $relate_link_join='')
280         {
281                 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
282                         if($custom_join)
283                                 $custom_join['join'] .= $relate_link_join;
284                          $query = "SELECT
285                                 contacts.*,email_addresses.email_address email_address,
286                                 accounts.name as account_name,
287                                 users.user_name as assigned_user_name ";
288                                                 if($custom_join){
289                                                         $query .= $custom_join['select'];
290                                                 }
291                                                  $query .= " FROM contacts ";
292                          $query .= "LEFT JOIN users
293                                         ON contacts.assigned_user_id=users.id ";
294                              $query .= "LEFT JOIN accounts_contacts
295                                         ON ( contacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0))
296                                         LEFT JOIN accounts
297                                         ON accounts_contacts.account_id=accounts.id ";
298
299                                                 //join email address table too.
300                                                 $query .=  ' LEFT JOIN  email_addr_bean_rel on contacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Contacts\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
301                                                 $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
302
303                                                 if($custom_join){
304                                                         $query .= $custom_join['join'];
305                                                 }
306
307                 $where_auto = "( accounts.deleted IS NULL OR accounts.deleted=0 )
308                       AND contacts.deleted=0 ";
309
310                 if($where != "")
311                         $query .= "where ($where) AND ".$where_auto;
312                 else
313                         $query .= "where ".$where_auto;
314
315                 if(!empty($order_by))
316                         $query .=  " ORDER BY ". $this->process_order_by($order_by, null);
317
318                 return $query;
319         }
320
321         function fill_in_additional_list_fields() {
322                 parent::fill_in_additional_list_fields();
323                 $this->_create_proper_name_field();
324                 // cn: bug 8586 - l10n names for Contacts in Email TO: field
325                 $this->email_and_name1 = "{$this->full_name} &lt;".$this->email1."&gt;";
326                 $this->email_and_name2 = "{$this->full_name} &lt;".$this->email2."&gt;";
327
328                 if($this->force_load_details == true) {
329                         $this->fill_in_additional_detail_fields();
330                 }
331         }
332
333         function fill_in_additional_detail_fields() {
334                 parent::fill_in_additional_detail_fields();
335         if(empty($this->id)) return;
336
337         global $locale, $app_list_strings, $current_user;
338
339                 // retrieve the account information and the information about the person the contact reports to.
340                 $query = "SELECT acc.id, acc.name, con_reports_to.first_name, con_reports_to.last_name
341                 from contacts
342                 left join accounts_contacts a_c on a_c.contact_id = '".$this->id."' and a_c.deleted=0
343                 left join accounts acc on a_c.account_id = acc.id and acc.deleted=0
344                 left join contacts con_reports_to on con_reports_to.id = contacts.reports_to_id
345                 where contacts.id = '".$this->id."'";
346                 // Bug 43196 - If a contact is related to multiple accounts, make sure we pull the one we are looking for
347                 // Bug 44730  was introduced due to this, fix is to simply clear any whitespaces around the account_id first
348
349         $clean_account_id = trim($this->account_id);
350
351         if ( !empty($clean_account_id) ) {
352                     $query .= " and acc.id = '{$this->account_id}'";
353                 }
354
355         $query .= " ORDER BY a_c.date_modified DESC";
356
357                 $result = $this->db->query($query,true," Error filling in additional detail fields: ");
358
359                 // Get the id and the name.
360                 $row = $this->db->fetchByAssoc($result);
361
362                 if($row != null)
363                 {
364                         $this->account_name = $row['name'];
365                         $this->account_id = $row['id'];
366                         $this->report_to_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'],'','','',null,true);
367                 }
368                 else
369                 {
370                         $this->account_name = '';
371                         $this->account_id = '';
372                         $this->report_to_name = '';
373                 }
374                 $this->load_contacts_users_relationship();
375                 /** concating this here because newly created Contacts do not have a
376                  * 'name' attribute constructed to pass onto related items, such as Tasks
377                  * Notes, etc.
378                  */
379                 $this->name = $locale->getLocaleFormattedName($this->first_name, $this->last_name);
380         if(!empty($this->contacts_users_id)) {
381                    $this->sync_contact = true;
382                 }
383
384                 if(!empty($this->portal_active) && $this->portal_active == 1) {
385                    $this->portal_active = true;
386                 }
387         // Set campaign name if there is a campaign id
388                 if( !empty($this->campaign_id)){
389
390                         $camp = new Campaign();
391                     $where = "campaigns.id='{$this->campaign_id}'";
392                     $campaign_list = $camp->get_full_list("campaigns.name", $where, true);
393                     $this->campaign_name = $campaign_list[0]->name;
394                 }
395         }
396
397                 /**
398                 loads the contacts_users relationship to populate a checkbox
399                 where a user can select if they would like to sync a particular
400                 contact to Outlook
401         */
402         function load_contacts_users_relationship(){
403                 global $current_user;
404
405                 $this->load_relationship("user_sync");
406
407         $beanIDs = $this->user_sync->get();
408
409         if( in_array($current_user->id, $beanIDs) )
410         {
411             $this->contacts_users_id = $current_user->id;
412         }
413         }
414
415         function get_list_view_data($filter_fields = array()) {
416                 global $system_config;
417                 global $current_user;
418
419                 $this->_create_proper_name_field();
420                 $temp_array = $this->get_list_view_array();
421                 $temp_array['NAME'] = $this->name;
422                 $temp_array['ENCODED_NAME'] = $this->name;
423
424                 if($filter_fields && !empty($filter_fields['sync_contact'])){
425                         $this->load_contacts_users_relationship();
426                         $temp_array['SYNC_CONTACT'] = !empty($this->contacts_users_id) ? 1 : 0;
427                 }
428                 $temp_array['EMAIL1'] = $this->emailAddress->getPrimaryAddress($this);
429                 $this->email1 = $temp_array['EMAIL1'];
430                 $temp_array['EMAIL1_LINK'] = $current_user->getEmailLink('email1', $this, '', '', 'ListView');
431                 $temp_array['EMAIL_AND_NAME1'] = "{$this->full_name} &lt;".$temp_array['EMAIL1']."&gt;";
432                 return $temp_array;
433         }
434
435         /**
436                 builds a generic search based on the query string using or
437                 do not include any $this-> because this is called on without having the class instantiated
438         */
439         function build_generic_where_clause ($the_query_string)
440         {
441                 $where_clauses = Array();
442                 $the_query_string = $this->db->quote($the_query_string);
443
444                 array_push($where_clauses, "contacts.last_name like '$the_query_string%'");
445                 array_push($where_clauses, "contacts.first_name like '$the_query_string%'");
446                 array_push($where_clauses, "accounts.name like '$the_query_string%'");
447                 array_push($where_clauses, "contacts.assistant like '$the_query_string%'");
448                 array_push($where_clauses, "ea.email_address like '$the_query_string%'");
449
450                 if (is_numeric($the_query_string))
451                 {
452                         array_push($where_clauses, "contacts.phone_home like '%$the_query_string%'");
453                         array_push($where_clauses, "contacts.phone_mobile like '%$the_query_string%'");
454                         array_push($where_clauses, "contacts.phone_work like '%$the_query_string%'");
455                         array_push($where_clauses, "contacts.phone_other like '%$the_query_string%'");
456                         array_push($where_clauses, "contacts.phone_fax like '%$the_query_string%'");
457                         array_push($where_clauses, "contacts.assistant_phone like '%$the_query_string%'");
458                 }
459
460                 $the_where = "";
461                 foreach($where_clauses as $clause)
462                 {
463                         if($the_where != "") $the_where .= " or ";
464                         $the_where .= $clause;
465                 }
466
467
468                 return $the_where;
469         }
470
471         function set_notification_body($xtpl, $contact)
472         {
473             global $locale;
474
475                 $xtpl->assign("CONTACT_NAME", trim($locale->getLocaleFormattedName($contact->first_name, $contact->last_name)));
476                 $xtpl->assign("CONTACT_DESCRIPTION", $contact->description);
477
478                 return $xtpl;
479         }
480
481         function get_contact_id_by_email($email)
482         {
483                 $email = trim($email);
484                 if(empty($email)){
485                         //email is empty, no need to query, return null
486                         return null;
487                 }
488
489                 $where_clause = "(email1='$email' OR email2='$email') AND deleted=0";
490
491         $query = "SELECT id FROM $this->table_name WHERE $where_clause";
492         $GLOBALS['log']->debug("Retrieve $this->object_name: ".$query);
493                 $result = $this->db->getOne($query, true, "Retrieving record $where_clause:");
494
495                 return empty($result)?null:$result;
496         }
497
498         function save_relationship_changes($is_update) {
499
500                 //if account_id was replaced unlink the previous account_id.
501                 //this rel_fields_before_value is populated by sugarbean during the retrieve call.
502                 if (!empty($this->account_id) and !empty($this->rel_fields_before_value['account_id']) and
503                                 (trim($this->account_id) != trim($this->rel_fields_before_value['account_id']))) {
504                                 //unlink the old record.
505                                 $this->load_relationship('accounts');
506                                 $this->accounts->delete($this->id,$this->rel_fields_before_value['account_id']);
507                 }
508                 parent::save_relationship_changes($is_update);
509         }
510
511         function bean_implements($interface)
512         {
513                 switch($interface){
514                         case 'ACL':return true;
515                 }
516                 return false;
517         }
518
519         function get_unlinked_email_query($type=array())
520         {
521                 return get_unlinked_email_query($type, $this);
522         }
523
524     /**
525      * used by import to add a list of users
526      *
527      * Parameter can be one of the following:
528      * - string 'all': add this contact for all users
529      * - comma deliminated lists of teams and/or users
530      *
531      * @param string $list_of_user
532      */
533     function process_sync_to_outlook($list_of_users)
534     {
535         static $focus_user;
536
537         // cache this object since we'll be reusing it a bunch
538         if ( !($focus_user instanceof User) ) {
539
540             $focus_user = new User();
541         }
542
543
544                 if ( empty($list_of_users) ) {
545             return;
546                 }
547         if ( !isset($this->users) ) {
548             $this->load_relationship('user_sync');
549         }
550
551                 if ( strtolower($list_of_users) == 'all' ) {
552             // add all non-deleted users
553                         $sql = "SELECT id FROM users WHERE deleted=0 AND is_group=0 AND portal_only=0";
554                         $result=$this->db->query($sql);
555                         while ( $hash = $this->db->fetchByAssoc($result) ) {
556                 $this->user_sync->add($hash['id']);
557                         }
558                 }
559         else {
560             $theList = explode(",",$list_of_users);
561             foreach ($theList as $eachItem) {
562                 if ( ($user_id = $focus_user->retrieve_user_id($eachItem))
563                         || $focus_user->retrieve($eachItem)) {
564                     // it is a user, add user
565                     $this->user_sync->add($user_id ? $user_id : $focus_user->id);
566                     return;
567                 }
568                         }
569                 }
570         }
571 }