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