CRM-15932 - Repeat tab must refresh page when switching repeat mode so js & vars...
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Repeat.php
1 <?php
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 /**
16 * Parent Event Start Date.
17 */
18 protected $_parentEventStartDate = NULL;
19
20 /**
21 * Parent Event End Date.
22 */
23 protected $_parentEventEndDate = NULL;
24
25
26 public function preProcess() {
27 parent::preProcess();
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 /**
34 * Get connected event information list
35 */
36 //Get all connected event ids
37 $allEventIdsArray = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($checkParentExistsForThisId, 'civicrm_event');
38 $allEventIds = array();
39 if (!empty($allEventIdsArray)) {
40 foreach ($allEventIdsArray as $key => $val) {
41 $allEventIds[] = $val['id'];
42 }
43 if (!empty($allEventIds)) {
44 $params = array();
45 $query = "
46 SELECT *
47 FROM civicrm_event
48 WHERE id IN (" . implode(",", $allEventIds) . ")
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();
54 while ($dao->fetch()) {
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 *
78 * @return array
79 */
80 public function setDefaultValues() {
81 $defaults = array();
82
83 //Always pass current event's start date by default
84 $currentEventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'start_date', 'id');
85 list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($currentEventStartDate, 'activityDateTime');
86 $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues();
87 return array_merge($defaults, $recurringEntityDefaults);
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);
99 $params['intervalDateColumns'] = array('end_date' => $interval);
100 }
101 $params['dateColumns'] = array('start_date');
102 $params['excludeDateRangeColumns'] = array('start_date', 'end_date');
103 $params['entity_table'] = 'civicrm_event';
104 $params['entity_id'] = $this->_id;
105 //Unset event id
106 unset($params['id']);
107
108 $url = 'civicrm/event/manage/repeat';
109 $urlParams = "action=update&reset=1&id={$this->_id}";
110
111 $linkedEntities = array(
112 array(
113 'table' => 'civicrm_price_set_entity',
114 'findCriteria' => array(
115 'entity_id' => $this->_id,
116 'entity_table' => 'civicrm_event',
117 ),
118 'linkedColumns' => array('entity_id'),
119 'isRecurringEntityRecord' => FALSE,
120 ),
121 array(
122 'table' => 'civicrm_uf_join',
123 'findCriteria' => array(
124 'entity_id' => $this->_id,
125 'entity_table' => 'civicrm_event',
126 ),
127 'linkedColumns' => array('entity_id'),
128 'isRecurringEntityRecord' => FALSE,
129 ),
130 array(
131 'table' => 'civicrm_tell_friend',
132 'findCriteria' => array(
133 'entity_id' => $this->_id,
134 'entity_table' => 'civicrm_event',
135 ),
136 'linkedColumns' => array('entity_id'),
137 'isRecurringEntityRecord' => TRUE,
138 ),
139 array(
140 'table' => 'civicrm_pcp_block',
141 'findCriteria' => array(
142 'entity_id' => $this->_id,
143 'entity_table' => 'civicrm_event',
144 ),
145 'linkedColumns' => array('entity_id'),
146 'isRecurringEntityRecord' => TRUE,
147 ),
148 );
149 CRM_Core_Form_RecurringEntity::postProcess($params, 'civicrm_event', $linkedEntities);
150 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
151 }
152 else {
153 CRM_Core_Error::fatal("Could not find Event ID");
154 }
155 parent::endPostProcess();
156 }
157
158 /**
159 * This function gets the number of participant count for the list of related event ids.
160 *
161 * @param array $listOfRelatedEntities
162 * List of related event ids .
163 *
164 *
165 * @return array
166 */
167 static public function getParticipantCountforEvent($listOfRelatedEntities = array()) {
168 $participantDetails = array();
169 if (!empty($listOfRelatedEntities)) {
170 $implodeRelatedEntities = implode(',', array_map(function ($entity) {
171 return $entity['id'];
172 }, $listOfRelatedEntities));
173 if ($implodeRelatedEntities) {
174 $query = "SELECT p.event_id as event_id,
175 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,
176 count(p.id) as participant_count
177 FROM civicrm_participant p, civicrm_event e
178 WHERE p.event_id = e.id AND p.event_id IN ({$implodeRelatedEntities})
179 GROUP BY p.event_id";
180 $dao = CRM_Core_DAO::executeQuery($query);
181 while ($dao->fetch()) {
182 $participantDetails['countByID'][$dao->event_id] = $dao->participant_count;
183 $participantDetails['countByName'][$dao->event_id][$dao->event_data] = $dao->participant_count;
184 }
185 }
186 }
187 return $participantDetails;
188 }
189
190 /**
191 * This function checks if there was any registraion for related event ids,
192 * and returns array of ids with no regsitrations
193 *
194 * @param string or int or object... $eventID
195 *
196 * @return array
197 */
198 public static function checkRegistrationForEvents($eventID) {
199 $eventIdsWithNoRegistration = array();
200 if ($eventID) {
201 $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($eventID, 'civicrm_event', TRUE);
202 $participantDetails = CRM_Event_Form_ManageEvent_Repeat::getParticipantCountforEvent($getRelatedEntities);
203 //Check if participants exists for events
204 foreach ($getRelatedEntities as $key => $value) {
205 if (!CRM_Utils_Array::value($value['id'], $participantDetails['countByID']) && $value['id'] != $eventID) {
206 //CRM_Event_BAO_Event::del($value['id']);
207 $eventIdsWithNoRegistration[] = $value['id'];
208 }
209 }
210 }
211 CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted = $eventIdsWithNoRegistration;
212 return CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted;
213 }
214
215 }