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;
74 public static $_entityType;
77 * Checks current entityID has parent
79 public static $_hasParent = FALSE;
81 static function preProcess($entityTable) {
82 self
::$_entityId = (int) CRM_Utils_Request
::retrieve('id', 'Positive');
83 self
::$_entityTable = $entityTable;
84 $entityType = array();
85 if (self
::$_entityId && $entityTable) {
86 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity
::getParentFor(self
::$_entityId, $entityTable);
87 $entityType = explode("_", $entityTable);
88 self
::$_entityType = $entityType[1];
89 if (self
::$_entityType) {
90 self
::$_entityType = self
::$_entityType;
92 if ($checkParentExistsForThisId) {
93 self
::$_hasParent = TRUE;
94 self
::$_parentEntityId = $checkParentExistsForThisId;
95 self
::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId($checkParentExistsForThisId, self
::$_entityType);
98 self
::$_parentEntityId = self
::$_entityId;
99 self
::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity
::getReminderDetailsByEntityId(self
::$_entityId, self
::$_entityType);
101 self
::$_scheduleReminderID = self
::$_scheduleReminderDetails->id
;
103 if (self
::$_entityType) {
104 CRM_Core_OptionValue
::getValues(array('name' => self
::$_entityType.'_repeat_exclude_dates_'.self
::$_parentEntityId), $optionValue);
105 $excludeOptionValues = array();
106 if (!empty($optionValue)) {
107 foreach($optionValue as $key => $val) {
108 $excludeOptionValues[$val['value']] = date('m/d/Y', strtotime($val['value']));
110 self
::$_excludeDateInfo = $excludeOptionValues;
116 * This function sets the default values for the form. For edit/view mode
117 * the default values are retrieved from the database
123 static function setDefaultValues() {
125 if (self
::$_scheduleReminderID) {
126 $defaults['repetition_frequency_unit'] = self
::$_scheduleReminderDetails->repetition_frequency_unit
;
127 $defaults['repetition_frequency_interval'] = self
::$_scheduleReminderDetails->repetition_frequency_interval
;
128 $defaults['start_action_condition'] = array_flip(explode(",",self
::$_scheduleReminderDetails->start_action_condition
));
129 foreach($defaults['start_action_condition'] as $key => $val) {
131 $defaults['start_action_condition'][$key] = $val;
133 $defaults['start_action_offset'] = self
::$_scheduleReminderDetails->start_action_offset
;
134 if (self
::$_scheduleReminderDetails->start_action_offset
) {
135 $defaults['ends'] = 1;
137 list($defaults['repeat_absolute_date']) = CRM_Utils_Date
::setDateDefaults(self
::$_scheduleReminderDetails->absolute_date
);
138 if (self
::$_scheduleReminderDetails->absolute_date
) {
139 $defaults['ends'] = 2;
141 $defaults['limit_to'] = self
::$_scheduleReminderDetails->limit_to
;
142 if (self
::$_scheduleReminderDetails->limit_to
) {
143 $defaults['repeats_by'] = 1;
145 $explodeStartActionCondition = array();
146 if (self
::$_scheduleReminderDetails->entity_status
) {
147 $explodeStartActionCondition = explode(" ", self
::$_scheduleReminderDetails->entity_status
);
148 $defaults['entity_status_1'] = $explodeStartActionCondition[0];
149 $defaults['entity_status_2'] = $explodeStartActionCondition[1];
151 if (self
::$_scheduleReminderDetails->entity_status
) {
152 $defaults['repeats_by'] = 2;
158 static function buildQuickForm(&$form) {
159 $form->assign('currentEntityId', self
::$_entityId);
160 $form->assign('entityTable', self
::$_entityTable);
161 $form->assign('scheduleReminderId', self
::$_scheduleReminderID);
162 $form->assign('hasParent', self
::$_hasParent);
164 $form->_freqUnits
= array('hour' => 'hour') + CRM_Core_OptionGroup
::values('recur_frequency_units');
165 foreach ($form->_freqUnits
as $val => $label) {
166 if ($label == "day") {
169 $freqUnitsDisplay[$val] = ts('%1ly', array(1 => $label));
171 // echo "<pre>";print_r($freqUnitsDisplay);
172 $dayOfTheWeek = array('monday' => 'Monday',
173 'tuesday' => 'Tuesday',
174 'wednesday' => 'Wednesday',
175 'thursday' => 'Thursday',
176 'friday' => 'Friday',
177 'saturday' => 'Saturday',
180 $form->add('select', 'repetition_frequency_unit', ts('Repeats:'), $freqUnitsDisplay, TRUE);
181 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(1, 30);
182 $form->add('select', 'repetition_frequency_interval', ts('Repeats every:'), $numericOptions, TRUE, array('style' => 'width:55px;'));
183 $form->addDateTime('repetition_start_date', ts('Repetition Start Date'), FALSE, array('formatType' => 'activityDateTime'));
184 foreach($dayOfTheWeek as $key => $val) {
185 $startActionCondition[] = $form->createElement('checkbox', $key, NULL, substr($val." ", 0, 3));
187 $form->addGroup($startActionCondition, 'start_action_condition', ts('Repeats on'));
188 $roptionTypes = array('1' => ts('day of the month'),
189 '2' => ts('day of the week'),
191 $form->addRadio('repeats_by', ts("Repeats By:"), $roptionTypes, array(), NULL);
192 $getMonths = CRM_Core_SelectValues
::getNumericOptions(1, 31);
193 $form->add('select', 'limit_to', '', $getMonths, FALSE, array('style' => 'width:55px;'));
194 $dayOfTheWeekNo = array('first' => 'First',
200 $form->add('select', 'entity_status_1', ts(''), $dayOfTheWeekNo);
201 $form->add('select', 'entity_status_2', ts(''), $dayOfTheWeek);
202 $eoptionTypes = array('1' => ts('After'),
205 $form->addRadio('ends', ts("Ends:"), $eoptionTypes, array(), NULL, TRUE);
206 $form->add('text', 'start_action_offset', ts(''), array('size' => 3, 'maxlength' => 2));
207 $form->addFormRule(array('CRM_Core_Form_RecurringEntity', 'formRule'));
208 $form->addDate('repeat_absolute_date', ts('On'), FALSE, array('formatType' => 'mailing'));
209 $form->addDate('exclude_date', ts('Exclude Date(s)'), FALSE);
210 $select = $form->add('select', 'exclude_date_list', ts(''), $form->_excludeDateInfo
, FALSE, array('style' => 'width:150px;', 'size' => 4));
211 $select->setMultiple(TRUE);
212 $form->addElement('button','add_to_exclude_list','>>','onClick="addToExcludeList(document.getElementById(\'exclude_date\').value);"');
213 $form->addElement('button','remove_from_exclude_list', '<<', 'onClick="removeFromExcludeList(\'exclude_date_list\')"');
214 $form->addElement('hidden', 'isChangeInRepeatConfiguration', '', array('id' => 'isChangeInRepeatConfiguration'));
215 $form->addElement('hidden', 'copyExcludeDates', '', array('id' => 'copyExcludeDates'));
216 $form->addButtons(array(
219 'name' => ts('Save'),
224 'name' => ts('Cancel')
231 * global validation rules for the form
233 * @param array $fields posted values of the form
235 * @return array list of errors to be posted back to the form
239 static function formRule($values) {
241 $dayOfTheWeek = array(monday
,tuesday
,wednesday
,thursday
,friday
,saturday
,sunday
);
244 if (!CRM_Utils_Array
::value('repetition_frequency_unit', $values)) {
245 $errors['repetition_frequency_unit'] = ts('This is a required field');
248 if (!CRM_Utils_Array
::value('repetition_frequency_interval', $values)) {
249 $errors['repetition_frequency_interval'] = ts('This is a required field');
252 if (CRM_Utils_Array
::value('ends', $values)) {
253 if ($values['ends'] == 1) {
254 if ($values['start_action_offset'] == "") {
255 $errors['start_action_offset'] = ts('This is a required field');
257 else if ($values['start_action_offset'] > 30) {
258 $errors['start_action_offset'] = ts('Occurrences should be less than or equal to 30');
261 if ($values['ends'] == 2) {
262 if ($values['repeat_absolute_date'] != "") {
263 $entityStartDate = CRM_Utils_Date
::processDate($values['repetition_start_date']);
264 $end = CRM_Utils_Date
::processDate($values['repeat_absolute_date']);
265 if (($end < $entityStartDate) && ($end != 0)) {
266 $errors['repeat_absolute_date'] = ts('End date should be after current entity\'s start date');
270 $errors['repeat_absolute_date'] = ts('This is a required field');
275 $errors['ends'] = ts('This is a required field');
279 if (CRM_Utils_Array
::value('repeats_by', $values)) {
280 if ($values['repeats_by'] == 1) {
281 if ($values['limit_to'] != "") {
282 if ($values['limit_to'] < 1 && $values['limit_to'] > 31) {
283 $errors['limit_to'] = ts('Invalid day of the month');
287 $errors['limit_to'] = ts('Invalid day of the month');
290 if ($values['repeats_by'] == 2) {
291 if ($values['entity_status_1'] != "" ) {
292 $dayOfTheWeekNo = array(first
, second
, third
, fourth
, last
);
293 if (!in_array($values['entity_status_1'], $dayOfTheWeekNo)) {
294 $errors['entity_status_1'] = ts('Invalid option');
298 $errors['entity_status_1'] = ts('Invalid option');
300 if ($values['entity_status_2'] != "" ) {
301 if (!in_array($values['entity_status_2'], $dayOfTheWeek)) {
302 $errors['entity_status_2'] = ts('Invalid day name');
306 $errors['entity_status_2'] = ts('Invalid day name');
314 * Function to process the form
320 static function postProcess($params = array(), $type, $linkedEntities = array()) {
321 $params['entity_id'] = self
::$_entityId;
322 if (CRM_Utils_Array
::value('entity_table', $params) && CRM_Utils_Array
::value('entity_id', $params) && $type) {
323 $params['used_for'] = $type;
324 $params['parent_entity_id'] = self
::$_parentEntityId;
325 $params['id'] = self
::$_scheduleReminderID;
327 //Save post params to the schedule reminder table
328 $dbParams = CRM_Core_BAO_RecurringEntity
::mapFormValuesToDB($params);
330 //Delete repeat configuration and rebuild
331 if (CRM_Utils_Array
::value('id', $params)) {
332 CRM_Core_BAO_ActionSchedule
::del($params['id']);
333 unset($params['id']);
335 $actionScheduleObj = CRM_Core_BAO_ActionSchedule
::add($dbParams);
338 $excludeDateList = array();
339 if (CRM_Utils_Array
::value('copyExcludeDates', $params) && CRM_Utils_Array
::value('parent_entity_id', $params)) {
340 //Since we get comma separated values lets get them in array
341 $exclude_date_list = array();
342 $exclude_date_list = explode(",", $params['copyExcludeDates']);
344 //Check if there exists any values for this option group
345 $optionGroupIdExists = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
346 $type.'_repeat_exclude_dates_'.$params['parent_entity_id'],
350 if ($optionGroupIdExists) {
351 CRM_Core_BAO_OptionGroup
::del($optionGroupIdExists);
355 'name' => $type.'_repeat_exclude_dates_'.$params['parent_entity_id'],
356 'title' => $type.' recursion',
360 $opGroup = CRM_Core_BAO_OptionGroup
::add($optionGroupParams);
363 $fieldValues = array('option_group_id' => $opGroup->id
);
364 foreach($exclude_date_list as $val) {
367 'option_group_id' => $opGroup->id
,
368 'label' => CRM_Utils_Date
::processDate($val),
369 'value' => CRM_Utils_Date
::processDate($val),
370 'name' => $opGroup->name
,
371 'description' => 'Used for recurring '.$type,
372 'weight' => CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues),
375 $excludeDateList[] = $optionGroupValue['value'];
376 CRM_Core_BAO_OptionValue
::add($optionGroupValue);
381 //Delete relations if any from recurring entity tables before inserting new relations for this entity id
382 if ($params['entity_id']) {
383 //If entity has any pre delete function, consider that first
384 if (CRM_Utils_Array
::value('pre_delete_func', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]) &&
385 CRM_Utils_Array
::value('helper_class', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']])) {
386 call_user_func(array(
387 CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['helper_class'],
388 call_user_func_array(CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['pre_delete_func'], array($params['entity_id'])))
391 //Ready to execute delete on entities if it has delete function set
392 if (CRM_Utils_Array
::value('delete_func', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]) &&
393 CRM_Utils_Array
::value('helper_class', CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']])) {
394 //Check if pre delete function has some ids to be deleted
395 if (!empty(CRM_Core_BAO_RecurringEntity
::$_entitiesToBeDeleted)) {
396 foreach (CRM_Core_BAO_RecurringEntity
::$_entitiesToBeDeleted as $value) {
397 $result = civicrm_api3(ucfirst(strtolower($type)), CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['delete_func'], array(
401 if ($result['error']) {
402 CRM_Core_Error
::statusBounce('Error creating recurring list');
407 $getRelatedEntities = CRM_Core_BAO_RecurringEntity
::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE);
408 foreach ($getRelatedEntities as $key => $value) {
409 $result = civicrm_api3(ucfirst(strtolower($type)), CRM_Core_BAO_RecurringEntity
::$_recurringEntityHelper[$params['entity_table']]['delete_func'], array(
411 'id' => $value['id'],
413 if ($result['error']) {
414 CRM_Core_Error
::statusBounce('Error creating recurring list');
419 CRM_Core_BAO_RecurringEntity
::delEntityRelations($params['entity_id'], $params['entity_table']);
422 $recursion = new CRM_Core_BAO_RecurringEntity();
423 $recursion->dateColumns
= $params['dateColumns'];
424 $recursion->scheduleId
= $actionScheduleObj->id
;
426 if (!empty($excludeDateList)) {
427 $recursion->excludeDates
= $excludeDateList;
428 $recursion->excludeDateRangeColumns
= $params['excludeDateRangeColumns'];
430 $recursion->intervalDateColumns
= $params['intervalDateColumns'];
431 $recursion->entity_id
= $params['entity_id'];
432 $recursion->entity_table
= $params['entity_table'];
433 if (!empty($linkedEntities)) {
434 $recursion->linkedEntities
= $linkedEntities;
437 $recurResult = $recursion->generate();
439 $status = ts('Repeat Configuration has been saved');
440 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
446 * Return a descriptive name for the page, used in wizard header
451 public function getTitle() {
452 return ts('Repeat Entity');