Merge pull request #4973 from PalanteJon/cleanup-master
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Location.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 /**
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 */
56 protected $_locationIds = array();
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 /**
100fef9d 66 * Get the db values for this form
6a488035
TO
67 */
68 public $_values = array();
69
70 /**
100fef9d 71 * Set variables up before form is built
6a488035
TO
72 *
73 * @return void
6a488035 74 */
00be9182 75 public function preProcess() {
6a488035
TO
76 parent::preProcess();
77
78 $this->_values = $this->get('values');
79 if ($this->_id && empty($this->_values)) {
6a488035
TO
80 //get location values.
81 $params = array(
82 'entity_id' => $this->_id,
83 'entity_table' => 'civicrm_event',
84 );
85 $this->_values = CRM_Core_BAO_Location::getValues($params);
86
87 //get event values.
88 $params = array('id' => $this->_id);
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 /**
c490a46a 98 * Set default values for the form. Note that in edit/view mode
6a488035
TO
99 * the default values are retrieved from the database
100 *
6a488035 101 *
355ba699 102 * @return void
6a488035 103 */
00be9182 104 public function setDefaultValues() {
6a488035
TO
105 $defaults = $this->_values;
106
5d92a7e7 107 if (!empty($defaults['loc_block_id'])) {
6a488035
TO
108 $defaults['loc_event_id'] = $defaults['loc_block_id'];
109 $countLocUsed = CRM_Event_BAO_Event::countEventsUsingLocBlockId($defaults['loc_block_id']);
62a0f5a1 110 $this->assign('locUsed', $countLocUsed);
6a488035
TO
111 }
112
113 $config = CRM_Core_Config::singleton();
114 if (!isset($defaults['address'][1]['country_id'])) {
115 $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
116 }
03e04002 117
6a488035
TO
118 if (!isset($defaults['address'][1]['state_province_id'])) {
119 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
120 }
121
6a488035
TO
122 $defaults['location_option'] = $this->_oldLocBlockId ? 2 : 1;
123
124 return $defaults;
125 }
126
127 /**
128 * Add local and global form rules
129 *
6a488035
TO
130 *
131 * @return void
132 */
00be9182 133 public function addRules() {
6a488035
TO
134 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Location', 'formRule'));
135 }
136
137 /**
100fef9d 138 * Global validation rules for the form
6a488035 139 *
d4dd1e85
TO
140 * @param array $fields
141 * Posted values of the form.
6a488035 142 *
a6c01b45
CW
143 * @return array
144 * list of errors to be posted back to the form
6a488035 145 */
00be9182 146 public static function formRule($fields) {
6a488035 147 // check for state/country mapping
ac79e2f5 148 $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject);
6a488035
TO
149
150 return empty($errors) ? TRUE : $errors;
151 }
152
153 /**
154 * function to build location block
155 *
355ba699 156 * @return void
6a488035
TO
157 */
158 public function buildQuickForm() {
159 //load form for child blocks
160 if ($this->_addBlockName) {
0e6e8724
DL
161 $className = "CRM_Contact_Form_Edit_{$this->_addBlockName}";
162 return $className::buildQuickForm($this);
6a488035
TO
163 }
164
165 $this->applyFilter('__ALL__', 'trim');
166
167 //build location blocks.
168 CRM_Contact_Form_Location::buildQuickForm($this);
169
170 //fix for CRM-1971
171 $this->assign('action', $this->_action);
172
173 if ($this->_id) {
174 $this->_oldLocBlockId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
175 $this->_id, 'loc_block_id'
176 );
177 }
178
179 // get the list of location blocks being used by other events
03e04002 180
6a488035
TO
181 $locationEvents = CRM_Event_BAO_Event::getLocationEvents();
182 // remove duplicates and make sure that the duplicate entry with key as
183 // loc_block_id of this event (this->_id) is preserved
a7488080 184 if (!empty($locationEvents[$this->_oldLocBlockId])) {
6a488035
TO
185 $possibleDuplicate = $locationEvents[$this->_oldLocBlockId];
186 $locationEvents = array_flip(array_unique($locationEvents));
a7488080 187 if (!empty($locationEvents[$possibleDuplicate])) {
6a488035
TO
188 $locationEvents[$possibleDuplicate] = $this->_oldLocBlockId;
189 }
190 $locationEvents = array_flip($locationEvents);
191 }
192 else {
193 $locationEvents = array_unique($locationEvents);
194 }
195
196 $events = array();
197 if (!empty($locationEvents)) {
198 $this->assign('locEvents', TRUE);
0479b4c8 199 $optionTypes = array(
353ffa53 200 '1' => ts('Create new location'),
6a488035
TO
201 '2' => ts('Use existing location'),
202 );
203
62a0f5a1 204 $this->addRadio('location_option', ts("Choose Location"), $optionTypes);
6a488035
TO
205
206 if (!isset($locationEvents[$this->_oldLocBlockId]) || (!$this->_oldLocBlockId)) {
207 $locationEvents = array(
353ffa53
TO
208 '' => ts('- select -')
209 ) + $locationEvents;
6a488035
TO
210 }
211 $this->add('select', 'loc_event_id', ts('Use Location'), $locationEvents);
212 }
213 $this->addElement('advcheckbox', 'is_show_location', ts('Show Location?'));
214 parent::buildQuickForm();
215 }
216
217 /**
c490a46a 218 * Process the form submission
6a488035 219 *
6a488035 220 *
355ba699 221 * @return void
6a488035
TO
222 */
223 public function postProcess() {
224 $params = $this->exportValues();
225 $deleteOldBlock = FALSE;
226
227 // if 'use existing location' option is selected -
8cc574cf 228 if (CRM_Utils_Array::value('location_option', $params) == 2 && !empty($params['loc_event_id']) &&
6a488035
TO
229 ($params['loc_event_id'] != $this->_oldLocBlockId)
230 ) {
231 // if new selected loc is different from old loc, update the loc_block_id
232 // so that loc update would affect the selected loc and not the old one.
233 $deleteOldBlock = TRUE;
234 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
235 'loc_block_id', $params['loc_event_id']
236 );
237 }
238
239 // if 'create new loc' option is selected, set the loc_block_id for this event to null
240 // so that an update would result in creating a new loc.
241 if ($this->_oldLocBlockId && (CRM_Utils_Array::value('location_option', $params) == 1)) {
242 $deleteOldBlock = TRUE;
243 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $this->_id,
244 'loc_block_id', 'null'
245 );
246 }
247
248 // if 'create new loc' optioin is selected OR selected new loc is different
249 // from old one, go ahead and delete the old loc provided thats not being
250 // used by any other event
251 if ($this->_oldLocBlockId && $deleteOldBlock) {
252 CRM_Event_BAO_Event::deleteEventLocBlock($this->_oldLocBlockId, $this->_id);
253 }
254
255 // get ready with location block params
256 $params['entity_table'] = 'civicrm_event';
257 $params['entity_id'] = $this->_id;
258
259 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
260 foreach (array(
353ffa53
TO
261 'address',
262 'phone',
263 'email'
264 ) as $block) {
a7488080 265 if (empty($params[$block]) || !is_array($params[$block])) {
6a488035
TO
266 continue;
267 }
268 foreach ($params[$block] as $count => & $values) {
269 if ($count == 1) {
270 $values['is_primary'] = 1;
271 }
272 $values['location_type_id'] = ($defaultLocationType->id) ? $defaultLocationType->id : 1;
273 }
274 }
275
276 // create/update event location
277 $location = CRM_Core_BAO_Location::create($params, TRUE, 'event');
278 $params['loc_block_id'] = $location['id'];
279
280 // finally update event params
281 $params['id'] = $this->_id;
282 CRM_Event_BAO_Event::add($params);
283
5d92a7e7
CW
284 // Update tab "disabled" css class
285 $this->ajaxResponse['tabValid'] = TRUE;
6a488035
TO
286 parent::endPostProcess();
287 }
6a488035
TO
288
289 /**
290 * Return a descriptive name for the page, used in wizard header
291 *
292 * @return string
6a488035
TO
293 */
294 public function getTitle() {
295 return ts('Event Location');
296 }
297}