]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Prospects/Prospect.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Prospects / Prospect.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-2011 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  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 require_once('include/SugarObjects/templates/person/Person.php');
47
48 require_once('include/SugarObjects/templates/person/Person.php');
49
50 class Prospect extends Person {
51     var $field_name_map;
52         // Stored fields
53         var $id;
54         var $name = '';
55         var $date_entered;
56         var $date_modified;
57         var $modified_user_id;
58         var $assigned_user_id;
59         var $created_by;
60         var $created_by_name;
61         var $modified_by_name;
62         var $description;
63         var $salutation;
64         var $first_name;
65         var $last_name;
66         var $full_name;
67         var $title;
68         var $department;
69         var $birthdate;
70         var $do_not_call;
71         var $phone_home;
72         var $phone_mobile;
73         var $phone_work;
74         var $phone_other;
75         var $phone_fax;
76         var $email1;
77         var $email2;
78         var $email_and_name1;
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 $tracker_key;
93         var $lead_id;
94         var $account_name;
95         var $assigned_real_user_name;
96         // These are for related fields
97         var $assigned_user_name;
98         var $module_dir = 'Prospects';
99         var $table_name = "prospects";
100         var $object_name = "Prospect";
101         var $new_schema = true;
102         var $emailAddress;
103         
104         var $importable = true;
105     // This is used to retrieve related fields from form posts.
106         var $additional_column_fields = Array('assigned_user_name');
107
108
109         function Prospect() {
110                 parent::Person();
111         }
112
113     function create_export_query(&$order_by, &$where, $relate_link_join='')
114     {
115         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
116                 if($custom_join)
117                                 $custom_join['join'] .= $relate_link_join;
118                          $query = "SELECT
119                                 prospects.*,email_addresses.email_address email_address,
120                                 users.user_name as assigned_user_name ";
121                                                 if($custom_join){
122                                                         $query .= $custom_join['select'];
123                                                 }
124                                                  $query .= " FROM prospects ";
125                          $query .= "LEFT JOIN users
126                                         ON prospects.assigned_user_id=users.id ";
127
128                                                 //join email address table too.
129                                                 $query .=  ' LEFT JOIN  email_addr_bean_rel on prospects.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'Prospects\' and email_addr_bean_rel.primary_address=1 and email_addr_bean_rel.deleted=0';
130                                                 $query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
131                                                 
132                                                 if($custom_join){
133                                                         $query .= $custom_join['join'];
134                                                 }
135
136                 $where_auto = " prospects.deleted=0 ";
137
138                 if($where != "")
139                         $query .= "where ($where) AND ".$where_auto;
140                 else
141                         $query .= "where ".$where_auto;
142
143                 if(!empty($order_by))
144                         $query .= " ORDER BY $order_by";
145
146                 return $query;
147         }
148
149     
150         function fill_in_additional_list_fields() {
151                 global $locale;
152
153                 $full_name = $locale->getLocaleFormattedName($this->first_name, $this->last_name, $this->salutation, $this->title);
154                 $this->name = $full_name;
155                 $this->full_name = $full_name;
156                 $this->email_and_name1 = $full_name." &lt;".$this->email1."&gt;";
157         }
158
159         function fill_in_additional_detail_fields() {
160                 global $locale;
161         parent::fill_in_additional_detail_fields();
162                 $full_name = $locale->getLocaleFormattedName($this->first_name, $this->last_name, $this->salutation, $this->title);
163                 $this->name = $full_name;
164                 $this->full_name = $full_name;
165         }
166
167         function get_list_view_data() {
168                 global $locale, $current_user;
169
170                 $full_name = $locale->getLocaleFormattedName($this->first_name, $this->last_name, $this->salutation, $this->title);
171                 $this->name = $full_name;
172                 $this->full_name = $full_name;
173
174                 $temp_array = $this->get_list_view_array();
175                 $temp_array["ENCODED_NAME"] = $full_name;
176                 $temp_array["FULL_NAME"] = $full_name;
177                 $temp_array["EMAIL1"] = $this->emailAddress->getPrimaryAddress($this);
178                 $this->email1 = $temp_array['EMAIL1'];
179                 $temp_array["EMAIL1_LINK"] = $current_user->getEmailLink('email1', $this, '', '', 'ListView');
180
181         return $temp_array;
182         }
183
184         /**
185                 builds a generic search based on the query string using or
186                 do not include any $this-> because this is called on without having the class instantiated
187         */
188         function build_generic_where_clause ($the_query_string)
189         {
190                 $where_clauses = Array();
191                 $the_query_string = $GLOBALS['db']->quote($the_query_string);
192
193                 array_push($where_clauses, "prospects.last_name like '$the_query_string%'");
194                 array_push($where_clauses, "prospects.first_name like '$the_query_string%'");
195                 array_push($where_clauses, "prospects.assistant like '$the_query_string%'");
196
197                 if (is_numeric($the_query_string))
198                 {
199                         array_push($where_clauses, "prospects.phone_home like '%$the_query_string%'");
200                         array_push($where_clauses, "prospects.phone_mobile like '%$the_query_string%'");
201                         array_push($where_clauses, "prospects.phone_work like '%$the_query_string%'");
202                         array_push($where_clauses, "prospects.phone_other like '%$the_query_string%'");
203                         array_push($where_clauses, "prospects.phone_fax like '%$the_query_string%'");
204                         array_push($where_clauses, "prospects.assistant_phone like '%$the_query_string%'");
205                 }
206
207                 $the_where = "";
208                 foreach($where_clauses as $clause)
209                 {
210                         if($the_where != "") $the_where .= " or ";
211                         $the_where .= $clause;
212                 }
213
214
215                 return $the_where;
216         }
217
218     function converted_prospect($prospectid, $contactid, $accountid, $opportunityid){
219         $query = "UPDATE prospects set  contact_id=$contactid, account_id=$accountid, opportunity_id=$opportunityid where  id=$prospectid and deleted=0";
220                 $this->db->query($query,true,"Error converting prospect: ");
221                 //todo--status='Converted', converted='1',
222     }
223      function bean_implements($interface){
224                 switch($interface){
225                         case 'ACL':return true;
226                 }
227                 return false;
228         }
229
230     /**
231      *  This method will be used by Mail Merge in order to retieve the targets as specified in the query
232      *  @param query String - this is the query which contains the where clause for the query
233      */
234     function retrieveTargetList($query, $fields, $offset = 0, $limit= -99, $max = -99, $deleted = 0, $module = ''){
235         global  $beanList, $beanFiles;
236         $module_name = $this->module_dir;
237
238         if(empty($module)){
239             $pattern = '/AND related_type = #(.*)#/i';
240             if(preg_match($pattern, $query, $matches) && count($matches) > 1){
241                 $module_name = $matches[1];
242                 $query = preg_replace($pattern, "", $query);
243             }
244              $GLOBALS['log']->debug("PROSPECT QUERY: ".$query);
245         }
246         $GLOBALS['log']->debug(var_export($matches, true));
247         $count = count($fields);
248         $index = 1;
249         $sel_fields = "";
250         if(!empty($fields)){
251             foreach($fields as $field){
252                 if($field == 'id'){
253                         $sel_fields .= 'prospect_lists_prospects.id id';
254                 }else{
255                         $sel_fields .= $module_name.".".$field;
256                 }
257                 if($index < $count){
258                     $sel_fields .= ",";  
259                 }
260                 $index++;
261             }
262         }
263
264         $module_name = ucfirst($module_name);
265         $class_name = $beanList[$module_name];
266         require_once($beanFiles[$class_name]);
267         $seed = new $class_name();
268         if(empty($sel_fields)){
269             $sel_fields = $seed->table_name.'.*';
270         }
271         $select = "SELECT ".$sel_fields." FROM ".$seed->table_name;
272         $select .= " INNER JOIN prospect_lists_prospects ON prospect_lists_prospects.related_id = ".$seed->table_name.".id";
273         $select .= " INNER JOIN prospect_lists ON prospect_lists_prospects.prospect_list_id = prospect_lists.id";
274         $select .= " INNER JOIN prospect_list_campaigns ON prospect_list_campaigns.prospect_list_id = prospect_lists.id";
275         $select .= " INNER JOIN campaigns on campaigns.id = prospect_list_campaigns.campaign_id";
276         $select .= " WHERE prospect_list_campaigns.deleted = 0";
277         $select .= " AND prospect_lists_prospects.deleted = 0";
278         $select .= " AND prospect_lists.deleted = 0";
279         if (!empty($query)) {
280             $select .= " AND ".$query;
281         }
282
283         return $this->process_list_query($select, $offset, $limit, $max, $query);
284     }
285
286     /**
287      *  Given an id, looks up in the prospect_lists_prospects table
288      *  and retrieve the correct type for this id
289      */
290     function retrieveTarget($id){
291         $query = "SELECT related_id, related_type FROM prospect_lists_prospects WHERE id = '".$id."'";
292         $result = $this->db->query($query);
293         if(($row = $this->db->fetchByAssoc($result))){
294              global  $beanList, $beanFiles; 
295              $module_name = $row['related_type'];
296              $class_name = $beanList[$module_name];
297              require_once($beanFiles[$class_name]);
298              $seed = new $class_name();
299              return $seed->retrieve($row['related_id']);        
300         }else{
301             return null;   
302         }
303     }
304
305     
306         function get_unlinked_email_query($type=array()) {
307                 
308                 return get_unlinked_email_query($type, $this);
309         }
310 }