'accounts','bug_id' => 'bugs', 'call_id'=>'calls','case_id'=>'cases','email_id'=>'emails', 'meeting_id'=>'meetings','note_id'=>'notes','task_id'=>'tasks', 'opportunity_id'=>'opportunities', 'pcontacts_users_id' => 'user_sync' ); function PContact() { parent::Person(); global $current_user; } function fill_in_relationship_fields(){ if(!empty($this->relDepth)) { if($this->relDepth > 1)return; }else $this->relDepth = 0; foreach($this->field_defs as $field){ if(0 == strcmp($field['type'],'relate') && !empty($field['module'])){ $name = $field['name']; if(empty($this->$name)){ // set the value of this relate field in this bean ($this->$field['name']) to the value of the 'name' field in the related module for the record identified by the value of $this->$field['id_name'] $related_module = $field['module']; $id_name = $field['id_name']; // first, load the link field. This is not done automatically by the bean; usually bean subclasses load any needed link fields in fill_in_additional_fields() // here though we need it to be loaded as it contains the id of the related record if (empty($this->$id_name)) $this->fill_in_link_field($id_name); if(!empty($this->$id_name) && $this->$id_name != $this->id){ if(isset($GLOBALS['beanList'][ $related_module])){ $class = $GLOBALS['beanList'][$related_module]; if(!empty($this->$id_name) && file_exists($GLOBALS['beanFiles'][$class]) && isset($this->$name)){ require_once($GLOBALS['beanFiles'][$class]); $mod = new $class(); $mod->relDepth = $this->relDepth + 1; $mod->retrieve($this->$id_name); if (!empty($field['rname'])) { $this->$name = $mod->$field['rname']; } else if (isset($mod->name)) { $this->$name = $mod->name; } if(isset($field['additionalFields'])){ foreach($field['additionalFields'] as $field=>$to){ if(isset($mod->$field)){ $this->$to = $mod->$field; } } } } } } } } } } function add_list_count_joins(&$query, $where) { // accounts.name if(eregi("accounts.name", $where)) { // add a join to the accounts table. $query .= " LEFT JOIN accounts_pcontacts ON pcontacts.id=accounts_contacts.contact_id LEFT JOIN accounts ON accounts_pcontacts.account_id=accounts.id "; } $custom_join = $this->custom_fields->getJOIN(); if($custom_join){ $query .= $custom_join['join']; } } function listviewACLHelper(){ $array_assign = parent::listviewACLHelper(); $is_owner = false; //MFH BUG 18281; JChi #15255 $is_owner = !empty($this->assigned_user_id) && $this->assigned_user_id == $GLOBALS['current_user']->id; if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){ $array_assign['ACCOUNT'] = 'a'; }else{ $array_assign['ACCOUNT'] = 'span'; } return $array_assign; } function create_list_query($order_by, $where, $show_deleted = 0) { $custom_join = $this->custom_fields->getJOIN(); // MFH - BUG #14208 creates alias name for select $query = "SELECT "; $query .= db_concat($this->table_name,array('first_name','last_name')) . " name, "; $query .= " $this->table_name.*, accounts.name as account_name, accounts.id as account_id, accounts.assigned_user_id account_id_owner, users.user_name as assigned_user_name "; if($custom_join){ $query .= $custom_join['select']; } $query .= " FROM pcontacts "; $query .= "LEFT JOIN users ON pcontacts.assigned_user_id=users.id LEFT JOIN accounts_pcontacts ON pcontacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0 LEFT JOIN accounts ON accounts_pcontacts.account_id=accounts.id AND accounts.deleted=0 "; $query .= "LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = pcontacts.id AND eabl.bean_module = 'PContacts' and eabl.primary_address = 1 and eabl.deleted=0 "; $query .= "LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) "; if($custom_join){ $query .= $custom_join['join']; } $where_auto = '1=1'; if($show_deleted == 0){ $where_auto = " $this->table_name.deleted=0 "; //$where_auto .= " AND accounts.deleted=0 "; }else if($show_deleted == 1){ $where_auto = " $this->table_name.deleted=1 "; } if($where != "") $query .= "where ($where) AND ".$where_auto; else $query .= "where ".$where_auto; if(!empty($order_by)) $query .= " ORDER BY ". $this->process_order_by($order_by, null); return $query; } function create_export_query(&$order_by, &$where) { $custom_join = $this->custom_fields->getJOIN(true, true); $query = "SELECT pcontacts.*,email_addresses.email_address email1, accounts.name as account_name, users.user_name as assigned_user_name "; if($custom_join){ $query .= $custom_join['select']; } $query .= " FROM pcontacts "; $query .= "LEFT JOIN users ON pcontacts.assigned_user_id=users.id "; $query .= "LEFT JOIN accounts_pcontacts ON ( pcontacts.id=accounts_contacts.contact_id and (accounts_contacts.deleted is null or accounts_contacts.deleted = 0)) LEFT JOIN accounts ON accounts_pcontacts.account_id=accounts.id "; //join email address table too. $query .= ' LEFT JOIN email_addr_bean_rel on pcontacts.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'PContacts\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 '; $query .= ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ; if($custom_join){ $query .= $custom_join['join']; } $where_auto = "( accounts.deleted IS NULL OR accounts.deleted=0 ) AND pcontacts.deleted=0 "; if($where != "") $query .= "where ($where) AND ".$where_auto; else $query .= "where ".$where_auto; if(!empty($order_by)) $query .= " ORDER BY ". $this->process_order_by($order_by, null); return $query; } function fill_in_additional_list_fields() { parent::fill_in_additional_list_fields(); // cn: bug 8586 - l10n names for PContacts in Email TO: field $this->_create_proper_name_field(); $this->email_and_name1 = "{$this->full_name} <".$this->email1.">"; $this->email_and_name2 = "{$this->full_name} <".$this->email2.">"; if($this->force_load_details == true) { $this->fill_in_additional_detail_fields(); } } function fill_in_additional_detail_fields() { parent::fill_in_additional_detail_fields(); global $locale, $app_list_strings, $current_user; // retrieve the account information and the information about the person the pcontact reports to. $query = "SELECT acc.id, acc.name, con_reports_to.first_name, con_reports_to.last_name from pcontacts left join accounts_pcontacts a_c on a_c.contact_id = '".$this->id."' and a_c.deleted=0 left join accounts acc on a_c.account_id = acc.id and acc.deleted=0 left join pcontacts con_reports_to on con_reports_to.id = contacts.reports_to_id where pcontacts.id = '".$this->id."'"; $result = $this->db->query($query,true," Error filling in additional detail fields: "); // Get the id and the name. $row = $this->db->fetchByAssoc($result); if($row != null) { $this->account_name = $row['name']; $this->account_id = $row['id']; $this->report_to_name = $row['first_name'].' '.$row['last_name']; } else { $this->account_name = ''; $this->account_id = ''; $this->report_to_name = ''; } $this->load_pcontacts_users_relationship(); /** concating this here because newly created PContacts do not have a * 'name' attribute constructed to pass onto related items, such as Tasks * Notes, etc. */ $this->name = $locale->getLocaleFormattedName($this->first_name, $this->last_name); if(!empty($this->pcontacts_users_id)) { $this->sync_pcontact = true; } if(!empty($this->portal_active) && $this->portal_active == 1) { $this->portal_active = true; } // Set campaign name if there is a campaign id if( !empty($this->campaign_id)){ require_once('modules/Campaigns/Campaign.php'); $camp = new Campaign(); $where = "campaigns.id='{$this->campaign_id}'"; $campaign_list = $camp->get_full_list("campaigns.name", $where, true); $this->campaign_name = $campaign_list[0]->name; } //asterisk phone extension if ($_REQUEST['action']=='DetailView') { require_once("include/ECM/EcmFormatPhoneNumber/EcmFormatPhoneNumber.php"); $this->phone_work = EcmFormatPhoneNumber($this->phone_work, "PContact", "Contacts", $this->id); $this->phone_mobile = EcmFormatPhoneNumber($this->phone_mobile, "PContact", "Contacts", $this->id); $this->phone_home = EcmFormatPhoneNumber($this->phone_home, "PContact", "Contacts", $this->id); $this->phone_other = EcmFormatPhoneNumber($this->phone_other, "PContact", "Contacts", $this->id); } } /** loads the pcontacts_users relationship to populate a checkbox where a user can select if they would like to sync a particular pcontact to Outlook */ function load_pcontacts_users_relationship(){ /*global $current_user; $this->load_relationship("user_sync"); $query_array=$this->user_sync->getQuery(true); $query_array['where'] .= " AND users.id = '$current_user->id'"; $query=''; foreach ($query_array as $qstring) { $query.=' '.$qstring; } $list = $this->build_related_list($query, new User()); if(!empty($list)){ //this should only return one possible value so set it $this->pcontacts_users_id = $list[0]->id; }*/ } function get_list_view_data() { global $system_config; global $current_user; $this->_create_proper_name_field(); $temp_array = $this->get_list_view_array(); $temp_array['NAME'] = $this->name; $temp_array['ENCODED_NAME'] = $this->name; /* if(isset($system_config->settings['system_skypeout_on']) && $system_config->settings['system_skypeout_on'] == 1) { if(!empty($temp_array['PHONE_WORK']) && skype_formatted($temp_array['PHONE_WORK'])) { $temp_array['PHONE_WORK'] = '' . $temp_array['PHONE_WORK']. '' ; } } */ require_once("include/ECM/EcmFormatPhoneNumber/EcmFormatPhoneNumber.php"); $temp_array['PHONE_WORK'] = EcmFormatPhoneNumber($temp_array['PHONE_WORK'], "PContact", "Contacts", $this->id); $temp_array['PHONE_MOBILE'] = EcmFormatPhoneNumber($temp_array['PHONE_MOBILE'], "PContact", "Contacts", $this->id); $temp_array['PHONE_HOME'] = EcmFormatPhoneNumber($temp_array['PHONE_HOME'], "PContact", "Contacts", $this->id); $temp_array['PHONE_OTHER'] = EcmFormatPhoneNumber($temp_array['PHONE_OTHER'], "PContact", "Contacts", $this->id); $temp_array['EMAIL1'] = $this->emailAddress->getPrimaryAddress($this); $this->email1 = $temp_array['EMAIL1']; $temp_array['EMAIL1_LINK'] = $current_user->getEmailLink('email1', $this, '', '', 'ListView'); $temp_array['EMAIL_AND_NAME1'] = "{$this->full_name} <".$temp_array['EMAIL1'].">"; include_once("modules/Accounts/Account.php"); $addrs=Account::get_email_addresses($this->id,'PContacts'); $temp_array["EMAIL"] = $r['addr']; $temp_array["ADDRS"] = @implode("||",$addrs); $temp_array["CNT_DATA"] = $cnt; $temp_array["ACC_DATA"] = $acc; return $temp_array; } /** builds a generic search based on the query string using or do not include any $this-> because this is called on without having the class instantiated */ function build_generic_where_clause ($the_query_string) { $where_clauses = Array(); $the_query_string = $this->db->quote($the_query_string); array_push($where_clauses, "pcontacts.last_name like '$the_query_string%'"); array_push($where_clauses, "pcontacts.first_name like '$the_query_string%'"); array_push($where_clauses, "accounts.name like '$the_query_string%'"); array_push($where_clauses, "pcontacts.assistant like '$the_query_string%'"); array_push($where_clauses, "ea.email_address like '$the_query_string%'"); if (is_numeric($the_query_string)) { array_push($where_clauses, "pcontacts.phone_home like '%$the_query_string%'"); array_push($where_clauses, "pcontacts.phone_mobile like '%$the_query_string%'"); array_push($where_clauses, "pcontacts.phone_work like '%$the_query_string%'"); array_push($where_clauses, "pcontacts.phone_other like '%$the_query_string%'"); array_push($where_clauses, "pcontacts.phone_fax like '%$the_query_string%'"); array_push($where_clauses, "pcontacts.assistant_phone like '%$the_query_string%'"); } $the_where = ""; foreach($where_clauses as $clause) { if($the_where != "") $the_where .= " or "; $the_where .= $clause; } return $the_where; } function set_notification_body($xtpl, $pcontact) { $xtpl->assign("PCONTACT_NAME", trim($pcontact->first_name . " " . $contact->last_name)); $xtpl->assign("PCONTACT_DESCRIPTION", $pcontact->description); return $xtpl; } function get_pcontact_id_by_email($email) { $email = trim($email); if(empty($email)){ //email is empty, no need to query, return null return null; } $where_clause = "(email1='$email' OR email2='$email') AND deleted=0"; $query = "SELECT * FROM $this->table_name WHERE $where_clause"; $GLOBALS['log']->debug("Retrieve $this->object_name: ".$query); //requireSingleResult has beeen deprecated. //$result = $this->db->requireSingleResult($query, true, "Retrieving record $where_clause:"); $result = $this->db->limitQuery($query,0,1,true, "Retrieving record $where_clause:"); if( empty($result)) { return null; } $row = $this->db->fetchByAssoc($result, -1, true); return $row['id']; } function save_relationship_changes($is_update) { //if account_id was replaced unlink the previous account_id. //this rel_fields_before_value is populated by sugarbean during the retrieve call. if (!empty($this->account_id) and !empty($this->rel_fields_before_value['account_id']) and (trim($this->account_id) != trim($this->rel_fields_before_value['account_id']))) { //unlink the old record. $this->load_relationship('accounts'); $this->accounts->delete($this->id,$this->rel_fields_before_value['account_id']); } parent::save_relationship_changes($is_update); } function bean_implements($interface){ switch($interface){ case 'ACL':return true; } return false; } function get_unlinked_email_query($type=array()) { require_once('include/utils.php'); return get_unlinked_email_query($type, $this); } /** * used by import to add a list of users * * Parameter can be one of the following: * - string 'all': add this pcontact for all users * - comma deliminated lists of teams and/or users * * @param string $list_of_user */ function process_sync_to_outlook( $list_of_users ) { static $focus_user; // cache this object since we'll be reusing it a bunch if ( !($focus_user instanceof User) ) { require_once('modules/Users/User.php'); $focus_user = new User(); } if ( empty($list_of_users) ) { return; } if ( !isset($this->users) ) { $this->load_relationship('user_sync'); } if ( strtolower($list_of_users) == 'all' ) { // add all non-deleted users $sql = "SELECT id FROM users WHERE deleted=0 AND is_group=0 AND portal_only=0"; $result=$this->db->query($sql); while ( $hash = $this->db->fetchByAssoc($result) ) { $this->user_sync->add($hash['id']); } } else { $theList = explode(",",$list_of_users); foreach ($theList as $eachItem) { if ( $focus_user->retrieve_user_id($eachItem) || $focus_user->retrieve($eachItem)) { // it is a user, add user $this->user_sync->add($this->id); return; } } } } } ?>