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