Commit | Line | Data |
---|---|---|
0479b4c8 | 1 | <?php |
0479b4c8 TO |
2 | /* |
3 | * To change this license header, choose License Headers in Project Properties. | |
4 | * To change this template file, choose Tools | Templates | |
5 | * and open the template in the editor. | |
6 | */ | |
7 | ||
8 | /** | |
9 | * Description of Repeat | |
10 | * | |
11 | * @author Priyanka | |
12 | */ | |
13 | class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent { | |
14 | ||
15 | /** | |
f9e31d7f | 16 | * Parent Event Start Date. |
0479b4c8 TO |
17 | */ |
18 | protected $_parentEventStartDate = NULL; | |
19 | ||
20 | /** | |
f9e31d7f | 21 | * Parent Event End Date. |
0479b4c8 TO |
22 | */ |
23 | protected $_parentEventEndDate = NULL; | |
24 | ||
25 | ||
26 | public function preProcess() { | |
27 | parent::preProcess(); | |
0479b4c8 TO |
28 | $this->assign('currentEventId', $this->_id); |
29 | ||
30 | $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event'); | |
31 | //If this ID has parent, send parent id | |
32 | if ($checkParentExistsForThisId) { | |
33 | /** | |
353ffa53 TO |
34 | * Get connected event information list |
35 | */ | |
0479b4c8 TO |
36 | //Get all connected event ids |
37 | $allEventIdsArray = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($checkParentExistsForThisId, 'civicrm_event'); | |
38 | $allEventIds = array(); | |
39 | if (!empty($allEventIdsArray)) { | |
22e263ad | 40 | foreach ($allEventIdsArray as $key => $val) { |
0479b4c8 TO |
41 | $allEventIds[] = $val['id']; |
42 | } | |
43 | if (!empty($allEventIds)) { | |
44 | $params = array(); | |
45 | $query = " | |
46 | SELECT * | |
47 | FROM civicrm_event | |
92fcb95f | 48 | WHERE id IN (" . implode(",", $allEventIds) . ") |
0479b4c8 TO |
49 | ORDER BY start_date asc |
50 | "; | |
51 | ||
52 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Event_DAO_Event'); | |
53 | $permissions = CRM_Event_BAO_Event::checkPermission(); | |
22e263ad | 54 | while ($dao->fetch()) { |
0479b4c8 TO |
55 | if (in_array($dao->id, $permissions[CRM_Core_Permission::VIEW])) { |
56 | $manageEvent[$dao->id] = array(); | |
57 | CRM_Core_DAO::storeValues($dao, $manageEvent[$dao->id]); | |
58 | } | |
59 | } | |
60 | } | |
61 | $this->assign('rows', $manageEvent); | |
62 | } | |
63 | } | |
64 | ||
65 | $parentEventParams = array('id' => $this->_id); | |
66 | $parentEventValues = array(); | |
67 | $parentEventReturnProperties = array('start_date', 'end_date'); | |
68 | $parentEventAttributes = CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', $parentEventParams, $parentEventValues, $parentEventReturnProperties); | |
69 | $this->_parentEventStartDate = $parentEventAttributes->start_date; | |
70 | $this->_parentEventEndDate = $parentEventAttributes->end_date; | |
71 | } | |
72 | ||
73 | /** | |
74 | * Set default values for the form. For edit/view mode | |
75 | * the default values are retrieved from the database | |
76 | * | |
77 | * | |
d2a4c29b | 78 | * @return array |
0479b4c8 TO |
79 | */ |
80 | public function setDefaultValues() { | |
81 | $defaults = array(); | |
82 | ||
83 | //Always pass current event's start date by default | |
4c5acec9 | 84 | $defaults['repetition_start_date'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'start_date', 'id'); |
0479b4c8 | 85 | $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues(); |
d2a4c29b | 86 | return array_merge($defaults, $recurringEntityDefaults); |
0479b4c8 TO |
87 | } |
88 | ||
89 | public function buildQuickForm() { | |
90 | CRM_Core_Form_RecurringEntity::buildQuickForm($this); | |
91 | } | |
92 | ||
93 | public function postProcess() { | |
94 | if ($this->_id) { | |
95 | $params = $this->controller->exportValues($this->_name); | |
96 | if ($this->_parentEventStartDate && $this->_parentEventEndDate) { | |
97 | $interval = CRM_Core_BAO_RecurringEntity::getInterval($this->_parentEventStartDate, $this->_parentEventEndDate); | |
98 | $params['intervalDateColumns'] = array('end_date' => $interval); | |
99 | } | |
100 | $params['dateColumns'] = array('start_date'); | |
101 | $params['excludeDateRangeColumns'] = array('start_date', 'end_date'); | |
102 | $params['entity_table'] = 'civicrm_event'; | |
c65ce939 | 103 | $params['entity_id'] = $this->_id; |
1d9e2fcb | 104 | |
105 | // CRM-16568 - check if parent exist for the event. | |
106 | $parentId = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event'); | |
107 | $params['parent_entity_id'] = !empty($parentId) ? $parentId : $params['entity_id']; | |
0479b4c8 TO |
108 | //Unset event id |
109 | unset($params['id']); | |
110 | ||
111 | $url = 'civicrm/event/manage/repeat'; | |
c1bd8375 | 112 | $urlParams = "action=update&reset=1&id={$this->_id}&selectedChild=repeat"; |
0479b4c8 TO |
113 | |
114 | $linkedEntities = array( | |
115 | array( | |
353ffa53 TO |
116 | 'table' => 'civicrm_price_set_entity', |
117 | 'findCriteria' => array( | |
118 | 'entity_id' => $this->_id, | |
21dfd5f5 | 119 | 'entity_table' => 'civicrm_event', |
0479b4c8 TO |
120 | ), |
121 | 'linkedColumns' => array('entity_id'), | |
122 | 'isRecurringEntityRecord' => FALSE, | |
123 | ), | |
124 | array( | |
353ffa53 TO |
125 | 'table' => 'civicrm_uf_join', |
126 | 'findCriteria' => array( | |
127 | 'entity_id' => $this->_id, | |
21dfd5f5 | 128 | 'entity_table' => 'civicrm_event', |
0479b4c8 TO |
129 | ), |
130 | 'linkedColumns' => array('entity_id'), | |
131 | 'isRecurringEntityRecord' => FALSE, | |
132 | ), | |
133 | array( | |
353ffa53 TO |
134 | 'table' => 'civicrm_tell_friend', |
135 | 'findCriteria' => array( | |
136 | 'entity_id' => $this->_id, | |
21dfd5f5 | 137 | 'entity_table' => 'civicrm_event', |
0479b4c8 TO |
138 | ), |
139 | 'linkedColumns' => array('entity_id'), | |
140 | 'isRecurringEntityRecord' => TRUE, | |
141 | ), | |
142 | array( | |
353ffa53 TO |
143 | 'table' => 'civicrm_pcp_block', |
144 | 'findCriteria' => array( | |
145 | 'entity_id' => $this->_id, | |
21dfd5f5 | 146 | 'entity_table' => 'civicrm_event', |
0479b4c8 TO |
147 | ), |
148 | 'linkedColumns' => array('entity_id'), | |
149 | 'isRecurringEntityRecord' => TRUE, | |
150 | ), | |
151 | ); | |
152 | CRM_Core_Form_RecurringEntity::postProcess($params, 'civicrm_event', $linkedEntities); | |
153 | CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams)); | |
154 | } | |
155 | else { | |
156 | CRM_Core_Error::fatal("Could not find Event ID"); | |
157 | } | |
158 | parent::endPostProcess(); | |
159 | } | |
160 | ||
161 | /** | |
f9e31d7f | 162 | * This function gets the number of participant count for the list of related event ids. |
0479b4c8 | 163 | * |
d4dd1e85 TO |
164 | * @param array $listOfRelatedEntities |
165 | * List of related event ids . | |
0479b4c8 | 166 | * |
0479b4c8 TO |
167 | * |
168 | * @return array | |
169 | */ | |
170 | static public function getParticipantCountforEvent($listOfRelatedEntities = array()) { | |
c65ce939 | 171 | $participantDetails = array(); |
0479b4c8 | 172 | if (!empty($listOfRelatedEntities)) { |
353ffa53 | 173 | $implodeRelatedEntities = implode(',', array_map(function ($entity) { |
0479b4c8 TO |
174 | return $entity['id']; |
175 | }, $listOfRelatedEntities)); | |
176 | if ($implodeRelatedEntities) { | |
177 | $query = "SELECT p.event_id as event_id, | |
178 | 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, | |
179 | count(p.id) as participant_count | |
180 | FROM civicrm_participant p, civicrm_event e | |
181 | WHERE p.event_id = e.id AND p.event_id IN ({$implodeRelatedEntities}) | |
182 | GROUP BY p.event_id"; | |
183 | $dao = CRM_Core_DAO::executeQuery($query); | |
22e263ad | 184 | while ($dao->fetch()) { |
0479b4c8 TO |
185 | $participantDetails['countByID'][$dao->event_id] = $dao->participant_count; |
186 | $participantDetails['countByName'][$dao->event_id][$dao->event_data] = $dao->participant_count; | |
187 | } | |
188 | } | |
189 | } | |
190 | return $participantDetails; | |
191 | } | |
192 | ||
193 | /** | |
b4d1a1bf MWMC |
194 | * This function checks if there was any registration for related event ids, |
195 | * and returns array of ids with no registrations | |
7a9ab499 | 196 | * |
0479b4c8 | 197 | * @param string or int or object... $eventID |
7a9ab499 EM |
198 | * |
199 | * @return array | |
0479b4c8 TO |
200 | */ |
201 | public static function checkRegistrationForEvents($eventID) { | |
202 | $eventIdsWithNoRegistration = array(); | |
203 | if ($eventID) { | |
204 | $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($eventID, 'civicrm_event', TRUE); | |
205 | $participantDetails = CRM_Event_Form_ManageEvent_Repeat::getParticipantCountforEvent($getRelatedEntities); | |
206 | //Check if participants exists for events | |
207 | foreach ($getRelatedEntities as $key => $value) { | |
208 | if (!CRM_Utils_Array::value($value['id'], $participantDetails['countByID']) && $value['id'] != $eventID) { | |
209 | //CRM_Event_BAO_Event::del($value['id']); | |
210 | $eventIdsWithNoRegistration[] = $value['id']; | |
211 | } | |
212 | } | |
213 | } | |
214 | CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted = $eventIdsWithNoRegistration; | |
215 | return CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted; | |
216 | } | |
96025800 | 217 | |
0479b4c8 | 218 | } |