'emails','call_id'=>'calls','meeting_id'=>'meetings','task_id'=>'tasks',); function Lead() { parent::Person(); } function get_account() { if(isset($this->account_id) && !empty($this->account_id)){ $query = "SELECT name , assigned_user_id account_name_owner FROM accounts WHERE id='{$this->account_id}'"; //requireSingleResult has beeen deprecated. //$result = $this->db->requireSingleResult($query); $result = $this->db->limitQuery($query,0,1,true, "Want only a single row"); if(!empty($result)){ $row = $this->db->fetchByAssoc($result); $this->account_name = $row['name']; $this->account_name_owner = $row['account_name_owner']; $this->account_name_mod = 'Accounts'; } }} function get_opportunity() { if(isset($this->opportunity_id) && !empty($this->opportunity_id)){ $query = "SELECT name, assigned_user_id opportunity_name_owner FROM opportunities WHERE id='{$this->opportunity_id}'"; //requireSingleResult has beeen deprecated. //$result = $this->db->requireSingleResult($query); $result = $this->db->limitQuery($query,0,1,true, "Want only a single row"); if(!empty($result)){ $row = $this->db->fetchByAssoc($result); $this->opportunity_name = $row['name']; $this->opportunity_name_owner = $row['opportunity_name_owner']; $this->opportunity_name_mod = 'Opportunities'; } }} function get_contact() { global $locale; if(isset($this->contact_id) && !empty($this->contact_id)){ $query = "SELECT first_name, last_name, assigned_user_id contact_name_owner FROM contacts WHERE id='{$this->contact_id}'"; //requireSingleResult has beeen deprecated. //$result = $this->db->requireSingleResult($query); $result = $this->db->limitQuery($query,0,1,true, "Want only a single row"); if(!empty($result)){ $row= $this->db->fetchByAssoc($result); $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']); $this->contact_name_owner = $row['contact_name_owner']; $this->contact_name_mod = 'Contacts'; } }} function create_list_query($order_by, $where, $show_deleted=0) { $custom_join = $this->custom_fields->getJOIN(); $query = "SELECT "; $query .= "$this->table_name.*, users.user_name assigned_user_name"; if($custom_join){ $query .= $custom_join['select']; } $query .= " FROM leads "; $query .= " LEFT JOIN users ON leads.assigned_user_id=users.id "; $query .= "LEFT JOIN email_addr_bean_rel eabl ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' 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 = " leads.deleted=0 "; }else if($show_deleted == 1){ $where_auto = " leads.deleted=1 "; } if($where != "") $query .= "where ($where) AND ".$where_auto; else $query .= "where ".$where_auto; //."and (leads.converted='0')"; if(!empty($order_by)) $query .= " ORDER BY $order_by"; return $query; } function create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false, $parentbean = null, $singleSelect = false){ $ret_array = parent::create_new_list_query($order_by, $where, $filter, $params, $show_deleted, $join_type, true, $parentbean, $singleSelect); if(strpos($ret_array['select'],"leads.account_name") == false && strpos($ret_array['select'],"leads.*") == false) $ret_array['select'] .= " ,leads.account_name"; if ( !$return_array ) return $ret_array['select'] . $ret_array['from'] . $ret_array['where']. $ret_array['order_by']; return $ret_array; } function create_export_query(&$order_by, &$where, $relate_link_join='') { $custom_join = $this->custom_fields->getJOIN(true, true,$where); if($custom_join) $custom_join['join'] .= $relate_link_join; $query = "SELECT leads.*, email_addresses.email_address email_address, users.user_name assigned_user_name"; if($custom_join){ $query .= $custom_join['select']; } $query .= " FROM leads "; $query .= " LEFT JOIN users ON leads.assigned_user_id=users.id "; //join email address table too. $query .= ' LEFT JOIN email_addr_bean_rel on leads.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Leads\' 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 = " leads.deleted=0 "; if($where != "") $query .= "where ($where) AND ".$where_auto; else $query .= "where ".$where_auto; if(!empty($order_by)) $query .= " ORDER BY $order_by"; return $query; } function converted_lead($leadid, $contactid, $accountid, $opportunityid){ $query = "UPDATE leads set converted='1', contact_id=$contactid, account_id=$accountid, opportunity_id=$opportunityid where id=$leadid and deleted=0"; $this->db->query($query,true,"Error converting lead: "); //we must move the status out here in order to be able to capture workflow conditions $leadid = str_replace("'","", $leadid); $lead = new Lead(); $lead->retrieve($leadid); $lead->status='Converted'; $lead->save(); } function fill_in_additional_list_fields() { parent::fill_in_additional_list_fields(); $this->_create_proper_name_field(); $this->get_account(); } function fill_in_additional_detail_fields() { //Fill in the assigned_user_name //if(!empty($this->status)) //$this->status = translate('lead_status_dom', '', $this->status); parent::fill_in_additional_detail_fields(); $this->_create_proper_name_field(); $this->get_contact(); $this->get_opportunity(); $this->get_account(); if(!empty($this->campaign_id)){ $camp = new Campaign(); $where = "campaigns.id='$this->campaign_id'"; $campaign_list = $camp->get_full_list("campaigns.name", $where, true); if(!empty($campaign_list)) $this->campaign_name = $campaign_list[0]->name; } } function get_list_view_data(){ /*global $app_list_strings; global $current_user; $temp_array = $this->get_list_view_array(); $temp_array['STATUS'] = (empty($temp_array['STATUS'])) ? '' : $temp_array['STATUS']; $temp_array['ENCODED_NAME']=$this->name; $temp_array['NAME']=$this->name; $temp_array['EMAIL1'] = $this->emailAddress->getPrimaryAddress($this); $this->email1 = $temp_array['EMAIL1']; $temp_array['EMAIL1_LINK'] = $current_user->getEmailLink('email1', $this, '', '', 'ListView'); $temp_array['ACC_NAME_FROM_ACCOUNTS'] = empty($temp_array['ACC_NAME_FROM_ACCOUNTS']) ? ($temp_array['ACCOUNT_NAME']) : ($temp_array['ACC_NAME_FROM_ACCOUNTS']); return $temp_array; */ $this->_create_proper_name_field(); $temp_array = parent::get_list_view_data(); $temp_array['ACC_NAME_FROM_ACCOUNTS'] = empty($temp_array['ACC_NAME_FROM_ACCOUNTS']) ? ($temp_array['ACCOUNT_NAME']) : ($temp_array['ACC_NAME_FROM_ACCOUNTS']); return $temp_array; } /** * Returns an array of fields that are of type link. * * @return array List of fields. * * Internal function, do not override. */ //fix for bug 27339 Shine function get_linked_fields() { $linked_fields=array(); $fieldDefs = $this->getFieldDefinitions(); //find all definitions of type link. if (!empty($fieldDefs)) { foreach ($fieldDefs as $name=>$properties) { if ($name == 'oldmeetings' || $name == 'oldcalls') { continue; } elseif (array_search('link',$properties) === 'type') { $linked_fields[$name]=$properties; } } } return $linked_fields; } /** 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 = $GLOBALS['db']->quote($the_query_string); array_push($where_clauses, "leads.last_name like '$the_query_string%'"); array_push($where_clauses, "leads.account_name like '$the_query_string%'"); array_push($where_clauses, "leads.first_name 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, "leads.phone_home like '%$the_query_string%'"); array_push($where_clauses, "leads.phone_mobile like '%$the_query_string%'"); array_push($where_clauses, "leads.phone_work like '%$the_query_string%'"); array_push($where_clauses, "leads.phone_other like '%$the_query_string%'"); array_push($where_clauses, "leads.phone_fax 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, $lead) { global $app_list_strings; global $locale; $xtpl->assign("LEAD_NAME", $locale->getLocaleFormattedName($lead->first_name, $lead->last_name, $lead->salutation)); $xtpl->assign("LEAD_SOURCE", (isset($lead->lead_source) ? $app_list_strings['lead_source_dom'][$lead->lead_source] : "")); $xtpl->assign("LEAD_STATUS", (isset($lead->status)? $app_list_strings['lead_status_dom'][$lead->status]:"")); $xtpl->assign("LEAD_DESCRIPTION", $lead->description); return $xtpl; } function bean_implements($interface){ switch($interface){ case 'ACL':return true; } return false; } function listviewACLHelper(){ $array_assign = parent::listviewACLHelper(); $is_owner = false; if(!empty($this->account_name)){ if(!empty($this->account_name_owner)){ global $current_user; $is_owner = $current_user->id == $this->account_name_owner; } } if( ACLController::checkAccess('Accounts', 'view', $is_owner)){ $array_assign['ACCOUNT'] = 'a'; }else{ $array_assign['ACCOUNT'] = 'span'; } $is_owner = false; if(!empty($this->opportunity_name)){ if(!empty($this->opportunity_name_owner)){ global $current_user; $is_owner = $current_user->id == $this->opportunity_name_owner; } } if( ACLController::checkAccess('Opportunities', 'view', $is_owner)){ $array_assign['OPPORTUNITY'] = 'a'; }else{ $array_assign['OPPORTUNITY'] = 'span'; } $is_owner = false; if(!empty($this->contact_name)){ if(!empty($this->contact_name_owner)){ global $current_user; $is_owner = $current_user->id == $this->contact_name_owner; } } if( ACLController::checkAccess('Contacts', 'view', $is_owner)){ $array_assign['CONTACT'] = 'a'; }else{ $array_assign['CONTACT'] = 'span'; } return $array_assign; } //carrys forward custom lead fields to contacts, accounts, opportunities during Lead Conversion function convertCustomFieldsForm(&$form, &$tempBean, &$prefix) { global $mod_strings, $app_list_strings, $app_strings, $lbl_required_symbol; foreach($this->field_defs as $field => $value) { if(!empty($value['source']) && $value['source'] == 'custom_fields') { if( !empty($tempBean->field_defs[$field]) AND isset($tempBean->field_defs[$field]) ) { $form .= "".$mod_strings[$tempBean->field_defs[$field]['vname']].":"; if( !empty($tempBean->custom_fields->avail_fields[$field]['required']) AND ( ($tempBean->custom_fields->avail_fields[$field]['required']== 1) OR ($tempBean->custom_fields->avail_fields[$field]['required']== '1') OR ($tempBean->custom_fields->avail_fields[$field]['required']== 'true') OR ($tempBean->custom_fields->avail_fields[$field]['required']== true) ) ) { $form .= " ".$lbl_required_symbol.""; } $form .= ""; $form .= ""; if(isset($value['isMultiSelect']) && $value['isMultiSelect'] == 1){ $this->$field = unencodeMultienum($this->$field); $multiple = "multiple"; $array = '[]'; } else { $multiple = null; $array = null; } if(!empty($value['options']) AND isset($value['options']) ) { $form .= ""; } elseif($value['type'] == 'text' ) { $form .= ""; } elseif($value['type'] == 'date' ) { $form .= " ".SugarThemeRegistry::current()->getImage("jscalendar", "id='jscal_trigger".$field."' align='absmiddle'", null, null, ".gif", $mod_strings['LBL_ENTERDATE'])."' yyyy-mm-dd"; } else { $form .= ""; if($this->custom_fields->avail_fields[$field]['type'] == 'int') { $form .= ""; } elseif($this->custom_fields->avail_fields[$field]['type'] == 'float') { $form .= ""; } } if( !empty($tempBean->custom_fields->avail_fields[$field]['required']) AND ( ($tempBean->custom_fields->avail_fields[$field]['required']== 1) OR ($tempBean->custom_fields->avail_fields[$field]['required']== '1') OR ($tempBean->custom_fields->avail_fields[$field]['required']== 'true') OR ($tempBean->custom_fields->avail_fields[$field]['required']== true) ) ) { $form .= ""; } $form .= ""; } } } return true; } function save($check_notify = false) { if(empty($this->status)) $this->status = 'New'; // call save first so that $this->id will be set $value = parent::save($check_notify); return $value; } function get_unlinked_email_query($type=array()) { return get_unlinked_email_query($type, $this); } /** * Returns query to find the related calls created pre-5.1 * * @return string SQL statement */ public function get_old_related_calls() { $return_array['select']='SELECT calls.id '; $return_array['from']='FROM calls '; $return_array['where']=" WHERE calls.parent_id = '$this->id' AND calls.parent_type = 'Leads' AND calls.id NOT IN ( SELECT call_id FROM calls_leads ) "; $return_array['join'] = ""; $return_array['join_tables'][0] = ''; return $return_array; } /** * Returns array of lead conversion activity options * * @return string SQL statement */ public static function getActivitiesOptions() { if (isset($GLOBALS['app_list_strings']['lead_conv_activity_opt'])) { return $GLOBALS['app_list_strings']['lead_conv_activity_opt']; } else { return array(); } } /** * Returns query to find the related meetings created pre-5.1 * * @return string SQL statement */ public function get_old_related_meetings() { $return_array['select']='SELECT meetings.id '; $return_array['from']='FROM meetings '; $return_array['where']=" WHERE meetings.parent_id = '$this->id' AND meetings.parent_type = 'Leads' AND meetings.id NOT IN ( SELECT meeting_id FROM meetings_leads ) "; $return_array['join'] = ""; $return_array['join_tables'][0] = ''; return $return_array; } } ?>