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