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