3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * This class generates form components for processing Event Location
41 class CRM_Event_Form_ManageEvent_Location
extends CRM_Event_Form_ManageEvent
{
44 * how many locationBlocks should we display?
49 CONST LOCATION_BLOCKS
= 1;
52 * the variable, for storing the location array
56 protected $_locationIds = array();
59 * the variable, for storing location block id with event
63 protected $_oldLocBlockId = 0;
66 * get the db values for this form
69 public $_values = array();
72 * Function to set variables up before form is built
77 function preProcess() {
80 $this->_values
= $this->get('values');
81 if ($this->_id
&& empty($this->_values
)) {
82 //get location values.
84 'entity_id' => $this->_id
,
85 'entity_table' => 'civicrm_event',
87 $this->_values
= CRM_Core_BAO_Location
::getValues($params);
90 $params = array('id' => $this->_id
);
91 CRM_Event_BAO_Event
::retrieve($params, $this->_values
);
92 $this->set('values', $this->_values
);
96 CRM_Contact_Form_Location
::preProcess($this);
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
107 function setDefaultValues() {
108 $defaults = $this->_values
;
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 if ($countLocUsed > 1) {
114 $this->assign('locUsed', TRUE);
118 $config = CRM_Core_Config
::singleton();
119 if (!isset($defaults['address'][1]['country_id'])) {
120 $defaults['address'][1]['country_id'] = $config->defaultContactCountry
;
123 if (!isset($defaults['address'][1]['state_province_id'])) {
124 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince
;
127 if (!empty($defaults['address'])) {
128 foreach ($defaults['address'] as $key => $value) {
129 CRM_Contact_Form_Edit_Address
::fixStateSelect($this,
130 "address[$key][country_id]",
131 "address[$key][state_province_id]",
132 "address[$key][county_id]",
133 CRM_Utils_Array
::value('country_id', $value,
134 $config->defaultContactCountry
136 CRM_Utils_Array
::value('state_province_id', $value)
140 $defaults['location_option'] = $this->_oldLocBlockId ?
2 : 1;
146 * Add local and global form rules
152 function addRules() {
153 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Location', 'formRule'));
157 * global validation rules for the form
159 * @param array $fields posted values of the form
161 * @return array list of errors to be posted back to the form
165 static function formRule($fields) {
166 // check for state/country mapping
167 $errors = CRM_Contact_Form_Edit_Address
::formRule($fields, CRM_Core_DAO
::$_nullArray, CRM_Core_DAO
::$_nullObject);
169 return empty($errors) ?
TRUE : $errors;
173 * function to build location block
178 public function buildQuickForm() {
179 //load form for child blocks
180 if ($this->_addBlockName
) {
181 $className = "CRM_Contact_Form_Edit_{$this->_addBlockName}";
182 return $className::buildQuickForm($this);
185 $this->applyFilter('__ALL__', 'trim');
187 //build location blocks.
188 CRM_Contact_Form_Location
::buildQuickForm($this);
191 $this->assign('action', $this->_action
);
194 $this->_oldLocBlockId
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event',
195 $this->_id
, 'loc_block_id'
199 // get the list of location blocks being used by other events
201 $locationEvents = CRM_Event_BAO_Event
::getLocationEvents();
202 // remove duplicates and make sure that the duplicate entry with key as
203 // loc_block_id of this event (this->_id) is preserved
204 if (!empty($locationEvents[$this->_oldLocBlockId
])) {
205 $possibleDuplicate = $locationEvents[$this->_oldLocBlockId
];
206 $locationEvents = array_flip(array_unique($locationEvents));
207 if (!empty($locationEvents[$possibleDuplicate])) {
208 $locationEvents[$possibleDuplicate] = $this->_oldLocBlockId
;
210 $locationEvents = array_flip($locationEvents);
213 $locationEvents = array_unique($locationEvents);
217 if (!empty($locationEvents)) {
218 $this->assign('locEvents', TRUE);
219 $optionTypes = array('1' => ts('Create new location'),
220 '2' => ts('Use existing location'),
223 $this->addRadio('location_option', ts("Choose Location"), $optionTypes,
225 'onclick' => "showLocFields();"), '<br/>', FALSE
228 if (!isset($locationEvents[$this->_oldLocBlockId
]) ||
(!$this->_oldLocBlockId
)) {
229 $locationEvents = array(
230 '' => ts('- select -')) +
$locationEvents;
232 $this->add('select', 'loc_event_id', ts('Use Location'), $locationEvents);
234 $this->addElement('advcheckbox', 'is_show_location', ts('Show Location?'));
235 parent
::buildQuickForm();
239 * Function to process the form
245 public function postProcess() {
246 $params = $this->exportValues();
247 $deleteOldBlock = FALSE;
249 // if 'use existing location' option is selected -
250 if (CRM_Utils_Array
::value('location_option', $params) == 2 && !empty($params['loc_event_id']) &&
251 ($params['loc_event_id'] != $this->_oldLocBlockId
)
253 // if new selected loc is different from old loc, update the loc_block_id
254 // so that loc update would affect the selected loc and not the old one.
255 $deleteOldBlock = TRUE;
256 CRM_Core_DAO
::setFieldValue('CRM_Event_DAO_Event', $this->_id
,
257 'loc_block_id', $params['loc_event_id']
261 // if 'create new loc' option is selected, set the loc_block_id for this event to null
262 // so that an update would result in creating a new loc.
263 if ($this->_oldLocBlockId
&& (CRM_Utils_Array
::value('location_option', $params) == 1)) {
264 $deleteOldBlock = TRUE;
265 CRM_Core_DAO
::setFieldValue('CRM_Event_DAO_Event', $this->_id
,
266 'loc_block_id', 'null'
270 // if 'create new loc' optioin is selected OR selected new loc is different
271 // from old one, go ahead and delete the old loc provided thats not being
272 // used by any other event
273 if ($this->_oldLocBlockId
&& $deleteOldBlock) {
274 CRM_Event_BAO_Event
::deleteEventLocBlock($this->_oldLocBlockId
, $this->_id
);
277 // get ready with location block params
278 $params['entity_table'] = 'civicrm_event';
279 $params['entity_id'] = $this->_id
;
281 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
283 'address', 'phone', 'email') as $block) {
284 if (empty($params[$block]) ||
!is_array($params[$block])) {
287 foreach ($params[$block] as $count => & $values) {
289 $values['is_primary'] = 1;
291 $values['location_type_id'] = ($defaultLocationType->id
) ?
$defaultLocationType->id
: 1;
295 // create/update event location
296 $location = CRM_Core_BAO_Location
::create($params, TRUE, 'event');
297 $params['loc_block_id'] = $location['id'];
299 // finally update event params
300 $params['id'] = $this->_id
;
301 CRM_Event_BAO_Event
::add($params);
303 // Update tab "disabled" css class
304 $this->ajaxResponse
['tabValid'] = TRUE;
305 parent
::endPostProcess();
310 * Return a descriptive name for the page, used in wizard header
315 public function getTitle() {
316 return ts('Event Location');