| 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 | * 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 | * Set 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 | // CRM-14716 - Pick up mailingID from session since most of the time it's not in the URL |
| 70 | if (!$mailingID) { |
| 71 | $mailingID = $this->get('mailing_id'); |
| 72 | } |
| 73 | $count = $this->get('count'); |
| 74 | $this->assign('count', $count); |
| 75 | $defaults = array(); |
| 76 | |
| 77 | $componentFields = array( |
| 78 | 'reply_id' => 'Reply', |
| 79 | 'optout_id' => 'OptOut', |
| 80 | 'unsubscribe_id' => 'Unsubscribe', |
| 81 | 'resubscribe_id' => 'Resubscribe', |
| 82 | ); |
| 83 | |
| 84 | foreach ($componentFields as $componentVar => $componentType) { |
| 85 | $defaults[$componentVar] = CRM_Mailing_PseudoConstant::defaultComponent($componentType, ''); |
| 86 | } |
| 87 | |
| 88 | if ($mailingID) { |
| 89 | $dao = new CRM_Mailing_DAO_Mailing(); |
| 90 | $dao->id = $mailingID; |
| 91 | $dao->find(TRUE); |
| 92 | // override_verp must be flipped, as in 3.2 we reverted |
| 93 | // its meaning to ‘should CiviMail manage replies?’ – i.e., |
| 94 | // ‘should it *not* override Reply-To: with VERP-ed address?’ |
| 95 | $dao->override_verp = !$dao->override_verp; |
| 96 | $dao->storeValues($dao, $defaults); |
| 97 | $defaults['visibility'] = $dao->visibility; |
| 98 | } |
| 99 | |
| 100 | return $defaults; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Build the form object |
| 105 | * |
| 106 | * @return void |
| 107 | * @access public |
| 108 | */ |
| 109 | public function buildQuickForm() { |
| 110 | |
| 111 | $this->addElement('checkbox', 'override_verp', ts('Track Replies?')); |
| 112 | |
| 113 | $defaults['override_verp'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, |
| 114 | 'track_civimail_replies', NULL, FALSE |
| 115 | ); |
| 116 | |
| 117 | $this->add('checkbox', 'forward_replies', ts('Forward Replies?')); |
| 118 | $defaults['forward_replies'] = FALSE; |
| 119 | |
| 120 | $this->add('checkbox', 'url_tracking', ts('Track Click-throughs?')); |
| 121 | $defaults['url_tracking'] = TRUE; |
| 122 | |
| 123 | $this->add('checkbox', 'open_tracking', ts('Track Opens?')); |
| 124 | $defaults['open_tracking'] = TRUE; |
| 125 | |
| 126 | $this->add('checkbox', 'auto_responder', ts('Auto-respond to Replies?')); |
| 127 | $defaults['auto_responder'] = FALSE; |
| 128 | |
| 129 | $this->add('select', 'visibility', ts('Mailing Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE); |
| 130 | |
| 131 | $this->add('select', 'reply_id', ts('Auto-responder'), |
| 132 | CRM_Mailing_PseudoConstant::component('Reply'), TRUE |
| 133 | ); |
| 134 | |
| 135 | $this->add('select', 'unsubscribe_id', ts('Unsubscribe Message'), |
| 136 | CRM_Mailing_PseudoConstant::component('Unsubscribe'), TRUE |
| 137 | ); |
| 138 | |
| 139 | $this->add('select', 'resubscribe_id', ts('Resubscribe Message'), |
| 140 | CRM_Mailing_PseudoConstant::component('Resubscribe'), TRUE |
| 141 | ); |
| 142 | |
| 143 | $this->add('select', 'optout_id', ts('Opt-out Message'), |
| 144 | CRM_Mailing_PseudoConstant::component('OptOut'), TRUE |
| 145 | ); |
| 146 | |
| 147 | $buttons = array( |
| 148 | array('type' => 'back', |
| 149 | 'name' => ts('<< Previous'), |
| 150 | ), |
| 151 | array( |
| 152 | 'type' => 'next', |
| 153 | 'name' => ts('Next >>'), |
| 154 | 'spacing' => ' ', |
| 155 | 'isDefault' => TRUE, |
| 156 | ), |
| 157 | array( |
| 158 | 'type' => 'submit', |
| 159 | 'name' => ts('Save & Continue Later'), |
| 160 | ), |
| 161 | array( |
| 162 | 'type' => 'cancel', |
| 163 | 'name' => ts('Cancel'), |
| 164 | ), |
| 165 | ); |
| 166 | |
| 167 | $this->addButtons($buttons); |
| 168 | |
| 169 | $this->setDefaults($defaults); |
| 170 | } |
| 171 | |
| 172 | public function postProcess() { |
| 173 | $params = $ids = array(); |
| 174 | |
| 175 | $session = CRM_Core_Session::singleton(); |
| 176 | $params['created_id'] = $session->get('userID'); |
| 177 | |
| 178 | $uploadParams = array('reply_id', 'unsubscribe_id', 'optout_id', 'resubscribe_id'); |
| 179 | $uploadParamsBoolean = array('override_verp', 'forward_replies', 'url_tracking', 'open_tracking', 'auto_responder'); |
| 180 | |
| 181 | $qf_Settings_submit = $this->controller->exportValue($this->_name, '_qf_Settings_submit'); |
| 182 | |
| 183 | foreach ($uploadParams as $key) { |
| 184 | $params[$key] = $this->controller->exportvalue($this->_name, $key); |
| 185 | $this->set($key, $this->controller->exportvalue($this->_name, $key)); |
| 186 | } |
| 187 | |
| 188 | foreach ($uploadParamsBoolean as $key) { |
| 189 | if ($this->controller->exportvalue($this->_name, $key)) { |
| 190 | $params[$key] = TRUE; |
| 191 | } |
| 192 | else { |
| 193 | $params[$key] = FALSE; |
| 194 | } |
| 195 | $this->set($key, $this->controller->exportvalue($this->_name, $key)); |
| 196 | } |
| 197 | |
| 198 | $params['visibility'] = $this->controller->exportvalue($this->_name, 'visibility'); |
| 199 | |
| 200 | // override_verp must be flipped, as in 3.2 we reverted |
| 201 | // its meaning to ‘should CiviMail manage replies?’ – i.e., |
| 202 | // ‘should it *not* override Reply-To: with VERP-ed address?’ |
| 203 | $params['override_verp'] = !$params['override_verp']; |
| 204 | |
| 205 | $ids['mailing_id'] = $this->get('mailing_id'); |
| 206 | |
| 207 | // update mailing |
| 208 | CRM_Mailing_BAO_Mailing::create($params, $ids); |
| 209 | |
| 210 | if ($qf_Settings_submit) { |
| 211 | //when user perform mailing from search context |
| 212 | //redirect it to search result CRM-3711. |
| 213 | $ssID = $this->get('ssID'); |
| 214 | if ($ssID && $this->_searchBasedMailing) { |
| 215 | if ($this->_action == CRM_Core_Action::BASIC) { |
| 216 | $fragment = 'search'; |
| 217 | } |
| 218 | elseif ($this->_action == CRM_Core_Action::PROFILE) { |
| 219 | $fragment = 'search/builder'; |
| 220 | } |
| 221 | elseif ($this->_action == CRM_Core_Action::ADVANCED) { |
| 222 | $fragment = 'search/advanced'; |
| 223 | } |
| 224 | else { |
| 225 | $fragment = 'search/custom'; |
| 226 | } |
| 227 | |
| 228 | $context = $this->get('context'); |
| 229 | if (!CRM_Contact_Form_Search::isSearchContext($context)) { |
| 230 | $context = 'search'; |
| 231 | } |
| 232 | $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}"; |
| 233 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this); |
| 234 | if (CRM_Utils_Rule::qfKey($qfKey)) { |
| 235 | $urlParams .= "&qfKey=$qfKey"; |
| 236 | } |
| 237 | |
| 238 | $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); |
| 239 | $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)); |
| 240 | |
| 241 | // Redirect user to search. |
| 242 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); |
| 243 | } |
| 244 | else { |
| 245 | $status = ts("Click the 'Continue' action to resume working on it."); |
| 246 | $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); |
| 247 | } |
| 248 | CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success'); |
| 249 | CRM_Utils_System::redirect($url); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Display Name of the form |
| 255 | * |
| 256 | * @access public |
| 257 | * |
| 258 | * @return string |
| 259 | */ |
| 260 | public function getTitle() { |
| 261 | return ts('Track and Respond'); |
| 262 | } |
| 263 | } |