Merge pull request #2236 from eileenmcnaughton/CRM-14008
[civicrm-core.git] / CRM / Contact / Form / Edit / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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 /**
42 * build form for address input fields
43 *
44 * @param object $form - CRM_Core_Form (or subclass)
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 *
49 * @return none
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();
69 if ( !$inlineEdit ) {
70 $js = array('onChange' => 'checkLocation( this.id );');
71 }
72
73 $form->addElement('select',
74 "address[$blockId][location_type_id]",
75 ts('Location Type'),
76 array(
77 '' => ts('- select -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'),
78 $js
79 );
80
81 if ( !$inlineEdit ) {
82 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
83 }
84 else {
85 //make location type required for inline edit
86 $form->addRule( "address[$blockId][location_type_id]", ts('%1 is a required field.', array(1 => ts('Location Type'))), 'required');
87 }
88
89 $form->addElement(
90 'checkbox',
91 "address[$blockId][is_primary]",
92 ts('Primary location for this contact'),
93 ts('Primary location for this contact'),
94 $js
95 );
96
97 if ( !$inlineEdit ) {
98 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
99 }
100
101 $form->addElement(
102 'checkbox',
103 "address[$blockId][is_billing]",
104 ts('Billing location for this contact'),
105 ts('Billing location for this contact'),
106 $js
107 );
108
109 // hidden element to store master address id
110 $form->addElement('hidden', "address[$blockId][master_id]");
111
112 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
113 'address_options', TRUE, NULL, TRUE
114 );
115 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address');
116
117 $elements = array(
118 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL),
119 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL),
120 'supplemental_address_1' => array(ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL),
121 'supplemental_address_2' => array(ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL),
122 'city' => array(ts('City'), $attributes['city'], NULL),
123 'postal_code' => array(ts('Zip / Postal Code'), array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')), NULL),
124 'postal_code_suffix' => array(ts('Postal Code Suffix'), array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'), NULL),
125 'county_id' => array(ts('County'), $attributes['county_id'], NULL),
126 'state_province_id' => array(ts('State / Province'), $attributes['state_province_id'], NULL),
127 'country_id' => array(ts('Country'), $attributes['country_id'], NULL),
128 'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 11), NULL),
129 'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 11), NULL),
130 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL),
131 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL),
132 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL),
133 );
134
135 $stateCountryMap = array();
136 foreach ($elements as $name => $v) {
137 list($title, $attributes, $select) = $v;
138
139 $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name;
140 if (!CRM_Utils_Array::value($nameWithoutID, $addressOptions)) {
141 $continue = TRUE;
142 if (in_array($nameWithoutID, array(
143 'street_number', 'street_name', 'street_unit')) &&
144 CRM_Utils_Array::value('street_address_parsing', $addressOptions)
145 ) {
146 $continue = FALSE;
147 }
148 if ($continue) {
149 continue;
150 }
151 }
152
153 if (!$attributes) {
154 $attributes = $attributes[$name];
155 }
156
157 //build normal select if country is not present in address block
158 if ($name == 'state_province_id' && !$addressOptions['country']) {
159 $select = 'stateProvince';
160 }
161
162 if (!$select) {
163 if ($name == 'country_id' || $name == 'state_province_id' || $name == 'county_id') {
164 if ($name == 'country_id') {
165 $stateCountryMap[$blockId]['country'] = "address_{$blockId}_{$name}";
166 $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country();
167 }
168 elseif ($name == 'state_province_id') {
169 $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
170 if ($countryDefault) {
171 $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($countryDefault);
172 }
173 else {
174 $selectOptions = array('' => ts('- select a country -'));
175 }
176 }
177 elseif ($name == 'county_id') {
178 $stateCountryMap[$blockId]['county'] = "address_{$blockId}_{$name}";
179 if ($form->getSubmitValue("address[{$blockId}][state_province_id]")) {
180 $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::countyForState($form->getSubmitValue("address[{$blockId}][state_province_id]"));
181 }
182 elseif ($form->getSubmitValue("address[{$blockId}][county_id]")) {
183 $selectOptions = array('' => ts('- select a state -')) + CRM_Core_PseudoConstant::county();
184 }
185 else {
186 $selectOptions = array('' => ts('- select a state -'));
187 }
188 }
189 $form->addElement('select',
190 "address[$blockId][$name]",
191 $title,
192 $selectOptions
193 );
194 }
195 else {
196 if ($name == 'address_name') {
197 $name = 'name';
198 }
199
200 $form->addElement('text',
201 "address[$blockId][$name]",
202 $title,
203 $attributes
204 );
205 }
206 }
207 else {
208 if ($name == 'state_province_id') {
209 $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
210 }
211 $form->addElement('select',
212 "address[$blockId][$name]",
213 $title,
214 array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select()
215 );
216 }
217 }
218
219 CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
220
221 $entityId = NULL;
222 if (!empty($form->_values['address']) && CRM_Utils_Array::value($blockId, $form->_values['address'])) {
223 $entityId = $form->_values['address'][$blockId]['id'];
224 }
225
226 // CRM-11665 geocode override option
227 $geoCode = FALSE;
228 if (!empty($config->geocodeMethod)) {
229 $geoCode = TRUE;
230 $form->addElement('checkbox',
231 "address[$blockId][manual_geo_code]",
232 ts('Override automatic geocoding')
233 );
234 }
235 $form->assign('geoCode', $geoCode);
236
237 // Process any address custom data -
238 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address',
239 $form,
240 $entityId
241 );
242
243 if (isset($groupTree) && is_array($groupTree)) {
244 // use simplified formatted groupTree
245 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
246
247 // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
248 foreach ($groupTree as $id => $group) {
249 foreach ($group['fields'] as $fldId => $field) {
250 $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
251 $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]";
252 }
253 }
254
255 $defaults = array();
256 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
257
258 // since we change element name for address custom data, we need to format the setdefault values
259 $addressDefaults = array();
260 foreach ($defaults as $key => $val) {
261 if ( empty( $val ) ) {
262 continue;
263 }
264
265 // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
266 // this works for all types custom data
267 $keyValues = explode('[', str_replace(']', '', $key));
268 $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
269 }
270
271 $form->setDefaults($addressDefaults);
272
273 // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
274 // And we can't set it to 'address_' because we want to set it in a slightly different format.
275 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
276
277 // during contact editing : if no address is filled
278 // required custom data must not produce 'required' form rule error
279 // more handling done in formRule func
280 if (!$inlineEdit) {
281 CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree);
282 }
283
284 $template = CRM_Core_Smarty::singleton();
285 $tplGroupTree = $template->get_template_vars('address_groupTree');
286 $tplGroupTree = empty($tplGroupTree) ? array(
287 ) : $tplGroupTree;
288
289 $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
290 // unset the temp smarty var that got created
291 $form->assign('dnc_groupTree', NULL);
292 }
293 // address custom data processing ends ..
294
295 if ($sharing) {
296 // shared address
297 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
298
299 // get the reserved for address
300 $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'shared_address', 'id', 'name');
301
302 if (!$profileId) {
303 CRM_Core_Error::fatal(ts('Your install is missing required "Shared Address" profile.'));
304 }
305
306 CRM_Contact_Form_NewContact::buildQuickForm($form, $blockId, array($profileId));
307 }
308 }
309
310 /**
311 * check for correct state / country mapping.
312 *
313 * @param array reference $fields - submitted form values.
314 * @param array reference $errors - if any errors found add to this array. please.
315 *
316 * @return true if no errors
317 * array of errors if any present.
318 *
319 * @access public
320 * @static
321 */
322 static function formRule($fields, $files, $self) {
323 $errors = array();
324
325 $customDataRequiredFields = array();
326 if ($self && property_exists($self, '_addressRequireOmission')) {
327 $customDataRequiredFields = explode(',', $self->_addressRequireOmission);
328 }
329
330 // check for state/county match if not report error to user.
331 if (CRM_Utils_Array::value('address', $fields) && is_array($fields['address'])) {
332 foreach ($fields['address'] as $instance => $addressValues) {
333
334 if (CRM_Utils_System::isNull($addressValues)) {
335 // DETACH 'required' form rule error to
336 // custom data only if address data not exists upon submission
337 if (!empty($customDataRequiredFields)) {
338 foreach($customDataRequiredFields as $customElementName) {
339 $elementName = "address[$instance][$customElementName]";
340 if ($self->getElementError($elementName)) {
341 // set element error to none
342 $self->setElementError($elementName, NULL);
343 }
344 }
345 }
346 continue;
347 }
348
349 // DETACH 'required' form rule error to
350 // custom data only if address data not exists upon submission
351 if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address::dataExists($addressValues)) {
352 foreach($customDataRequiredFields as $customElementName) {
353 $elementName = "address[$instance][$customElementName]";
354 if ($self->getElementError($elementName)) {
355 // set element error to none
356 $self->setElementError($elementName, NULL);
357 }
358 }
359 }
360
361 $countryId = CRM_Utils_Array::value('country_id', $addressValues);
362
363 $stateProvinceId = CRM_Utils_Array::value('state_province_id', $addressValues);
364
365 //do check for mismatch countries
366 if ($stateProvinceId && $countryId) {
367 $stateProvinceDAO = new CRM_Core_DAO_StateProvince();
368 $stateProvinceDAO->id = $stateProvinceId;
369 $stateProvinceDAO->find(TRUE);
370 if ($stateProvinceDAO->country_id != $countryId) {
371 // countries mismatch hence display error
372 $stateProvinces = CRM_Core_PseudoConstant::stateProvince();
373 $countries = CRM_Core_PseudoConstant::country();
374 $errors["address[$instance][state_province_id]"] = ts('State/Province %1 is not part of %2. It belongs to %3.',
375 array(
376 1 => $stateProvinces[$stateProvinceId],
377 2 => $countries[$countryId],
378 3 => $countries[$stateProvinceDAO->country_id]
379 )
380 );
381 }
382 }
383
384 $countyId = CRM_Utils_Array::value('county_id', $addressValues);
385
386 //state county validation
387 if ($stateProvinceId && $countyId) {
388 $countyDAO = new CRM_Core_DAO_County();
389 $countyDAO->id = $countyId;
390 $countyDAO->find(TRUE);
391 if ($countyDAO->state_province_id != $stateProvinceId) {
392 $counties = CRM_Core_PseudoConstant::county();
393 $errors["address[$instance][county_id]"] = ts('County %1 is not part of %2. It belongs to %3.',
394 array(
395 1 => $counties[$countyId],
396 2 => $stateProvinces[$stateProvinceId],
397 3 => $stateProvinces[$countyDAO->state_province_id]
398 )
399 );
400 }
401 }
402
403 if (CRM_Utils_Array::value('use_shared_address', $addressValues) && !CRM_Utils_Array::value('master_id', $addressValues)) {
404 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
405 }
406 }
407 }
408
409 return empty($errors) ? TRUE : $errors;
410 }
411
412 static function fixStateSelect(&$form,
413 $countryElementName,
414 $stateElementName,
415 $countyElementName,
416 $countryDefaultValue,
417 $stateDefaultValue = NULL
418 ) {
419 $countryID = $stateID = NULL;
420 if (isset($form->_elementIndex[$countryElementName])) {
421 //get the country id to load states -
422 //first check for submitted value,
423 //then check for user passed value.
424 //finally check for element default val.
425 $submittedVal = $form->getSubmitValue($countryElementName);
426 if ($submittedVal) {
427 $countryID = $submittedVal;
428 }
429 elseif ($countryDefaultValue) {
430 $countryID = $countryDefaultValue;
431 }
432 else {
433 $countryID = CRM_Utils_Array::value(0, $form->getElementValue($countryElementName));
434 }
435 }
436 $stateTitle = ts('State/Province');
437 if (isset($form->_fields[$stateElementName]['title'])) {
438 $stateTitle = $form->_fields[$stateElementName]['title'];
439 }
440
441 if (isset($form->_elementIndex[$stateElementName])) {
442 $submittedValState = $form->getSubmitValue($stateElementName);
443 if ($submittedValState) {
444 $stateID = $submittedValState;
445 }
446 elseif ($stateDefaultValue) {
447 $stateID = $stateDefaultValue;
448 }
449 else {
450 $stateID = CRM_Utils_Array::value(0, $form->getElementValue($stateElementName));
451 }
452 }
453
454 if (isset($form->_elementIndex[$stateElementName])) {
455 if ($countryID) {
456 $stateProvinces = CRM_Core_PseudoConstant::stateProvinceForCountry($countryID);
457 }
458 else {
459 $stateProvinces = CRM_Core_PseudoConstant::stateProvince();
460 }
461
462 $stateSelect = & $form->addElement('select', $stateElementName, $stateTitle,
463 array('' => ts('- select -')) + $stateProvinces);
464 }
465
466 if (isset($form->_elementIndex[$stateElementName]) && isset($form->_elementIndex[$countyElementName])) {
467 if ($stateID) {
468 $counties = CRM_Core_PseudoConstant::countyForState($stateID);
469 }
470 else {
471 $counties = CRM_Core_PseudoConstant::country();
472 }
473
474 $form->addElement('select', $countyElementName, ts('County'), array('' => ts('- select -')) + $counties);
475 }
476
477 // CRM-7296 freeze the select for state if address is shared with household
478 // CRM-9070 freeze the select for state if it is view only
479 if (isset($form->_fields) &&
480 CRM_Utils_Array::value($stateElementName, $form->_fields) &&
481 (CRM_Utils_Array::value('is_shared', $form->_fields[$stateElementName]) ||
482 CRM_Utils_Array::value('is_view', $form->_fields[$stateElementName]))
483 ) {
484 $stateSelect->freeze();
485 }
486 }
487
488 /**
489 * function to set default values for address block
490 *
491 * @param array $defaults defaults associated array
492 * @param object $form form object
493 *
494 * @static
495 * @access public
496 */
497 static function setDefaultValues( &$defaults, &$form ) {
498 $addressValues = array();
499 if (isset($defaults['address']) && is_array($defaults['address']) &&
500 !CRM_Utils_System::isNull($defaults['address'])
501 ) {
502
503 // start of contact shared adddress defaults
504 $sharedAddresses = array();
505 $masterAddress = array();
506
507 // get contact name of shared contact names
508 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils::getAddressShareContactNames($defaults['address']);
509
510 foreach ($defaults['address'] as $key => $addressValue) {
511 if (CRM_Utils_Array::value('master_id', $addressValue) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
512 $sharedAddresses[$key]['shared_address_display'] = array(
513 'address' => $addressValue['display'],
514 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
515 );
516 }
517 else {
518 $defaults['address'][$key]['use_shared_address'] = 0;
519 }
520
521 //check if any address is shared by any other contacts
522 $masterAddress[$key] = CRM_Core_BAO_Address::checkContactSharedAddress($addressValue['id']);
523 }
524
525 $form->assign('sharedAddresses', $sharedAddresses);
526 $form->assign('masterAddress', $masterAddress);
527 // end of shared address defaults
528
529 // start of parse address functionality
530 // build street address, CRM-5450.
531 if ($form->_parseStreetAddress) {
532 $parseFields = array('street_address', 'street_number', 'street_name', 'street_unit');
533 foreach ($defaults['address'] as $cnt => & $address) {
534 $streetAddress = NULL;
535 foreach (array(
536 'street_number', 'street_number_suffix', 'street_name', 'street_unit') as $fld) {
537 if (in_array($fld, array(
538 'street_name', 'street_unit'))) {
539 $streetAddress .= ' ';
540 }
541 $streetAddress .= CRM_Utils_Array::value($fld, $address);
542 }
543 $streetAddress = trim($streetAddress);
544 if (!empty($streetAddress)) {
545 $address['street_address'] = $streetAddress;
546 }
547 if (isset($address['street_number'])) {
548 $address['street_number'] .= CRM_Utils_Array::value('street_number_suffix', $address);
549 }
550
551 // build array for set default.
552 foreach ($parseFields as $field) {
553 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array::value($field, $address);
554 }
555 // don't load fields, use js to populate.
556 foreach (array('street_number', 'street_name', 'street_unit') as $f) {
557 if (isset($address[$f])) {
558 unset($address[$f]);
559 }
560 }
561 }
562 $form->assign('allAddressFieldValues', json_encode($addressValues));
563
564 //hack to handle show/hide address fields.
565 $parsedAddress = array();
566 if ($form->_contactId &&
567 CRM_Utils_Array::value('address', $_POST)
568 && is_array($_POST['address'])
569 ) {
570 foreach ($_POST['address'] as $cnt => $values) {
571 $showField = 'streetAddress';
572 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
573 if (CRM_Utils_Array::value($fld, $values)) {
574 $showField = 'addressElements';
575 break;
576 }
577 }
578 $parsedAddress[$cnt] = $showField;
579 }
580 }
581 $form->assign('showHideAddressFields', $parsedAddress);
582 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ? FALSE : TRUE);
583 }
584 // end of parse address functionality
585 }
586 }
587
588
589 static function storeRequiredCustomDataInfo(&$form, $groupTree) {
590 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
591 $requireOmission = NULL;
592 foreach ($groupTree as $csId => $csVal) {
593 // only process Address entity fields
594 if ($csVal['extends'] != 'Address') {
595 continue;
596 }
597
598 foreach ($csVal['fields'] as $cdId => $cdVal) {
599 if ($cdVal['is_required']) {
600 $elementName = $cdVal['element_name'];
601 if (in_array($elementName, $form->_required)) {
602 // store the omitted rule for a element, to be used later on
603 $requireOmission .= $cdVal['element_custom_name'] . ',';
604 }
605 }
606 }
607 }
608
609 $form->_addressRequireOmission = rtrim($requireOmission, ',');
610 }
611 }
612 }