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-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
41 ********************************************************************************/
49 class ProspectList extends SugarBean {
56 var $modified_user_id;
57 var $assigned_user_id;
60 var $modified_by_name;
68 var $assigned_user_name;
73 // module name definitions and table relations
74 var $table_name = "prospect_lists";
75 var $module_dir = 'ProspectLists';
76 var $rel_prospects_table = "prospect_lists_prospects";
77 var $object_name = "ProspectList";
79 // This is used to retrieve related fields from form posts.
80 var $additional_column_fields = array(
81 'assigned_user_name', 'assigned_user_id', 'campaign_id',
83 var $relationship_fields = array(
84 'campaign_id'=>'campaigns',
85 'prospect_list_prospects' => 'prospects',
90 function ProspectList() {
96 var $new_schema = true;
98 function get_summary_text()
100 return "$this->name";
103 function create_list_query($order_by, $where, $show_deleted = 0)
105 $custom_join = $this->getCustomJoin();
108 $query .= "users.user_name as assigned_user_name, ";
109 $query .= "prospect_lists.*";
111 $query .= $custom_join['select'];
112 $query .= " FROM prospect_lists ";
114 $query .= "LEFT JOIN users
115 ON prospect_lists.assigned_user_id=users.id ";
117 $query .= $custom_join['join'];
120 if($show_deleted == 0){
121 $where_auto = "$this->table_name.deleted=0";
122 }else if($show_deleted == 1){
123 $where_auto = "$this->table_name.deleted=1";
127 $query .= "where $where AND ".$where_auto;
129 $query .= "where ".$where_auto;
132 $query .= " ORDER BY $order_by";
134 $query .= " ORDER BY prospect_lists.name";
140 function create_export_query($order_by, $where)
145 users.user_name as assigned_user_name ";
146 $query .= "FROM prospect_lists ";
147 $query .= "LEFT JOIN users
148 ON prospect_lists.assigned_user_id=users.id ";
150 $where_auto = " prospect_lists.deleted=0";
153 $query .= " WHERE $where AND ".$where_auto;
155 $query .= " WHERE ".$where_auto;
158 $query .= " ORDER BY $order_by";
160 $query .= " ORDER BY prospect_lists.name";
164 function create_export_members_query($record_id)
166 $leads_query = "SELECT l.id AS id, 'Leads' AS related_type, '' AS \"name\", l.first_name AS first_name, l.last_name AS last_name, l.title AS title, l.salutation AS salutation,
167 l.primary_address_street AS primary_address_street,l.primary_address_city AS primary_address_city, l.primary_address_state AS primary_address_state, l.primary_address_postalcode AS primary_address_postalcode, l.primary_address_country AS primary_address_country,
168 l.account_name AS account_name,
169 ea.email_address AS primary_email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
170 l.do_not_call AS do_not_call, l.phone_fax AS phone_fax, l.phone_other AS phone_other, l.phone_home AS phone_home, l.phone_mobile AS phone_mobile, l.phone_work AS phone_work
171 FROM prospect_lists_prospects plp
172 INNER JOIN leads l ON plp.related_id=l.id
173 LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=l.id AND ear.deleted=0
174 LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
175 WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
177 AND (ear.deleted=0 OR ear.deleted IS NULL)";
179 $users_query = "SELECT u.id AS id, 'Users' AS related_type, '' AS \"name\", u.first_name AS first_name, u.last_name AS last_name,u.title AS title, '' AS salutation,
180 u.address_street AS primary_address_street,u.address_city AS primary_address_city, u.address_state AS primary_address_state, u.address_postalcode AS primary_address_postalcode, u.address_country AS primary_address_country,
182 ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
183 0 AS do_not_call, u.phone_fax AS phone_fax, u.phone_other AS phone_other, u.phone_home AS phone_home, u.phone_mobile AS phone_mobile, u.phone_work AS phone_work
184 FROM prospect_lists_prospects plp
185 INNER JOIN users u ON plp.related_id=u.id
186 LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=u.id AND ear.deleted=0
187 LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
188 WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
190 AND (ear.deleted=0 OR ear.deleted IS NULL)";
192 $contacts_query = "SELECT c.id AS id, 'Contacts' AS related_type, '' AS \"name\", c.first_name AS first_name, c.last_name AS last_name,c.title AS title, c.salutation AS salutation,
193 c.primary_address_street AS primary_address_street,c.primary_address_city AS primary_address_city, c.primary_address_state AS primary_address_state, c.primary_address_postalcode AS primary_address_postalcode, c.primary_address_country AS primary_address_country,
194 a.name AS account_name,
195 ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
196 c.do_not_call AS do_not_call, c.phone_fax AS phone_fax, c.phone_other AS phone_other, c.phone_home AS phone_home, c.phone_mobile AS phone_mobile, c.phone_work AS phone_work
197 FROM prospect_lists_prospects plp
198 INNER JOIN contacts c ON plp.related_id=c.id LEFT JOIN accounts_contacts ac ON ac.contact_id=c.id LEFT JOIN accounts a ON ac.account_id=a.id
199 LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=c.id AND ear.deleted=0
200 LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
201 WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
203 AND (ear.deleted=0 OR ear.deleted IS NULL)";
205 $prospects_query = "SELECT p.id AS id, 'Prospects' AS related_type, '' AS \"name\", p.first_name AS first_name, p.last_name AS last_name,p.title AS title, p.salutation AS salutation,
206 p.primary_address_street AS primary_address_street,p.primary_address_city AS primary_address_city, p.primary_address_state AS primary_address_state, p.primary_address_postalcode AS primary_address_postalcode, p.primary_address_country AS primary_address_country,
207 p.account_name AS account_name,
208 ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
209 p.do_not_call AS do_not_call, p.phone_fax AS phone_fax, p.phone_other AS phone_other, p.phone_home AS phone_home, p.phone_mobile AS phone_mobile, p.phone_work AS phone_work
210 FROM prospect_lists_prospects plp
211 INNER JOIN prospects p ON plp.related_id=p.id
212 LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=p.id AND ear.deleted=0
213 LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
214 WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
216 AND (ear.deleted=0 OR ear.deleted IS NULL)";
218 $accounts_query = "SELECT a.id AS id, 'Accounts' AS related_type, a.name AS \"name\", '' AS first_name, '' AS last_name,'' AS title, '' AS salutation,
219 a.billing_address_street AS primary_address_street,a.billing_address_city AS primary_address_city, a.billing_address_state AS primary_address_state, a.billing_address_postalcode AS primary_address_postalcode, a.billing_address_country AS primary_address_country,
221 ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
222 0 AS do_not_call, a.phone_fax as phone_fax, a.phone_alternate AS phone_other, '' AS phone_home, '' AS phone_mobile, a.phone_office AS phone_office
223 FROM prospect_lists_prospects plp
224 INNER JOIN accounts a ON plp.related_id=a.id
225 LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=a.id AND ear.deleted=0
226 LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
227 WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
229 AND (ear.deleted=0 OR ear.deleted IS NULL)";
230 $order_by = "ORDER BY related_type, id, primary_address DESC";
231 $query = "$leads_query UNION ALL $users_query UNION ALL $contacts_query UNION ALL $prospects_query UNION ALL $accounts_query $order_by";
235 function save_relationship_changes($is_update)
237 parent::save_relationship_changes($is_update);
238 if($this->lead_id != "")
239 $this->set_prospect_relationship($this->id, $this->lead_id, "lead");
240 if($this->contact_id != "")
241 $this->set_prospect_relationship($this->id, $this->contact_id, "contact");
242 if($this->prospect_id != "")
243 $this->set_prospect_relationship($this->id, $this->contact_id, "prospect");
246 function set_prospect_relationship($prospect_list_id, &$link_ids, $link_name)
248 $link_field = sprintf("%s_id", $link_name);
250 foreach($link_ids as $link_id)
252 $this->set_relationship('prospect_lists_prospects', array( $link_field=>$link_id, 'prospect_list_id'=>$prospect_list_id ));
256 function set_prospect_relationship_single($prospect_list_id, $link_id, $link_name)
258 $link_field = sprintf("%s_id", $link_name);
260 $this->set_relationship('prospect_lists_prospects', array( $link_field=>$link_id, 'prospect_list_id'=>$prospect_list_id ));
264 function clear_prospect_relationship($prospect_list_id, $link_id, $link_name)
266 $link_field = sprintf("%s_id", $link_name);
267 $where_clause = " AND $link_field = '$link_id' ";
269 $query = sprintf("DELETE FROM prospect_lists_prospects WHERE prospect_list_id='%s' AND deleted = '0' %s", $prospect_list_id, $where_clause);
271 $this->db->query($query, true, "Error clearing prospect/prospect_list relationship: ");
275 function mark_relationships_deleted($id)
279 function fill_in_additional_list_fields()
283 function fill_in_additional_detail_fields()
285 parent::fill_in_additional_detail_fields();
286 $this->entry_count = $this->get_entry_count();
290 function update_currency_id($fromid, $toid){
294 function get_entry_count()
296 $query = "SELECT count(*) AS num FROM prospect_lists_prospects WHERE prospect_list_id='$this->id' AND deleted = '0'";
297 $result = $this->db->query($query, true, "Grabbing prospect_list entry count");
299 $row = $this->db->fetchByAssoc($result);
308 function get_list_view_data(){
309 $temp_array = $this->get_list_view_array();
310 $temp_array["ENTRY_COUNT"] = $this->get_entry_count();
314 builds a generic search based on the query string using or
315 do not include any $this-> because this is called on without having the class instantiated
317 function build_generic_where_clause ($the_query_string)
319 $where_clauses = Array();
320 $the_query_string = $GLOBALS['db']->quote($the_query_string);
321 array_push($where_clauses, "prospect_lists.name like '$the_query_string%'");
324 foreach($where_clauses as $clause)
326 if($the_where != "") $the_where .= " or ";
327 $the_where .= $clause;
334 function save($check_notify = FALSE) {
336 return parent::save($check_notify);
340 function mark_deleted($id){
341 $query = "UPDATE prospect_lists_prospects SET deleted = 1 WHERE prospect_list_id = '{$id}' ";
342 $this->db->query($query);
343 return parent::mark_deleted($id);
346 function bean_implements($interface){
348 case 'ACL':return true;