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