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