CRM-14106 - Regex targeting inline conditonal statements
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Location.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36
37/**
38 * This class generates form components for processing Event Location
39 * civicrm_event_page.
40 */
41class CRM_Event_Form_ManageEvent_Location extends CRM_Event_Form_ManageEvent {
42
43 /**
44 * how many locationBlocks should we display?
45 *
46 * @var int
47 * @const
48 */
49 CONST LOCATION_BLOCKS = 1;
50
51 /**
52 * the variable, for storing the location array
53 *
54 * @var array
55 */
56 protected $_locationIds = array();
57
58 /**
59 * the variable, for storing location block id with event
60 *
61 * @var int
62 */
63 protected $_oldLocBlockId = 0;
64
65 /**
66 * get the db values for this form
67 *
68 */
69 public $_values = array();
70
71 /**
72 * Function to set variables up before form is built
73 *
74 * @return void
75 * @access public
76 */
77 function preProcess() {
78 parent::preProcess();
79
80 $this->_values = $this->get('values');
81 if ($this->_id && empty($this->_values)) {
82
83 //get location values.
84 $params = array(
85 'entity_id' => $this->_id,
86 'entity_table' => 'civicrm_event',
87 );
88 $this->_values = CRM_Core_BAO_Location::getValues($params);
89
90 //get event values.
91 $params = array('id' => $this->_id);
92 CRM_Event_BAO_Event::retrieve($params, $this->_values);
93 $this->set('values', $this->_values);
94 }
95
96 //location blocks.
97 CRM_Contact_Form_Location::preProcess($this);
98 }
99
100 /**
101 * This function sets the default values for the form. Note that in edit/view mode
102 * the default values are retrieved from the database
103 *
104 * @access public
105 *
355ba699 106 * @return void
6a488035
TO
107 */
108 function setDefaultValues() {
109 $defaults = $this->_values;
110
5d92a7e7 111 if (!empty($defaults['loc_block_id'])) {
6a488035
TO
112 $defaults['loc_event_id'] = $defaults['loc_block_id'];
113 $countLocUsed = CRM_Event_BAO_Event::countEventsUsingLocBlockId($defaults['loc_block_id']);
114 if ($countLocUsed > 1) {
115 $this->assign('locUsed', TRUE);
116 }
117 }
118
119 $config = CRM_Core_Config::singleton();
120 if (!isset($defaults['address'][1]['country_id'])) {
121 $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
122 }
03e04002 123
6a488035
TO
124 if (!isset($defaults['address'][1]['state_province_id'])) {
125 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
126 }
127
128 if (!empty($defaults['address'])) {
129 foreach ($defaults['address'] as $key => $value) {
130 CRM_Contact_Form_Edit_Address::fixStateSelect($this,
131 "address[$key][country_id]",
132 "address[$key][state_province_id]",
133 "address[$key][county_id]",
134 CRM_Utils_Array::value('country_id', $value,
135 $config->defaultContactCountry
136 ),
137 CRM_Utils_Array::value('state_province_id', $value)
138 );
139 }
140 }
141 $defaults['location_option'] = $this->_oldLocBlockId ? 2 : 1;
142
143 return $defaults;
144 }
145
146 /**
147 * Add local and global form rules
148 *
149 * @access protected
150 *
151 * @return void
152 */
153 function addRules() {
154 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Location', 'formRule'));
155 }
156
157 /**
158 * global validation rules for the form
159 *
160 * @param array $fields posted values of the form
161 *
162 * @return array list of errors to be posted back to the form
163 * @static
164 * @access public
165 */
166 static function formRule($fields) {
167 // check for state/country mapping
ac79e2f5 168 $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject);
6a488035
TO
169
170 return empty($errors) ? TRUE : $errors;
171 }
172
173 /**
174 * function to build location block
175 *
355ba699 176 * @return void
6a488035
TO
177 * @access public
178 */
179 public function buildQuickForm() {
180 //load form for child blocks
181 if ($this->_addBlockName) {
0e6e8724
DL
182 $className = "CRM_Contact_Form_Edit_{$this->_addBlockName}";
183 return $className::buildQuickForm($this);
6a488035
TO
184 }
185
186 $this->applyFilter('__ALL__', 'trim');
187
188 //build location blocks.
189 CRM_Contact_Form_Location::buildQuickForm($this);
190
191 //fix for CRM-1971
192 $this->assign('action', $this->_action);
193
194 if ($this->_id) {
195 $this->_oldLocBlockId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
196 $this->_id, 'loc_block_id'
197 );
198 }
199
200 // get the list of location blocks being used by other events
03e04002 201
6a488035
TO
202 $locationEvents = CRM_Event_BAO_Event::getLocationEvents();
203 // remove duplicates and make sure that the duplicate entry with key as
204 // loc_block_id of this event (this->_id) is preserved
205 if (CRM_Utils_Array::value($this->_oldLocBlockId, $locationEvents)) {
206 $possibleDuplicate = $locationEvents[$this->_oldLocBlockId];
207 $locationEvents = array_flip(array_unique($locationEvents));
208 if (CRM_Utils_Array::value($possibleDuplicate, $locationEvents)) {
209 $locationEvents[$possibleDuplicate] = $this->_oldLocBlockId;
210 }
211 $locationEvents = array_flip($locationEvents);
212 }
213 else {
214 $locationEvents = array_unique($locationEvents);
215 }
216
217 $events = array();
218 if (!empty($locationEvents)) {
219 $this->assign('locEvents', TRUE);
220 $optionTypes = array('1' => ts('Create new location'),
221 '2' => ts('Use existing location'),
222 );
223
224 $this->addRadio('location_option', ts("Choose Location"), $optionTypes,
225 array(
226 'onclick' => "showLocFields();"), '<br/>', FALSE
227 );
228
229 if (!isset($locationEvents[$this->_oldLocBlockId]) || (!$this->_oldLocBlockId)) {
230 $locationEvents = array(
231 '' => ts('- select -')) + $locationEvents;
232 }
233 $this->add('select', 'loc_event_id', ts('Use Location'), $locationEvents);
234 }
235 $this->addElement('advcheckbox', 'is_show_location', ts('Show Location?'));
236 parent::buildQuickForm();
237 }
238
239 /**
240 * Function to process the form
241 *
242 * @access public
243 *
355ba699 244 * @return void
6a488035
TO
245 */
246 public function postProcess() {
247 $params = $this->exportValues();
248 $deleteOldBlock = FALSE;
249
250 // if 'use existing location' option is selected -
251 if (CRM_Utils_Array::value('location_option', $params) == 2 &&
252 CRM_Utils_Array::value('loc_event_id', $params) &&
253 ($params['loc_event_id'] != $this->_oldLocBlockId)
254 ) {
255 // if new selected loc is different from old loc, update the loc_block_id
256 // so that loc update would affect the selected loc and not the old one.
257 $deleteOldBlock = TRUE;
258 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
259 'loc_block_id', $params['loc_event_id']
260 );
261 }
262
263 // if 'create new loc' option is selected, set the loc_block_id for this event to null
264 // so that an update would result in creating a new loc.
265 if ($this->_oldLocBlockId && (CRM_Utils_Array::value('location_option', $params) == 1)) {
266 $deleteOldBlock = TRUE;
267 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
268 'loc_block_id', 'null'
269 );
270 }
271
272 // if 'create new loc' optioin is selected OR selected new loc is different
273 // from old one, go ahead and delete the old loc provided thats not being
274 // used by any other event
275 if ($this->_oldLocBlockId && $deleteOldBlock) {
276 CRM_Event_BAO_Event::deleteEventLocBlock($this->_oldLocBlockId, $this->_id);
277 }
278
279 // get ready with location block params
280 $params['entity_table'] = 'civicrm_event';
281 $params['entity_id'] = $this->_id;
282
283 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
284 foreach (array(
285 'address', 'phone', 'email') as $block) {
286 if (!CRM_Utils_Array::value($block, $params) || !is_array($params[$block])) {
287 continue;
288 }
289 foreach ($params[$block] as $count => & $values) {
290 if ($count == 1) {
291 $values['is_primary'] = 1;
292 }
293 $values['location_type_id'] = ($defaultLocationType->id) ? $defaultLocationType->id : 1;
294 }
295 }
296
297 // create/update event location
298 $location = CRM_Core_BAO_Location::create($params, TRUE, 'event');
299 $params['loc_block_id'] = $location['id'];
300
301 // finally update event params
302 $params['id'] = $this->_id;
303 CRM_Event_BAO_Event::add($params);
304
5d92a7e7
CW
305 // Update tab "disabled" css class
306 $this->ajaxResponse['tabValid'] = TRUE;
6a488035
TO
307 parent::endPostProcess();
308 }
309 //end of function
310
311 /**
312 * Return a descriptive name for the page, used in wizard header
313 *
314 * @return string
315 * @access public
316 */
317 public function getTitle() {
318 return ts('Event Location');
319 }
320}
321