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.
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 /*********************************************************************************
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 ********************************************************************************/
55 $campaign = new Campaign();
56 $campaign->retrieve($_REQUEST['record']);
57 $err_messages=array();
60 if (isset($_REQUEST['mode']) && $_REQUEST['mode'] =='test') {
64 //this is to account for the case of sending directly from summary page in wizards
66 if (isset($_REQUEST['wiz_mass'])) {
67 $mass[] = $_REQUEST['wiz_mass'];
68 $_POST['mass'] = $mass;
71 if (isset($_REQUEST['from_wiz'])) {
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'];
79 if ($campaign->db->dbType=='oci8') {
81 $current_date= "'".$timedate->nowDb()."'";
84 //start scheduling now.....
85 foreach ($_POST['mass'] as $message_id) {
87 //fetch email marketing definition.
88 if (!class_exists('EmailMarketing')) require_once('modules/EmailMarketing/EmailMarketing.php');
91 $marketing = new EmailMarketing();
92 $marketing->retrieve($message_id);
94 //make sure that the marketing message has a mailbox.
96 if (empty($marketing->inbound_email_id)) {
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>";
107 $mergedvalue=$timedate->merge_date_time($marketing->date_start,$marketing->time_start);
109 if ($campaign->db->dbType=='oci8') {
112 $send_date_time="'".$timedate->getNow()->get("-60 seconds")->asDb() ."'";
114 $send_date_time= "'".$timedate->to_db($mergedvalue)."'";
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";
126 $query.=" AND prospect_lists.list_type='test'";
128 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
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";
136 $query.=" AND prospect_lists.list_type='test'";
138 $query.=" AND prospect_lists.list_type!='test' AND prospect_lists.list_type not like 'exempt%'";
141 $result=$campaign->db->query($query);
142 while (($row=$campaign->db->fetchByAssoc($result))!=null ) {
145 $prospect_list_id=$row['prospect_list_id'];
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);
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') {
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') {
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}'";
165 if ($campaign->db->dbType=='oci8') {
167 $campaign->db->query($insert_query);
171 //delete all entries from the emailman table that belong to the exempt list.
173 //id based exempt list treatment.
174 if ($campaign->db->dbType =='mysql') {
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);
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.deleted = 0 and 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.deleted = 0 and 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);
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'];
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";}
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";}
210 $GLOBALS['log']->debug("about to post header URL of: $header_URL");