]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/QueueCampaign.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Campaigns / QueueCampaign.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: 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 $current_date = $campaign->db->now();
80
81 //start scheduling now.....
82 foreach ($_POST['mass'] as $message_id) {
83
84         //fetch email marketing definition.
85         if (!class_exists('EmailMarketing')) require_once('modules/EmailMarketing/EmailMarketing.php');
86
87
88         $marketing = new EmailMarketing();
89         $marketing->retrieve($message_id);
90
91         //make sure that the marketing message has a mailbox.
92         //
93         if (empty($marketing->inbound_email_id)) {
94
95                 echo "<p>";
96                 echo "<h4>{$mod_strings['ERR_NO_MAILBOX']}</h4>";
97                 echo "<BR><a href='index.php?module=EmailMarketing&action=EditView&record={$marketing->id}'>$marketing->name</a>";
98                 echo "</p>";
99                 sugar_die('');
100         }
101
102
103         global $timedate;
104         $mergedvalue=$timedate->merge_date_time($marketing->date_start,$marketing->time_start);
105         if($test) {
106             $send_date_time = $timedate->getNow()->get("-60 seconds")->asDb();
107         } else {
108             $send_date_time = $timedate->to_db($mergedvalue);
109         }
110     $send_date_time = $campaign->db->convert($campaign->db->quoted($send_date_time), "datetime");
111
112         //find all prospect lists associated with this email marketing message.
113         if ($marketing->all_prospect_lists == 1) {
114                 $query="SELECT prospect_lists.id prospect_list_id from prospect_lists ";
115                 $query.=" INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = prospect_lists.id";
116                 $query.=" WHERE plc.campaign_id='{$campaign->id}'";
117                 $query.=" AND prospect_lists.deleted=0";
118                 $query.=" AND plc.deleted=0";
119                 if ($test) {
120                         $query.=" AND prospect_lists.list_type='test'";
121                 } else {
122                         $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
123                 }
124         } else {
125                 $query="select email_marketing_prospect_lists.* FROM email_marketing_prospect_lists ";
126                 $query.=" inner join prospect_lists on prospect_lists.id = email_marketing_prospect_lists.prospect_list_id";
127                 $query.=" WHERE prospect_lists.deleted=0 and email_marketing_id = '$message_id' and email_marketing_prospect_lists.deleted=0";
128
129                 if ($test) {
130                         $query.=" AND prospect_lists.list_type='test'";
131                 } else {
132                         $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
133                 }
134         }
135         $result=$campaign->db->query($query);
136         while (($row=$campaign->db->fetchByAssoc($result))!=null ) {
137                 $prospect_list_id=$row['prospect_list_id'];
138
139                 //delete all messages for the current campaign and current email marketing message.
140                 $delete_emailman_query="delete from emailman where campaign_id='{$campaign->id}' and marketing_id='{$message_id}' and list_id='{$prospect_list_id}'";
141                 $campaign->db->query($delete_emailman_query);
142         $auto = $campaign->db->getAutoIncrementSQL("emailman", "id");
143
144                 $insert_query= "INSERT INTO emailman (date_entered, user_id, campaign_id, marketing_id,list_id, related_id, related_type, send_date_time";
145                 $insert_query.= empty($auto)?"":",id";
146                 $insert_query.=')';
147                 $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}";
148                 $insert_query.= empty($auto)?"":",$auto";
149                 $insert_query.= " FROM prospect_lists_prospects plp ";
150                 $insert_query.= "INNER JOIN prospect_list_campaigns plc ON plc.prospect_list_id = plp.prospect_list_id ";
151                 $insert_query.= "WHERE plp.prospect_list_id = '{$prospect_list_id}' ";
152                 $insert_query.= "AND plp.deleted=0 ";
153                 $insert_query.= "AND plc.deleted=0 ";
154                 $insert_query.= "AND plc.campaign_id='{$campaign->id}'";
155
156                 $campaign->db->query($insert_query);
157         }
158 }
159
160 //delete all entries from the emailman table that belong to the exempt list.
161 //TODO:SM: may want to move this to query clause above instead
162 if (!$test) {
163     $delete_query =  "DELETE FROM emailman WHERE emailman.campaign_id='{$campaign->id}' AND (emailman.related_id, emailman.related_type) IN
164         (SELECT plp.related_id, plp.related_type FROM prospect_lists_prospects plp
165                 INNER JOIN prospect_lists pl ON pl.id = plp.prospect_list_id
166                 INNER JOIN prospect_list_campaigns plc ON plp.prospect_list_id = plc.prospect_list_id
167                 WHERE plp.deleted = 0 AND plc.deleted = 0 AND pl.deleted = 0 AND pl.list_type = 'exempt' AND  plc.campaign_id = '{$campaign->id}')
168         ";
169     $campaign->db->query($delete_query);
170 }
171
172 $return_module=isset($_REQUEST['return_module'])?$_REQUEST['return_module']:'Campaigns';
173 $return_action=isset($_REQUEST['return_action'])?$_REQUEST['return_action']:'DetailView';
174 $return_id=$_REQUEST['record'];
175
176 if ($test) {
177         //navigate to EmailManDelivery..
178         $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";
179     if($from_wiz){$header_URL .= "&from_wiz=true";}
180 } else {
181         //navigate back to campaign detail view...
182         $header_URL = "Location: index.php?action={$return_action}&module={$return_module}&record={$return_id}";
183     if($from_wiz){$header_URL .= "&from=send";}
184 }
185 $GLOBALS['log']->debug("about to post header URL of: $header_URL");
186 header($header_URL);
187 ?>