| 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 | * Choose include / exclude groups and mass sms |
| 38 | * |
| 39 | */ |
| 40 | class CRM_SMS_Form_Group extends CRM_Contact_Form_Task { |
| 41 | |
| 42 | /** |
| 43 | * Function to set variables up before form is built |
| 44 | * |
| 45 | * @return void |
| 46 | * @access public |
| 47 | */ |
| 48 | public function preProcess() { |
| 49 | if (!CRM_SMS_BAO_Provider::activeProviderCount()) { |
| 50 | CRM_Core_Error::fatal(ts('The <a href="%1">SMS Provider</a> has not been configured or is not active.', array(1 => CRM_Utils_System::url('civicrm/admin/sms/provider', 'reset=1')))); |
| 51 | } |
| 52 | |
| 53 | $session = CRM_Core_Session::singleton(); |
| 54 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/mailing/browse', 'reset=1&sms=1')); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * This function sets the default values for the form. |
| 59 | * the default values are retrieved from the database |
| 60 | * |
| 61 | * @access public |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | function setDefaultValues() { |
| 66 | $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL); |
| 67 | $continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL); |
| 68 | |
| 69 | $defaults = array(); |
| 70 | |
| 71 | if ($mailingID) { |
| 72 | $mailing = new CRM_Mailing_DAO_Mailing(); |
| 73 | $mailing->id = $mailingID; |
| 74 | $mailing->addSelect('name'); |
| 75 | $mailing->find(TRUE); |
| 76 | |
| 77 | $defaults['name'] = $mailing->name; |
| 78 | if (!$continue) { |
| 79 | $defaults['name'] = ts('Copy of %1', array(1 => $mailing->name)); |
| 80 | } |
| 81 | else { |
| 82 | // CRM-7590, reuse same mailing ID if we are continuing |
| 83 | $this->set('mailing_id', $mailingID); |
| 84 | } |
| 85 | |
| 86 | $dao = new CRM_Mailing_DAO_MailingGroup(); |
| 87 | |
| 88 | $mailingGroups = array(); |
| 89 | $dao->mailing_id = $mailingID; |
| 90 | $dao->find(); |
| 91 | while ($dao->fetch()) { |
| 92 | $mailingGroups[$dao->entity_table][$dao->group_type][] = $dao->entity_id; |
| 93 | } |
| 94 | |
| 95 | $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include']; |
| 96 | $defaults['excludeGroups'] = CRM_Utils_Array::value('Exclude', $mailingGroups['civicrm_group']); |
| 97 | |
| 98 | $defaults['includeMailings'] = CRM_Utils_Array::value('Include', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups)); |
| 99 | $defaults['excludeMailings'] = CRM_Utils_Array::value('Exclude', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups)); |
| 100 | } |
| 101 | |
| 102 | return $defaults; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Function to actually build the form |
| 107 | * |
| 108 | * @return void |
| 109 | * @access public |
| 110 | */ |
| 111 | public function buildQuickForm() { |
| 112 | |
| 113 | //get the context |
| 114 | $context = $this->get('context'); |
| 115 | |
| 116 | $this->assign('context', $context); |
| 117 | |
| 118 | $this->add('text', 'name', ts('Name Your SMS'), |
| 119 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'name'), |
| 120 | TRUE |
| 121 | ); |
| 122 | |
| 123 | //get the mailing groups. |
| 124 | $groups = CRM_Core_PseudoConstant::group('Mailing'); |
| 125 | |
| 126 | //get the sms mailing list |
| 127 | $mailings = CRM_Mailing_PseudoConstant::completed('sms'); |
| 128 | if (!$mailings) { |
| 129 | $mailings = array(); |
| 130 | } |
| 131 | |
| 132 | // run the groups through a hook so users can trim it if needed |
| 133 | CRM_Utils_Hook::mailingGroups($this, $groups, $mailings); |
| 134 | |
| 135 | $inG = &$this->addElement('advmultiselect', 'includeGroups', |
| 136 | ts('Include Group(s)') . ' ', |
| 137 | $groups, |
| 138 | array( |
| 139 | 'size' => 5, |
| 140 | 'style' => 'width:240px', |
| 141 | 'class' => 'advmultiselect', |
| 142 | ) |
| 143 | ); |
| 144 | |
| 145 | $this->addRule('includeGroups', ts('Please select a group to be SMSed.'), 'required'); |
| 146 | |
| 147 | $outG = &$this->addElement('advmultiselect', 'excludeGroups', |
| 148 | ts('Exclude Group(s)') . ' ', |
| 149 | $groups, |
| 150 | array( |
| 151 | 'size' => 5, |
| 152 | 'style' => 'width:240px', |
| 153 | 'class' => 'advmultiselect', |
| 154 | ) |
| 155 | ); |
| 156 | |
| 157 | $inG->setButtonAttributes('add', array('value' => ts('Add >>'))); |
| 158 | $outG->setButtonAttributes('add', array('value' => ts('Add >>'))); |
| 159 | $inG->setButtonAttributes('remove', array('value' => ts('<< Remove'))); |
| 160 | $outG->setButtonAttributes('remove', array('value' => ts('<< Remove'))); |
| 161 | |
| 162 | $inM = &$this->addElement('advmultiselect', 'includeMailings', |
| 163 | ts('INCLUDE Recipients of These Mailing(s)') . ' ', |
| 164 | $mailings, |
| 165 | array( |
| 166 | 'size' => 5, |
| 167 | 'style' => 'width:240px', |
| 168 | 'class' => 'advmultiselect', |
| 169 | ) |
| 170 | ); |
| 171 | $outM = &$this->addElement('advmultiselect', 'excludeMailings', |
| 172 | ts('EXCLUDE Recipients of These Mailing(s)') . ' ', |
| 173 | $mailings, |
| 174 | array( |
| 175 | 'size' => 5, |
| 176 | 'style' => 'width:240px', |
| 177 | 'class' => 'advmultiselect', |
| 178 | ) |
| 179 | ); |
| 180 | |
| 181 | $inM->setButtonAttributes('add', array('value' => ts('Add >>'))); |
| 182 | $outM->setButtonAttributes('add', array('value' => ts('Add >>'))); |
| 183 | $inM->setButtonAttributes('remove', array('value' => ts('<< Remove'))); |
| 184 | $outM->setButtonAttributes('remove', array('value' => ts('<< Remove'))); |
| 185 | |
| 186 | $this->addFormRule(array('CRM_SMS_Form_Group', 'formRule')); |
| 187 | |
| 188 | $buttons = array( |
| 189 | array('type' => 'next', |
| 190 | 'name' => ts('Next >>'), |
| 191 | 'spacing' => ' ', |
| 192 | 'isDefault' => TRUE, |
| 193 | ), |
| 194 | array( |
| 195 | 'type' => 'cancel', |
| 196 | 'name' => ts('Cancel'), |
| 197 | ), |
| 198 | ); |
| 199 | |
| 200 | $this->addButtons($buttons); |
| 201 | |
| 202 | $this->assign('groupCount', count($groups)); |
| 203 | $this->assign('mailingCount', count($mailings)); |
| 204 | } |
| 205 | |
| 206 | public function postProcess() { |
| 207 | $values = $this->controller->exportValues($this->_name); |
| 208 | |
| 209 | $groups = array(); |
| 210 | |
| 211 | foreach (array( |
| 212 | 'name', 'group_id', 'is_sms') as $n) { |
| 213 | if (!empty($values[$n])) { |
| 214 | $params[$n] = $values[$n]; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit'); |
| 219 | $this->set('name', $params['name']); |
| 220 | |
| 221 | $inGroups = $values['includeGroups']; |
| 222 | $outGroups = $values['excludeGroups']; |
| 223 | $inMailings = $values['includeMailings']; |
| 224 | $outMailings = $values['excludeMailings']; |
| 225 | |
| 226 | if (is_array($inGroups)) { |
| 227 | foreach ($inGroups as $key => $id) { |
| 228 | if ($id) { |
| 229 | $groups['include'][] = $id; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | if (is_array($outGroups)) { |
| 234 | foreach ($outGroups as $key => $id) { |
| 235 | if ($id) { |
| 236 | $groups['exclude'][] = $id; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | $mailings = array(); |
| 242 | if (is_array($inMailings)) { |
| 243 | foreach ($inMailings as $key => $id) { |
| 244 | if ($id) { |
| 245 | $mailings['include'][] = $id; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | if (is_array($outMailings)) { |
| 250 | foreach ($outMailings as $key => $id) { |
| 251 | if ($id) { |
| 252 | $mailings['exclude'][] = $id; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | $session = CRM_Core_Session::singleton(); |
| 258 | $params['groups'] = $groups; |
| 259 | $params['mailings'] = $mailings; |
| 260 | $ids = array(); |
| 261 | if ($this->get('mailing_id')) { |
| 262 | |
| 263 | // don't create a new mass sms if already exists |
| 264 | $ids['mailing_id'] = $this->get('mailing_id'); |
| 265 | |
| 266 | $groupTableName = CRM_Contact_BAO_Group::getTableName(); |
| 267 | $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName(); |
| 268 | |
| 269 | // delete previous includes/excludes, if mailing already existed |
| 270 | foreach (array( |
| 271 | 'groups', 'mailings') as $entity) { |
| 272 | $mg = new CRM_Mailing_DAO_MailingGroup(); |
| 273 | $mg->mailing_id = $ids['mailing_id']; |
| 274 | $mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName; |
| 275 | $mg->find(); |
| 276 | while ($mg->fetch()) { |
| 277 | $mg->delete(); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | else { |
| 282 | // new mailing, so lets set the created_id |
| 283 | $session = CRM_Core_Session::singleton(); |
| 284 | $params['created_id'] = $session->get('userID'); |
| 285 | $params['created_date'] = date('YmdHis'); |
| 286 | } |
| 287 | |
| 288 | $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids); |
| 289 | |
| 290 | $this->set('mailing_id', $mailing->id); |
| 291 | |
| 292 | // also compute the recipients and store them in the mailing recipients table |
| 293 | CRM_Mailing_BAO_Mailing::getRecipients($mailing->id, |
| 294 | $mailing->id, |
| 295 | NULL, |
| 296 | NULL, |
| 297 | TRUE, |
| 298 | FALSE, |
| 299 | 'sms' |
| 300 | ); |
| 301 | |
| 302 | $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id); |
| 303 | $this->set('count', $count); |
| 304 | $this->assign('count', $count); |
| 305 | $this->set('groups', $groups); |
| 306 | $this->set('mailings', $mailings); |
| 307 | |
| 308 | if ($qf_Group_submit) { |
| 309 | $status = ts("Your Mass SMS has been saved."); |
| 310 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); |
| 311 | $url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1'); |
| 312 | return $this->controller->setDestination($url); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Display Name of the form |
| 318 | * |
| 319 | * @access public |
| 320 | * |
| 321 | * @return string |
| 322 | */ |
| 323 | public function getTitle() { |
| 324 | return ts('Select Recipients'); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * global validation rules for the form |
| 329 | * |
| 330 | * @param array $fields posted values of the form |
| 331 | * |
| 332 | * @return array list of errors to be posted back to the form |
| 333 | * @static |
| 334 | * @access public |
| 335 | */ |
| 336 | static function formRule($fields) { |
| 337 | $errors = array(); |
| 338 | if (isset($fields['includeGroups']) && |
| 339 | is_array($fields['includeGroups']) && |
| 340 | isset($fields['excludeGroups']) && |
| 341 | is_array($fields['excludeGroups']) |
| 342 | ) { |
| 343 | $checkGroups = array(); |
| 344 | $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']); |
| 345 | if (!empty($checkGroups)) { |
| 346 | $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).'); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (isset($fields['includeMailings']) && |
| 351 | is_array($fields['includeMailings']) && |
| 352 | isset($fields['excludeMailings']) && |
| 353 | is_array($fields['excludeMailings']) |
| 354 | ) { |
| 355 | $checkMailings = array(); |
| 356 | $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']); |
| 357 | if (!empty($checkMailings)) { |
| 358 | $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).'); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return empty($errors) ? TRUE : $errors; |
| 363 | } |
| 364 | } |
| 365 | |