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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class generates form components for processing Entity
40 class CRM_Core_Form_RecurringEntity
{
44 protected static $_entityId = NULL;
47 * Schedule Reminder ID
49 protected static $_scheduleReminderID = NULL;
52 * Schedule Reminder data
54 protected static $_scheduleReminderDetails = array();
59 protected static $_parentEntityId = NULL;
62 * Exclude date information
64 public static $_excludeDateInfo = array();
69 public static $_entityTable;
72 * Checks current entityID has parent
74 public static $_hasParent = FALSE;
76 static function preProcess($entityTable) {
77 self
::$_entityId = (int) CRM_Utils_Request
::retrieve('id', 'Positive');
78 self
::$_entityTable = $entityTable;
80 if (self
::$_entityId && $entityTable) {
81 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor(self
::$_entityId, $entityTable);
82 if ($checkParentExistsForThisId) {
83 self
::$_hasParent = TRUE;
84 self
::$_parentEntityId = $checkParentExistsForThisId;
85 self
::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, $entityTable);
88 self
::$_parentEntityId = self
::$_entityId;
89 self
::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId(self
::$_entityId, $entityTable);
91 self
::$_scheduleReminderID = self
::$_scheduleReminderDetails->id
;
94 CRM_Core_OptionValue
::getValues(array('name' => $entityTable.'_repeat_exclude_dates_'.self
::$_parentEntityId), $optionValue);
95 $excludeOptionValues = array();
96 if (!empty($optionValue)) {
97 foreach($optionValue as $key => $val) {
98 $excludeOptionValues[$val['value']] = date('m/d/Y', strtotime($val['value']));
100 self
::$_excludeDateInfo = $excludeOptionValues;
106 * This function sets the default values for the form. For edit/view mode
107 * the default values are retrieved from the database
113 static function setDefaultValues() {
115 if (self
::$_scheduleReminderID) {
116 $defaults['repetition_frequency_unit'] = self
::$_scheduleReminderDetails->repetition_frequency_unit
;
117 $defaults['repetition_frequency_interval'] = self
::$_scheduleReminderDetails->repetition_frequency_interval
;
118 $defaults['start_action_condition'] = array_flip(explode(",",self
::$_scheduleReminderDetails->start_action_condition
));
119 foreach($defaults['start_action_condition'] as $key => $val) {
121 $defaults['start_action_condition'][$key] = $val;
123 $defaults['start_action_offset'] = self
::$_scheduleReminderDetails->start_action_offset
;
124 if (self
::$_scheduleReminderDetails->start_action_offset
) {
125 $defaults['ends'] = 1;
127 list($defaults['repeat_absolute_date']) = CRM_Utils_Date
::setDateDefaults(self
::$_scheduleReminderDetails->absolute_date
);
128 if (self
::$_scheduleReminderDetails->absolute_date
) {
129 $defaults['ends'] = 2;
131 $defaults['limit_to'] = self
::$_scheduleReminderDetails->limit_to
;
132 if (self
::$_scheduleReminderDetails->limit_to
) {
133 $defaults['repeats_by'] = 1;
135 $explodeStartActionCondition = array();
136 if (self
::$_scheduleReminderDetails->entity_status
) {
137 $explodeStartActionCondition = explode(" ", self
::$_scheduleReminderDetails->entity_status
);
138 $defaults['entity_status_1'] = $explodeStartActionCondition[0];
139 $defaults['entity_status_2'] = $explodeStartActionCondition[1];
141 if (self
::$_scheduleReminderDetails->entity_status
) {
142 $defaults['repeats_by'] = 2;
148 static function buildQuickForm(&$form) {
149 $form->assign('currentEntityId', self
::$_entityId);
150 $form->assign('entityTable', self
::$_entityTable);
151 $form->assign('scheduleReminderId', self
::$_scheduleReminderID);
152 $form->assign('hasParent', self
::$_hasParent);
154 $form->_freqUnits
= array('hour' => 'hour') + CRM_Core_OptionGroup
::values('recur_frequency_units');
155 foreach ($form->_freqUnits
as $val => $label) {
156 if ($label == "day") {
159 $freqUnitsDisplay[$val] = ts('%1ly', array(1 => $label));
161 // echo "<pre>";print_r($freqUnitsDisplay);
162 $dayOfTheWeek = array('monday' => 'Monday',
163 'tuesday' => 'Tuesday',
164 'wednesday' => 'Wednesday',
165 'thursday' => 'Thursday',
166 'friday' => 'Friday',
167 'saturday' => 'Saturday',
170 $form->add('select', 'repetition_frequency_unit', ts('Repeats:'), $freqUnitsDisplay);
171 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(1, 30);
172 $form->add('select', 'repetition_frequency_interval', ts('Repeats every:'), $numericOptions, '', array('style' => 'width:55px;'));
173 $form->addDateTime('repetition_start_date', ts('Repetition Start Date'), FALSE, array('formatType' => 'activityDateTime'));
174 foreach($dayOfTheWeek as $key => $val) {
175 $startActionCondition[] = $form->createElement('checkbox', $key, NULL, substr($val." ", 0, 3));
177 $form->addGroup($startActionCondition, 'start_action_condition', ts('Repeats on'));
178 $roptionTypes = array('1' => ts('day of the month'),
179 '2' => ts('day of the week'),
181 $form->addRadio('repeats_by', ts("Repeats By:"), $roptionTypes, array(), NULL);
182 $getMonths = CRM_Core_SelectValues
::getNumericOptions(1, 31);
183 $form->add('select', 'limit_to', '', $getMonths, FALSE, array('style' => 'width:55px;'));
184 $dayOfTheWeekNo = array('first' => 'First',
190 $form->add('select', 'entity_status_1', ts(''), $dayOfTheWeekNo);
191 $form->add('select', 'entity_status_2', ts(''), $dayOfTheWeek);
192 $eoptionTypes = array('1' => ts('After'),
195 $form->addRadio('ends', ts("Ends:"), $eoptionTypes, array(), NULL);
196 $form->add('text', 'start_action_offset', ts(''), array('size' => 3, 'maxlength' => 2));
197 $form->addFormRule(array('CRM_Core_Form_RecurringEntity', 'formRule'));
198 $form->addDate('repeat_absolute_date', ts('On'), FALSE, array('formatType' => 'mailing'));
199 $form->addDate('exclude_date', ts('Exclude Date(s)'), FALSE);
200 $select = $form->add('select', 'exclude_date_list', ts(''), self
::$_excludeDateInfo, FALSE, array('style' => 'width:150px;', 'size' => 4));
201 $select->setMultiple(TRUE);
202 $form->addElement('button','add_to_exclude_list','>>','onClick="addToExcludeList(document.getElementById(\'exclude_date\').value);"');
203 $form->addElement('button','remove_from_exclude_list', '<<', 'onClick="removeFromExcludeList(\'exclude_date_list\')"');
204 $form->addElement('hidden', 'copyExcludeDates', '', array('id' => 'copyExcludeDates'));
205 $form->addElement('hidden', 'allowRepeatConfigToSubmit', '', array('id' => 'allowRepeatConfigToSubmit'));
206 $form->addButtons(array(
209 'name' => ts('Save'),
214 'name' => ts('Cancel')
221 * global validation rules for the form
223 * @param array $fields posted values of the form
225 * @return array list of errors to be posted back to the form
229 static function formRule($values) {
231 //Process this function only when you get this variable
232 if ($values['allowRepeatConfigToSubmit'] == 1) {
233 $dayOfTheWeek = array(monday
,tuesday
,wednesday
,thursday
,friday
,saturday
,sunday
);
235 if (!CRM_Utils_Array
::value('repetition_frequency_unit', $values)) {
236 $errors['repetition_frequency_unit'] = ts('This is a required field');
239 if (!CRM_Utils_Array
::value('repetition_frequency_interval', $values)) {
240 $errors['repetition_frequency_interval'] = ts('This is a required field');
243 if (CRM_Utils_Array
::value('ends', $values)) {
244 if ($values['ends'] == 1) {
245 if (empty($values['start_action_offset'])) {
246 $errors['start_action_offset'] = ts('This is a required field');
248 else if ($values['start_action_offset'] > 30) {
249 $errors['start_action_offset'] = ts('Occurrences should be less than or equal to 30');
252 if ($values['ends'] == 2) {
253 if (CRM_Utils_Array
::value('repeat_absolute_date', $values)) {
254 $entityStartDate = CRM_Utils_Date
::processDate($values['repetition_start_date']);
255 $end = CRM_Utils_Date
::processDate($values['repeat_absolute_date']);
256 if (($end < $entityStartDate) && ($end != 0)) {
257 $errors['repeat_absolute_date'] = ts('End date should be after current entity\'s start date');
261 $errors['repeat_absolute_date'] = ts('This is a required field');
266 $errors['ends'] = ts('This is a required field');
270 if (CRM_Utils_Array
::value('repeats_by', $values)) {
271 if ($values['repeats_by'] == 1) {
272 if (CRM_Utils_Array
::value('limit_to', $values)) {
273 if ($values['limit_to'] < 1 && $values['limit_to'] > 31) {
274 $errors['limit_to'] = ts('Invalid day of the month');
278 $errors['limit_to'] = ts('Invalid day of the month');
281 if ($values['repeats_by'] == 2) {
282 if (CRM_Utils_Array
::value('entity_status_1', $values)) {
283 $dayOfTheWeekNo = array(first
, second
, third
, fourth
, last
);
284 if (!in_array($values['entity_status_1'], $dayOfTheWeekNo)) {
285 $errors['entity_status_1'] = ts('Invalid option');
289 $errors['entity_status_1'] = ts('Invalid option');
291 if (CRM_Utils_Array
::value('entity_status_2', $values)) {
292 if (!in_array($values['entity_status_2'], $dayOfTheWeek)) {
293 $errors['entity_status_2'] = ts('Invalid day name');
297 $errors['entity_status_2'] = ts('Invalid day name');
306 * Function to process the form
312 static function postProcess($params = array(), $type, $linkedEntities = array()) {
313 //Check entity_id not present in params take it from class variable
314 if (!CRM_Utils_Array
::value('entity_id', $params)) {
315 $params['entity_id'] = self
::$_entityId;
317 //Process this function only when you get this variable
318 if ($params['allowRepeatConfigToSubmit'] == 1) {
319 if (CRM_Utils_Array
::value('entity_table', $params) && CRM_Utils_Array
::value('entity_id', $params) && $type) {
320 $params['used_for'] = $type;
321 if (!CRM_Utils_Array
::value('parent_entity_id', $params)) {
322 $params['parent_entity_id'] = self
::$_parentEntityId;
324 if (CRM_Utils_Array
::value('schedule_reminder_id', $params)) {
325 $params['id'] = $params['schedule_reminder_id'];
328 $params['id'] = self
::$_scheduleReminderID;
331 //Save post params to the schedule reminder table
332 $dbParams = CRM_Core_BAO_RecurringEntity
::mapFormValuesToDB($params);
334 //Delete repeat configuration and rebuild
335 if (CRM_Utils_Array
::value('id', $params)) {
336 CRM_Core_BAO_ActionSchedule
::del($params['id']);
337 unset($params['id']);
339 $actionScheduleObj = CRM_Core_BAO_ActionSchedule
::add($dbParams);
342 $excludeDateList = array();
343 if (CRM_Utils_Array
::value('copyExcludeDates', $params) && CRM_Utils_Array
::value('parent_entity_id', $params) && $actionScheduleObj->entity_value
) {
344 //Since we get comma separated values lets get them in array
345 $excludeDates = array();
346 $excludeDates = explode(",", $params['copyExcludeDates']);
348 //Check if there exists any values for this option group
349 $optionGroupIdExists = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
350 $type.'_repeat_exclude_dates_'.$params['parent_entity_id'],
354 if ($optionGroupIdExists) {
355 CRM_Core_BAO_OptionGroup
::del($optionGroupIdExists);
359 'name' => $type.'_repeat_exclude_dates_'.$actionScheduleObj->entity_value
,
360 'title' => $type.' recursion',
364 $opGroup = CRM_Core_BAO_OptionGroup
::add($optionGroupParams);
367 $fieldValues = array('option_group_id' => $opGroup->id
);
368 foreach($excludeDates as $val) {
371 'option_group_id' => $opGroup->id
,
372 'label' => CRM_Utils_Date
::processDate($val),
373 'value' => CRM_Utils_Date
::processDate($val),
374 'name' => $opGroup->name
,
375 'description' => 'Used for recurring '.$type,
376 'weight' => CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues),
379 $excludeDateList[] = $optionGroupValue['value'];
380 CRM_Core_BAO_OptionValue
::add($optionGroupValue);
386 $apiEntityType = array();
387 $apiEntityType = explode("_", $type);
388 if (!empty($apiEntityType[1])) {
389 $apiType = $apiEntityType[1];
391 //Delete relations if any from recurring entity tables before inserting new relations for this entity id
392 if ($params['entity_id']) {
393 //If entity has any pre delete function, consider that first
394 if (CRM_Utils_Array
::value('pre_delete_func', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]) &&
395 CRM_Utils_Array
::value('helper_class', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']])) {
396 call_user_func(array(
397 CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['helper_class'],
398 call_user_func_array(CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['pre_delete_func'], array($params['entity_id'])))
401 //Ready to execute delete on entities if it has delete function set
402 if (CRM_Utils_Array
::value('delete_func', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]) &&
403 CRM_Utils_Array
::value('helper_class', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']])) {
404 //Check if pre delete function has some ids to be deleted
405 if (!empty(CRM_Core_BAO_RecurringEntity
::$_entitiesToBeDeleted)) {
406 foreach (CRM_Core_BAO_RecurringEntity
::$_entitiesToBeDeleted as $eid) {
407 $result = civicrm_api3(
408 ucfirst(strtolower($apiType)),
409 CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['delete_func'],
415 if ($result['error']) {
416 CRM_Core_Error
::statusBounce('Error creating recurring list');
421 $getRelatedEntities = CRM_Core_BAO_RecurringEntity
::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE);
422 foreach ($getRelatedEntities as $key => $value) {
423 $result = civicrm_api3(
424 ucfirst(strtolower($apiType)),
425 CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['delete_func'],
428 'id' => $value['id'],
431 if ($result['error']) {
432 CRM_Core_Error
::statusBounce('Error creating recurring list');
437 // lets delete current entity from recurring-entity table, which is going to be a new parent
438 CRM_Core_BAO_RecurringEntity
::delEntity($params['entity_id'], $params['entity_table'], TRUE);
441 $recursion = new CRM_Core_BAO_RecurringEntity();
442 $recursion->dateColumns
= $params['dateColumns'];
443 $recursion->scheduleId
= $actionScheduleObj->id
;
445 if (!empty($excludeDateList)) {
446 $recursion->excludeDates
= $excludeDateList;
447 $recursion->excludeDateRangeColumns
= $params['excludeDateRangeColumns'];
449 $recursion->intervalDateColumns
= $params['intervalDateColumns'];
450 $recursion->entity_id
= $params['entity_id'];
451 $recursion->entity_table
= $params['entity_table'];
452 if (!empty($linkedEntities)) {
453 $recursion->linkedEntities
= $linkedEntities;
456 $recurResult = $recursion->generate();
458 $status = ts('Repeat Configuration has been saved');
459 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
466 * Return a descriptive name for the page, used in wizard header
471 public function getTitle() {
472 return ts('Repeat Entity');