3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 * This class generates form components for processing Event Location
25 class CRM_Event_Form_ManageEvent_Location
extends CRM_Event_Form_ManageEvent
{
28 * How many locationBlocks should we display?
33 const LOCATION_BLOCKS
= 1;
36 * The variable, for storing the location array
40 protected $_locationIds = [];
43 * The variable, for storing location block id with event
47 protected $_oldLocBlockId = 0;
50 * Get the db values for this form.
56 * Set variables up before form is built.
58 public function preProcess() {
60 $this->setSelectedChild('location');
62 $this->_values
= $this->get('values');
63 if ($this->_id
&& empty($this->_values
)) {
64 //get location values.
66 'entity_id' => $this->_id
,
67 'entity_table' => 'civicrm_event',
69 $this->_values
= CRM_Core_BAO_Location
::getValues($params);
72 $params = ['id' => $this->_id
];
73 CRM_Event_BAO_Event
::retrieve($params, $this->_values
);
74 $this->set('values', $this->_values
);
78 CRM_Contact_Form_Location
::preProcess($this);
82 * Set default values for the form.
84 * Note that in edit/view mode the default values are retrieved from the database.
86 public function setDefaultValues() {
87 $defaults = $this->_values
;
89 if (!empty($defaults['loc_block_id'])) {
90 $defaults['loc_event_id'] = $defaults['loc_block_id'];
91 $countLocUsed = CRM_Event_BAO_Event
::countEventsUsingLocBlockId($defaults['loc_block_id']);
92 $this->assign('locUsed', $countLocUsed);
95 $config = CRM_Core_Config
::singleton();
96 if (!isset($defaults['address'][1]['country_id'])) {
97 $defaults['address'][1]['country_id'] = $config->defaultContactCountry
;
100 if (!isset($defaults['address'][1]['state_province_id'])) {
101 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince
;
104 $defaults['location_option'] = $this->_oldLocBlockId ?
2 : 1;
110 * Add local and global form rules.
112 public function addRules() {
113 $this->addFormRule(['CRM_Event_Form_ManageEvent_Location', 'formRule']);
117 * Global validation rules for the form.
119 * @param array $fields
120 * Posted values of the form.
123 * list of errors to be posted back to the form
125 public static function formRule($fields) {
126 // check for state/country mapping
127 $errors = CRM_Contact_Form_Edit_Address
::formRule($fields);
129 return empty($errors) ?
TRUE : $errors;
133 * Function to build location block.
135 public function buildQuickForm() {
136 CRM_Contact_Form_Edit_Address
::buildQuickForm($this, 1);
137 CRM_Contact_Form_Edit_Email
::buildQuickForm($this, 1);
138 CRM_Contact_Form_Edit_Email
::buildQuickForm($this, 2);
139 CRM_Contact_Form_Edit_Phone
::buildQuickForm($this, 1);
140 CRM_Contact_Form_Edit_Phone
::buildQuickForm($this, 2);
142 $this->applyFilter('__ALL__', 'trim');
145 $this->assign('action', $this->_action
);
148 $this->_oldLocBlockId
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event',
149 $this->_id
, 'loc_block_id'
153 // get the list of location blocks being used by other events
155 $locationEvents = CRM_Event_BAO_Event
::getLocationEvents();
156 // remove duplicates and make sure that the duplicate entry with key as
157 // loc_block_id of this event (this->_id) is preserved
158 if (!empty($locationEvents[$this->_oldLocBlockId
])) {
159 $possibleDuplicate = $locationEvents[$this->_oldLocBlockId
];
160 $locationEvents = array_flip(array_unique($locationEvents));
161 if (!empty($locationEvents[$possibleDuplicate])) {
162 $locationEvents[$possibleDuplicate] = $this->_oldLocBlockId
;
164 $locationEvents = array_flip($locationEvents);
167 $locationEvents = array_unique($locationEvents);
170 if (!empty($locationEvents)) {
171 $this->assign('locEvents', TRUE);
173 '1' => ts('Create new location'),
174 '2' => ts('Use existing location'),
177 $this->addRadio('location_option', ts("Choose Location"), $optionTypes);
179 if (!isset($locationEvents[$this->_oldLocBlockId
]) ||
(!$this->_oldLocBlockId
)) {
180 $locationEvents = ['' => ts('- select -')] +
$locationEvents;
182 $this->add('select', 'loc_event_id', ts('Use Location'), $locationEvents, FALSE, ['class' => 'crm-select2']);
184 $this->addElement('advcheckbox', 'is_show_location', ts('Show Location?'));
185 parent
::buildQuickForm();
189 * Process the form submission.
191 public function postProcess() {
192 $params = $this->exportValues();
193 $deleteOldBlock = FALSE;
195 // if 'use existing location' option is selected -
196 if (CRM_Utils_Array
::value('location_option', $params) == 2 && !empty($params['loc_event_id']) &&
197 ($params['loc_event_id'] != $this->_oldLocBlockId
)
199 // if new selected loc is different from old loc, update the loc_block_id
200 // so that loc update would affect the selected loc and not the old one.
201 $deleteOldBlock = TRUE;
202 CRM_Core_DAO
::setFieldValue('CRM_Event_DAO_Event', $this->_id
,
203 'loc_block_id', $params['loc_event_id']
207 // if 'create new loc' option is selected, set the loc_block_id for this event to null
208 // so that an update would result in creating a new loc.
209 if ($this->_oldLocBlockId
&& (CRM_Utils_Array
::value('location_option', $params) == 1)) {
210 $deleteOldBlock = TRUE;
211 CRM_Core_DAO
::setFieldValue('CRM_Event_DAO_Event', $this->_id
,
212 'loc_block_id', 'null'
216 $this->_values
['address'] = $this->_values
['phone'] = $this->_values
['email'] = [];
217 // if 'create new loc' option is selected OR selected new loc is different
218 // from old one, go ahead and delete the old loc provided thats not being
219 // used by any other event
220 if ($this->_oldLocBlockId
&& $deleteOldBlock) {
221 CRM_Event_BAO_Event
::deleteEventLocBlock($this->_oldLocBlockId
, $this->_id
);
224 // get ready with location block params
225 $params['entity_table'] = 'civicrm_event';
226 $params['entity_id'] = $this->_id
;
228 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
234 if (empty($params[$block]) ||
!is_array($params[$block])) {
237 foreach ($params[$block] as $count => & $values) {
239 $values['is_primary'] = 1;
241 $values['location_type_id'] = ($defaultLocationType->id
) ?
$defaultLocationType->id
: 1;
242 if (isset($this->_values
[$block][$count])) {
243 $values['id'] = $this->_values
[$block][$count]['id'];
248 // create/update event location
249 $params['loc_block_id'] = CRM_Core_BAO_Location
::create($params, TRUE, 'event')['id'];
251 // finally update event params
252 $params['id'] = $this->_id
;
253 CRM_Event_BAO_Event
::add($params);
255 // Update tab "disabled" css class
256 $this->ajaxResponse
['tabValid'] = TRUE;
257 parent
::endPostProcess();
261 * Return a descriptive name for the page, used in wizard header
265 public function getTitle() {
266 return ts('Event Location');