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