Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-05-21-13-15-18
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Location.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 *
106 * @return None
107 */
108 function setDefaultValues() {
109 $defaults = $this->_values;
110
111 if (CRM_Utils_Array::value('loc_block_id', $defaults)) {
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
168 $errors = CRM_Contact_Form_Edit_Address::formRule($fields);
169
170 return empty($errors) ? TRUE : $errors;
171 }
172
173 /**
174 * function to build location block
175 *
176 * @return None
177 * @access public
178 */
179 public function buildQuickForm() {
180 //load form for child blocks
181 if ($this->_addBlockName) {
182 return eval('CRM_Contact_Form_Edit_' . $this->_addBlockName . '::buildQuickForm( $this );');
183 }
184
185 $this->applyFilter('__ALL__', 'trim');
186
187 //build location blocks.
188 CRM_Contact_Form_Location::buildQuickForm($this);
189
190 //fix for CRM-1971
191 $this->assign('action', $this->_action);
192
193 if ($this->_id) {
194 $this->_oldLocBlockId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
195 $this->_id, 'loc_block_id'
196 );
197 }
198
199 // get the list of location blocks being used by other events
03e04002 200
6a488035
TO
201 $locationEvents = CRM_Event_BAO_Event::getLocationEvents();
202 // remove duplicates and make sure that the duplicate entry with key as
203 // loc_block_id of this event (this->_id) is preserved
204 if (CRM_Utils_Array::value($this->_oldLocBlockId, $locationEvents)) {
205 $possibleDuplicate = $locationEvents[$this->_oldLocBlockId];
206 $locationEvents = array_flip(array_unique($locationEvents));
207 if (CRM_Utils_Array::value($possibleDuplicate, $locationEvents)) {
208 $locationEvents[$possibleDuplicate] = $this->_oldLocBlockId;
209 }
210 $locationEvents = array_flip($locationEvents);
211 }
212 else {
213 $locationEvents = array_unique($locationEvents);
214 }
215
216 $events = array();
217 if (!empty($locationEvents)) {
218 $this->assign('locEvents', TRUE);
219 $optionTypes = array('1' => ts('Create new location'),
220 '2' => ts('Use existing location'),
221 );
222
223 $this->addRadio('location_option', ts("Choose Location"), $optionTypes,
224 array(
225 'onclick' => "showLocFields();"), '<br/>', FALSE
226 );
227
228 if (!isset($locationEvents[$this->_oldLocBlockId]) || (!$this->_oldLocBlockId)) {
229 $locationEvents = array(
230 '' => ts('- select -')) + $locationEvents;
231 }
232 $this->add('select', 'loc_event_id', ts('Use Location'), $locationEvents);
233 }
234 $this->addElement('advcheckbox', 'is_show_location', ts('Show Location?'));
235 parent::buildQuickForm();
236 }
237
238 /**
239 * Function to process the form
240 *
241 * @access public
242 *
243 * @return None
244 */
245 public function postProcess() {
246 $params = $this->exportValues();
247 $deleteOldBlock = FALSE;
248
249 // if 'use existing location' option is selected -
250 if (CRM_Utils_Array::value('location_option', $params) == 2 &&
251 CRM_Utils_Array::value('loc_event_id', $params) &&
252 ($params['loc_event_id'] != $this->_oldLocBlockId)
253 ) {
254 // if new selected loc is different from old loc, update the loc_block_id
255 // so that loc update would affect the selected loc and not the old one.
256 $deleteOldBlock = TRUE;
257 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
258 'loc_block_id', $params['loc_event_id']
259 );
260 }
261
262 // if 'create new loc' option is selected, set the loc_block_id for this event to null
263 // so that an update would result in creating a new loc.
264 if ($this->_oldLocBlockId && (CRM_Utils_Array::value('location_option', $params) == 1)) {
265 $deleteOldBlock = TRUE;
266 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
267 'loc_block_id', 'null'
268 );
269 }
270
271 // if 'create new loc' optioin is selected OR selected new loc is different
272 // from old one, go ahead and delete the old loc provided thats not being
273 // used by any other event
274 if ($this->_oldLocBlockId && $deleteOldBlock) {
275 CRM_Event_BAO_Event::deleteEventLocBlock($this->_oldLocBlockId, $this->_id);
276 }
277
278 // get ready with location block params
279 $params['entity_table'] = 'civicrm_event';
280 $params['entity_id'] = $this->_id;
281
282 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
283 foreach (array(
284 'address', 'phone', 'email') as $block) {
285 if (!CRM_Utils_Array::value($block, $params) || !is_array($params[$block])) {
286 continue;
287 }
288 foreach ($params[$block] as $count => & $values) {
289 if ($count == 1) {
290 $values['is_primary'] = 1;
291 }
292 $values['location_type_id'] = ($defaultLocationType->id) ? $defaultLocationType->id : 1;
293 }
294 }
295
296 // create/update event location
297 $location = CRM_Core_BAO_Location::create($params, TRUE, 'event');
298 $params['loc_block_id'] = $location['id'];
299
300 // finally update event params
301 $params['id'] = $this->_id;
302 CRM_Event_BAO_Event::add($params);
303
304 parent::endPostProcess();
305 }
306 //end of function
307
308 /**
309 * Return a descriptive name for the page, used in wizard header
310 *
311 * @return string
312 * @access public
313 */
314 public function getTitle() {
315 return ts('Event Location');
316 }
317}
318