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