3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class generates form components for processing Event
40 class CRM_Core_Form_RecurringEntity
{
44 protected static $_entityId = NULL;
46 static function buildQuickForm(&$form) {
47 //$attributes_schedule = CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionMapping');
48 self
::$_entityId = CRM_Utils_Array
::value('id', $_GET);
49 $form->assign('currentEntityId', self
::$_entityId);
51 $form->_freqUnits
= array('hour' => 'hour') + CRM_Core_OptionGroup
::values('recur_frequency_units');
52 foreach ($form->_freqUnits
as $val => $label) {
56 $freqUnitsDisplay[$val] = ts('%1ly', array(1 => $label));
58 // echo "<pre>";print_r($freqUnitsDisplay);
59 $dayOfTheWeek = array('monday' => 'Monday',
60 'tuesday' => 'Tuesday',
61 'wednesday' => 'Wednesday',
62 'thursday' => 'Thursday',
64 'saturday' => 'Saturday',
67 $form->add('select', 'repetition_frequency_unit', ts('Repeats:'), $freqUnitsDisplay, TRUE);
68 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(1, 30);
69 $form->add('select', 'repetition_frequency_interval', ts('Repeats every:'), $numericOptions, TRUE, array('style' => 'width:49px;'));
70 foreach($dayOfTheWeek as $key => $val){
71 $startActionCondition[] = $form->createElement('checkbox', $key, NULL, substr($val." ", 0, 3));
73 $form->addGroup($startActionCondition, 'start_action_condition', ts('Repeats on'));
74 $roptionTypes = array('1' => ts('day of the month'),
75 '2' => ts('day of the week'),
77 $form->addRadio('repeats_by', ts("Repeats By:"), $roptionTypes, array(), NULL);
78 $getMonths = CRM_Core_SelectValues
::getNumericOptions(1, 31);
79 $form->add('select', 'limit_to', '', $getMonths, FALSE, array('style' => 'width:49px;'));
80 $dayOfTheWeekNo = array('first' => 'First',
86 $form->add('select', 'start_action_date_1', ts(''), $dayOfTheWeekNo);
87 $form->add('select', 'start_action_date_2', ts(''), $dayOfTheWeek);
88 $eoptionTypes = array('1' => ts('After'),
91 $form->addRadio('ends', ts("Ends:"), $eoptionTypes, array(), NULL, TRUE);
92 $form->add('text', 'start_action_offset', ts(''), array('maxlength' => 2));
93 $form->addFormRule(array('CRM_Core_Form_RecurringEntity', 'formRule'));
94 $form->addDate('repeat_absolute_date', ts('On'), FALSE, array('formatType' => 'mailing'));
95 $form->addDate('exclude_date', ts('Exclude Date(s)'), FALSE);
96 $select = $form->add('select', 'exclude_date_list', ts(''), $form->_excludeDateInfo
, FALSE, array('style' => 'width:150px;', 'size' => 4));
97 $select->setMultiple(TRUE);
98 $form->addElement('button','add_to_exclude_list','>>','onClick="addToExcludeList(document.getElementById(\'exclude_date\').value);"');
99 $form->addElement('button','remove_from_exclude_list', '<<', 'onClick="removeFromExcludeList(\'exclude_date_list\')"');
100 $form->addElement('hidden', 'isChangeInRepeatConfiguration', '', array('id' => 'isChangeInRepeatConfiguration'));
101 $form->addElement('hidden', 'copyExcludeDates', '', array('id' => 'copyExcludeDates'));
102 $form->addButtons(array(
105 'name' => ts('Save'),
110 'name' => ts('Cancel')
117 * global validation rules for the form
119 * @param array $fields posted values of the form
121 * @return array list of errors to be posted back to the form
125 static function formRule($values) {
127 $dayOfTheWeek = array(monday
,tuesday
,wednesday
,thursday
,friday
,saturday
,sunday
);
130 if(!CRM_Utils_Array
::value('repetition_frequency_unit', $values)){
131 $errors['repetition_frequency_unit'] = ts('This is a required field');
134 if(!CRM_Utils_Array
::value('repetition_frequency_interval', $values)){
135 $errors['repetition_frequency_interval'] = ts('This is a required field');
138 if(CRM_Utils_Array
::value('ends', $values)){
139 if($values['ends'] == 1){
140 if ($values['start_action_offset'] == "") {
141 $errors['start_action_offset'] = ts('This is a required field');
142 }else if($values['start_action_offset'] > 30){
143 $errors['start_action_offset'] = ts('Occurrences should be less than or equal to 30');
146 if($values['ends'] == 2){
147 if ($values['repeat_absolute_date'] != "") {
148 $today = date("Y-m-d H:i:s");
149 $today = CRM_Utils_Date
::processDate($today);
150 $end = CRM_Utils_Date
::processDate($values['repeat_absolute_date']);
151 if (($end <= $today) && ($end != 0)) {
152 $errors['repeat_absolute_date'] = ts('End date should be after today\'s date');
155 $errors['repeat_absolute_date'] = ts('This is a required field');
159 $errors['ends'] = ts('This is a required field');
163 if(CRM_Utils_Array
::value('repeats_by', $values)){
164 if($values['repeats_by'] == 1){
165 if($values['limit_to'] != ""){
166 if($values['limit_to'] < 1 && $values['limit_to'] > 31){
167 $errors['limit_to'] = ts('Invalid day of the month');
170 $errors['limit_to'] = ts('Invalid day of the month');
173 if($values['repeats_by'] == 2){
174 if($values['start_action_date_1'] != "" ) {
175 $dayOfTheWeekNo = array(first
, second
, third
, fourth
, last
);
176 if(!in_array($values['start_action_date_1'], $dayOfTheWeekNo)){
177 $errors['start_action_date_1'] = ts('Invalid option');
180 $errors['start_action_date_1'] = ts('Invalid option');
182 if($values['start_action_date_2'] != "" ) {
183 if(!in_array($values['start_action_date_2'], $dayOfTheWeek)){
184 $errors['start_action_date_2'] = ts('Invalid day name');
187 $errors['start_action_date_2'] = ts('Invalid day name');
195 * Function to process the form
201 static function postProcess($params=array(), $type) {
203 $params['used_for'] = $type;
206 if(CRM_Utils_Array
::value('id', $params)){
207 CRM_Core_BAO_ActionSchedule
::del($params['id']);
208 unset($params['id']);
210 //Save post params to the schedule reminder table
211 $dbParams = CRM_Core_BAO_RecurringEntity
::mapFormValuesToDB($params);
212 $actionScheduleObj = CRM_Core_BAO_ActionSchedule
::add($dbParams);
214 //Build Recursion Object
215 if($actionScheduleObj->id
){
216 $recursionObject = CRM_Core_BAO_RecurringEntity
::getRecursionFromReminder($actionScheduleObj->id
);
220 //TO DO - Exclude date functionality
221 if(CRM_Utils_Array
::value('copyExcludeDates', $params) && CRM_Utils_Array
::value('parent_event_id', $params)){
222 //Since we get comma separated values lets get them in array
223 $exclude_date_list = array();
224 $exclude_date_list = explode(",", $params['copyExcludeDates']);
226 //Check if there exists any values for this option group
227 $optionGroupIdExists = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
228 'event_repeat_exclude_dates_'.$params['parent_event_id'],
232 if($optionGroupIdExists){
233 CRM_Core_BAO_OptionGroup
::del($optionGroupIdExists);
237 'name' => 'event_repeat_exclude_dates_'.$params['parent_event_id'],
238 'title' => 'Event Recursion',
242 $opGroup = CRM_Core_BAO_OptionGroup
::add($optionGroupParams);
245 $fieldValues = array('option_group_id' => $opGroup->id
);
246 foreach($exclude_date_list as $val){
249 'option_group_id' => $opGroup->id
,
250 'label' => CRM_Utils_Date
::processDate($val),
251 'value' => CRM_Utils_Date
::processDate($val),
252 'name' => $opGroup->name
,
253 'description' => 'Used for event recursion',
254 'weight' => CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues),
257 CRM_Core_BAO_OptionValue
::add($optionGroupValue);
262 //Delete relations if any from recurring entity tables before inserting new relations for this entity id
263 if($params['parent_event_id']){
264 CRM_Core_BAO_RecurringEntity
::delEntityRelations($params['parent_event_id'], 'civicrm_event');
266 //Give call to create recursions
267 $recurResult = CRM_Core_BAO_RecurringEntity
::generateRecursions($recursionObject, $params);
268 if(!empty($recurResult) && $params['parent_event_id']){
269 CRM_Core_BAO_RecurringEntity
::addEntityThroughRecursion($recurResult, $params['parent_event_id']);
271 $status = ts('Repeat Configuration has been saved');
272 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
277 * Return a descriptive name for the page, used in wizard header
282 public function getTitle() {
283 return ts('Repeat Event');