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