]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Campaigns/Subscriptions.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Campaigns / Subscriptions.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
46 require_once('modules/Campaigns/utils.php');
47
48 global $mod_strings, $app_list_strings, $app_strings, $current_user, $import_bean_map;
49 global $import_file_name, $theme;
50
51 $focus = 0;
52 if(isset($_REQUEST['return_module'])){
53     if($_REQUEST['return_module'] == 'Contacts'){
54
55         $focus = new Contact();
56     }
57     if($_REQUEST['return_module'] == 'Leads'){
58
59         $focus = new Lead();
60     }
61     if($_REQUEST['return_module'] == 'Prospects'){
62
63         $focus = new Prospect();
64     }
65 }
66
67 if(isset($_REQUEST['record'])) {
68     $GLOBALS['log']->debug("In Subscriptions, about to retrieve record: ".$_REQUEST['record']);
69     $result = $focus->retrieve($_REQUEST['record']);
70     if($result == null)
71     {
72         sugar_die($app_strings['ERROR_NO_RECORD']);
73     }
74 }
75
76
77 $this->ss->assign("MOD", $mod_strings);
78 $this->ss->assign("APP", $app_strings);
79
80 if(isset($_REQUEST['return_module'])) {
81     $this->ss->assign("RETURN_MODULE", $_REQUEST['return_module']);
82 } else {
83     $this->ss->assign("RETURN_MODULE", '');
84 }
85 if(isset($_REQUEST['return_id'])){
86     $this->ss->assign("RETURN_ID", $_REQUEST['return_id']);
87 } else {
88     $this->ss->assign("RETURN_ID", '');
89 }
90 if(isset($_REQUEST['return_action'])){
91     $this->ss->assign("RETURN_ACTION", $_REQUEST['return_action']);
92 } else {
93     $this->ss->assign("RETURN_ACTION", '');
94 }
95 if(isset($_REQUEST['record'])){
96     $this->ss->assign("RECORD", $_REQUEST['record']);
97 } else {
98     $this->ss->assign("RECORD", '');
99 }
100
101 //if subsaction has been set, then process subscriptions
102 if(isset($_REQUEST['subs_action'])){manageSubscriptions($focus);}
103
104 //$title = $GLOBALS['app_strings']['LBL_MANAGE_SUBSCRIPTIONS_FOR'].$focus->name;
105 $params = array();
106 $params[]  = "<a href='index.php?module={$focus->module_dir}&action=index'>{$focus->module_dir}</a>";
107 $params[] = "<a href='index.php?module={$focus->module_dir}&action=DetailView&record={$focus->id}'>{$focus->name}</a>";
108 $params[] = $mod_strings['LBL_MANAGE_SUBSCRIPTIONS_TITLE'];
109 $title = getClassicModuleTitle($focus->module_dir, $params, true);
110 $orig_vals_str = printOriginalValues($focus);
111 $orig_vals_array = constructDDSubscriptionList($focus);
112
113 $this->ss->assign('APP', $app_strings);
114 $this->ss->assign('MOD', $mod_strings);
115 $this->ss->assign('title',  $title);
116
117 $this->ss->assign('enabled_subs', $orig_vals_array[0]);
118 $this->ss->assign('disabled_subs', $orig_vals_array[1]);
119 $this->ss->assign('enabled_subs_string', $orig_vals_str[0]);
120 $this->ss->assign('disabled_subs_string', $orig_vals_str[1]);
121
122 $buttons = array(
123     '<input id="save_button" title="'.$app_strings['LBL_SAVE_BUTTON_TITLE'].'" accessKey="'.$app_strings['LBL_SAVE_BUTTON_KEY'].'" class="button" onclick="save();this.form.action.value=\'Subscriptions\'; " type="submit" name="button" value="'.$app_strings['LBL_SAVE_BUTTON_LABEL'].'">',
124     '<input id="cancel_button" title="'.$app_strings['LBL_CANCEL_BUTTON_TITLE'].'" accessKey="'.$app_strings['LBL_CANCEL_BUTTON_KEY'].'" class="button" onclick="this.form.action.value=\''.$this->ss->get_template_vars('RETURN_ACTION').'\'; this.form.module.value=\''.$this->ss->get_template_vars('RETURN_MODULE').'\';" type="submit" name="button" value="'.$app_strings['LBL_CANCEL_BUTTON_LABEL'].'">'
125 );
126 $this->ss->assign('BUTTONS', $buttons);
127 $this->ss->display('modules/Campaigns/Subscriptions.tpl');
128
129 /*
130  *This function constructs Drag and Drop multiselect box of subscriptions for display in manage subscription form
131 */
132 function constructDDSubscriptionList($focus,$classname=''){
133     require_once("include/templates/TemplateDragDropChooser.php");
134     global $mod_strings;
135     $unsubs_arr = '';
136     $subs_arr =  '';
137
138     // Lets start by creating the subscription and unsubscription arrays
139     $subscription_arrays = get_subscription_lists($focus);
140     $unsubs_arr = $subscription_arrays['unsubscribed'];
141     $subs_arr =  $subscription_arrays['subscribed'];
142
143     $comb_array = array();
144         $comb_array [0] = array();
145         $comb_array [1] = array();
146
147     foreach ($subs_arr as $key=>$val){
148         $comb_array [0][$val] = $key;
149     }
150
151
152         foreach ($unsubs_arr as $key=>$val){
153         $comb_array [1][$val] = $key;
154     }
155
156     return $comb_array ;
157
158 }
159
160
161
162 /*
163  *This function constructs multiselect box of subscriptions for display in manage subscription form
164 */
165 function printOriginalValues($focus){
166     global $app_strings;
167     $unsubs_arr = '';
168     $subs_arr =  '';
169     $return_arr =  '';
170
171      // Lets start by creating the subscription and unsubscription arrays
172         $subscription_arrays = get_subscription_lists($focus);
173         $unsubs_arr = $subscription_arrays['unsubscribed'];
174         $subs_arr =  $subscription_arrays['subscribed'];
175
176 //    ORIG_UNSUBS_VALUES
177         $unsubs_vals = ' ';
178         $subs_vals = ' ';
179         foreach($subs_arr as $name => $id){
180             $subs_vals .= ", $id";
181         }
182         $return_arr[]=$subs_vals;
183
184         foreach($unsubs_arr as $name => $id){
185             $unsubs_vals .= ", $id";
186         }
187
188         $return_arr[]=$unsubs_vals;
189
190         return $return_arr;
191     }
192
193
194 /*
195  * Perform Subscription management work.  This function processes selected subscriptions and calls the
196  * right methods to subscribe or unsubscribe the user
197  * */
198
199 function manageSubscriptions($focus){
200
201
202     //Process Subscription Lists first
203     //compare current list of subscriptions to original list and see if there are any additions
204     $orig_subscription_arr = array();
205     $curr_subscription_arr = array();
206     //build array of original subscriptions
207     if(isset($_REQUEST['orig_enabled_values'])  && ! empty($_REQUEST['orig_enabled_values'])){
208      $orig_subscription_arr = explode(",", $_REQUEST['orig_enabled_values']);
209      $orig_subscription_arr = process_subscriptions($orig_subscription_arr);
210     }
211
212     //build array of current subscriptions
213     if(isset($_REQUEST['enabled_subs'])  && ! empty($_REQUEST['enabled_subs'])){
214      $curr_subscription_arr = explode(",", $_REQUEST['enabled_subs']);
215      $curr_subscription_arr = process_subscriptions($curr_subscription_arr);
216     }
217
218     //compare both arrays and find differences
219     $i=0;
220     while($i<(count($curr_subscription_arr)/2)){
221         //if current subscription existed in original subscription list, do nothing
222         if(in_array($curr_subscription_arr['campaign'.$i], $orig_subscription_arr)){
223             //nothing to process
224         }else{
225          //current subscription is new, so subscribe
226             subscribe($curr_subscription_arr['campaign'.$i], $curr_subscription_arr['prospect_list'.$i], $focus);
227         }
228         $i = $i +1;
229     }
230
231     //Now process UnSubscription Lists first
232     //compare current list of subscriptions to original list and see if there are any additions
233     $orig_unsubscription_arr = array();
234     $curr_unsubscription_arr = array();
235
236     //build array of original subscriptions
237     if(isset($_REQUEST['orig_disabled_values'])  && ! empty($_REQUEST['orig_disabled_values'])){
238      $orig_unsubscription_arr = explode(",", $_REQUEST['orig_disabled_values']);
239      $orig_unsubscription_arr = process_subscriptions($orig_unsubscription_arr);
240     }
241
242     //build array of current subscriptions
243     if(isset($_REQUEST['disabled_subs'])  && ! empty($_REQUEST['disabled_subs'])){
244      $curr_unsubscription_arr = explode(",", $_REQUEST['disabled_subs']);
245      $curr_unsubscription_arr = process_subscriptions($curr_unsubscription_arr);
246     }
247     //compare both arrays and find differences
248     $i=0;
249     while($i<(count($curr_unsubscription_arr)/2)){
250         //if current subscription existed in original subscription list, do nothing
251         if(in_array($curr_unsubscription_arr['campaign'.$i], $orig_unsubscription_arr)){
252             //nothing to process
253         }else{
254          //current subscription is new, so subscribe
255             unsubscribe($curr_unsubscription_arr['campaign'.$i], $focus);
256         }
257         $i = $i +1;
258     }
259
260 }
261
262 ?>