3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright (C) 2011 Marty Wright |
7 | Licensed to CiviCRM under the Academic Free License version 3.0. |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * This class generates form components for Scheduling Reminders
41 class CRM_Admin_Form_ScheduleReminders
extends CRM_Admin_Form
{
44 * Scheduled Reminder ID
46 protected $_id = NULL;
51 * Function to build the form
56 public function buildQuickForm() {
57 parent
::buildQuickForm();
58 $this->_mappingID
= $mappingID = NULL;
59 $providersCount = CRM_SMS_BAO_Provider
::activeProviderCount();
61 if ($this->_action
& (CRM_Core_Action
::DELETE
)) {
63 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'title');
64 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
65 if ($this->_context
== 'event') {
66 $this->_eventId
= CRM_Utils_Request
::retrieve('eventId', 'Integer', $this);
68 $this->assign('reminderName', $reminderName);
71 elseif ($this->_action
& (CRM_Core_Action
::UPDATE
)) {
73 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'mapping_id');
74 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
75 if ($this->_context
== 'event') {
76 $this->_eventId
= CRM_Utils_Request
::retrieve('eventId', 'Integer', $this);
80 if (!empty($_POST) && !empty($_POST['entity'])) {
81 $mappingID = $_POST['entity'][0];
83 elseif ($this->_mappingID
) {
84 $mappingID = $this->_mappingID
;
91 array('size' => 45, 'maxlength' => 128),
95 $selectionOptions = CRM_Core_BAO_ActionSchedule
::getSelection($mappingID);
96 extract($selectionOptions);
99 CRM_Core_Error
::fatal('Could not find mapping for scheduled reminders.');
101 $this->assign('entityMapping', json_encode($entityMapping));
102 $this->assign('recipientMapping', json_encode($recipientMapping));
109 'name' => 'entity[0]',
110 'style' => 'vertical-align: top;',
114 $sel->setOptions(array($sel1, $sel2, $sel3));
116 if (is_a($sel->_elements
[1], 'HTML_QuickForm_select')) {
117 // make second selector a multi-select -
118 $sel->_elements
[1]->setMultiple(TRUE);
119 $sel->_elements
[1]->setSize(5);
122 if (is_a($sel->_elements
[2], 'HTML_QuickForm_select')) {
123 // make third selector a multi-select -
124 $sel->_elements
[2]->setMultiple(TRUE);
125 $sel->_elements
[2]->setSize(5);
128 //get the frequency units.
129 $this->_freqUnits
= CRM_Core_SelectValues
::getScheduleReminderFrequencyUnits();
131 //pass the mapping ID in UPDATE mode
132 $mappings = CRM_Core_BAO_ActionSchedule
::getMapping($mappingID);
134 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(0, 30);
137 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
138 $title = ts('Email');
139 $isActive = ts('Send email');
140 $recordActivity = ts('Record activity for automated email');
141 if ($providersCount) {
142 $this->assign('sms', $providersCount);
143 $title = ts('Email or SMS');
144 $isActive = ts('Send email or SMS');
145 $recordActivity = ts('Record activity for automated email or SMS');
146 $options = CRM_Core_OptionGroup
::values('msg_mode');
147 $this->add('select', 'mode', ts('Send as'), $options);
149 $providers = CRM_SMS_BAO_Provider
::getProviders(NULL, NULL, TRUE, 'is_default desc');
151 $providerSelect = array();
152 foreach ($providers as $provider) {
153 $providerSelect[$provider['id']] = $provider['title'];
155 $this->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE);
158 $this->assign('title', $title);
159 foreach ($this->_freqUnits
as $val => $label) {
160 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
163 $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing'));
166 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
168 $condition = array('before' => ts('before'),
169 'after' => ts('after'),
172 $this->add('select', 'start_action_condition', ts('Action Condition'), $condition);
174 $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE);
176 $this->addElement('checkbox', 'record_activity', $recordActivity);
178 $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
179 NULL, array('onclick' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
182 $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay);
183 $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions);
184 $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay);
185 $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions);
186 $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE);
187 $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE);
189 $this->add('text', 'from_name', ts('From Name'));
190 $this->add('text', 'from_email', ts('From Email'));
192 $recipient = 'activity_contacts';
193 $recipientListingOptions = array();
196 $recipient = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping',
202 $limitOptions = array(1 => ts('Limit to'), 0 => ts('Also include'));
203 $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions);
205 $this->add('select', 'recipient', ts('Recipients'), $sel5[$recipient],
206 FALSE, array('onClick' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
209 if (!empty($_POST['is_recipient_listing'])) {
210 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($_POST['entity'][0], $_POST['recipient']);
212 elseif (!empty($this->_values
['recipient_listing'])) {
213 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_values
['mapping_id'], $this->_values
['recipient']);
215 $recipientListing = $this->add('select', 'recipient_listing', ts('Recipient Listing'), $recipientListingOptions);
216 $recipientListing->setMultiple(TRUE);
217 $this->add('hidden', 'is_recipient_listing', empty($recipientListingOptions) ?
FALSE : TRUE, array('id' => 'is_recipient_listing'));
219 $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE));
226 CRM_Core_PseudoConstant
::group()
229 CRM_Mailing_BAO_Mailing
::commonCompose($this);
231 $this->add('text', 'subject', ts('Subject'),
232 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
235 $this->add('checkbox', 'is_active', $isActive);
237 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'));
242 * @param array $fields the input form values
244 * @return true if no errors, else array of errors
248 static function formRule($fields) {
250 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
251 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
253 $errors['entity'] = ts('Please select appropriate value');
256 if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) {
257 if (count($fields['entity'][1]) > 1) {
258 $errors['entity'] = ts('You may only select one contact field per reminder');
260 elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) {
261 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
265 if (!empty($fields['is_active']) &&
266 CRM_Utils_System
::isNull($fields['subject'])
268 $errors['subject'] = ts('Subject is a required field.');
271 if (CRM_Utils_System
::isNull(CRM_Utils_Array
::value(1, $fields['entity']))) {
272 $errors['entity'] = ts('Please select entity value');
275 if (!CRM_Utils_System
::isNull($fields['absolute_date'])) {
276 if (CRM_Utils_Date
::format(CRM_Utils_Date
::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date
::format(date('Ymd'))) {
277 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
281 if (!empty($errors)) {
285 return empty($errors) ?
TRUE : $errors;
291 function setDefaultValues() {
292 if ($this->_action
& CRM_Core_Action
::ADD
) {
293 $defaults['is_active'] = 1;
294 $defaults['mode'] = 'Email';
295 $defaults['record_activity'] = 1;
298 $defaults = $this->_values
;
299 $entityValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
300 CRM_Utils_Array
::value('entity_value', $defaults)
302 $entityStatus = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
303 CRM_Utils_Array
::value('entity_status', $defaults)
305 $defaults['entity'][0] = CRM_Utils_Array
::value('mapping_id', $defaults);
306 $defaults['entity'][1] = $entityValue;
307 $defaults['entity'][2] = $entityStatus;
308 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $defaults)) {
309 list($date, $time) = CRM_Utils_Date
::setDateDefaults($absoluteDate);
310 $defaults['absolute_date'] = $date;
313 if ($recipientListing = CRM_Utils_Array
::value('recipient_listing', $defaults)) {
314 $defaults['recipient_listing'] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
318 $defaults['text_message'] = CRM_Utils_Array
::value('body_text', $defaults);
319 $defaults['html_message'] = CRM_Utils_Array
::value('body_html', $defaults);
320 $defaults['template'] = CRM_Utils_Array
::value('msg_template_id', $defaults);
321 if (!empty($defaults['group_id'])) {
322 $defaults['recipient'] = 'group';
324 elseif (!empty($defaults['recipient_manual'])) {
325 $defaults['recipient'] = 'manual';
326 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
334 * Function to process the form
340 public function postProcess() {
341 if ($this->_action
& CRM_Core_Action
::DELETE
) {
343 CRM_Core_BAO_ActionSchedule
::del($this->_id
);
344 CRM_Core_Session
::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
345 if ($this->_context
== 'event' && $this->_eventId
) {
346 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder',
347 "reset=1&action=update&id={$this->_eventId}"
349 $session = CRM_Core_Session
::singleton();
350 $session->pushUserContext($url);
354 $values = $this->controller
->exportValues($this->getName());
368 foreach ($keys as $key) {
369 $params[$key] = CRM_Utils_Array
::value($key, $values);
373 'start_action_offset',
375 'start_action_condition',
377 'repetition_frequency_unit',
378 'repetition_frequency_interval',
379 'end_frequency_unit',
380 'end_frequency_interval',
385 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $params)) {
386 $params['absolute_date'] = CRM_Utils_Date
::processDate($absoluteDate);
387 foreach ($moreKeys as $mkey) {
388 $params[$mkey] = 'null';
392 $params['absolute_date'] = 'null';
393 foreach ($moreKeys as $mkey) {
394 $params[$mkey] = CRM_Utils_Array
::value($mkey, $values);
398 $params['body_text'] = CRM_Utils_Array
::value('text_message', $values);
399 $params['body_html'] = CRM_Utils_Array
::value('html_message', $values);
401 if (CRM_Utils_Array
::value('recipient', $values) == 'manual') {
402 $params['recipient_manual'] = CRM_Utils_Array
::value('recipient_manual_id', $values);
403 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
405 elseif (CRM_Utils_Array
::value('recipient', $values) == 'group') {
406 $params['group_id'] = $values['group_id'];
407 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
409 elseif (!CRM_Utils_System
::isNull($values['recipient_listing'])) {
410 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
411 $params['recipient_listing'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
412 CRM_Utils_Array
::value('recipient_listing', $values)
414 $params['group_id'] = $params['recipient_manual'] = 'null';
417 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
418 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
421 $params['mapping_id'] = $values['entity'][0];
422 $entity_value = $values['entity'][1];
423 $entity_status = $values['entity'][2];
426 'entity_value', 'entity_status') as $key) {
427 $params[$key] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $
$key);
430 $params['is_active'] = CRM_Utils_Array
::value('is_active', $values, 0);
431 $params['is_repeat'] = CRM_Utils_Array
::value('is_repeat', $values, 0);
433 if (CRM_Utils_Array
::value('is_repeat', $values) == 0) {
434 $params['repetition_frequency_unit'] = 'null';
435 $params['repetition_frequency_interval'] = 'null';
436 $params['end_frequency_unit'] = 'null';
437 $params['end_frequency_interval'] = 'null';
438 $params['end_action'] = 'null';
439 $params['end_date'] = 'null';
442 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
443 $params['id'] = $this->_id
;
445 elseif ($this->_action
& CRM_Core_Action
::ADD
) {
446 // we do this only once, so name never changes
447 $params['name'] = CRM_Utils_String
::munge($params['title'], '_', 64);
450 $composeFields = array(
451 'template', 'saveTemplate',
452 'updateTemplate', 'saveTemplateName',
455 //mail template is composed
457 $composeParams = array();
458 foreach ($composeFields as $key) {
459 if (!empty($values[$key])) {
460 $composeParams[$key] = $values[$key];
464 if (!empty($composeParams['updateTemplate'])) {
465 $templateParams = array(
466 'msg_text' => $params['body_text'],
467 'msg_html' => $params['body_html'],
468 'msg_subject' => $params['subject'],
472 $templateParams['id'] = $values['template'];
474 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
477 if (!empty($composeParams['saveTemplate'])) {
478 $templateParams = array(
479 'msg_text' => $params['body_text'],
480 'msg_html' => $params['body_html'],
481 'msg_subject' => $params['subject'],
485 $templateParams['msg_title'] = $composeParams['saveTemplateName'];
487 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
490 if (isset($msgTemplate->id
)) {
491 $params['msg_template_id'] = $msgTemplate->id
;
494 $params['msg_template_id'] = CRM_Utils_Array
::value('template', $values);
497 $bao = CRM_Core_BAO_ActionSchedule
::add($params);
498 // we need to set this on the form so that hooks can identify the created entity
499 $this->set('id', $bao->id
);
502 $status = ts("Your new Reminder titled %1 has been saved.",
503 array(1 => "<strong>{$values['title']}</strong>")
505 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
506 $status = ts("Your Reminder titled %1 has been updated.",
507 array(1 => "<strong>{$values['title']}</strong>")
510 if ($this->_context
== 'event' && $this->_eventId
) {
511 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder',
512 "reset=1&action=update&id={$this->_eventId}"
514 $session = CRM_Core_Session
::singleton();
515 $session->pushUserContext($url);
518 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');