]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/QueueCampaign.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Campaigns / QueueCampaign.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM 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: Schedules email for delivery. emailman table holds emails for delivery.
41  * A cron job polls the emailman table and delivers emails when intended send date time is reached.
42  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43  * All Rights Reserved.
44  * Contributor(s): ______________________________________..
45  ********************************************************************************/
46
47
48
49
50
51 global $timedate;
52 global $current_user;
53 global $mod_strings;
54
55 $campaign = new Campaign();
56 $campaign->retrieve($_REQUEST['record']);
57 $err_messages=array();
58
59 $test=false;
60 if (isset($_REQUEST['mode']) && $_REQUEST['mode'] =='test') {
61         $test=true;
62 }
63
64 //this is to account for the case of sending directly from summary page in wizards
65 $from_wiz =false;
66 if (isset($_REQUEST['wiz_mass'])) {
67     $mass[] = $_REQUEST['wiz_mass'];
68     $_POST['mass'] = $mass;
69     $from_wiz =true;
70 }
71 if (isset($_REQUEST['from_wiz'])) {
72     $from_wiz =true;
73 }
74
75 //if campaign status is 'sending' disallow this step.
76 if (!empty($campaign->status) && $campaign->status == 'sending') {
77         $err_messages[]=$mod_strings['ERR_SENDING_NOW'];
78 }
79 if ($campaign->db->dbType=='oci8') {
80 } else {
81         $current_date= "'".gmdate($GLOBALS['timedate']->get_db_date_time_format())."'";
82 }
83
84 //start scheduling now.....
85 foreach ($_POST['mass'] as $message_id) {
86
87         //fetch email marketing definition.
88         if (!class_exists('EmailMarketing')) require_once('modules/EmailMarketing/EmailMarketing.php');
89      
90         
91         $marketing = new EmailMarketing();
92         $marketing->retrieve($message_id);
93         
94         //make sure that the marketing message has a mailbox.
95         //
96         if (empty($marketing->inbound_email_id)) {
97                 
98                 echo "<p>";
99                 echo "<h4>{$mod_strings['ERR_NO_MAILBOX']}</h4>";
100                 echo "<BR><a href='index.php?module=EmailMarketing&action=EditView&record={$marketing->id}'>$marketing->name</a>";
101                 echo "</p>";
102                 sugar_die('');          
103         }
104
105
106         global $timedate;
107         $mergedvalue=$timedate->merge_date_time($marketing->date_start,$marketing->time_start);
108         
109         if ($campaign->db->dbType=='oci8') {
110         } else {
111                 if ($test) {
112                         $send_date_time="'".gmdate($GLOBALS['timedate']->get_db_date_time_format(), mktime() -60) ."'";         
113                 } else {
114                         $send_date_time= "'".$timedate->to_db_date($mergedvalue) . ' ' .$timedate->to_db_time($mergedvalue)."'";        
115                 }
116         }
117
118         //find all prospect lists associated with this email marketing message.
119         if ($marketing->all_prospect_lists == 1) {
120                 $query="SELECT prospect_lists.id prospect_list_id from prospect_lists ";
121                 $query.=" INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = prospect_lists.id";
122                 $query.=" WHERE plc.campaign_id='{$campaign->id}'"; 
123                 $query.=" AND prospect_lists.deleted=0";
124                 $query.=" AND plc.deleted=0";
125                 if ($test) {
126                         $query.=" AND prospect_lists.list_type='test'";         
127                 } else {
128                         $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";                                        
129                 }
130         } else {
131                 $query="select email_marketing_prospect_lists.* FROM email_marketing_prospect_lists ";
132                 $query.=" inner join prospect_lists on prospect_lists.id = email_marketing_prospect_lists.prospect_list_id";
133                 $query.=" WHERE prospect_lists.deleted=0 and email_marketing_id = '$message_id' and email_marketing_prospect_lists.deleted=0";
134                 
135                 if ($test) {
136                         $query.=" AND prospect_lists.list_type='test'";         
137                 } else {
138                         $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";                                        
139                 }
140         }
141         $result=$campaign->db->query($query);
142         while (($row=$campaign->db->fetchByAssoc($result))!=null ) {
143                 
144
145                 $prospect_list_id=$row['prospect_list_id'];
146                 
147                 //delete all messages for the current campaign and current email marketing message.
148                 $delete_emailman_query="delete from emailman where campaign_id='{$campaign->id}' and marketing_id='{$message_id}' and list_id='{$prospect_list_id}'";
149                 $campaign->db->query($delete_emailman_query);
150                 
151                 $insert_query= "INSERT INTO emailman (date_entered, user_id, campaign_id, marketing_id,list_id, related_id, related_type, send_date_time";
152                 if ($campaign->db->dbType=='oci8') {
153                 }
154                 $insert_query.=')'; 
155                 $insert_query.= " SELECT $current_date,'{$current_user->id}',plc.campaign_id,'{$message_id}',plp.prospect_list_id, plp.related_id, plp.related_type,{$send_date_time} ";  
156                 if ($campaign->db->dbType=='oci8') {
157                 }
158                 $insert_query.= "FROM prospect_lists_prospects plp ";
159                 $insert_query.= "INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = plp.prospect_list_id "; 
160                 $insert_query.= "WHERE plp.prospect_list_id = '{$prospect_list_id}' ";
161                 $insert_query.= "AND plp.deleted=0 ";
162                 $insert_query.= "AND plc.deleted=0 ";
163                 $insert_query.= "AND plc.campaign_id='{$campaign->id}'";
164
165                 if ($campaign->db->dbType=='oci8') {
166                 }
167                 $campaign->db->query($insert_query);
168         }
169 }
170
171 //delete all entries from the emailman table that belong to the exempt list.
172 if (!$test) {   
173     //id based exempt list treatment.
174     if ($campaign->db->dbType =='mysql') {
175         
176         $delete_query = "DELETE emailman.* FROM emailman ";  
177         $delete_query.= "INNER JOIN prospect_lists_prospects plp on plp.related_id = emailman.related_id and  plp.related_type = emailman.related_type ";
178         $delete_query.= "INNER JOIN prospect_lists pl ON pl.id = plp.prospect_list_id ";
179         $delete_query .= "INNER JOIN prospect_list_campaigns plc on plp.prospect_list_id = plc.prospect_list_id ";
180         $delete_query.= "WHERE plp.deleted=0 ";
181         $delete_query.= "AND plc.campaign_id = '{$campaign->id}'"; 
182         $delete_query.= "AND pl.list_type = 'exempt' ";
183         $delete_query.= "AND emailman.campaign_id='{$campaign->id}'";
184         $campaign->db->query($delete_query);
185             
186     }elseif($campaign->db->dbType =='mssql'){
187         $delete_query =  "DELETE FROM emailman ";         
188         $delete_query .= "WHERE emailman.campaign_id='".$campaign->id."' ";
189         $delete_query .= "and emailman.related_id in ";
190         $delete_query .= "(select prospect_lists_prospects.related_id from prospect_lists_prospects where prospect_lists_prospects.prospect_list_id in (select prospect_lists.id from prospect_lists where prospect_lists.list_type = 'exempt' and prospect_lists_prospects.prospect_list_id in(select prospect_list_campaigns.prospect_list_id from prospect_list_campaigns where prospect_list_campaigns.campaign_id = '".$campaign->id."'))) ";
191         $delete_query .= "and emailman.related_type in ";
192         $delete_query .= "(select prospect_lists_prospects.related_type from prospect_lists_prospects where prospect_lists_prospects.prospect_list_id in (select prospect_lists.id from prospect_lists where prospect_lists.list_type = 'exempt' and prospect_lists_prospects.prospect_list_id in(select prospect_list_campaigns.prospect_list_id from prospect_list_campaigns where prospect_list_campaigns.campaign_id = '".$campaign->id."'))) ";
193         $campaign->db->query($delete_query);
194     }
195 }
196
197 $return_module=isset($_REQUEST['return_module'])?$_REQUEST['return_module']:'Campaigns';
198 $return_action=isset($_REQUEST['return_action'])?$_REQUEST['return_action']:'DetailView';
199 $return_id=$_REQUEST['record'];
200
201 if ($test) {
202         //navigate to EmailManDelivery..
203         $header_URL = "Location: index.php?action=EmailManDelivery&module=EmailMan&campaign_id={$_REQUEST['record']}&return_module={$return_module}&return_action={$return_action}&return_id={$return_id}&mode=test";
204     if($from_wiz){$header_URL .= "&from_wiz=true";}
205 } else {
206         //navigate back to campaign detail view...
207         $header_URL = "Location: index.php?action={$return_action}&module={$return_module}&record={$return_id}";
208     if($from_wiz){$header_URL .= "&from=send";}
209 }
210 $GLOBALS['log']->debug("about to post header URL of: $header_URL");
211 header($header_URL);
212 ?>