Merge pull request #15316 from mepps/order-campaign-dashboard
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Location.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
17 * $Id$
18 *
19 */
20
21/**
22 * This class generates form components for processing Event Location
23 * civicrm_event_page.
24 */
25class CRM_Event_Form_ManageEvent_Location extends CRM_Event_Form_ManageEvent {
26
27 /**
100fef9d 28 * How many locationBlocks should we display?
6a488035
TO
29 *
30 * @var int
31 * @const
32 */
7da04cde 33 const LOCATION_BLOCKS = 1;
6a488035
TO
34
35 /**
100fef9d 36 * The variable, for storing the location array
6a488035
TO
37 *
38 * @var array
39 */
be2fb01f 40 protected $_locationIds = [];
6a488035
TO
41
42 /**
100fef9d 43 * The variable, for storing location block id with event
6a488035
TO
44 *
45 * @var int
46 */
47 protected $_oldLocBlockId = 0;
48
49 /**
66f9e52b 50 * Get the db values for this form.
90b461f1 51 * @var array
6a488035 52 */
be2fb01f 53 public $_values = [];
6a488035
TO
54
55 /**
66f9e52b 56 * Set variables up before form is built.
6a488035 57 */
00be9182 58 public function preProcess() {
6a488035 59 parent::preProcess();
e4b857f8 60 $this->setSelectedChild('location');
6a488035
TO
61
62 $this->_values = $this->get('values');
63 if ($this->_id && empty($this->_values)) {
6a488035 64 //get location values.
be2fb01f 65 $params = [
6a488035
TO
66 'entity_id' => $this->_id,
67 'entity_table' => 'civicrm_event',
be2fb01f 68 ];
6a488035
TO
69 $this->_values = CRM_Core_BAO_Location::getValues($params);
70
71 //get event values.
be2fb01f 72 $params = ['id' => $this->_id];
6a488035
TO
73 CRM_Event_BAO_Event::retrieve($params, $this->_values);
74 $this->set('values', $this->_values);
75 }
be84b210 76
77 //location blocks.
78 CRM_Contact_Form_Location::preProcess($this);
6a488035
TO
79 }
80
81 /**
3bdf1f3a 82 * Set default values for the form.
6a488035 83 *
3bdf1f3a 84 * Note that in edit/view mode the default values are retrieved from the database.
6a488035 85 */
00be9182 86 public function setDefaultValues() {
6a488035
TO
87 $defaults = $this->_values;
88
5d92a7e7 89 if (!empty($defaults['loc_block_id'])) {
6a488035
TO
90 $defaults['loc_event_id'] = $defaults['loc_block_id'];
91 $countLocUsed = CRM_Event_BAO_Event::countEventsUsingLocBlockId($defaults['loc_block_id']);
62a0f5a1 92 $this->assign('locUsed', $countLocUsed);
6a488035
TO
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 }
03e04002 99
6a488035
TO
100 if (!isset($defaults['address'][1]['state_province_id'])) {
101 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
102 }
103
6a488035
TO
104 $defaults['location_option'] = $this->_oldLocBlockId ? 2 : 1;
105
106 return $defaults;
107 }
108
109 /**
66f9e52b 110 * Add local and global form rules.
6a488035 111 */
00be9182 112 public function addRules() {
be2fb01f 113 $this->addFormRule(['CRM_Event_Form_ManageEvent_Location', 'formRule']);
6a488035
TO
114 }
115
116 /**
66f9e52b 117 * Global validation rules for the form.
6a488035 118 *
d4dd1e85
TO
119 * @param array $fields
120 * Posted values of the form.
6a488035 121 *
a6c01b45
CW
122 * @return array
123 * list of errors to be posted back to the form
6a488035 124 */
00be9182 125 public static function formRule($fields) {
6a488035 126 // check for state/country mapping
1273d77c 127 $errors = CRM_Contact_Form_Edit_Address::formRule($fields);
6a488035
TO
128
129 return empty($errors) ? TRUE : $errors;
130 }
131
132 /**
3bdf1f3a 133 * Function to build location block.
6a488035
TO
134 */
135 public function buildQuickForm() {
59f8bad6
CW
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);
6a488035
TO
141
142 $this->applyFilter('__ALL__', 'trim');
143
6a488035
TO
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
03e04002 154
6a488035
TO
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
a7488080 158 if (!empty($locationEvents[$this->_oldLocBlockId])) {
6a488035
TO
159 $possibleDuplicate = $locationEvents[$this->_oldLocBlockId];
160 $locationEvents = array_flip(array_unique($locationEvents));
a7488080 161 if (!empty($locationEvents[$possibleDuplicate])) {
6a488035
TO
162 $locationEvents[$possibleDuplicate] = $this->_oldLocBlockId;
163 }
164 $locationEvents = array_flip($locationEvents);
165 }
166 else {
167 $locationEvents = array_unique($locationEvents);
168 }
169
6a488035
TO
170 if (!empty($locationEvents)) {
171 $this->assign('locEvents', TRUE);
be2fb01f 172 $optionTypes = [
353ffa53 173 '1' => ts('Create new location'),
6a488035 174 '2' => ts('Use existing location'),
be2fb01f 175 ];
6a488035 176
62a0f5a1 177 $this->addRadio('location_option', ts("Choose Location"), $optionTypes);
6a488035
TO
178
179 if (!isset($locationEvents[$this->_oldLocBlockId]) || (!$this->_oldLocBlockId)) {
be2fb01f 180 $locationEvents = ['' => ts('- select -')] + $locationEvents;
6a488035 181 }
be2fb01f 182 $this->add('select', 'loc_event_id', ts('Use Location'), $locationEvents, FALSE, ['class' => 'crm-select2']);
6a488035
TO
183 }
184 $this->addElement('advcheckbox', 'is_show_location', ts('Show Location?'));
185 parent::buildQuickForm();
186 }
187
188 /**
66f9e52b 189 * Process the form submission.
6a488035
TO
190 */
191 public function postProcess() {
192 $params = $this->exportValues();
193 $deleteOldBlock = FALSE;
194
195 // if 'use existing location' option is selected -
8cc574cf 196 if (CRM_Utils_Array::value('location_option', $params) == 2 && !empty($params['loc_event_id']) &&
6a488035
TO
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
be2fb01f 216 $this->_values['address'] = $this->_values['phone'] = $this->_values['email'] = [];
077f88fe 217 // if 'create new loc' option is selected OR selected new loc is different
6a488035
TO
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();
be2fb01f 229 foreach ([
90b461f1
SL
230 'address',
231 'phone',
232 'email',
233 ] as $block) {
a7488080 234 if (empty($params[$block]) || !is_array($params[$block])) {
6a488035
TO
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;
045bc380 242 if (isset($this->_values[$block][$count])) {
243 $values['id'] = $this->_values[$block][$count]['id'];
244 }
6a488035
TO
245 }
246 }
247
248 // create/update event location
9a350644 249 $params['loc_block_id'] = CRM_Core_BAO_Location::create($params, TRUE, 'event')['id'];
6a488035
TO
250
251 // finally update event params
252 $params['id'] = $this->_id;
253 CRM_Event_BAO_Event::add($params);
254
5d92a7e7
CW
255 // Update tab "disabled" css class
256 $this->ajaxResponse['tabValid'] = TRUE;
6a488035
TO
257 parent::endPostProcess();
258 }
6a488035
TO
259
260 /**
261 * Return a descriptive name for the page, used in wizard header
262 *
263 * @return string
6a488035
TO
264 */
265 public function getTitle() {
266 return ts('Event Location');
267 }
96025800 268
6a488035 269}