Merge pull request #3126 from pradpnayak/CRM-9288
[civicrm-core.git] / CRM / Mailing / Form / Settings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This file is used to build the form configuring mailing details
38 */
39 class CRM_Mailing_Form_Settings extends CRM_Core_Form {
40
41 /**
42 * Function to set variables up before form is built
43 *
44 * @return void
45 * @access public
46 */
47 public function preProcess() {
48 //when user come from search context.
49 $ssID = $this->get('ssID');
50 $this->assign('ssid',$ssID);
51 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
52 if(CRM_Contact_Form_Search::isSearchContext($this->get('context')) && !$ssID){
53 $params = array();
54 $result = CRM_Core_BAO_PrevNextCache::getSelectedContacts();
55 $this->assign("value", $result);
56 }
57 }
58
59 /**
60 * This function sets the default values for the form.
61 * the default values are retrieved from the database
62 *
63 * @access public
64 *
65 * @return void
66 */
67 function setDefaultValues() {
68 $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
69 $count = $this->get('count');
70 $this->assign('count', $count);
71 $defaults = array();
72
73 $componentFields = array(
74 'reply_id' => 'Reply',
75 'optout_id' => 'OptOut',
76 'unsubscribe_id' => 'Unsubscribe',
77 'resubscribe_id' => 'Resubscribe',
78 );
79
80 foreach ($componentFields as $componentVar => $componentType) {
81 $defaults[$componentVar] = CRM_Mailing_PseudoConstant::defaultComponent($componentType, '');
82 }
83
84 if ($mailingID) {
85 $dao = new CRM_Mailing_DAO_Mailing();
86 $dao->id = $mailingID;
87 $dao->find(TRUE);
88 // override_verp must be flipped, as in 3.2 we reverted
89 // its meaning to ‘should CiviMail manage replies?’ – i.e.,
90 // ‘should it *not* override Reply-To: with VERP-ed address?’
91 $dao->override_verp = !$dao->override_verp;
92 $dao->storeValues($dao, $defaults);
93 $defaults['visibility'] = $dao->visibility;
94
95 crm_core_error::debug('$dao->visibility', $dao->visibility);
96 exit();
97
98 }
99 return $defaults;
100 }
101
102 /**
103 * Function to actually build the form
104 *
105 * @return void
106 * @access public
107 */
108 public function buildQuickForm() {
109
110 $this->addElement('checkbox', 'override_verp', ts('Track Replies?'));
111
112 $defaults['override_verp'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
113 'track_civimail_replies', NULL, FALSE
114 );
115
116 $this->add('checkbox', 'forward_replies', ts('Forward Replies?'));
117 $defaults['forward_replies'] = FALSE;
118
119 $this->add('checkbox', 'url_tracking', ts('Track Click-throughs?'));
120 $defaults['url_tracking'] = TRUE;
121
122 $this->add('checkbox', 'open_tracking', ts('Track Opens?'));
123 $defaults['open_tracking'] = TRUE;
124
125 $this->add('checkbox', 'auto_responder', ts('Auto-respond to Replies?'));
126 $defaults['auto_responder'] = FALSE;
127
128 $this->add('select', 'visibility', ts('Mailing Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE);
129
130 $this->add('select', 'reply_id', ts('Auto-responder'),
131 CRM_Mailing_PseudoConstant::component('Reply'), TRUE
132 );
133
134 $this->add('select', 'unsubscribe_id', ts('Unsubscribe Message'),
135 CRM_Mailing_PseudoConstant::component('Unsubscribe'), TRUE
136 );
137
138 $this->add('select', 'resubscribe_id', ts('Resubscribe Message'),
139 CRM_Mailing_PseudoConstant::component('Resubscribe'), TRUE
140 );
141
142 $this->add('select', 'optout_id', ts('Opt-out Message'),
143 CRM_Mailing_PseudoConstant::component('OptOut'), TRUE
144 );
145
146 $buttons = array(
147 array('type' => 'back',
148 'name' => ts('<< Previous'),
149 ),
150 array(
151 'type' => 'next',
152 'name' => ts('Next >>'),
153 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
154 'isDefault' => TRUE,
155 ),
156 array(
157 'type' => 'submit',
158 'name' => ts('Save & Continue Later'),
159 ),
160 array(
161 'type' => 'cancel',
162 'name' => ts('Cancel'),
163 ),
164 );
165
166 $this->addButtons($buttons);
167
168 $this->setDefaults($defaults);
169 }
170
171 public function postProcess() {
172 $params = $ids = array();
173
174 $session = CRM_Core_Session::singleton();
175 $params['created_id'] = $session->get('userID');
176
177 $uploadParams = array('reply_id', 'unsubscribe_id', 'optout_id', 'resubscribe_id');
178 $uploadParamsBoolean = array('override_verp', 'forward_replies', 'url_tracking', 'open_tracking', 'auto_responder');
179
180 $qf_Settings_submit = $this->controller->exportValue($this->_name, '_qf_Settings_submit');
181
182 foreach ($uploadParams as $key) {
183 $params[$key] = $this->controller->exportvalue($this->_name, $key);
184 $this->set($key, $this->controller->exportvalue($this->_name, $key));
185 }
186
187 foreach ($uploadParamsBoolean as $key) {
188 if ($this->controller->exportvalue($this->_name, $key)) {
189 $params[$key] = TRUE;
190 }
191 else {
192 $params[$key] = FALSE;
193 }
194 $this->set($key, $this->controller->exportvalue($this->_name, $key));
195 }
196
197 $params['visibility'] = $this->controller->exportvalue($this->_name, 'visibility');
198
199 // override_verp must be flipped, as in 3.2 we reverted
200 // its meaning to ‘should CiviMail manage replies?’ – i.e.,
201 // ‘should it *not* override Reply-To: with VERP-ed address?’
202 $params['override_verp'] = !$params['override_verp'];
203
204 $ids['mailing_id'] = $this->get('mailing_id');
205
206 // update mailing
207 CRM_Mailing_BAO_Mailing::create($params, $ids);
208
209 if ($qf_Settings_submit) {
210 //when user perform mailing from search context
211 //redirect it to search result CRM-3711.
212 $ssID = $this->get('ssID');
213 if ($ssID && $this->_searchBasedMailing) {
214 if ($this->_action == CRM_Core_Action::BASIC) {
215 $fragment = 'search';
216 }
217 elseif ($this->_action == CRM_Core_Action::PROFILE) {
218 $fragment = 'search/builder';
219 }
220 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
221 $fragment = 'search/advanced';
222 }
223 else {
224 $fragment = 'search/custom';
225 }
226
227 $context = $this->get('context');
228 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
229 $context = 'search';
230 }
231 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
232 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
233 if (CRM_Utils_Rule::qfKey($qfKey)) {
234 $urlParams .= "&qfKey=$qfKey";
235 }
236
237 $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
238 $status = ts("You can continue later by clicking the 'Continue' action to resume working on it.<br />From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL));
239
240 // Redirect user to search.
241 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
242 }
243 else {
244 $status = ts("Click the 'Continue' action to resume working on it.");
245 $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
246 }
247 CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success');
248 CRM_Utils_System::redirect($url);
249 }
250 }
251
252 /**
253 * Display Name of the form
254 *
255 * @access public
256 *
257 * @return string
258 */
259 public function getTitle() {
260 return ts('Track and Respond');
261 }
262 }