Merge pull request #16091 from civicrm/5.21
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Location.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21 /**
22 * This class generates form components for processing Event Location
23 * civicrm_event_page.
24 */
25 class CRM_Event_Form_ManageEvent_Location extends CRM_Event_Form_ManageEvent {
26
27 /**
28 * How many locationBlocks should we display?
29 *
30 * @var int
31 * @const
32 */
33 const LOCATION_BLOCKS = 1;
34
35 /**
36 * The variable, for storing the location array
37 *
38 * @var array
39 */
40 protected $_locationIds = [];
41
42 /**
43 * The variable, for storing location block id with event
44 *
45 * @var int
46 */
47 protected $_oldLocBlockId = 0;
48
49 /**
50 * Get the db values for this form.
51 * @var array
52 */
53 public $_values = [];
54
55 /**
56 * Set variables up before form is built.
57 */
58 public function preProcess() {
59 parent::preProcess();
60 $this->setSelectedChild('location');
61
62 $this->_values = $this->get('values');
63 if ($this->_id && empty($this->_values)) {
64 //get location values.
65 $params = [
66 'entity_id' => $this->_id,
67 'entity_table' => 'civicrm_event',
68 ];
69 $this->_values = CRM_Core_BAO_Location::getValues($params);
70
71 //get event values.
72 $params = ['id' => $this->_id];
73 CRM_Event_BAO_Event::retrieve($params, $this->_values);
74 $this->set('values', $this->_values);
75 }
76
77 //location blocks.
78 CRM_Contact_Form_Location::preProcess($this);
79 }
80
81 /**
82 * Set default values for the form.
83 *
84 * Note that in edit/view mode the default values are retrieved from the database.
85 */
86 public function setDefaultValues() {
87 $defaults = $this->_values;
88
89 if (!empty($defaults['loc_block_id'])) {
90 $defaults['loc_event_id'] = $defaults['loc_block_id'];
91 $countLocUsed = CRM_Event_BAO_Event::countEventsUsingLocBlockId($defaults['loc_block_id']);
92 $this->assign('locUsed', $countLocUsed);
93 }
94
95 $config = CRM_Core_Config::singleton();
96 if (!isset($defaults['address'][1]['country_id'])) {
97 $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
98 }
99
100 if (!isset($defaults['address'][1]['state_province_id'])) {
101 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
102 }
103
104 $defaults['location_option'] = $this->_oldLocBlockId ? 2 : 1;
105
106 return $defaults;
107 }
108
109 /**
110 * Add local and global form rules.
111 */
112 public function addRules() {
113 $this->addFormRule(['CRM_Event_Form_ManageEvent_Location', 'formRule']);
114 }
115
116 /**
117 * Global validation rules for the form.
118 *
119 * @param array $fields
120 * Posted values of the form.
121 *
122 * @return array
123 * list of errors to be posted back to the form
124 */
125 public static function formRule($fields) {
126 // check for state/country mapping
127 $errors = CRM_Contact_Form_Edit_Address::formRule($fields);
128
129 return empty($errors) ? TRUE : $errors;
130 }
131
132 /**
133 * Function to build location block.
134 */
135 public function buildQuickForm() {
136 CRM_Contact_Form_Edit_Address::buildQuickForm($this, 1);
137 CRM_Contact_Form_Edit_Email::buildQuickForm($this, 1);
138 CRM_Contact_Form_Edit_Email::buildQuickForm($this, 2);
139 CRM_Contact_Form_Edit_Phone::buildQuickForm($this, 1);
140 CRM_Contact_Form_Edit_Phone::buildQuickForm($this, 2);
141
142 $this->applyFilter('__ALL__', 'trim');
143
144 //fix for CRM-1971
145 $this->assign('action', $this->_action);
146
147 if ($this->_id) {
148 $this->_oldLocBlockId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
149 $this->_id, 'loc_block_id'
150 );
151 }
152
153 // get the list of location blocks being used by other events
154
155 $locationEvents = CRM_Event_BAO_Event::getLocationEvents();
156 // remove duplicates and make sure that the duplicate entry with key as
157 // loc_block_id of this event (this->_id) is preserved
158 if (!empty($locationEvents[$this->_oldLocBlockId])) {
159 $possibleDuplicate = $locationEvents[$this->_oldLocBlockId];
160 $locationEvents = array_flip(array_unique($locationEvents));
161 if (!empty($locationEvents[$possibleDuplicate])) {
162 $locationEvents[$possibleDuplicate] = $this->_oldLocBlockId;
163 }
164 $locationEvents = array_flip($locationEvents);
165 }
166 else {
167 $locationEvents = array_unique($locationEvents);
168 }
169
170 if (!empty($locationEvents)) {
171 $this->assign('locEvents', TRUE);
172 $optionTypes = [
173 '1' => ts('Create new location'),
174 '2' => ts('Use existing location'),
175 ];
176
177 $this->addRadio('location_option', ts("Choose Location"), $optionTypes);
178
179 if (!isset($locationEvents[$this->_oldLocBlockId]) || (!$this->_oldLocBlockId)) {
180 $locationEvents = ['' => ts('- select -')] + $locationEvents;
181 }
182 $this->add('select', 'loc_event_id', ts('Use Location'), $locationEvents, FALSE, ['class' => 'crm-select2']);
183 }
184 $this->addElement('advcheckbox', 'is_show_location', ts('Show Location?'));
185 parent::buildQuickForm();
186 }
187
188 /**
189 * Process the form submission.
190 */
191 public function postProcess() {
192 $params = $this->exportValues();
193 $deleteOldBlock = FALSE;
194
195 // if 'use existing location' option is selected -
196 if (CRM_Utils_Array::value('location_option', $params) == 2 && !empty($params['loc_event_id']) &&
197 ($params['loc_event_id'] != $this->_oldLocBlockId)
198 ) {
199 // if new selected loc is different from old loc, update the loc_block_id
200 // so that loc update would affect the selected loc and not the old one.
201 $deleteOldBlock = TRUE;
202 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
203 'loc_block_id', $params['loc_event_id']
204 );
205 }
206
207 // if 'create new loc' option is selected, set the loc_block_id for this event to null
208 // so that an update would result in creating a new loc.
209 if ($this->_oldLocBlockId && (CRM_Utils_Array::value('location_option', $params) == 1)) {
210 $deleteOldBlock = TRUE;
211 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
212 'loc_block_id', 'null'
213 );
214 }
215
216 $this->_values['address'] = $this->_values['phone'] = $this->_values['email'] = [];
217 // if 'create new loc' option is selected OR selected new loc is different
218 // from old one, go ahead and delete the old loc provided thats not being
219 // used by any other event
220 if ($this->_oldLocBlockId && $deleteOldBlock) {
221 CRM_Event_BAO_Event::deleteEventLocBlock($this->_oldLocBlockId, $this->_id);
222 }
223
224 // get ready with location block params
225 $params['entity_table'] = 'civicrm_event';
226 $params['entity_id'] = $this->_id;
227
228 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
229 foreach ([
230 'address',
231 'phone',
232 'email',
233 ] as $block) {
234 if (empty($params[$block]) || !is_array($params[$block])) {
235 continue;
236 }
237 foreach ($params[$block] as $count => & $values) {
238 if ($count == 1) {
239 $values['is_primary'] = 1;
240 }
241 $values['location_type_id'] = ($defaultLocationType->id) ? $defaultLocationType->id : 1;
242 if (isset($this->_values[$block][$count])) {
243 $values['id'] = $this->_values[$block][$count]['id'];
244 }
245 }
246 }
247
248 // create/update event location
249 $params['loc_block_id'] = CRM_Core_BAO_Location::create($params, TRUE, 'event')['id'];
250
251 // finally update event params
252 $params['id'] = $this->_id;
253 CRM_Event_BAO_Event::add($params);
254
255 // Update tab "disabled" css class
256 $this->ajaxResponse['tabValid'] = TRUE;
257 parent::endPostProcess();
258 }
259
260 /**
261 * Return a descriptive name for the page, used in wizard header
262 *
263 * @return string
264 */
265 public function getTitle() {
266 return ts('Event Location');
267 }
268
269 }