]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/WizardNewsletterSave.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Campaigns / WizardNewsletterSave.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-2012 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:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45 require_once('include/formbase.php');
46
47 global $mod_strings;
48
49     //create new campaign bean and populate
50     $campaign_focus = new Campaign();
51     if(isset($_REQUEST['record'])) {
52         $campaign_focus->retrieve($_REQUEST['record']);
53     }
54
55     $camp_steps[] = 'wiz_step1_';
56     $camp_steps[] = 'wiz_step2_';
57
58     $campaign_focus = populateFromPost('', $campaign_focus);
59
60     foreach($camp_steps as $step){
61        $campaign_focus =  populate_wizard_bean_from_request($campaign_focus,$step);
62     }
63
64     //save here so we can link relationships
65         $campaign_focus->save();
66         $GLOBALS['log']->debug("Saved record with id of ".$campaign_focus->id);
67
68     //process prospect lists
69
70         //process subscription lists if this is a newsletter
71         if($campaign_focus->campaign_type =='NewsLetter'){
72             $pl_list = process_subscriptions_from_request($campaign_focus->name);
73
74             $campaign_focus->load_relationship('prospectlists');
75             $existing_pls = $campaign_focus->prospectlists->get();
76             $ui_ids = array();
77
78             //for each list returned, add the list to the relationship
79             foreach($pl_list as $pl){
80                 $campaign_focus->prospectlists->add($pl->id);
81                 //populate array with id's from UI'
82                 $ui_ids[] = $pl->id;
83             }
84
85             //now remove the lists that may have existed before, but were not specified in UI.
86             //this will enforce that Newsletters only have 3 available target lists.
87             foreach($existing_pls as $pl_del){
88                 if (!in_array($pl_del, $ui_ids)){
89                     $campaign_focus->prospectlists->delete($campaign_focus->id, $pl_del);
90                 }
91             }
92         }else{
93             //process target lists if this is not a newsletter
94             //remove Target Lists if defined
95
96             if(isset($_REQUEST['wiz_remove_target_list'])){
97
98                 $remove_target_strings = explode(",", $_REQUEST['wiz_remove_target_list']);
99                 foreach($remove_target_strings as $remove_trgt_string){
100                         if(!empty($remove_trgt_string)){
101                         //load relationship and add to the list
102                             $campaign_focus->load_relationship('prospectlists');
103                             $campaign_focus->prospectlists->delete($campaign_focus->id,$remove_trgt_string);
104                         }
105                 }
106             }
107
108
109     //create new campaign tracker and save if defined
110     if(isset($_REQUEST['wiz_list_of_targets'])){
111         $target_strings = explode(",", $_REQUEST['wiz_list_of_targets']);
112         foreach($target_strings as $trgt_string){
113             $target_values = explode("@@", $trgt_string);
114             if(count($target_values)==3){
115
116                 if(!empty($target_values[0])){
117                     //this is a selected target, as the id is already populated, retrieve and link
118                     $trgt_focus = new ProspectList();
119                     $trgt_focus->retrieve($target_values[0]);
120
121                     //load relationship and add to the list
122                     $campaign_focus->load_relationship('prospectlists');
123                     $campaign_focus->prospectlists->add($trgt_focus ->id);
124                 }else{
125
126                     //this is a new target, as the id is not populated, need to create and link
127                     $trgt_focus = new ProspectList();
128                     $trgt_focus->name = $target_values[1];
129                     $trgt_focus->list_type = $target_values[2];
130                     $trgt_focus->save();
131
132                     //load relationship and add to the list
133                     $campaign_focus->load_relationship('prospectlists');
134                     $campaign_focus->prospectlists->add($trgt_focus->id);
135                 }
136
137             }
138
139
140         }
141     }
142
143
144         }
145
146
147
148     //remove campaign trackers if defined
149     if(isset($_REQUEST['wiz_remove_tracker_list'])){
150
151         $remove_tracker_strings = explode(",", $_REQUEST['wiz_remove_tracker_list']);
152         foreach($remove_tracker_strings as $remove_trkr_string){
153                 if(!empty($remove_trkr_string)){
154                 //load relationship and add to the list
155                     $campaign_focus->load_relationship('tracked_urls');
156                     $campaign_focus->tracked_urls->delete($campaign_focus->id,$remove_trkr_string);
157                 }
158         }
159     }
160
161
162     //save  campaign trackers and save if defined
163     if(isset($_REQUEST['wiz_list_of_existing_trackers'])){
164         $tracker_strings = explode(",", $_REQUEST['wiz_list_of_existing_trackers']);
165         foreach($tracker_strings as $trkr_string){
166             $tracker_values = explode("@@", $trkr_string);
167             $ct_focus = new CampaignTracker();
168             $ct_focus->retrieve($tracker_values[0]);
169             if(!empty($ct_focus->tracker_name)){
170                 $ct_focus->tracker_name = $tracker_values[1];
171                 $ct_focus->is_optout = $tracker_values[2];
172                 $ct_focus->tracker_url = $tracker_values[3];
173                 $ct_focus->save();
174
175                 //load relationship and add to the list
176                 $campaign_focus->load_relationship('tracked_urls');
177                 $campaign_focus->tracked_urls->add($ct_focus->id);
178             }
179         }
180     }
181
182
183     //create new campaign tracker and save if defined
184     if(isset($_REQUEST['wiz_list_of_trackers'])){
185         $tracker_strings = explode(",", $_REQUEST['wiz_list_of_trackers']);
186         foreach($tracker_strings as $trkr_string){
187             $tracker_values = explode("@@", $trkr_string);
188             if(count($tracker_values)==3){
189                 $ct_focus = new CampaignTracker();
190                 $ct_focus->tracker_name = $tracker_values[0];
191                 $ct_focus->is_optout = $tracker_values[1];
192                 $ct_focus->tracker_url = $tracker_values[2];
193                 $ct_focus->save();
194
195                 //load relationship and add to the list
196                 $campaign_focus->load_relationship('tracked_urls');
197                 $campaign_focus->tracked_urls->add($ct_focus->id);
198                 // save campaign_trkrs after populating campaign id
199                 $ct_focus->save();
200             }
201         }
202     }
203
204 //set navigation details
205 $_REQUEST['return_id'] = $campaign_focus->id;
206 $_REQUEST['return_module'] = $campaign_focus->module_dir;
207 $_REQUEST['return_action'] = "WizardNewsLetter";
208 $_REQUEST['action'] = "WizardMarketing";
209 $_REQUEST['record'] = $campaign_focus->id;;
210
211 $action = '';
212 if(isset($_REQUEST['wiz_direction'])  &&  $_REQUEST['wiz_direction']== 'continue'){
213     $action = 'WizardMarketing';
214 }else{
215     $action = 'WizardHome&record='.$campaign_focus->id;
216 }
217 //require_once('modules/Campaigns/WizardMarketing.php');
218 $header_URL = "Location: index.php?return_module=Campaigns&module=Campaigns&action=".$action."&campaign_id=".$campaign_focus->id."&return_action=WizardNewsLetter&return_id=".$campaign_focus->id;
219 $GLOBALS['log']->debug("about to post header URL of: $header_URL");
220  header($header_URL);
221
222
223
224 /*
225  * This function will populate the passed in bean with the post variables
226  * that contain the specified prefix
227  */
228 function populate_wizard_bean_from_request($bean,$prefix){
229     foreach($_REQUEST as $key=> $val){
230      $key = trim($key);
231      if((strstr($key, $prefix )) && (strpos($key, $prefix )== 0)){
232           $field  =substr($key, strlen($prefix)) ;
233           if(isset($_REQUEST[$key]) && !empty($_REQUEST[$key])){
234               //echo "prefix is $prefix, field is $field,    key is $key,   and value is $val<br>";
235               $value = $_REQUEST[$key];
236               $bean->$field = $value;
237           }
238      }
239     }
240
241     return $bean;
242 }
243
244
245 /*
246  * This function will process any specified prospect lists and attach them to current campaign
247  * If no prospect lists have been specified, then it will create one for you.  A total of 3 prospect lists
248  * will be created for you (Subscription, Unsubscription, and test)
249  */
250 function process_subscriptions_from_request($campaign_name){
251     global $mod_strings;
252     $pl_list = array();
253
254     //process default target list
255     $create_new = true;
256     $pl_subs = new ProspectList($campaign_name);
257     if(!empty($_REQUEST['wiz_step3_subscription_list_id'])){
258         //if subscription list is specified then attach
259         $pl_subs->retrieve($_REQUEST['wiz_step3_subscription_list_id']);
260         //check to see name matches the bean, if not, then the user has chosen to create new bean
261         if($pl_subs->name == $_REQUEST['wiz_step3_subscription_name']){
262             $pl_list[] = $pl_subs;
263             $create_new = false;
264        }
265
266     }
267     //create new bio if one was not retrieved successfully
268     if($create_new){
269         //use default name if one has not been specified
270         $name = $campaign_name . " ".$mod_strings['LBL_SUBSCRIPTION_LIST'];
271         if(isset($_REQUEST['wiz_step3_subscription_name']) && !empty($_REQUEST['wiz_step3_subscription_name'])){
272             $name = $_REQUEST['wiz_step3_subscription_name'];
273         }
274         //if subscription list is not specified then create and attach default one
275         $pl_subs->name = $name;
276         $pl_subs->list_type = 'default';
277         $pl_subs->assigned_user_id= $GLOBALS['current_user']->id;
278         $pl_subs->save();
279         $pl_list[] = $pl_subs;
280     }
281
282     //process exempt target list
283     $create_new = true;
284     $pl_un_subs = new ProspectList();
285     if(!empty($_REQUEST['wiz_step3_unsubscription_list_id'])){
286         //if unsubscription list is specified then attach
287         $pl_un_subs->retrieve($_REQUEST['wiz_step3_unsubscription_list_id']);
288         //check to see name matches the bean, if not, then the user has chosen to create new bean
289         if($pl_un_subs->name == $_REQUEST['wiz_step3_unsubscription_name']){
290             $pl_list[] = $pl_un_subs;
291             $create_new = false;
292        }
293
294     }
295     //create new bean if one was not retrieved successfully
296     if($create_new){
297         //use default name if one has not been specified
298         $name = $campaign_name . " ".$mod_strings['LBL_UNSUBSCRIPTION_LIST'];
299         if(isset($_REQUEST['wiz_step3_unsubscription_name']) && !empty($_REQUEST['wiz_step3_unsubscription_name'])){
300             $name = $_REQUEST['wiz_step3_unsubscription_name'];
301         }
302         //if unsubscription list is not specified then create and attach default one
303         $pl_un_subs->name = $name;
304         $pl_un_subs->list_type = 'exempt';
305         $pl_un_subs->assigned_user_id= $GLOBALS['current_user']->id;
306         $pl_un_subs->save();
307         $pl_list[] = $pl_un_subs;
308     }
309
310     //process test target list
311     $pl_test = new ProspectList();
312     $create_new = true;
313     if(!empty($_REQUEST['wiz_step3_test_list_id'])){
314         //if test list is specified then attach
315         $pl_test->retrieve($_REQUEST['wiz_step3_test_list_id']);
316         //check to see name matches the bean, if not, then the user has chosen to create new bean
317         if($pl_test->name == $_REQUEST['wiz_step3_test_name']){
318             $pl_list[] = $pl_test;
319             $create_new = false;
320         }
321     }
322     //create new bio if one was not retrieved successfully
323     if($create_new){
324         //use default name if one has not been specified
325         $name = $campaign_name . " ".$mod_strings['LBL_TEST_LIST'];
326         if(isset($_REQUEST['wiz_step3_test_name']) && !empty($_REQUEST['wiz_step3_test_name'])){
327             $name = $_REQUEST['wiz_step3_test_name'];
328         }
329         //if test list is not specified then create and attach default one
330         $pl_test->name = $name;
331         $pl_test->list_type = 'test';
332         $pl_test->assigned_user_id= $GLOBALS['current_user']->id;
333         $pl_test->save();
334         $pl_list[] = $pl_test;
335     }
336
337     return $pl_list;
338 }
339 ?>