Merge pull request #5335 from colemanw/CRM-14353
[civicrm-core.git] / CRM / Core / Form / RecurringEntity.php
CommitLineData
2aa397bc
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
2aa397bc
TO
27
28/**
29 *
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
353ffa53 36
2aa397bc
TO
37/**
38 * This class generates form components for processing Entity
39 *
40 */
41class CRM_Core_Form_RecurringEntity {
42 /**
43 * Current entity id
44 */
45 protected static $_entityId = NULL;
46
47 /**
48 * Schedule Reminder ID
49 */
50 protected static $_scheduleReminderID = NULL;
51
52 /**
53 * Schedule Reminder data
54 */
55 protected static $_scheduleReminderDetails = array();
56
57 /**
58 * Parent Entity ID
59 */
60 protected static $_parentEntityId = NULL;
61
62 /**
63 * Exclude date information
64 */
65 public static $_excludeDateInfo = array();
66
67 /**
68 * Entity Table
69 */
70 public static $_entityTable;
71
72 /**
73 * Checks current entityID has parent
74 */
75 public static $_hasParent = FALSE;
76
7a9ab499
EM
77 /**
78 * @param $entityTable
79 */
2aa397bc
TO
80 public static function preProcess($entityTable) {
81 self::$_entityId = (int) CRM_Utils_Request::retrieve('id', 'Positive');
82 self::$_entityTable = $entityTable;
83
84 if (self::$_entityId && $entityTable) {
85 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor(self::$_entityId, $entityTable);
86 if ($checkParentExistsForThisId) {
87 self::$_hasParent = TRUE;
88 self::$_parentEntityId = $checkParentExistsForThisId;
89 self::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($checkParentExistsForThisId, $entityTable);
90 }
91 else {
92 self::$_parentEntityId = self::$_entityId;
93 self::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId(self::$_entityId, $entityTable);
94 }
95 if (property_exists(self::$_scheduleReminderDetails, 'id')) {
96 self::$_scheduleReminderID = self::$_scheduleReminderDetails->id;
97 }
98 }
b1e18356
CW
99 CRM_Core_OptionValue::getValues(array('name' => $entityTable . '_repeat_exclude_dates_' . self::$_parentEntityId), $optionValue);
100 $excludeOptionValues = array();
101 if (!empty($optionValue)) {
102 foreach ($optionValue as $key => $val) {
9cc21021 103 $excludeOptionValues[$val['value']] = substr(CRM_Utils_Date::mysqlToIso($val['value']), 0, 10);
2aa397bc 104 }
b1e18356 105 self::$_excludeDateInfo = $excludeOptionValues;
2aa397bc 106 }
b1e18356
CW
107
108 // Assign variables
109 $entityType = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($entityTable));
110 $tpl = CRM_Core_Smarty::singleton();
111 $tpl->assign('recurringEntityType', ts($entityType));
112 $tpl->assign('currentEntityId', self::$_entityId);
113 $tpl->assign('entityTable', self::$_entityTable);
114 $tpl->assign('scheduleReminderId', self::$_scheduleReminderID);
115 $tpl->assign('hasParent', self::$_hasParent);
2aa397bc
TO
116 }
117
118 /**
119 * Set default values for the form. For edit/view mode
120 * the default values are retrieved from the database
121 *
122 *
16b10e64 123 * @return array
2aa397bc
TO
124 */
125 public static function setDefaultValues() {
d2a4c29b
CW
126 // Defaults for new entity
127 $defaults = array(
128 'repetition_frequency_unit' => 'week',
129 );
130
131 // Default for existing entity
2aa397bc
TO
132 if (self::$_scheduleReminderID) {
133 $defaults['repetition_frequency_unit'] = self::$_scheduleReminderDetails->repetition_frequency_unit;
134 $defaults['repetition_frequency_interval'] = self::$_scheduleReminderDetails->repetition_frequency_interval;
135 $defaults['start_action_condition'] = array_flip(explode(",", self::$_scheduleReminderDetails->start_action_condition));
22e263ad 136 foreach ($defaults['start_action_condition'] as $key => $val) {
2aa397bc
TO
137 $val = 1;
138 $defaults['start_action_condition'][$key] = $val;
139 }
140 $defaults['start_action_offset'] = self::$_scheduleReminderDetails->start_action_offset;
141 if (self::$_scheduleReminderDetails->start_action_offset) {
142 $defaults['ends'] = 1;
143 }
144 list($defaults['repeat_absolute_date']) = CRM_Utils_Date::setDateDefaults(self::$_scheduleReminderDetails->absolute_date);
145 if (self::$_scheduleReminderDetails->absolute_date) {
146 $defaults['ends'] = 2;
147 }
148 $defaults['limit_to'] = self::$_scheduleReminderDetails->limit_to;
149 if (self::$_scheduleReminderDetails->limit_to) {
150 $defaults['repeats_by'] = 1;
151 }
2aa397bc
TO
152 if (self::$_scheduleReminderDetails->entity_status) {
153 $explodeStartActionCondition = explode(" ", self::$_scheduleReminderDetails->entity_status);
154 $defaults['entity_status_1'] = $explodeStartActionCondition[0];
155 $defaults['entity_status_2'] = $explodeStartActionCondition[1];
156 }
157 if (self::$_scheduleReminderDetails->entity_status) {
158 $defaults['repeats_by'] = 2;
159 }
530dd28b
CW
160 if (self::$_excludeDateInfo) {
161 $defaults['exclude_date_list'] = implode(',', self::$_excludeDateInfo);
162 }
2aa397bc
TO
163 }
164 return $defaults;
165 }
166
2e2605fe
EM
167 /**
168 * Build form.
169 *
530dd28b 170 * @param CRM_Core_Form $form
2e2605fe 171 */
2aa397bc 172 public static function buildQuickForm(&$form) {
b1e18356 173 // For some reason this is using the following as keys rather than the standard numeric keys returned by CRM_Utils_Date
2aa397bc 174 $dayOfTheWeek = array(
b1e18356
CW
175 'sunday',
176 'monday',
177 'tuesday',
178 'wednesday',
179 'thursday',
180 'friday',
181 'saturday',
353ffa53 182 );
b1e18356 183 $dayOfTheWeek = array_combine($dayOfTheWeek, CRM_Utils_Date::getAbbrWeekdayNames());
8fe4b69f 184 $form->add('select', 'repetition_frequency_unit', ts('Repeats every'), CRM_Core_SelectValues::getRecurringFrequencyUnits(), FALSE, array('class' => 'required'));
2aa397bc 185 $numericOptions = CRM_Core_SelectValues::getNumericOptions(1, 30);
d2a4c29b 186 $form->add('select', 'repetition_frequency_interval', NULL, $numericOptions, FALSE, array('class' => 'required'));
2aa397bc 187 $form->addDateTime('repetition_start_date', ts('Repetition Start Date'), FALSE, array('formatType' => 'activityDateTime'));
22e263ad 188 foreach ($dayOfTheWeek as $key => $val) {
b1e18356 189 $startActionCondition[] = $form->createElement('checkbox', $key, NULL, $val);
2aa397bc
TO
190 }
191 $form->addGroup($startActionCondition, 'start_action_condition', ts('Repeats on'));
192 $roptionTypes = array(
353ffa53
TO
193 '1' => ts('day of the month'),
194 '2' => ts('day of the week'),
195 );
d2a4c29b
CW
196 $form->addRadio('repeats_by', ts("Repeats by"), $roptionTypes, array('required' => TRUE), NULL);
197 $form->add('select', 'limit_to', '', CRM_Core_SelectValues::getNumericOptions(1, 31));
2aa397bc 198 $dayOfTheWeekNo = array(
b1e18356
CW
199 'first' => ts('First'),
200 'second' => ts('Second'),
201 'third' => ts('Third'),
202 'fourth' => ts('Fourth'),
203 'last' => ts('Last'),
353ffa53 204 );
b1e18356
CW
205 $form->add('select', 'entity_status_1', '', $dayOfTheWeekNo);
206 $form->add('select', 'entity_status_2', '', $dayOfTheWeek);
2aa397bc 207 $eoptionTypes = array(
353ffa53
TO
208 '1' => ts('After'),
209 '2' => ts('On'),
210 );
d2a4c29b
CW
211 $form->addRadio('ends', ts("Ends"), $eoptionTypes, array('class' => 'required'), NULL);
212 $form->add('select', 'start_action_offset', NULL, CRM_Core_SelectValues::getNumericOptions(1, 30), FALSE);
2aa397bc
TO
213 $form->addFormRule(array('CRM_Core_Form_RecurringEntity', 'formRule'));
214 $form->addDate('repeat_absolute_date', ts('On'), FALSE, array('formatType' => 'mailing'));
530dd28b
CW
215 $form->add('text', 'exclude_date_list', ts('Exclude Dates'), array(
216 'class' => 'twenty',
353ffa53 217 ));
2aa397bc
TO
218 $form->addElement('hidden', 'allowRepeatConfigToSubmit', '', array('id' => 'allowRepeatConfigToSubmit'));
219 $form->addButtons(array(
220 array(
221 'type' => 'submit',
222 'name' => ts('Save'),
223 'isDefault' => TRUE,
224 ),
225 array(
226 'type' => 'cancel',
21dfd5f5 227 'name' => ts('Cancel'),
2aa397bc
TO
228 ),
229 )
230 );
8fe4b69f
CW
231 // For client-side pluralization
232 $form->assign('recurringFrequencyOptions', array(
233 'single' => CRM_Utils_Array::makeNonAssociative(CRM_Core_SelectValues::getRecurringFrequencyUnits()),
234 'plural' => CRM_Utils_Array::makeNonAssociative(CRM_Core_SelectValues::getRecurringFrequencyUnits(2)),
235 ));
2aa397bc
TO
236 }
237
238 /**
fe482240 239 * Global validation rules for the form.
2aa397bc 240 *
a1a2a83d
TO
241 * @param array $values
242 * Posted values of the form.
2aa397bc 243 *
a6c01b45
CW
244 * @return array
245 * list of errors to be posted back to the form
2aa397bc
TO
246 */
247 public static function formRule($values) {
248 $errors = array();
249 //Process this function only when you get this variable
250 if ($values['allowRepeatConfigToSubmit'] == 1) {
251 $dayOfTheWeek = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
252 //Repeats
b53cbfbc 253 if (empty($values['repetition_frequency_unit'])) {
2aa397bc
TO
254 $errors['repetition_frequency_unit'] = ts('This is a required field');
255 }
256 //Repeats every
b53cbfbc 257 if (empty($values['repetition_frequency_interval'])) {
2aa397bc
TO
258 $errors['repetition_frequency_interval'] = ts('This is a required field');
259 }
260 //Ends
b53cbfbc 261 if (!empty($values['ends'])) {
2aa397bc
TO
262 if ($values['ends'] == 1) {
263 if (empty($values['start_action_offset'])) {
264 $errors['start_action_offset'] = ts('This is a required field');
265 }
4c9b6178 266 elseif ($values['start_action_offset'] > 30) {
2aa397bc
TO
267 $errors['start_action_offset'] = ts('Occurrences should be less than or equal to 30');
268 }
269 }
270 if ($values['ends'] == 2) {
b53cbfbc 271 if (!empty($values['repeat_absolute_date'])) {
2aa397bc
TO
272 $entityStartDate = CRM_Utils_Date::processDate($values['repetition_start_date']);
273 $end = CRM_Utils_Date::processDate($values['repeat_absolute_date']);
274 if (($end < $entityStartDate) && ($end != 0)) {
275 $errors['repeat_absolute_date'] = ts('End date should be after current entity\'s start date');
276 }
277 }
278 else {
279 $errors['repeat_absolute_date'] = ts('This is a required field');
280 }
281 }
282 }
283 else {
284 $errors['ends'] = ts('This is a required field');
285 }
286
287 //Repeats BY
b53cbfbc 288 if (!empty($values['repeats_by'])) {
2aa397bc 289 if ($values['repeats_by'] == 1) {
b53cbfbc 290 if (!empty($values['limit_to'])) {
2aa397bc
TO
291 if ($values['limit_to'] < 1 && $values['limit_to'] > 31) {
292 $errors['limit_to'] = ts('Invalid day of the month');
293 }
294 }
295 else {
296 $errors['limit_to'] = ts('Invalid day of the month');
297 }
298 }
299 if ($values['repeats_by'] == 2) {
b53cbfbc 300 if (!empty($values['entity_status_1'])) {
5c97b65f 301 $dayOfTheWeekNo = array('first', 'second', 'third', 'fourth', 'last');
2aa397bc
TO
302 if (!in_array($values['entity_status_1'], $dayOfTheWeekNo)) {
303 $errors['entity_status_1'] = ts('Invalid option');
304 }
305 }
306 else {
307 $errors['entity_status_1'] = ts('Invalid option');
308 }
b53cbfbc 309 if (!empty($values['entity_status_2'])) {
2aa397bc
TO
310 if (!in_array($values['entity_status_2'], $dayOfTheWeek)) {
311 $errors['entity_status_2'] = ts('Invalid day name');
312 }
313 }
314 else {
315 $errors['entity_status_2'] = ts('Invalid day name');
316 }
317 }
318 }
319 }
320 return $errors;
321 }
322
323 /**
fe482240 324 * Process the form submission.
2aa397bc
TO
325 *
326 *
16b10e64 327 * @return void
2aa397bc
TO
328 */
329 public static function postProcess($params = array(), $type, $linkedEntities = array()) {
330 //Check entity_id not present in params take it from class variable
b53cbfbc 331 if (empty($params['entity_id'])) {
2aa397bc
TO
332 $params['entity_id'] = self::$_entityId;
333 }
334 //Process this function only when you get this variable
335 if ($params['allowRepeatConfigToSubmit'] == 1) {
336 if (CRM_Utils_Array::value('entity_table', $params) && CRM_Utils_Array::value('entity_id', $params) && $type) {
337 $params['used_for'] = $type;
b53cbfbc 338 if (empty($params['parent_entity_id'])) {
2aa397bc
TO
339 $params['parent_entity_id'] = self::$_parentEntityId;
340 }
b53cbfbc 341 if (!empty($params['schedule_reminder_id'])) {
2aa397bc
TO
342 $params['id'] = $params['schedule_reminder_id'];
343 }
344 else {
345 $params['id'] = self::$_scheduleReminderID;
346 }
347
348 //Save post params to the schedule reminder table
349 $recurobj = new CRM_Core_BAO_RecurringEntity();
350 $dbParams = $recurobj->mapFormValuesToDB($params);
351
352 //Delete repeat configuration and rebuild
b53cbfbc 353 if (!empty($params['id'])) {
2aa397bc
TO
354 CRM_Core_BAO_ActionSchedule::del($params['id']);
355 unset($params['id']);
356 }
357 $actionScheduleObj = CRM_Core_BAO_ActionSchedule::add($dbParams);
358
359 //exclude dates
360 $excludeDateList = array();
530dd28b 361 if (CRM_Utils_Array::value('exclude_date_list', $params) && CRM_Utils_Array::value('parent_entity_id', $params) && $actionScheduleObj->entity_value) {
2aa397bc 362 //Since we get comma separated values lets get them in array
530dd28b 363 $excludeDates = explode(",", $params['exclude_date_list']);
2aa397bc
TO
364
365 //Check if there exists any values for this option group
366 $optionGroupIdExists = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
353ffa53
TO
367 $type . '_repeat_exclude_dates_' . $params['parent_entity_id'],
368 'id',
369 'name'
370 );
2aa397bc
TO
371 if ($optionGroupIdExists) {
372 CRM_Core_BAO_OptionGroup::del($optionGroupIdExists);
373 }
6c552737
TO
374 $optionGroupParams = array(
375 'name' => $type . '_repeat_exclude_dates_' . $actionScheduleObj->entity_value,
376 'title' => $type . ' recursion',
377 'is_reserved' => 0,
378 'is_active' => 1,
379 );
2aa397bc
TO
380 $opGroup = CRM_Core_BAO_OptionGroup::add($optionGroupParams);
381 if ($opGroup->id) {
382 $oldWeight = 0;
383 $fieldValues = array('option_group_id' => $opGroup->id);
22e263ad 384 foreach ($excludeDates as $val) {
6c552737
TO
385 $optionGroupValue = array(
386 'option_group_id' => $opGroup->id,
387 'label' => CRM_Utils_Date::processDate($val),
388 'value' => CRM_Utils_Date::processDate($val),
389 'name' => $opGroup->name,
390 'description' => 'Used for recurring ' . $type,
391 'weight' => CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues),
392 'is_active' => 1,
393 );
2aa397bc 394 $excludeDateList[] = $optionGroupValue['value'];
bcbbb3a9 395 CRM_Core_BAO_OptionValue::create($optionGroupValue);
2aa397bc
TO
396 }
397 }
398 }
399
400 //Set type for API
2aa397bc
TO
401 $apiEntityType = explode("_", $type);
402 if (!empty($apiEntityType[1])) {
403 $apiType = $apiEntityType[1];
404 }
405 //Delete relations if any from recurring entity tables before inserting new relations for this entity id
406 if ($params['entity_id']) {
407 //If entity has any pre delete function, consider that first
408 if (CRM_Utils_Array::value('pre_delete_func', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]) &&
353ffa53
TO
409 CRM_Utils_Array::value('helper_class', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']])
410 ) {
2aa397bc
TO
411 call_user_func(array(
412 CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['helper_class'],
af9b09df 413 call_user_func_array(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['pre_delete_func'], array($params['entity_id'])),
353ffa53
TO
414 )
415 );
2aa397bc
TO
416 }
417 //Ready to execute delete on entities if it has delete function set
418 if (CRM_Utils_Array::value('delete_func', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]) &&
353ffa53
TO
419 CRM_Utils_Array::value('helper_class', CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']])
420 ) {
2aa397bc
TO
421 //Check if pre delete function has some ids to be deleted
422 if (!empty(CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted)) {
423 foreach (CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted as $eid) {
424 $result = civicrm_api3(
425 ucfirst(strtolower($apiType)),
426 CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'],
427 array(
428 'sequential' => 1,
429 'id' => $eid,
430 )
431 );
432 if ($result['error']) {
433 CRM_Core_Error::statusBounce('Error creating recurring list');
434 }
435 }
436 }
437 else {
438 $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE);
439 foreach ($getRelatedEntities as $key => $value) {
440 $result = civicrm_api3(
441 ucfirst(strtolower($apiType)),
442 CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'],
443 array(
444 'sequential' => 1,
445 'id' => $value['id'],
446 )
447 );
448 if ($result['error']) {
449 CRM_Core_Error::statusBounce('Error creating recurring list');
450 }
451 }
452 }
453 }
454
455 // find all entities from the recurring set. At this point we 'll get entities which were not deleted
456 // for e.g due to participants being present. We need to delete them from recurring tables anyway.
457 $pRepeatingEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table']);
22e263ad 458 foreach ($pRepeatingEntities as $val) {
2aa397bc
TO
459 CRM_Core_BAO_RecurringEntity::delEntity($val['id'], $val['table'], TRUE);
460 }
461 }
462
463 $recursion = new CRM_Core_BAO_RecurringEntity();
353ffa53
TO
464 $recursion->dateColumns = $params['dateColumns'];
465 $recursion->scheduleId = $actionScheduleObj->id;
2aa397bc
TO
466
467 if (!empty($excludeDateList)) {
468 $recursion->excludeDates = $excludeDateList;
469 $recursion->excludeDateRangeColumns = $params['excludeDateRangeColumns'];
470 }
b53cbfbc 471 if (!empty($params['intervalDateColumns'])) {
2aa397bc
TO
472 $recursion->intervalDateColumns = $params['intervalDateColumns'];
473 }
474 $recursion->entity_id = $params['entity_id'];
475 $recursion->entity_table = $params['entity_table'];
476 if (!empty($linkedEntities)) {
477 $recursion->linkedEntities = $linkedEntities;
478 }
479
b1e18356 480 $recursion->generate();
2aa397bc
TO
481
482 $status = ts('Repeat Configuration has been saved');
483 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
484 }
485 }
486 }
487
488 /**
489 * Return a descriptive name for the page, used in wizard header
490 *
491 * @return string
492 */
493 public function getTitle() {
494 return ts('Repeat Entity');
495 }
496
497}