]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/Campaign.php
Release 6.2.3
[Github/sugarcrm.git] / modules / Campaigns / Campaign.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:
41  ********************************************************************************/
42
43
44
45
46
47
48
49
50 class Campaign extends SugarBean {
51         var $field_name_map;
52         
53         // Stored fields
54         var $id;
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 $currency_id;
62         var $modified_by_name;
63         var $name;
64         var $start_date;
65         var $end_date;
66         var $status;
67         var $expected_cost;
68         var $budget;
69         var $actual_cost;
70         var $expected_revenue;
71         var $campaign_type;
72         var $objective;
73         var $content;
74         var $tracker_key;
75         var $tracker_text;
76         var $tracker_count;
77         var $refer_url;
78     var $impressions;
79         
80         // These are related
81         var $assigned_user_name;
82
83         // module name definitions and table relations
84         var $table_name = "campaigns";
85         var $rel_prospect_list_table = "prospect_list_campaigns";
86         var $object_name = "Campaign";
87         var $module_dir = 'Campaigns';
88         var $importable = true;
89     
90         // This is used to retrieve related fields from form posts.
91         var $additional_column_fields = array(
92                                 'assigned_user_name', 'assigned_user_id',
93         );
94
95         var $relationship_fields = Array('prospect_list_id'=>'prospect_lists');
96     
97         function Campaign() {
98                 global $sugar_config;
99                 parent::SugarBean();
100         }
101
102         var $new_schema = true;
103         
104         function list_view_parse_additional_sections(&$listTmpl) {
105                 global $locale;
106                 
107                 // take $assigned_user_id and get the Username value to assign
108                 $assId = $this->getFieldValue('assigned_user_id');
109                 
110                 $query = "SELECT first_name, last_name FROM users WHERE id = '".$assId."'";
111                 $result = $this->db->query($query);
112                 $user = $this->db->fetchByAssoc($result);
113                 
114                 //_ppd($user);
115                 if(!empty($user)) {
116                         $fullName = $locale->getLocaleFormattedName($user->first_name, $user->last_name);
117                         $listTmpl->assign('ASSIGNED_USER_NAME', $fullName);
118                 }
119         }
120         
121
122         function get_summary_text()
123         {
124                 return "$this->name";
125         }
126
127         function create_export_query(&$order_by, &$where, $relate_link_join='')
128         {
129                 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
130                         if($custom_join)
131                                 $custom_join['join'] .= $relate_link_join;
132             $query = "SELECT
133             campaigns.*,
134             users.user_name as assigned_user_name ";
135                 if($custom_join){
136                                 $query .=  $custom_join['select'];
137                         }
138                 $query .= " FROM campaigns ";
139                         $query .= "LEFT JOIN users
140                       ON campaigns.assigned_user_id=users.id";
141                 if($custom_join){
142                                 $query .=  $custom_join['join'];
143                         }
144
145                 $where_auto = " campaigns.deleted=0";
146
147         if($where != "")
148                 $query .= " where $where AND ".$where_auto;
149         else
150                 $query .= " where ".$where_auto;
151
152         if($order_by != "")
153                 $query .= " ORDER BY $order_by";
154         else
155                 $query .= " ORDER BY campaigns.name";
156         return $query;
157     }
158
159
160
161         function clear_campaign_prospect_list_relationship($campaign_id, $prospect_list_id='')
162         {
163                 if(!empty($prospect_list_id))
164                         $prospect_clause = " and prospect_list_id = '$prospect_list_id' ";
165                 else
166                         $prospect_clause = '';
167                         
168                 $query = "DELETE FROM $this->rel_prospect_list_table WHERE campaign_id='$campaign_id' AND deleted = '0' " . $prospect_clause;
169                 $this->db->query($query, true, "Error clearing campaign to prospect_list relationship: ");
170         }
171         
172                 
173
174         function mark_relationships_deleted($id)
175         {
176                 $this->clear_campaign_prospect_list_relationship($id);
177         }
178
179         function fill_in_additional_list_fields()
180         {
181                 parent::fill_in_additional_list_fields();
182         }
183
184         function fill_in_additional_detail_fields()
185         {
186         parent::fill_in_additional_detail_fields();             
187                 //format numbers.
188                 
189                 //don't need additional formatting here.
190                 //$this->budget=format_number($this->budget);
191                 //$this->expected_cost=format_number($this->expected_cost);
192                 //$this->actual_cost=format_number($this->actual_cost);
193                 //$this->expected_revenue=format_number($this->expected_revenue);
194         }
195
196         
197         function update_currency_id($fromid, $toid){
198         }
199
200
201         function get_list_view_data(){
202
203                 $temp_array = $this->get_list_view_array();
204                 if ($this->campaign_type != 'Email') {
205                         $temp_array['OPTIONAL_LINK']="display:none";
206                 }
207                 $temp_array['TRACK_CAMPAIGN_TITLE'] = translate("LBL_TRACK_BUTTON_TITLE",'Campaigns');
208                 $temp_array['TRACK_CAMPAIGN_IMAGE'] = SugarThemeRegistry::current()->getImageURL('view_status.gif');
209                 $temp_array['LAUNCH_WIZARD_TITLE'] = translate("LBL_TO_WIZARD_TITLE",'Campaigns');
210                 $temp_array['LAUNCH_WIZARD_IMAGE'] = SugarThemeRegistry::current()->getImageURL('edit_wizard.gif');
211                 
212                 return $temp_array;
213         }
214         /**
215                 builds a generic search based on the query string using or
216                 do not include any $this-> because this is called on without having the class instantiated
217         */
218         function build_generic_where_clause ($the_query_string) 
219         {
220                 $where_clauses = Array();
221                 $the_query_string = $this->db->quote($the_query_string);
222                 array_push($where_clauses, "campaigns.name like '$the_query_string%'");
223
224                 $the_where = "";
225                 foreach($where_clauses as $clause)
226                 {
227                         if($the_where != "") $the_where .= " or ";
228                         $the_where .= $clause;
229                 }
230
231
232                 return $the_where;
233         }
234
235         function save($check_notify = FALSE) {
236                 
237                         //US DOLLAR
238                         if(isset($this->amount) && !empty($this->amount)){
239
240                                 $currency = new Currency();
241                                 $currency->retrieve($this->currency_id);
242                                 $this->amount_usdollar = $currency->convertToDollar($this->amount);
243
244                         }
245                         
246                 $this->unformat_all_fields();
247                         
248                 return parent::save($check_notify);
249
250         }
251
252
253         function mark_deleted($id){
254         $query = "update contacts set campaign_id = null where campaign_id = '{$id}' ";
255         $this->db->query($query);
256                 return parent::mark_deleted($id);
257         }
258
259         function set_notification_body($xtpl, $camp)
260         {
261                 $xtpl->assign("CAMPAIGN_NAME", $camp->name);
262                 $xtpl->assign("CAMPAIGN_AMOUNT", $camp->budget);
263                 $xtpl->assign("CAMPAIGN_CLOSEDATE", $camp->end_date);
264                 $xtpl->assign("CAMPAIGN_STATUS", $camp->status);
265                 $xtpl->assign("CAMPAIGN_DESCRIPTION", $camp->content);
266
267                 return $xtpl;
268         }
269
270         function track_log_entries($type=array()) {
271         //get arguments being passed in
272         $args = func_get_args();
273         $mkt_id ='';
274         
275                 $this->load_relationship('log_entries');
276                 $query_array = $this->log_entries->getQuery(true);
277         
278         //if one of the arguments is marketing ID, then we need to filter by it
279         foreach($args as $arg){
280             if(isset($arg['EMAIL_MARKETING_ID_VALUE'])){
281                 $mkt_id = $arg['EMAIL_MARKETING_ID_VALUE'];
282             }
283             
284             if(isset($arg['group_by'])) {
285                 $query_array['group_by'] = $arg['group_by'];
286             }            
287         }
288         
289         
290         
291                 if (empty($type)) 
292                         $type[0]='targeted';
293
294                 $query_array['select'] ="SELECT campaign_log.* ";
295                 $query_array['where'] = $query_array['where']. " AND activity_type='{$type[0]}' AND archived=0";
296         //add filtering by marketing id, if it exists
297         if (!empty($mkt_id)) $query_array['where'] = $query_array['where']. " AND marketing_id ='$mkt_id' ";
298                 
299         //B.F. #37943 
300         if( isset($query_array['group_by']) && $this->db->dbType != 'mysql' ) 
301         { 
302                         //perform the inner join with the group by if a marketing id is defined, which means we need to filter out duplicates.
303                         //if no marketing id is specified then we are displaying results from multiple marketing emails and it is understood there might be duplicate target entries
304                         if (!empty($mkt_id)){
305                                 $group_by = str_replace("campaign_log", "cl", $query_array['group_by']);
306                                 $join_where = str_replace("campaign_log", "cl", $query_array['where']);
307                                 $query_array['from'] .= " INNER JOIN (select min(id) as id from campaign_log cl $join_where GROUP BY $group_by  ) secondary
308                                         on campaign_log.id = secondary.id       ";
309                         }
310             unset($query_array['group_by']);
311         }
312         else if(isset($query_array['group_by'])) {
313            $query_array['where'] = $query_array['where'] . ' GROUP BY ' . $query_array['group_by'];
314            unset($query_array['group_by']);
315         }
316        
317         $query = (implode(" ",$query_array));
318         return $query;     
319         }
320
321
322         function get_queue_items() {
323         //get arguments being passed in
324         $args = func_get_args();
325         $mkt_id ='';
326
327         $this->load_relationship('queueitems');
328                 $query_array = $this->queueitems->getQuery(true);        
329         
330         //if one of the arguments is marketing ID, then we need to filter by it
331         foreach($args as $arg){
332             if(isset($arg['EMAIL_MARKETING_ID_VALUE'])){
333                 $mkt_id = $arg['EMAIL_MARKETING_ID_VALUE'];
334             }
335             
336             if(isset($arg['group_by'])) {
337                 $query_array['group_by'] = $arg['group_by'];
338             }
339         }
340                                 
341         //add filtering by marketing id, if it exists, and if where key is not empty
342         if (!empty($mkt_id) && !empty($query_array['where'])){
343              $query_array['where'] = $query_array['where']. " AND marketing_id ='$mkt_id' ";
344         }
345
346                 //get select query from email man
347                 $man = new EmailMan();
348                 $listquery= $man->create_queue_items_query('',str_replace(array("WHERE","where"),"",$query_array['where']),null,$query_array);  
349                 return $listquery;
350                 
351         }
352 //      function get_prospect_list_entries() {
353 //              $this->load_relationship('prospectlists');
354 //              $query_array = $this->prospectlists->getQuery(true);
355 //
356 //              $query=<<<EOQ
357 //                      SELECT distinct prospect_lists.*,
358 //                      (case  when (email_marketing.id is null) then default_message.id else email_marketing.id end) marketing_id, 
359 //                      (case  when  (email_marketing.id is null) then default_message.name else email_marketing.name end) marketing_name
360 //      
361 //                      FROM prospect_lists 
362 //
363 //                      INNER JOIN prospect_list_campaigns ON (prospect_lists.id=prospect_list_campaigns.prospect_list_id AND prospect_list_campaigns.campaign_id='{$this->id}')
364 //              
365 //                      LEFT JOIN email_marketing on email_marketing.message_for = prospect_lists.id and email_marketing.campaign_id = '{$this->id}'
366 //                      and email_marketing.deleted =0 and email_marketing.status='active'
367 //
368 //                      LEFT JOIN email_marketing default_message on default_message.message_for = prospect_list_campaigns.campaign_id and 
369 //                      default_message.campaign_id = '{$this->id}' and default_message.deleted =0 
370 //                      and default_message.status='active'
371 //
372 //                      WHERE prospect_list_campaigns.deleted=0 AND prospect_lists.deleted=0
373 //
374 //EOQ;
375 //              return $query;
376 //      }
377         
378          function bean_implements($interface){
379                 switch($interface){
380                         case 'ACL':return true;
381                 }
382                 return false;
383         }
384         
385         
386         /**
387          * create_list_count_query
388          * Overrode this method from SugarBean to handle the distinct parameter used to filter out
389          * duplicate entries for some of the subpanel listivews.  Without the distinct filter, the
390          * list count would be inaccurate because one-to-many email_marketing entries may be associated
391          * with a campaign.
392      *
393      * @param string $query Select query string
394      * @param array $param array of arguments
395      * @return string count query
396      *
397          */
398     function create_list_count_query($query, $params=array())
399     {
400                 //include the distinct filter if a marketing id is defined, which means we need to filter out duplicates by the passed in group by.
401                 //if no marketing id is specified, it is understood there might be duplicate target entries so no need to filter out
402                 if((strpos($query,'marketing_id') !== false )&& isset($params['distinct'])) {
403                    $pattern = '/SELECT(.*?)(\s){1}FROM(\s){1}/is';  // ignores the case
404            $replacement = 'SELECT COUNT(DISTINCT ' . $params['distinct'] . ') c FROM ';
405            $query = preg_replace($pattern, $replacement, $query, 1);
406            return $query;
407                 }
408                 
409                 //If distinct parameter not found, default to SugarBean's function
410         return parent::create_list_count_query($query);
411     }   
412 }
413 ?>