3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * Choose include / exclude groups and mass sms
40 class CRM_SMS_Form_Group
extends CRM_Contact_Form_Task
{
43 * Function to set variables up before form is built
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'))));
53 $session = CRM_Core_Session
::singleton();
54 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/mailing/browse', 'reset=1&sms=1'));
58 * This function sets the default values for the form.
59 * the default values are retrieved from the database
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);
72 $mailing = new CRM_Mailing_DAO_Mailing();
73 $mailing->id
= $mailingID;
74 $mailing->addSelect('name');
77 $defaults['name'] = $mailing->name
;
79 $defaults['name'] = ts('Copy of %1', array(1 => $mailing->name
));
82 // CRM-7590, reuse same mailing ID if we are continuing
83 $this->set('mailing_id', $mailingID);
86 $dao = new CRM_Mailing_DAO_MailingGroup();
88 $mailingGroups = array();
89 $dao->mailing_id
= $mailingID;
91 while ($dao->fetch()) {
92 $mailingGroups[$dao->entity_table
][$dao->group_type
][] = $dao->entity_id
;
95 $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include'];
96 $defaults['excludeGroups'] = CRM_Utils_Array
::value('Exclude', $mailingGroups['civicrm_group']);
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));
106 * Function to actually build the form
111 public function buildQuickForm() {
114 $context = $this->get('context');
116 $this->assign('context', $context);
118 $this->add('text', 'name', ts('Name Your SMS'),
119 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Mailing', 'name'),
123 //get the mailing groups.
124 $groups = CRM_Core_PseudoConstant
::group('Mailing');
126 //get the sms mailing list
127 $mailings = CRM_Mailing_PseudoConstant
::completed('sms');
132 // run the groups through a hook so users can trim it if needed
133 CRM_Utils_Hook
::mailingGroups($this, $groups, $mailings);
135 $inG = &$this->addElement('advmultiselect', 'includeGroups',
136 ts('Include Group(s)') . ' ',
140 'style' => 'width:240px',
141 'class' => 'advmultiselect',
145 $this->addRule('includeGroups', ts('Please select a group to be SMSed.'), 'required');
147 $outG = &$this->addElement('advmultiselect', 'excludeGroups',
148 ts('Exclude Group(s)') . ' ',
152 'style' => 'width:240px',
153 'class' => 'advmultiselect',
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')));
162 $inM = &$this->addElement('advmultiselect', 'includeMailings',
163 ts('INCLUDE Recipients of These Mailing(s)') . ' ',
167 'style' => 'width:240px',
168 'class' => 'advmultiselect',
171 $outM = &$this->addElement('advmultiselect', 'excludeMailings',
172 ts('EXCLUDE Recipients of These Mailing(s)') . ' ',
176 'style' => 'width:240px',
177 'class' => 'advmultiselect',
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')));
186 $this->addFormRule(array('CRM_SMS_Form_Group', 'formRule'));
189 array('type' => 'next',
190 'name' => ts('Next >>'),
191 'spacing' => ' ',
196 'name' => ts('Cancel'),
200 $this->addButtons($buttons);
202 $this->assign('groupCount', count($groups));
203 $this->assign('mailingCount', count($mailings));
206 public function postProcess() {
207 $values = $this->controller
->exportValues($this->_name
);
212 'name', 'group_id', 'is_sms') as $n) {
213 if (!empty($values[$n])) {
214 $params[$n] = $values[$n];
218 $qf_Group_submit = $this->controller
->exportValue($this->_name
, '_qf_Group_submit');
219 $this->set('name', $params['name']);
221 $inGroups = $values['includeGroups'];
222 $outGroups = $values['excludeGroups'];
223 $inMailings = $values['includeMailings'];
224 $outMailings = $values['excludeMailings'];
226 if (is_array($inGroups)) {
227 foreach ($inGroups as $key => $id) {
229 $groups['include'][] = $id;
233 if (is_array($outGroups)) {
234 foreach ($outGroups as $key => $id) {
236 $groups['exclude'][] = $id;
242 if (is_array($inMailings)) {
243 foreach ($inMailings as $key => $id) {
245 $mailings['include'][] = $id;
249 if (is_array($outMailings)) {
250 foreach ($outMailings as $key => $id) {
252 $mailings['exclude'][] = $id;
257 $session = CRM_Core_Session
::singleton();
258 $params['groups'] = $groups;
259 $params['mailings'] = $mailings;
261 if ($this->get('mailing_id')) {
263 // don't create a new mass sms if already exists
264 $ids['mailing_id'] = $this->get('mailing_id');
266 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
267 $mailingTableName = CRM_Mailing_BAO_Mailing
::getTableName();
269 // delete previous includes/excludes, if mailing already existed
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;
276 while ($mg->fetch()) {
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');
288 $mailing = CRM_Mailing_BAO_Mailing
::create($params, $ids);
290 $this->set('mailing_id', $mailing->id
);
292 // also compute the recipients and store them in the mailing recipients table
293 CRM_Mailing_BAO_Mailing
::getRecipients($mailing->id
,
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);
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);
317 * Display Name of the form
323 public function getTitle() {
324 return ts('Select Recipients');
328 * global validation rules for the form
330 * @param array $fields posted values of the form
332 * @return array list of errors to be posted back to the form
336 static function formRule($fields) {
338 if (isset($fields['includeGroups']) &&
339 is_array($fields['includeGroups']) &&
340 isset($fields['excludeGroups']) &&
341 is_array($fields['excludeGroups'])
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).');
350 if (isset($fields['includeMailings']) &&
351 is_array($fields['includeMailings']) &&
352 isset($fields['excludeMailings']) &&
353 is_array($fields['excludeMailings'])
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).');
362 return empty($errors) ?
TRUE : $errors;