3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Choose include / exclude groups and mass sms.
21 class CRM_SMS_Form_Group
extends CRM_Contact_Form_Task
{
24 * Set variables up before form is built.
26 public function preProcess() {
27 if (!CRM_SMS_BAO_Provider
::activeProviderCount()) {
28 CRM_Core_Error
::statusBounce(ts('The <a href="%1">SMS Provider</a> has not been configured or is not active.', [1 => CRM_Utils_System
::url('civicrm/admin/sms/provider', 'reset=1')]));
31 $session = CRM_Core_Session
::singleton();
32 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/mailing/browse', 'reset=1&sms=1'));
33 $this->assign('isAdmin', CRM_Core_Permission
::check('administer CiviCRM'));
37 * Set default values for the form.
38 * The default values are retrieved from the database.
40 public function setDefaultValues() {
41 $mailingID = CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
42 $continue = CRM_Utils_Request
::retrieve('continue', 'String', $this, FALSE, NULL);
47 $mailing = new CRM_Mailing_DAO_Mailing();
48 $mailing->id
= $mailingID;
49 $mailing->addSelect('name');
52 $defaults['name'] = $mailing->name
;
54 $defaults['name'] = ts('Copy of %1', [1 => $mailing->name
]);
57 // CRM-7590, reuse same mailing ID if we are continuing
58 $this->set('mailing_id', $mailingID);
61 $dao = new CRM_Mailing_DAO_MailingGroup();
64 $dao->mailing_id
= $mailingID;
66 while ($dao->fetch()) {
67 $mailingGroups[$dao->entity_table
][$dao->group_type
][] = $dao->entity_id
;
70 $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include'];
71 $defaults['excludeGroups'] = $mailingGroups['civicrm_group']['Exclude'] ??
NULL;
73 $defaults['includeMailings'] = CRM_Utils_Array
::value('Include', CRM_Utils_Array
::value('civicrm_mailing', $mailingGroups));
74 $defaults['excludeMailings'] = CRM_Utils_Array
::value('Exclude', CRM_Utils_Array
::value('civicrm_mailing', $mailingGroups));
81 * Build the form object.
83 public function buildQuickForm() {
86 $context = $this->get('context');
88 $this->assign('context', $context);
90 $this->add('text', 'name', ts('Name Your SMS'),
91 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Mailing', 'name'),
95 $this->add('select', 'sms_provider_id',
96 ts('Select SMS Provider'),
97 CRM_Utils_Array
::collect('title', CRM_SMS_BAO_Provider
::getProviders(NULL, ['is_active' => 1])),
101 // Get the mailing groups.
102 $groups = CRM_Core_PseudoConstant
::nestedGroup(TRUE, 'Mailing');
104 // Get the sms mailing list.
105 $mailings = CRM_Mailing_PseudoConstant
::completed('sms');
110 // run the groups through a hook so users can trim it if needed
111 CRM_Utils_Hook
::mailingGroups($this, $groups, $mailings);
115 'style' => 'width: 100%; max-width: 60em;',
116 'class' => 'crm-select2',
117 'placeholder' => ts('- select -'),
120 $this->add('select', 'includeGroups',
121 ts('Include Group(s)'),
127 $this->add('select', 'excludeGroups',
128 ts('Exclude Group(s)'),
134 $this->add('select', 'includeMailings',
135 ts('INCLUDE Recipients of These Message(s)'),
140 $this->add('select', 'excludeMailings',
141 ts('EXCLUDE Recipients of These Message(s)'),
147 $this->addFormRule(['CRM_SMS_Form_Group', 'formRule']);
152 'name' => ts('Next'),
153 'spacing' => ' ',
158 'name' => ts('Cancel'),
162 $this->addButtons($buttons);
164 $this->assign('groupCount', count($groups));
165 $this->assign('mailingCount', count($mailings));
168 public function postProcess() {
169 $values = $this->controller
->exportValues($this->_name
);
179 if (!empty($values[$n])) {
180 $params[$n] = $values[$n];
181 if ($n == 'sms_provider_id') {
182 // Get the from Name.
183 $params['from_name'] = CRM_Core_DAO
::getFieldValue('CRM_SMS_DAO_Provider', $params['sms_provider_id'], 'username');
188 $qf_Group_submit = $this->controller
->exportValue($this->_name
, '_qf_Group_submit');
189 $this->set('name', $params['name']);
191 $inGroups = $values['includeGroups'];
192 $outGroups = $values['excludeGroups'];
193 $inMailings = $values['includeMailings'];
194 $outMailings = $values['excludeMailings'];
196 if (is_array($inGroups)) {
197 foreach ($inGroups as $key => $id) {
199 $groups['include'][] = $id;
203 if (is_array($outGroups)) {
204 foreach ($outGroups as $key => $id) {
206 $groups['exclude'][] = $id;
212 if (is_array($inMailings)) {
213 foreach ($inMailings as $key => $id) {
215 $mailings['include'][] = $id;
219 if (is_array($outMailings)) {
220 foreach ($outMailings as $key => $id) {
222 $mailings['exclude'][] = $id;
227 $session = CRM_Core_Session
::singleton();
228 $params['groups'] = $groups;
229 $params['mailings'] = $mailings;
231 if ($this->get('mailing_id')) {
233 // don't create a new mass sms if already exists
234 $ids['mailing_id'] = $this->get('mailing_id');
236 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
237 $mailingTableName = CRM_Mailing_BAO_Mailing
::getTableName();
239 // delete previous includes/excludes, if mailing already existed
244 $mg = new CRM_Mailing_DAO_MailingGroup();
245 $mg->mailing_id
= $ids['mailing_id'];
246 $mg->entity_table
= ($entity == 'groups') ?
$groupTableName : $mailingTableName;
248 while ($mg->fetch()) {
254 // new mailing, so lets set the created_id
255 $session = CRM_Core_Session
::singleton();
256 $params['created_id'] = $session->get('userID');
257 $params['created_date'] = date('YmdHis');
260 $mailing = CRM_Mailing_BAO_Mailing
::create($params, $ids);
262 $this->set('mailing_id', $mailing->id
);
264 // also compute the recipients and store them in the mailing recipients table
265 CRM_Mailing_BAO_Mailing
::getRecipients($mailing->id
);
267 $count = CRM_Mailing_BAO_Recipients
::mailingSize($mailing->id
);
268 $this->set('count', $count);
269 $this->assign('count', $count);
270 $this->set('groups', $groups);
271 $this->set('mailings', $mailings);
273 if ($qf_Group_submit) {
274 $status = ts("Your Mass SMS has been saved.");
275 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
276 $url = CRM_Utils_System
::url('civicrm/mailing', 'reset=1&sms=1');
277 return $this->controller
->setDestination($url);
282 * Display Name of the form.
287 public function getTitle() {
288 return ts('Select Recipients');
292 * Global validation rules for the form.
294 * @param array $fields
295 * Posted values of the form.
298 * list of errors to be posted back to the form
300 public static function formRule($fields) {
302 if (isset($fields['includeGroups']) &&
303 is_array($fields['includeGroups']) &&
304 isset($fields['excludeGroups']) &&
305 is_array($fields['excludeGroups'])
308 $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']);
309 if (!empty($checkGroups)) {
310 $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
314 if (isset($fields['includeMailings']) &&
315 is_array($fields['includeMailings']) &&
316 isset($fields['excludeMailings']) &&
317 is_array($fields['excludeMailings'])
320 $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
321 if (!empty($checkMailings)) {
322 $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).');
326 return empty($errors) ?
TRUE : $errors;