| 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 | * Parent Event Start Date |
| 18 | */ |
| 19 | protected $_parentEventStartDate = NULL; |
| 20 | |
| 21 | /** |
| 22 | * Parent Event End Date |
| 23 | */ |
| 24 | protected $_parentEventEndDate = NULL; |
| 25 | |
| 26 | |
| 27 | public function preProcess() { |
| 28 | parent::preProcess(); |
| 29 | CRM_Core_Form_RecurringEntity::preProcess('civicrm_event'); |
| 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 | /** |
| 36 | * Get connected event information list |
| 37 | */ |
| 38 | //Get all connected event ids |
| 39 | $allEventIdsArray = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($checkParentExistsForThisId, 'civicrm_event'); |
| 40 | $allEventIds = array(); |
| 41 | if (!empty($allEventIdsArray)) { |
| 42 | foreach($allEventIdsArray as $key => $val) { |
| 43 | $allEventIds[] = $val['id']; |
| 44 | } |
| 45 | if (!empty($allEventIds)) { |
| 46 | $params = array(); |
| 47 | $query = " |
| 48 | SELECT * |
| 49 | FROM civicrm_event |
| 50 | WHERE id IN (".implode(",", $allEventIds).") |
| 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(); |
| 56 | while($dao->fetch()) { |
| 57 | if (in_array($dao->id, $permissions[CRM_Core_Permission::VIEW])) { |
| 58 | $manageEvent[$dao->id] = array(); |
| 59 | CRM_Core_DAO::storeValues($dao, $manageEvent[$dao->id]); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | $this->assign('rows', $manageEvent); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $parentEventParams = array('id' => $this->_id); |
| 68 | $parentEventValues = array(); |
| 69 | $parentEventReturnProperties = array('start_date', 'end_date'); |
| 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 | * |
| 80 | * @return None |
| 81 | */ |
| 82 | public function setDefaultValues() { |
| 83 | $defaults = array(); |
| 84 | |
| 85 | //Always pass current event's start date by default |
| 86 | $currentEventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'start_date', 'id'); |
| 87 | list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($currentEventStartDate, 'activityDateTime'); |
| 88 | $defaults['allowRepeatConfigToSubmit'] = 1; |
| 89 | $recurringEntityDefaults = array(); |
| 90 | $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues(); |
| 91 | $defaults = array_merge($defaults, $recurringEntityDefaults); |
| 92 | return $defaults; |
| 93 | } |
| 94 | |
| 95 | public function buildQuickForm() { |
| 96 | CRM_Core_Form_RecurringEntity::buildQuickForm($this); |
| 97 | } |
| 98 | |
| 99 | public function postProcess() { |
| 100 | if ($this->_id) { |
| 101 | $params = $this->controller->exportValues($this->_name); |
| 102 | if ($this->_parentEventStartDate && $this->_parentEventEndDate) { |
| 103 | $interval = CRM_Core_BAO_RecurringEntity::getInterval($this->_parentEventStartDate, $this->_parentEventEndDate); |
| 104 | $params['intervalDateColumns'] = array('end_date' => $interval); |
| 105 | } |
| 106 | $params['dateColumns'] = array('start_date'); |
| 107 | $params['excludeDateRangeColumns'] = array('start_date', 'end_date'); |
| 108 | $params['entity_table'] = 'civicrm_event'; |
| 109 | //Unset event id |
| 110 | unset($params['id']); |
| 111 | |
| 112 | $url = 'civicrm/event/manage/repeat'; |
| 113 | $urlParams = "action=update&reset=1&id={$this->_id}"; |
| 114 | |
| 115 | $linkedEntities = array( |
| 116 | array( |
| 117 | 'table' => 'civicrm_price_set_entity', |
| 118 | 'findCriteria' => array( |
| 119 | 'entity_id' => $this->_id, |
| 120 | 'entity_table' => 'civicrm_event' |
| 121 | ), |
| 122 | 'linkedColumns' => array('entity_id'), |
| 123 | 'isRecurringEntityRecord' => FALSE, |
| 124 | ), |
| 125 | array( |
| 126 | 'table' => 'civicrm_uf_join', |
| 127 | 'findCriteria' => array( |
| 128 | 'entity_id' => $this->_id, |
| 129 | 'entity_table' => 'civicrm_event' |
| 130 | ), |
| 131 | 'linkedColumns' => array('entity_id'), |
| 132 | 'isRecurringEntityRecord' => FALSE, |
| 133 | ), |
| 134 | array( |
| 135 | 'table' => 'civicrm_tell_friend', |
| 136 | 'findCriteria' => array( |
| 137 | 'entity_id' => $this->_id, |
| 138 | 'entity_table' => 'civicrm_event' |
| 139 | ), |
| 140 | 'linkedColumns' => array('entity_id'), |
| 141 | 'isRecurringEntityRecord' => TRUE, |
| 142 | ), |
| 143 | array( |
| 144 | 'table' => 'civicrm_pcp_block', |
| 145 | 'findCriteria' => array( |
| 146 | 'entity_id' => $this->_id, |
| 147 | 'entity_table' => 'civicrm_event' |
| 148 | ), |
| 149 | 'linkedColumns' => array('entity_id'), |
| 150 | 'isRecurringEntityRecord' => TRUE, |
| 151 | ), |
| 152 | ); |
| 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 | /** |
| 163 | * This function gets the number of participant count for the list of related event ids |
| 164 | * |
| 165 | * @param array $listOfRelatedEntities |
| 166 | * List of related event ids . |
| 167 | * |
| 168 | * @static |
| 169 | * |
| 170 | * @return array |
| 171 | */ |
| 172 | static public function getParticipantCountforEvent($listOfRelatedEntities = array()) { |
| 173 | if (!empty($listOfRelatedEntities)) { |
| 174 | $implodeRelatedEntities = implode(',', array_map(function($entity) { |
| 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); |
| 185 | $participantDetails = array(); |
| 186 | while($dao->fetch()) { |
| 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 | /** |
| 196 | * This function checks if there was any registraion for related event ids, |
| 197 | * and returns array of ids with no regsitrations |
| 198 | * @param string or int or object... $eventID |
| 199 | */ |
| 200 | public static function checkRegistrationForEvents($eventID) { |
| 201 | $eventIdsWithNoRegistration = array(); |
| 202 | if ($eventID) { |
| 203 | $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($eventID, 'civicrm_event', TRUE); |
| 204 | $participantDetails = CRM_Event_Form_ManageEvent_Repeat::getParticipantCountforEvent($getRelatedEntities); |
| 205 | //Check if participants exists for events |
| 206 | foreach ($getRelatedEntities as $key => $value) { |
| 207 | if (!CRM_Utils_Array::value($value['id'], $participantDetails['countByID']) && $value['id'] != $eventID) { |
| 208 | //CRM_Event_BAO_Event::del($value['id']); |
| 209 | $eventIdsWithNoRegistration[] = $value['id']; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted = $eventIdsWithNoRegistration; |
| 214 | return CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted; |
| 215 | } |
| 216 | } |