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