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