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'));
34 if (CRM_Core_Permission
::check('administer CiviCRM')) {
35 $this->assign('isAdmin', 1);
40 * Set default values for the form.
41 * The default values are retrieved from the database.
43 public function setDefaultValues() {
44 $mailingID = CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
45 $continue = CRM_Utils_Request
::retrieve('continue', 'String', $this, FALSE, NULL);
50 $mailing = new CRM_Mailing_DAO_Mailing();
51 $mailing->id
= $mailingID;
52 $mailing->addSelect('name');
55 $defaults['name'] = $mailing->name
;
57 $defaults['name'] = ts('Copy of %1', [1 => $mailing->name
]);
60 // CRM-7590, reuse same mailing ID if we are continuing
61 $this->set('mailing_id', $mailingID);
64 $dao = new CRM_Mailing_DAO_MailingGroup();
67 $dao->mailing_id
= $mailingID;
69 while ($dao->fetch()) {
70 $mailingGroups[$dao->entity_table
][$dao->group_type
][] = $dao->entity_id
;
73 $defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include'];
74 $defaults['excludeGroups'] = $mailingGroups['civicrm_group']['Exclude'] ??
NULL;
76 $defaults['includeMailings'] = CRM_Utils_Array
::value('Include', CRM_Utils_Array
::value('civicrm_mailing', $mailingGroups));
77 $defaults['excludeMailings'] = CRM_Utils_Array
::value('Exclude', CRM_Utils_Array
::value('civicrm_mailing', $mailingGroups));
84 * Build the form object.
86 public function buildQuickForm() {
89 $context = $this->get('context');
91 $this->assign('context', $context);
93 $this->add('text', 'name', ts('Name Your SMS'),
94 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Mailing', 'name'),
98 $this->add('select', 'sms_provider_id',
99 ts('Select SMS Provider'),
100 CRM_Utils_Array
::collect('title', CRM_SMS_BAO_Provider
::getProviders()),
104 // Get the mailing groups.
105 $groups = CRM_Core_PseudoConstant
::nestedGroup(TRUE, 'Mailing');
107 // Get the sms mailing list.
108 $mailings = CRM_Mailing_PseudoConstant
::completed('sms');
113 // run the groups through a hook so users can trim it if needed
114 CRM_Utils_Hook
::mailingGroups($this, $groups, $mailings);
118 'style' => 'width: 100%; max-width: 60em;',
119 'class' => 'crm-select2',
120 'placeholder' => ts('- select -'),
123 $this->add('select', 'includeGroups',
124 ts('Include Group(s)'),
130 $this->add('select', 'excludeGroups',
131 ts('Exclude Group(s)'),
137 $this->add('select', 'includeMailings',
138 ts('INCLUDE Recipients of These Message(s)'),
143 $this->add('select', 'excludeMailings',
144 ts('EXCLUDE Recipients of These Message(s)'),
150 $this->addFormRule(['CRM_SMS_Form_Group', 'formRule']);
155 'name' => ts('Next'),
156 'spacing' => ' ',
161 'name' => ts('Cancel'),
165 $this->addButtons($buttons);
167 $this->assign('groupCount', count($groups));
168 $this->assign('mailingCount', count($mailings));
171 public function postProcess() {
172 $values = $this->controller
->exportValues($this->_name
);
182 if (!empty($values[$n])) {
183 $params[$n] = $values[$n];
184 if ($n == 'sms_provider_id') {
185 // Get the from Name.
186 $params['from_name'] = CRM_Core_DAO
::getFieldValue('CRM_SMS_DAO_Provider', $params['sms_provider_id'], 'username');
191 $qf_Group_submit = $this->controller
->exportValue($this->_name
, '_qf_Group_submit');
192 $this->set('name', $params['name']);
194 $inGroups = $values['includeGroups'];
195 $outGroups = $values['excludeGroups'];
196 $inMailings = $values['includeMailings'];
197 $outMailings = $values['excludeMailings'];
199 if (is_array($inGroups)) {
200 foreach ($inGroups as $key => $id) {
202 $groups['include'][] = $id;
206 if (is_array($outGroups)) {
207 foreach ($outGroups as $key => $id) {
209 $groups['exclude'][] = $id;
215 if (is_array($inMailings)) {
216 foreach ($inMailings as $key => $id) {
218 $mailings['include'][] = $id;
222 if (is_array($outMailings)) {
223 foreach ($outMailings as $key => $id) {
225 $mailings['exclude'][] = $id;
230 $session = CRM_Core_Session
::singleton();
231 $params['groups'] = $groups;
232 $params['mailings'] = $mailings;
234 if ($this->get('mailing_id')) {
236 // don't create a new mass sms if already exists
237 $ids['mailing_id'] = $this->get('mailing_id');
239 $groupTableName = CRM_Contact_BAO_Group
::getTableName();
240 $mailingTableName = CRM_Mailing_BAO_Mailing
::getTableName();
242 // delete previous includes/excludes, if mailing already existed
247 $mg = new CRM_Mailing_DAO_MailingGroup();
248 $mg->mailing_id
= $ids['mailing_id'];
249 $mg->entity_table
= ($entity == 'groups') ?
$groupTableName : $mailingTableName;
251 while ($mg->fetch()) {
257 // new mailing, so lets set the created_id
258 $session = CRM_Core_Session
::singleton();
259 $params['created_id'] = $session->get('userID');
260 $params['created_date'] = date('YmdHis');
263 $mailing = CRM_Mailing_BAO_Mailing
::create($params, $ids);
265 $this->set('mailing_id', $mailing->id
);
267 // also compute the recipients and store them in the mailing recipients table
268 CRM_Mailing_BAO_Mailing
::getRecipients($mailing->id
);
270 $count = CRM_Mailing_BAO_Recipients
::mailingSize($mailing->id
);
271 $this->set('count', $count);
272 $this->assign('count', $count);
273 $this->set('groups', $groups);
274 $this->set('mailings', $mailings);
276 if ($qf_Group_submit) {
277 $status = ts("Your Mass SMS has been saved.");
278 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
279 $url = CRM_Utils_System
::url('civicrm/mailing', 'reset=1&sms=1');
280 return $this->controller
->setDestination($url);
285 * Display Name of the form.
290 public function getTitle() {
291 return ts('Select Recipients');
295 * Global validation rules for the form.
297 * @param array $fields
298 * Posted values of the form.
301 * list of errors to be posted back to the form
303 public static function formRule($fields) {
305 if (isset($fields['includeGroups']) &&
306 is_array($fields['includeGroups']) &&
307 isset($fields['excludeGroups']) &&
308 is_array($fields['excludeGroups'])
311 $checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']);
312 if (!empty($checkGroups)) {
313 $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
317 if (isset($fields['includeMailings']) &&
318 is_array($fields['includeMailings']) &&
319 isset($fields['excludeMailings']) &&
320 is_array($fields['excludeMailings'])
323 $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
324 if (!empty($checkMailings)) {
325 $errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).');
329 return empty($errors) ?
TRUE : $errors;