3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
35 * Choose include / exclude groups and mass sms.
37 class CRM_SMS_Form_Group
extends CRM_Contact_Form_Task
{
40 * Set variables up before form is built.
42 public function preProcess() {
43 if (!CRM_SMS_BAO_Provider
::activeProviderCount()) {
44 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'))));
47 $session = CRM_Core_Session
::singleton();
48 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/mailing/browse', 'reset=1&sms=1'));
52 * Set default values for the form.
53 * The default values are retrieved from the database.
55 public function setDefaultValues() {
56 $mailingID = CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
57 $continue = CRM_Utils_Request
::retrieve('continue', 'String', $this, FALSE, NULL);
62 $mailing = new CRM_Mailing_DAO_Mailing();
63 $mailing->id
= $mailingID;
64 $mailing->addSelect('name');
67 $defaults['name'] = $mailing->name
;
69 $defaults['name'] = ts('Copy of %1', array(1 => $mailing->name
));
72 // CRM-7590, reuse same mailing ID if we are continuing
73 $this->set('mailing_id', $mailingID);
76 $dao = new CRM_Mailing_DAO_MailingGroup();
78 $mailingGroups = array();
79 $dao->mailing_id
= $mailingID;
81 while ($dao->fetch()) {
82 $mailingGroups[$dao->entity_table
][$dao->group_type
][] = $dao->entity_id
;
85 $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include'];
86 $defaults['excludeGroups'] = CRM_Utils_Array
::value('Exclude', $mailingGroups['civicrm_group']);
88 $defaults['includeMailings'] = CRM_Utils_Array
::value('Include', CRM_Utils_Array
::value('civicrm_mailing', $mailingGroups));
89 $defaults['excludeMailings'] = CRM_Utils_Array
::value('Exclude', CRM_Utils_Array
::value('civicrm_mailing', $mailingGroups));
96 * Build the form object.
98 public function buildQuickForm() {
101 $context = $this->get('context');
103 $this->assign('context', $context);
105 $this->add('text', 'name', ts('Name Your SMS'),
106 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Mailing', 'name'),
110 // Get the mailing groups.
111 $groups = CRM_Core_PseudoConstant
::nestedGroup('Mailing');
113 // Get the sms mailing list.
114 $mailings = CRM_Mailing_PseudoConstant
::completed('sms');
119 // run the groups through a hook so users can trim it if needed
120 CRM_Utils_Hook
::mailingGroups($this, $groups, $mailings);
122 $select2style = array(
124 'style' => 'width: 100%; max-width: 60em;',
125 'class' => 'crm-select2',
126 'placeholder' => ts('- select -'),
129 $this->add('select', 'includeGroups',
130 ts('Include Group(s)'),
136 $this->add('select', 'excludeGroups',
137 ts('Exclude Group(s)'),
143 $this->add('select', 'includeMailings',
144 ts('INCLUDE Recipients of These Message(s)'),
149 $this->add('select', 'excludeMailings',
150 ts('EXCLUDE Recipients of These Message(s)'),
156 $this->addFormRule(array('CRM_SMS_Form_Group', 'formRule'));
161 'name' => ts('Next'),
162 'spacing' => ' ',
167 'name' => ts('Cancel'),
171 $this->addButtons($buttons);
173 $this->assign('groupCount', count($groups));
174 $this->assign('mailingCount', count($mailings));
177 public function postProcess() {
178 $values = $this->controller
->exportValues($this->_name
);
187 if (!empty($values[$n])) {
188 $params[$n] = $values[$n];
192 $qf_Group_submit = $this->controller
->exportValue($this->_name
, '_qf_Group_submit');
193 $this->set('name', $params['name']);
195 $inGroups = $values['includeGroups'];
196 $outGroups = $values['excludeGroups'];
197 $inMailings = $values['includeMailings'];
198 $outMailings = $values['excludeMailings'];
200 if (is_array($inGroups)) {
201 foreach ($inGroups as $key => $id) {
203 $groups['include'][] = $id;
207 if (is_array($outGroups)) {
208 foreach ($outGroups as $key => $id) {
210 $groups['exclude'][] = $id;
216 if (is_array($inMailings)) {
217 foreach ($inMailings as $key => $id) {
219 $mailings['include'][] = $id;
223 if (is_array($outMailings)) {
224 foreach ($outMailings as $key => $id) {
226 $mailings['exclude'][] = $id;
231 $session = CRM_Core_Session
::singleton();
232 $params['groups'] = $groups;
233 $params['mailings'] = $mailings;
235 if ($this->get('mailing_id')) {
237 // don't create a new mass sms if already exists
238 $ids['mailing_id'] = $this->get('mailing_id');
240 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
241 $mailingTableName = CRM_Mailing_BAO_Mailing
::getTableName();
243 // delete previous includes/excludes, if mailing already existed
248 $mg = new CRM_Mailing_DAO_MailingGroup();
249 $mg->mailing_id
= $ids['mailing_id'];
250 $mg->entity_table
= ($entity == 'groups') ?
$groupTableName : $mailingTableName;
252 while ($mg->fetch()) {
258 // new mailing, so lets set the created_id
259 $session = CRM_Core_Session
::singleton();
260 $params['created_id'] = $session->get('userID');
261 $params['created_date'] = date('YmdHis');
264 $mailing = CRM_Mailing_BAO_Mailing
::create($params, $ids);
266 $this->set('mailing_id', $mailing->id
);
268 // also compute the recipients and store them in the mailing recipients table
269 CRM_Mailing_BAO_Mailing
::getRecipients($mailing->id
,
276 $count = CRM_Mailing_BAO_Recipients
::mailingSize($mailing->id
);
277 $this->set('count', $count);
278 $this->assign('count', $count);
279 $this->set('groups', $groups);
280 $this->set('mailings', $mailings);
282 if ($qf_Group_submit) {
283 $status = ts("Your Mass SMS has been saved.");
284 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
285 $url = CRM_Utils_System
::url('civicrm/mailing', 'reset=1&sms=1');
286 return $this->controller
->setDestination($url);
291 * Display Name of the form.
296 public function getTitle() {
297 return ts('Select Recipients');
301 * Global validation rules for the form.
303 * @param array $fields
304 * Posted values of the form.
307 * list of errors to be posted back to the form
309 public static function formRule($fields) {
311 if (isset($fields['includeGroups']) &&
312 is_array($fields['includeGroups']) &&
313 isset($fields['excludeGroups']) &&
314 is_array($fields['excludeGroups'])
316 $checkGroups = array();
317 $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']);
318 if (!empty($checkGroups)) {
319 $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
323 if (isset($fields['includeMailings']) &&
324 is_array($fields['includeMailings']) &&
325 isset($fields['excludeMailings']) &&
326 is_array($fields['excludeMailings'])
328 $checkMailings = array();
329 $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
330 if (!empty($checkMailings)) {
331 $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).');
335 return empty($errors) ?
TRUE : $errors;