]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/WizardNewsletterSave.php
Release 6.2.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-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:  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             }
199         }
200     }
201
202 //set navigation details
203 $_REQUEST['return_id'] = $campaign_focus->id;
204 $_REQUEST['return_module'] = $campaign_focus->module_dir;
205 $_REQUEST['return_action'] = "WizardNewsLetter";
206 $_REQUEST['action'] = "WizardMarketing";
207 $_REQUEST['record'] = $campaign_focus->id;;
208
209 $action = '';
210 if(isset($_REQUEST['wiz_direction'])  &&  $_REQUEST['wiz_direction']== 'continue'){
211     $action = 'WizardMarketing';    
212 }else{
213     $action = 'WizardHome&record='.$campaign_focus->id;
214 }
215 //require_once('modules/Campaigns/WizardMarketing.php');
216 $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;
217 $GLOBALS['log']->debug("about to post header URL of: $header_URL");
218  header($header_URL);
219
220
221
222 /*
223  * This function will populate the passed in bean with the post variables
224  * that contain the specified prefix
225  */
226 function populate_wizard_bean_from_request($bean,$prefix){ 
227     foreach($_REQUEST as $key=> $val){
228      $key = trim($key);
229      if((strstr($key, $prefix )) && (strpos($key, $prefix )== 0)){
230           $field  =substr($key, strlen($prefix)) ;
231           if(isset($_REQUEST[$key]) && !empty($_REQUEST[$key])){
232               //echo "prefix is $prefix, field is $field,    key is $key,   and value is $val<br>";
233               $value = $_REQUEST[$key];
234               $bean->$field = $value;
235           }
236      }   
237     }
238
239     return $bean;
240 }
241
242
243 /*
244  * This function will process any specified prospect lists and attach them to current campaign
245  * If no prospect lists have been specified, then it will create one for you.  A total of 3 prospect lists
246  * will be created for you (Subscription, Unsubscription, and test)
247  */
248 function process_subscriptions_from_request($campaign_name){
249     global $mod_strings;
250     $pl_list = array();
251
252     //process default target list
253     $create_new = true;     
254     $pl_subs = new ProspectList($campaign_name);
255     if(!empty($_REQUEST['wiz_step3_subscription_list_id'])){
256         //if subscription list is specified then attach
257         $pl_subs->retrieve($_REQUEST['wiz_step3_subscription_list_id']);
258         //check to see name matches the bean, if not, then the user has chosen to create new bean
259         if($pl_subs->name == $_REQUEST['wiz_step3_subscription_name']){
260             $pl_list[] = $pl_subs;
261             $create_new = false;
262        }
263
264     }
265     //create new bio if one was not retrieved succesfully    
266     if($create_new){
267         //use default name if one has not been specified
268         $name = $campaign_name . " ".$mod_strings['LBL_SUBSCRIPTION_LIST'];
269         if(isset($_REQUEST['wiz_step3_subscription_name']) && !empty($_REQUEST['wiz_step3_subscription_name'])){
270             $name = $_REQUEST['wiz_step3_subscription_name'];
271         }
272         //if subscription list is not specified then create and attach default one
273         $pl_subs->name = $name;
274         $pl_subs->list_type = 'default';
275         $pl_subs->assigned_user_id= $GLOBALS['current_user']->id;
276         $pl_subs->save();
277         $pl_list[] = $pl_subs; 
278     }
279
280     //process exempt target list
281     $create_new = true;    
282     $pl_un_subs = new ProspectList();
283     if(!empty($_REQUEST['wiz_step3_unsubscription_list_id'])){
284         //if unsubscription list is specified then attach 
285         $pl_un_subs->retrieve($_REQUEST['wiz_step3_unsubscription_list_id']);
286         //check to see name matches the bean, if not, then the user has chosen to create new bean
287         if($pl_un_subs->name == $_REQUEST['wiz_step3_unsubscription_name']){
288             $pl_list[] = $pl_un_subs; 
289             $create_new = false;
290        }
291
292     }
293     //create new bean if one was not retrieved succesfully
294     if($create_new){
295         //use default name if one has not been specified
296         $name = $campaign_name . " ".$mod_strings['LBL_UNSUBSCRIPTION_LIST'];
297         if(isset($_REQUEST['wiz_step3_unsubscription_name']) && !empty($_REQUEST['wiz_step3_unsubscription_name'])){
298             $name = $_REQUEST['wiz_step3_unsubscription_name'];
299         }
300         //if unsubscription list is not specified then create and attach default one
301         $pl_un_subs->name = $name;
302         $pl_un_subs->list_type = 'exempt';
303         $pl_un_subs->assigned_user_id= $GLOBALS['current_user']->id;
304         $pl_un_subs->save();
305         $pl_list[] = $pl_un_subs; 
306     }
307     
308     //process test target list
309     $pl_test = new ProspectList();
310     $create_new = true;
311     if(!empty($_REQUEST['wiz_step3_test_list_id'])){
312         //if test list is specified then attach         
313         $pl_test->retrieve($_REQUEST['wiz_step3_test_list_id']);
314         //check to see name matches the bean, if not, then the user has chosen to create new bean        
315         if($pl_test->name == $_REQUEST['wiz_step3_test_name']){
316             $pl_list[] = $pl_test;
317             $create_new = false;
318         }
319     }
320     //create new bio if one was not retrieved succesfully
321     if($create_new){
322         //use default name if one has not been specified        
323         $name = $campaign_name . " ".$mod_strings['LBL_TEST_LIST'];
324         if(isset($_REQUEST['wiz_step3_test_name']) && !empty($_REQUEST['wiz_step3_test_name'])){
325             $name = $_REQUEST['wiz_step3_test_name'];
326         }
327         //if test list is not specified then create and attach default one        
328         $pl_test->name = $name; 
329         $pl_test->list_type = 'test';
330         $pl_test->assigned_user_id= $GLOBALS['current_user']->id;
331         $pl_test->save();
332         $pl_list[] = $pl_test; 
333     }
334     
335     return $pl_list;
336 }
337 ?>