62933949 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 4.4 | |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
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-2013 |
33 | * $Id$ |
34 | * |
35 | */ |
36 | /** |
78fe87e6 |
37 | * This class generates form components for processing Entity |
62933949 |
38 | * |
39 | */ |
40 | class CRM_Core_Form_RecurringEntity { |
41 | /** |
42 | * Current entity id |
43 | */ |
44 | protected static $_entityId = NULL; |
45 | |
78fe87e6 |
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 | |
a7ea18bf |
66 | /** |
8cec51b0 |
67 | * Entity Table |
68 | */ |
69 | public static $_entityTable; |
70 | |
71 | /** |
72 | * Entity Type |
a7ea18bf |
73 | */ |
74 | public static $_entityType; |
75 | |
6266861e |
76 | /** |
77 | * Checks current entityID has parent |
78 | */ |
79 | public static $_hasParent = FALSE; |
80 | |
8cec51b0 |
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; |
91 | } |
92 | if ($checkParentExistsForThisId) { |
93 | self::$_hasParent = TRUE; |
94 | self::$_parentEntityId = $checkParentExistsForThisId; |
95 | self::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($checkParentExistsForThisId, self::$_entityType); |
96 | } |
97 | else { |
98 | self::$_parentEntityId = self::$_entityId; |
99 | self::$_scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId(self::$_entityId, self::$_entityType); |
100 | } |
101 | self::$_scheduleReminderID = self::$_scheduleReminderDetails->id; |
102 | } |
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'])); |
109 | } |
110 | self::$_excludeDateInfo = $excludeOptionValues; |
78fe87e6 |
111 | } |
6266861e |
112 | } |
78fe87e6 |
113 | } |
114 | |
115 | /** |
116 | * This function sets the default values for the form. For edit/view mode |
117 | * the default values are retrieved from the database |
118 | * |
119 | * @access public |
120 | * |
121 | * @return None |
122 | */ |
123 | static function setDefaultValues() { |
124 | $defaults = array(); |
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) { |
130 | $val = 1; |
131 | $defaults['start_action_condition'][$key] = $val; |
132 | } |
133 | $defaults['start_action_offset'] = self::$_scheduleReminderDetails->start_action_offset; |
134 | if (self::$_scheduleReminderDetails->start_action_offset) { |
135 | $defaults['ends'] = 1; |
136 | } |
137 | list($defaults['repeat_absolute_date']) = CRM_Utils_Date::setDateDefaults(self::$_scheduleReminderDetails->absolute_date); |
138 | if (self::$_scheduleReminderDetails->absolute_date) { |
139 | $defaults['ends'] = 2; |
140 | } |
141 | $defaults['limit_to'] = self::$_scheduleReminderDetails->limit_to; |
142 | if (self::$_scheduleReminderDetails->limit_to) { |
143 | $defaults['repeats_by'] = 1; |
144 | } |
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]; |
150 | } |
151 | if (self::$_scheduleReminderDetails->entity_status) { |
152 | $defaults['repeats_by'] = 2; |
153 | } |
154 | } |
155 | return $defaults; |
156 | } |
157 | |
62933949 |
158 | static function buildQuickForm(&$form) { |
62933949 |
159 | $form->assign('currentEntityId', self::$_entityId); |
8cec51b0 |
160 | $form->assign('entityTable', self::$_entityTable); |
a83e7558 |
161 | $form->assign('scheduleReminderId', self::$_scheduleReminderID); |
6266861e |
162 | $form->assign('hasParent', self::$_hasParent); |
62933949 |
163 | |
164 | $form->_freqUnits = array('hour' => 'hour') + CRM_Core_OptionGroup::values('recur_frequency_units'); |
165 | foreach ($form->_freqUnits as $val => $label) { |
a50a97b8 |
166 | if ($label == "day") { |
62933949 |
167 | $label = "dai"; |
168 | } |
169 | $freqUnitsDisplay[$val] = ts('%1ly', array(1 => $label)); |
170 | } |
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', |
178 | 'sunday' => 'Sunday' |
179 | ); |
180 | $form->add('select', 'repetition_frequency_unit', ts('Repeats:'), $freqUnitsDisplay, TRUE); |
181 | $numericOptions = CRM_Core_SelectValues::getNumericOptions(1, 30); |
b1d1479a |
182 | $form->add('select', 'repetition_frequency_interval', ts('Repeats every:'), $numericOptions, TRUE, array('style' => 'width:55px;')); |
66f66040 |
183 | $form->addDateTime('repetition_start_date', ts('Repetition Start Date'), FALSE, array('formatType' => 'activityDateTime')); |
a50a97b8 |
184 | foreach($dayOfTheWeek as $key => $val) { |
62933949 |
185 | $startActionCondition[] = $form->createElement('checkbox', $key, NULL, substr($val." ", 0, 3)); |
186 | } |
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'), |
190 | ); |
191 | $form->addRadio('repeats_by', ts("Repeats By:"), $roptionTypes, array(), NULL); |
192 | $getMonths = CRM_Core_SelectValues::getNumericOptions(1, 31); |
b1d1479a |
193 | $form->add('select', 'limit_to', '', $getMonths, FALSE, array('style' => 'width:55px;')); |
62933949 |
194 | $dayOfTheWeekNo = array('first' => 'First', |
195 | 'second'=> 'Second', |
196 | 'third' => 'Third', |
197 | 'fourth'=> 'Fourth', |
198 | 'last' => 'Last' |
199 | ); |
a1a821bc |
200 | $form->add('select', 'entity_status_1', ts(''), $dayOfTheWeekNo); |
201 | $form->add('select', 'entity_status_2', ts(''), $dayOfTheWeek); |
62933949 |
202 | $eoptionTypes = array('1' => ts('After'), |
203 | '2' => ts('On'), |
204 | ); |
205 | $form->addRadio('ends', ts("Ends:"), $eoptionTypes, array(), NULL, TRUE); |
431b0ca8 |
206 | $form->add('text', 'start_action_offset', ts(''), array('size' => 3, 'maxlength' => 2)); |
62933949 |
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); |
30051f5b |
210 | $select = $form->add('select', 'exclude_date_list', ts(''), $form->_excludeDateInfo, FALSE, array('style' => 'width:150px;', 'size' => 4)); |
62933949 |
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( |
217 | array( |
218 | 'type' => 'submit', |
219 | 'name' => ts('Save'), |
220 | 'isDefault' => TRUE, |
221 | ), |
222 | array( |
223 | 'type' => 'cancel', |
224 | 'name' => ts('Cancel') |
225 | ), |
226 | ) |
227 | ); |
228 | } |
229 | |
230 | /** |
231 | * global validation rules for the form |
232 | * |
233 | * @param array $fields posted values of the form |
234 | * |
235 | * @return array list of errors to be posted back to the form |
236 | * @static |
237 | * @access public |
238 | */ |
239 | static function formRule($values) { |
240 | $errors = array(); |
241 | $dayOfTheWeek = array(monday,tuesday,wednesday,thursday,friday,saturday,sunday); |
62933949 |
242 | |
243 | //Repeats |
a50a97b8 |
244 | if (!CRM_Utils_Array::value('repetition_frequency_unit', $values)) { |
62933949 |
245 | $errors['repetition_frequency_unit'] = ts('This is a required field'); |
246 | } |
247 | //Repeats every |
a50a97b8 |
248 | if (!CRM_Utils_Array::value('repetition_frequency_interval', $values)) { |
62933949 |
249 | $errors['repetition_frequency_interval'] = ts('This is a required field'); |
250 | } |
251 | //Ends |
a50a97b8 |
252 | if (CRM_Utils_Array::value('ends', $values)) { |
253 | if ($values['ends'] == 1) { |
62933949 |
254 | if ($values['start_action_offset'] == "") { |
255 | $errors['start_action_offset'] = ts('This is a required field'); |
a50a97b8 |
256 | } |
257 | else if ($values['start_action_offset'] > 30) { |
62933949 |
258 | $errors['start_action_offset'] = ts('Occurrences should be less than or equal to 30'); |
259 | } |
260 | } |
a50a97b8 |
261 | if ($values['ends'] == 2) { |
62933949 |
262 | if ($values['repeat_absolute_date'] != "") { |
78fe87e6 |
263 | $entityStartDate = CRM_Utils_Date::processDate($values['repetition_start_date']); |
62933949 |
264 | $end = CRM_Utils_Date::processDate($values['repeat_absolute_date']); |
78fe87e6 |
265 | if (($end < $entityStartDate) && ($end != 0)) { |
fbdee7b7 |
266 | $errors['repeat_absolute_date'] = ts('End date should be after current entity\'s start date'); |
62933949 |
267 | } |
a50a97b8 |
268 | } |
269 | else { |
62933949 |
270 | $errors['repeat_absolute_date'] = ts('This is a required field'); |
271 | } |
272 | } |
a50a97b8 |
273 | } |
274 | else { |
62933949 |
275 | $errors['ends'] = ts('This is a required field'); |
276 | } |
277 | |
278 | //Repeats BY |
a50a97b8 |
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) { |
62933949 |
283 | $errors['limit_to'] = ts('Invalid day of the month'); |
284 | } |
a50a97b8 |
285 | } |
286 | else { |
62933949 |
287 | $errors['limit_to'] = ts('Invalid day of the month'); |
288 | } |
289 | } |
a50a97b8 |
290 | if ($values['repeats_by'] == 2) { |
291 | if ($values['entity_status_1'] != "" ) { |
62933949 |
292 | $dayOfTheWeekNo = array(first, second, third, fourth, last); |
a50a97b8 |
293 | if (!in_array($values['entity_status_1'], $dayOfTheWeekNo)) { |
a1a821bc |
294 | $errors['entity_status_1'] = ts('Invalid option'); |
62933949 |
295 | } |
a50a97b8 |
296 | } |
297 | else { |
a1a821bc |
298 | $errors['entity_status_1'] = ts('Invalid option'); |
62933949 |
299 | } |
a50a97b8 |
300 | if ($values['entity_status_2'] != "" ) { |
301 | if (!in_array($values['entity_status_2'], $dayOfTheWeek)) { |
a1a821bc |
302 | $errors['entity_status_2'] = ts('Invalid day name'); |
62933949 |
303 | } |
a50a97b8 |
304 | } |
305 | else { |
a1a821bc |
306 | $errors['entity_status_2'] = ts('Invalid day name'); |
62933949 |
307 | } |
308 | } |
309 | } |
310 | return $errors; |
311 | } |
312 | |
313 | /** |
314 | * Function to process the form |
315 | * |
316 | * @access public |
317 | * |
318 | * @return None |
319 | */ |
78fe87e6 |
320 | static function postProcess($params = array(), $type, $linkedEntities = array()) { |
321 | $params['entity_id'] = self::$_entityId; |
5165b19c |
322 | if (CRM_Utils_Array::value('entity_table', $params) && CRM_Utils_Array::value('entity_id', $params) && $type) { |
62933949 |
323 | $params['used_for'] = $type; |
78fe87e6 |
324 | $params['parent_entity_id'] = self::$_parentEntityId; |
325 | $params['id'] = self::$_scheduleReminderID; |
1b1bde74 |
326 | |
78fe87e6 |
327 | //Save post params to the schedule reminder table |
328 | $dbParams = CRM_Core_BAO_RecurringEntity::mapFormValuesToDB($params); |
62933949 |
329 | |
78fe87e6 |
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']); |
62933949 |
334 | } |
78fe87e6 |
335 | $actionScheduleObj = CRM_Core_BAO_ActionSchedule::add($dbParams); |
336 | |
337 | //exclude dates |
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']); |
343 | |
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'], |
347 | 'id', |
348 | 'name' |
62933949 |
349 | ); |
78fe87e6 |
350 | if ($optionGroupIdExists) { |
351 | CRM_Core_BAO_OptionGroup::del($optionGroupIdExists); |
352 | } |
353 | $optionGroupParams = |
354 | array( |
355 | 'name' => $type.'_repeat_exclude_dates_'.$params['parent_entity_id'], |
356 | 'title' => $type.' recursion', |
357 | 'is_reserved' => 0, |
358 | 'is_active' => 1 |
359 | ); |
360 | $opGroup = CRM_Core_BAO_OptionGroup::add($optionGroupParams); |
361 | if ($opGroup->id) { |
362 | $oldWeight= 0; |
363 | $fieldValues = array('option_group_id' => $opGroup->id); |
364 | foreach($exclude_date_list as $val) { |
365 | $optionGroupValue = |
366 | array( |
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), |
373 | 'is_active' => 1 |
374 | ); |
375 | $excludeDateList[] = $optionGroupValue['value']; |
376 | CRM_Core_BAO_OptionValue::add($optionGroupValue); |
377 | } |
62933949 |
378 | } |
379 | } |
dfbe9b91 |
380 | |
78fe87e6 |
381 | //Delete relations if any from recurring entity tables before inserting new relations for this entity id |
382 | if ($params['entity_id']) { |
5165b19c |
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']))) |
389 | ); |
390 | } |
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) { |
defe2873 |
397 | $result = civicrm_api3(ucfirst(strtolower($type)), CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'], array( |
398 | 'sequential' => 1, |
399 | 'id' => $value, |
400 | )); |
401 | if ($result['error']) { |
402 | CRM_Core_Error::statusBounce('Error creating recurring list'); |
403 | } |
5165b19c |
404 | } |
405 | } |
406 | else { |
407 | $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE); |
408 | foreach ($getRelatedEntities as $key => $value) { |
defe2873 |
409 | $result = civicrm_api3(ucfirst(strtolower($type)), CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'], array( |
410 | 'sequential' => 1, |
411 | 'id' => $value['id'], |
412 | )); |
413 | if ($result['error']) { |
414 | CRM_Core_Error::statusBounce('Error creating recurring list'); |
415 | } |
5165b19c |
416 | } |
78fe87e6 |
417 | } |
5d4bf2ff |
418 | } |
5165b19c |
419 | CRM_Core_BAO_RecurringEntity::delEntityRelations($params['entity_id'], $params['entity_table']); |
5d4bf2ff |
420 | } |
d10273dc |
421 | |
78fe87e6 |
422 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
fbdee7b7 |
423 | $recursion->dateColumns = $params['dateColumns']; |
78fe87e6 |
424 | $recursion->scheduleId = $actionScheduleObj->id; |
dfbe9b91 |
425 | |
78fe87e6 |
426 | if (!empty($excludeDateList)) { |
427 | $recursion->excludeDates = $excludeDateList; |
fbdee7b7 |
428 | $recursion->excludeDateRangeColumns = $params['excludeDateRangeColumns']; |
78fe87e6 |
429 | } |
fbdee7b7 |
430 | $recursion->intervalDateColumns = $params['intervalDateColumns']; |
78fe87e6 |
431 | $recursion->entity_id = $params['entity_id']; |
5165b19c |
432 | $recursion->entity_table = $params['entity_table']; |
78fe87e6 |
433 | if (!empty($linkedEntities)) { |
434 | $recursion->linkedEntities = $linkedEntities; |
435 | } |
dc07e5a7 |
436 | |
78fe87e6 |
437 | $recurResult = $recursion->generate(); |
dfbe9b91 |
438 | |
78fe87e6 |
439 | $status = ts('Repeat Configuration has been saved'); |
440 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); |
441 | } |
62933949 |
442 | } |
443 | //end of function |
444 | |
445 | /** |
446 | * Return a descriptive name for the page, used in wizard header |
447 | * |
448 | * @return string |
449 | * @access public |
450 | */ |
451 | public function getTitle() { |
fbdee7b7 |
452 | return ts('Repeat Entity'); |
62933949 |
453 | } |
2eeee284 |
454 | |
62933949 |
455 | } |