Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class is used to build address block | |
38 | */ | |
39 | class CRM_Contact_Form_Edit_Address { | |
40 | ||
41 | /** | |
100fef9d | 42 | * Build form for address input fields |
6a488035 | 43 | * |
c490a46a | 44 | * @param CRM_Core_Form $form |
6a488035 TO |
45 | * @param int $addressBlockCount - the index of the address array (if multiple addresses on a page) |
46 | * @param boolean $sharing - false, if we want to skip the address sharing features | |
47 | * @param boolean $inlineEdit true when edit used in inline edit | |
48 | * | |
355ba699 | 49 | * @return void |
6a488035 TO |
50 | * |
51 | * @access public | |
52 | * @static | |
53 | */ | |
54 | static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) { | |
55 | // passing this via the session is AWFUL. we need to fix this | |
56 | if (!$addressBlockCount) { | |
57 | $blockId = ($form->get('Address_Block_Count')) ? $form->get('Address_Block_Count') : 1; | |
58 | } | |
59 | else { | |
60 | $blockId = $addressBlockCount; | |
61 | } | |
62 | ||
63 | $config = CRM_Core_Config::singleton(); | |
64 | $countryDefault = $config->defaultContactCountry; | |
65 | ||
66 | $form->applyFilter('__ALL__', 'trim'); | |
67 | ||
68 | $js = array(); | |
0bdc50a1 | 69 | if (!$inlineEdit) { |
dd7240b1 | 70 | $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL); |
6a488035 TO |
71 | } |
72 | ||
dd7240b1 CW |
73 | //make location type required for inline edit |
74 | $form->addSelect("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') + $js, $inlineEdit); | |
6a488035 | 75 | |
0bdc50a1 CW |
76 | if (!$inlineEdit) { |
77 | $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );'); | |
6a488035 | 78 | } |
6a488035 TO |
79 | |
80 | $form->addElement( | |
81 | 'checkbox', | |
82 | "address[$blockId][is_primary]", | |
83 | ts('Primary location for this contact'), | |
84 | ts('Primary location for this contact'), | |
85 | $js | |
86 | ); | |
87 | ||
dd7240b1 CW |
88 | if (!$inlineEdit) { |
89 | $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );'); | |
6a488035 TO |
90 | } |
91 | ||
92 | $form->addElement( | |
93 | 'checkbox', | |
94 | "address[$blockId][is_billing]", | |
95 | ts('Billing location for this contact'), | |
96 | ts('Billing location for this contact'), | |
97 | $js | |
98 | ); | |
99 | ||
100 | // hidden element to store master address id | |
101 | $form->addElement('hidden', "address[$blockId][master_id]"); | |
102 | ||
103 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
104 | 'address_options', TRUE, NULL, TRUE | |
105 | ); | |
106 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address'); | |
107 | ||
108 | $elements = array( | |
109 | 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL), | |
110 | 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL), | |
2dc3fbdc DG |
111 | 'supplemental_address_1' => array(ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL), |
112 | 'supplemental_address_2' => array(ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL), | |
6a488035 TO |
113 | 'city' => array(ts('City'), $attributes['city'], NULL), |
114 | 'postal_code' => array(ts('Zip / Postal Code'), array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')), NULL), | |
115 | 'postal_code_suffix' => array(ts('Postal Code Suffix'), array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'), NULL), | |
c927c151 | 116 | 'country_id' => array(ts('Country'), $attributes['country_id'], 'country'), |
757069de | 117 | 'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL), |
c927c151 | 118 | 'county_id' => array(ts('County'), $attributes['county_id'], NULL), |
6a488035 TO |
119 | 'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 11), NULL), |
120 | 'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 11), NULL), | |
121 | 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL), | |
122 | 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL), | |
123 | 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL), | |
124 | ); | |
125 | ||
6a488035 TO |
126 | foreach ($elements as $name => $v) { |
127 | list($title, $attributes, $select) = $v; | |
128 | ||
129 | $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name; | |
a7488080 | 130 | if (empty($addressOptions[$nameWithoutID])) { |
6a488035 TO |
131 | $continue = TRUE; |
132 | if (in_array($nameWithoutID, array( | |
8cc574cf | 133 | 'street_number', 'street_name', 'street_unit')) && !empty($addressOptions['street_address_parsing'])) { |
6a488035 TO |
134 | $continue = FALSE; |
135 | } | |
136 | if ($continue) { | |
137 | continue; | |
138 | } | |
139 | } | |
140 | ||
6a488035 TO |
141 | //build normal select if country is not present in address block |
142 | if ($name == 'state_province_id' && !$addressOptions['country']) { | |
143 | $select = 'stateProvince'; | |
144 | } | |
145 | ||
146 | if (!$select) { | |
c927c151 CW |
147 | if ($name == 'state_province_id' || $name == 'county_id') { |
148 | $form->addChainSelect("address[$blockId][$name]"); | |
6a488035 TO |
149 | } |
150 | else { | |
151 | if ($name == 'address_name') { | |
152 | $name = 'name'; | |
153 | } | |
154 | ||
155 | $form->addElement('text', | |
156 | "address[$blockId][$name]", | |
157 | $title, | |
158 | $attributes | |
159 | ); | |
160 | } | |
161 | } | |
162 | else { | |
163 | $form->addElement('select', | |
164 | "address[$blockId][$name]", | |
165 | $title, | |
166 | array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select() | |
167 | ); | |
168 | } | |
169 | } | |
170 | ||
6a488035 | 171 | $entityId = NULL; |
8cc574cf | 172 | if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) { |
6a488035 TO |
173 | $entityId = $form->_values['address'][$blockId]['id']; |
174 | } | |
175 | ||
176 | // CRM-11665 geocode override option | |
177 | $geoCode = FALSE; | |
178 | if (!empty($config->geocodeMethod)) { | |
179 | $geoCode = TRUE; | |
180 | $form->addElement('checkbox', | |
181 | "address[$blockId][manual_geo_code]", | |
182 | ts('Override automatic geocoding') | |
183 | ); | |
184 | } | |
185 | $form->assign('geoCode', $geoCode); | |
186 | ||
187 | // Process any address custom data - | |
188 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', | |
189 | $form, | |
190 | $entityId | |
191 | ); | |
192 | ||
193 | if (isset($groupTree) && is_array($groupTree)) { | |
194 | // use simplified formatted groupTree | |
195 | $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form); | |
196 | ||
197 | // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]' | |
198 | foreach ($groupTree as $id => $group) { | |
199 | foreach ($group['fields'] as $fldId => $field) { | |
200 | $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name']; | |
201 | $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]"; | |
202 | } | |
203 | } | |
204 | ||
205 | $defaults = array(); | |
206 | CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults); | |
207 | ||
208 | // since we change element name for address custom data, we need to format the setdefault values | |
209 | $addressDefaults = array(); | |
210 | foreach ($defaults as $key => $val) { | |
211 | if ( empty( $val ) ) { | |
212 | continue; | |
213 | } | |
214 | ||
215 | // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array | |
216 | // this works for all types custom data | |
217 | $keyValues = explode('[', str_replace(']', '', $key)); | |
218 | $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val; | |
219 | } | |
220 | ||
221 | $form->setDefaults($addressDefaults); | |
222 | ||
223 | // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var. | |
224 | // And we can't set it to 'address_' because we want to set it in a slightly different format. | |
7b226831 | 225 | CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); |
6a488035 | 226 | |
ac79e2f5 PJ |
227 | // during contact editing : if no address is filled |
228 | // required custom data must not produce 'required' form rule error | |
229 | // more handling done in formRule func | |
230 | if (!$inlineEdit) { | |
1d961366 | 231 | CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree); |
ac79e2f5 PJ |
232 | } |
233 | ||
6a488035 TO |
234 | $template = CRM_Core_Smarty::singleton(); |
235 | $tplGroupTree = $template->get_template_vars('address_groupTree'); | |
236 | $tplGroupTree = empty($tplGroupTree) ? array( | |
237 | ) : $tplGroupTree; | |
238 | ||
239 | $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree)); | |
240 | // unset the temp smarty var that got created | |
241 | $form->assign('dnc_groupTree', NULL); | |
242 | } | |
243 | // address custom data processing ends .. | |
244 | ||
245 | if ($sharing) { | |
246 | // shared address | |
247 | $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address')); | |
248 | ||
1a90e0dd CW |
249 | // Override the default profile links to add address form |
250 | $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array('new_individual', 'new_organization', 'new_household'), 'shared_address'); | |
251 | $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks)); | |
6a488035 TO |
252 | } |
253 | } | |
254 | ||
255 | /** | |
100fef9d | 256 | * Check for correct state / country mapping. |
6a488035 | 257 | * |
dd244018 EM |
258 | * @param $fields |
259 | * @param $files | |
260 | * @param $self | |
261 | * | |
c490a46a | 262 | * @return array|bool if no errors |
6a488035 TO |
263 | * |
264 | * @access public | |
265 | * @static | |
266 | */ | |
ac79e2f5 | 267 | static function formRule($fields, $files, $self) { |
6a488035 | 268 | $errors = array(); |
ac79e2f5 PJ |
269 | |
270 | $customDataRequiredFields = array(); | |
233e6285 | 271 | if ($self && property_exists($self, '_addressRequireOmission')) { |
ac79e2f5 PJ |
272 | $customDataRequiredFields = explode(',', $self->_addressRequireOmission); |
273 | } | |
274 | ||
a7488080 | 275 | if (!empty($fields['address']) && is_array($fields['address'])) { |
6a488035 | 276 | foreach ($fields['address'] as $instance => $addressValues) { |
1d961366 | 277 | |
6a488035 | 278 | if (CRM_Utils_System::isNull($addressValues)) { |
1d961366 PJ |
279 | // DETACH 'required' form rule error to |
280 | // custom data only if address data not exists upon submission | |
281 | if (!empty($customDataRequiredFields)) { | |
282 | foreach($customDataRequiredFields as $customElementName) { | |
283 | $elementName = "address[$instance][$customElementName]"; | |
284 | if ($self->getElementError($elementName)) { | |
285 | // set element error to none | |
286 | $self->setElementError($elementName, NULL); | |
287 | } | |
288 | } | |
289 | } | |
6a488035 TO |
290 | continue; |
291 | } | |
292 | ||
1d961366 PJ |
293 | // DETACH 'required' form rule error to |
294 | // custom data only if address data not exists upon submission | |
295 | if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address::dataExists($addressValues)) { | |
296 | foreach($customDataRequiredFields as $customElementName) { | |
297 | $elementName = "address[$instance][$customElementName]"; | |
298 | if ($self->getElementError($elementName)) { | |
299 | // set element error to none | |
300 | $self->setElementError($elementName, NULL); | |
ac79e2f5 PJ |
301 | } |
302 | } | |
303 | } | |
304 | ||
8cc574cf | 305 | if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) { |
6a488035 TO |
306 | $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.'); |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | return empty($errors) ? TRUE : $errors; | |
312 | } | |
313 | ||
6a488035 | 314 | /** |
100fef9d | 315 | * Set default values for address block |
6a488035 TO |
316 | * |
317 | * @param array $defaults defaults associated array | |
c490a46a | 318 | * @param CRM_Core_Form $form form object |
6a488035 TO |
319 | * |
320 | * @static | |
321 | * @access public | |
322 | */ | |
323 | static function setDefaultValues( &$defaults, &$form ) { | |
324 | $addressValues = array(); | |
325 | if (isset($defaults['address']) && is_array($defaults['address']) && | |
395d8dc6 | 326 | !CRM_Utils_System::isNull($defaults['address']) |
6a488035 TO |
327 | ) { |
328 | ||
329 | // start of contact shared adddress defaults | |
330 | $sharedAddresses = array(); | |
331 | $masterAddress = array(); | |
332 | ||
333 | // get contact name of shared contact names | |
334 | $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils::getAddressShareContactNames($defaults['address']); | |
335 | ||
336 | foreach ($defaults['address'] as $key => $addressValue) { | |
a7488080 | 337 | if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) { |
88820b18 | 338 | $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id']; |
6a488035 TO |
339 | $sharedAddresses[$key]['shared_address_display'] = array( |
340 | 'address' => $addressValue['display'], | |
341 | 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'], | |
88820b18 CW |
342 | 'options' => CRM_Core_BAO_Address::getValues(array('entity_id' => $master_cid, 'contact_id' => $master_cid)), |
343 | 'master_id' => $addressValue['master_id'], | |
6a488035 | 344 | ); |
88820b18 | 345 | $defaults['address'][$key]['master_contact_id'] = $master_cid; |
6a488035 TO |
346 | } |
347 | else { | |
348 | $defaults['address'][$key]['use_shared_address'] = 0; | |
349 | } | |
350 | ||
351 | //check if any address is shared by any other contacts | |
352 | $masterAddress[$key] = CRM_Core_BAO_Address::checkContactSharedAddress($addressValue['id']); | |
353 | } | |
354 | ||
355 | $form->assign('sharedAddresses', $sharedAddresses); | |
356 | $form->assign('masterAddress', $masterAddress); | |
357 | // end of shared address defaults | |
358 | ||
359 | // start of parse address functionality | |
360 | // build street address, CRM-5450. | |
361 | if ($form->_parseStreetAddress) { | |
362 | $parseFields = array('street_address', 'street_number', 'street_name', 'street_unit'); | |
363 | foreach ($defaults['address'] as $cnt => & $address) { | |
364 | $streetAddress = NULL; | |
365 | foreach (array( | |
366 | 'street_number', 'street_number_suffix', 'street_name', 'street_unit') as $fld) { | |
367 | if (in_array($fld, array( | |
368 | 'street_name', 'street_unit'))) { | |
369 | $streetAddress .= ' '; | |
370 | } | |
371 | $streetAddress .= CRM_Utils_Array::value($fld, $address); | |
372 | } | |
373 | $streetAddress = trim($streetAddress); | |
374 | if (!empty($streetAddress)) { | |
375 | $address['street_address'] = $streetAddress; | |
376 | } | |
377 | if (isset($address['street_number'])) { | |
378 | $address['street_number'] .= CRM_Utils_Array::value('street_number_suffix', $address); | |
379 | } | |
380 | ||
381 | // build array for set default. | |
382 | foreach ($parseFields as $field) { | |
383 | $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array::value($field, $address); | |
384 | } | |
6a488035 TO |
385 | // don't load fields, use js to populate. |
386 | foreach (array('street_number', 'street_name', 'street_unit') as $f) { | |
387 | if (isset($address[$f])) { | |
388 | unset($address[$f]); | |
389 | } | |
390 | } | |
391 | } | |
392 | $form->assign('allAddressFieldValues', json_encode($addressValues)); | |
393 | ||
394 | //hack to handle show/hide address fields. | |
395 | $parsedAddress = array(); | |
8cc574cf | 396 | if ($form->_contactId && !empty($_POST['address']) && is_array($_POST['address']) |
6a488035 TO |
397 | ) { |
398 | foreach ($_POST['address'] as $cnt => $values) { | |
399 | $showField = 'streetAddress'; | |
400 | foreach (array('street_number', 'street_name', 'street_unit') as $fld) { | |
a7488080 | 401 | if (!empty($values[$fld])) { |
6a488035 TO |
402 | $showField = 'addressElements'; |
403 | break; | |
404 | } | |
405 | } | |
406 | $parsedAddress[$cnt] = $showField; | |
407 | } | |
408 | } | |
409 | $form->assign('showHideAddressFields', $parsedAddress); | |
410 | $form->assign('loadShowHideAddressFields', empty($parsedAddress) ? FALSE : TRUE); | |
411 | } | |
412 | // end of parse address functionality | |
413 | } | |
414 | } | |
6a488035 | 415 | |
1d961366 | 416 | |
86538308 | 417 | /** |
c490a46a | 418 | * @param CRM_Core_Form $form |
86538308 EM |
419 | * @param $groupTree |
420 | */ | |
1d961366 PJ |
421 | static function storeRequiredCustomDataInfo(&$form, $groupTree) { |
422 | if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') { | |
423 | $requireOmission = NULL; | |
424 | foreach ($groupTree as $csId => $csVal) { | |
425 | // only process Address entity fields | |
426 | if ($csVal['extends'] != 'Address') { | |
427 | continue; | |
428 | } | |
429 | ||
430 | foreach ($csVal['fields'] as $cdId => $cdVal) { | |
431 | if ($cdVal['is_required']) { | |
432 | $elementName = $cdVal['element_name']; | |
433 | if (in_array($elementName, $form->_required)) { | |
434 | // store the omitted rule for a element, to be used later on | |
435 | $requireOmission .= $cdVal['element_custom_name'] . ','; | |
436 | } | |
437 | } | |
438 | } | |
439 | } | |
440 | ||
441 | $form->_addressRequireOmission = rtrim($requireOmission, ','); | |
442 | } | |
443 | } | |
355ba699 | 444 | } |