5d7eeca773b08de2b16a32f2a9aa01dd77e74df9
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Repeat.php
1 <?php
2
3 /*
4 * To change this license header, choose License Headers in Project Properties.
5 * To change this template file, choose Tools | Templates
6 * and open the template in the editor.
7 */
8
9 /**
10 * Description of Repeat
11 *
12 * @author Priyanka
13 */
14 class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent {
15
16 /**
17 * Schedule Reminder Id
18 */
19 protected $_scheduleReminderId = NULL;
20
21 /**
22 * Schedule Reminder data
23 */
24 protected $_scheduleReminderDetails = array();
25
26 /**
27 * Parent Event ID
28 */
29 protected $_parentEventId = NULL;
30
31 /**
32 * Parent Event Start Date
33 */
34 protected $_parentEventStartDate = NULL;
35
36 /**
37 * Parent Event End Date
38 */
39 protected $_parentEventEndDate = NULL;
40
41 /**
42 * Exclude date information
43 */
44 public $_excludeDateInfo = array();
45
46 protected $_pager = NULL;
47
48
49
50 function preProcess() {
51 parent::preProcess();
52 $this->assign('currentEventId', $this->_id);
53
54 $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
55 $checkParentExistsForThisId;
56 //If this ID has parent, send parent id
57 if($checkParentExistsForThisId){
58 $this->_scheduleReminderDetails = self::getReminderDetailsByEventId($checkParentExistsForThisId, 'event');
59 $this->_parentEventId = $checkParentExistsForThisId;
60
61 /**
62 * Get connected event information list
63 */
64 //Get all connected event ids
65 //$allEventIds = CRM_Core_Form_RecurringEntity::getAllConnectedEvents($checkParentExistsForThisId);
66 $allEventIdsArray = CRM_Core_BAo_RecurringEntity::getEntitiesForParent($checkParentExistsForThisId, 'civicrm_event');
67 $allEventIds = array();
68 if(!empty($allEventIdsArray)){
69 foreach($allEventIdsArray as $key => $val){
70 $allEventIds[] = $val['id'];
71 }
72 if(!empty($allEventIds)){
73 $params = array();
74 $query = "
75 SELECT *
76 FROM civicrm_event
77 WHERE id IN (".implode(",", $allEventIds).")
78 ORDER BY start_date asc
79 ";
80
81 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Event_DAO_Event');
82 $permissions = CRM_Event_BAO_Event::checkPermission();
83 while($dao->fetch()){
84 if(in_array($dao->id, $permissions[CRM_Core_Permission::VIEW])){
85 $manageEvent[$dao->id] = array();
86 CRM_Core_DAO::storeValues($dao, $manageEvent[$dao->id]);
87 }
88 }
89 }
90 $this->assign('rows', $manageEvent);
91 }
92 }else{
93 //ELse send this id as parent
94 $this->_scheduleReminderDetails = self::getReminderDetailsByEventId($this->_id, 'event');
95 $this->_parentEventId = $this->_id;
96 }
97 //Assign this to hide summary
98 $this->assign('scheduleReminderId', $this->_scheduleReminderDetails->id);
99
100 $parentEventParams = array('id' => $this->_id);
101 $parentEventValues = array();
102 $parentEventReturnProperties = array('start_date', 'end_date');
103 $parentEventAttributes = CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', $parentEventParams, $parentEventValues, $parentEventReturnProperties);
104 $this->_parentEventStartDate = $parentEventAttributes->start_date;
105 $this->_parentEventEndDate = $parentEventAttributes->end_date;
106
107 //Get option exclude date information
108 //$groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'event_repeat_exclude_dates_'.$this->_parentEventId, 'id', 'name');
109 CRM_Core_OptionValue::getValues(array('name' => 'event_repeat_exclude_dates_'.$this->_parentEventId), $optionValue);
110 $excludeOptionValues = array();
111 if(!empty($optionValue)){
112 foreach($optionValue as $key => $val){
113 $excludeOptionValues[$val['value']] = date('m/d/Y', strtotime($val['value']));
114 }
115 $this->_excludeDateInfo = $excludeOptionValues;
116 }
117 }
118
119 /**
120 * This function sets the default values for the form. For edit/view mode
121 * the default values are retrieved from the database
122 *
123 * @access public
124 *
125 * @return None
126 */
127 function setDefaultValues() {
128 $defaults = array();
129
130 //Set Schedule Reminder Id
131 $this->_scheduleReminderId = $this->_scheduleReminderDetails->id;
132 //Always pass current event's start date by default
133 $currentEventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'start_date', 'id');
134 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($currentEventStartDate, 'activityDateTime');
135
136 // Check if there is id for this event in Reminder table
137 if($this->_scheduleReminderId){
138 $defaults['repetition_frequency_unit'] = $this->_scheduleReminderDetails->repetition_frequency_unit;
139 $defaults['repetition_frequency_interval'] = $this->_scheduleReminderDetails->repetition_frequency_interval;
140 $defaults['start_action_condition'] = array_flip(explode(",",$this->_scheduleReminderDetails->start_action_condition));
141 foreach($defaults['start_action_condition'] as $key => $val){
142 $val = 1;
143 $defaults['start_action_condition'][$key] = $val;
144 }
145 list($defaults['repeat_event_start_date'], $defaults['repeat_event_start_date_time']) = CRM_Utils_Date::setDateDefaults($this->_parentEventStartDate, 'activityDateTime');
146 $defaults['start_action_offset'] = $this->_scheduleReminderDetails->start_action_offset;
147 if($this->_scheduleReminderDetails->start_action_offset){
148 $defaults['ends'] = 1;
149 }
150 list($defaults['repeat_absolute_date']) = CRM_Utils_Date::setDateDefaults($this->_scheduleReminderDetails->absolute_date);
151 if($this->_scheduleReminderDetails->absolute_date){
152 $defaults['ends'] = 2;
153 }
154 $defaults['limit_to'] = $this->_scheduleReminderDetails->limit_to;
155 if($this->_scheduleReminderDetails->limit_to){
156 $defaults['repeats_by'] = 1;
157 }
158 $explodeStartActionCondition = array();
159 $explodeStartActionCondition = explode(" ", $this->_scheduleReminderDetails->entity_status);
160 $defaults['entity_status_1'] = $explodeStartActionCondition[0];
161 $defaults['entity_status_2'] = $explodeStartActionCondition[1];
162 if($this->_scheduleReminderDetails->entity_status){
163 $defaults['repeats_by'] = 2;
164 }
165 $defaults['exclude_date_list'] = array('a', 'b');
166 }
167 return $defaults;
168 }
169
170 public function buildQuickForm() {
171 CRM_Core_Form_RecurringEntity::buildQuickForm($this);
172 }
173
174 public function postProcess() {
175 if($this->_id){
176 $params = $this->controller->exportValues($this->_name);
177 $params['event_id'] = $this->_id;
178 $params['parent_event_id'] = $this->_parentEventId;
179 $params['parent_event_start_date'] = $this->_parentEventStartDate;
180 $params['parent_event_end_date'] = $this->_parentEventEndDate;
181 //Unset event id
182 unset($params['id']);
183
184 //Set Schedule Reminder id
185 $params['id'] = $this->_scheduleReminderId;
186 $url = 'civicrm/event/manage/repeat';
187 $urlParams = "action=update&reset=1&id={$this->_id}";
188
189 CRM_Core_Form_RecurringEntity::postProcess($params, 'event');
190 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
191 }else{
192 CRM_Core_Error::fatal("Could not find Event ID");
193 }
194 }
195
196 /**
197 * This function gets the number of participant count for the list of related event ids
198 *
199 * @param array $listOfRelatedEntities list of related event ids
200 *
201 * @access public
202 * @static
203 *
204 * @return array
205 */
206 static public function getParticipantCountforEvent($listOfRelatedEntities = array()){
207 if(!empty($listOfRelatedEntities)){
208 $implodeRelatedEntities = implode(',', array_map(function($entity){
209 return $entity['id'];
210 }, $listOfRelatedEntities));
211 if($implodeRelatedEntities){
212 $query = "SELECT p.event_id as event_id,
213 concat_ws(' ', e.title, concat_ws(' - ', DATE_FORMAT(e.start_date, '%b %d %Y %h:%i %p'), DATE_FORMAT(e.end_date, '%b %d %Y %h:%i %p'))) as event_data,
214 count(p.id) as participant_count
215 FROM civicrm_participant p, civicrm_event e
216 WHERE p.event_id = e.id AND p.event_id IN ({$implodeRelatedEntities})
217 GROUP BY p.event_id";
218 $dao = CRM_Core_DAO::executeQuery($query);
219 $participantDetails = array();
220 while($dao->fetch()) {
221 $participantDetails['countByID'][$dao->event_id] = $dao->participant_count;
222 $participantDetails['countByName'][$dao->event_id][$dao->event_data] = $dao->participant_count;
223 }
224 }
225 }
226 return $participantDetails;
227 }
228
229 /**
230 * This function gets all columns from civicrm_action_schedule on the basis of event id
231 *
232 * @param int $eventId Event ID
233 * @param string $used_for Specifies for which entity type it's used for
234 *
235 * @access public
236 * @static
237 *
238 * @return object
239 */
240 static public function getReminderDetailsByEventId($eventId, $used_for){
241 if($eventId){
242 $query = "
243 SELECT *
244 FROM civicrm_action_schedule
245 WHERE entity_value = %1";
246 if($used_for){
247 $query .= " AND used_for = %2";
248 }
249 $params = array(
250 1 => array($eventId, 'Integer'),
251 2 => array($used_for, 'String')
252 );
253 $dao = CRM_Core_DAO::executeQuery($query, $params);
254 $dao->fetch();
255 }
256 return $dao;
257 }
258
259 }