Merge pull request #1810 from monishdeb/CRM-13344
[civicrm-core.git] / CRM / Contact / Form / Edit / Address.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 */
39class 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(
b2b0530a 77 '' => ts('- select -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'),
6a488035
TO
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),
2dc3fbdc
DG
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),
6a488035
TO
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 {
fe3098fd
PJ
208 if ($name == 'state_province_id') {
209 $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
210 }
6a488035
TO
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.
7b226831 275 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
6a488035
TO
276
277 $template = CRM_Core_Smarty::singleton();
278 $tplGroupTree = $template->get_template_vars('address_groupTree');
279 $tplGroupTree = empty($tplGroupTree) ? array(
280 ) : $tplGroupTree;
281
282 $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
283 // unset the temp smarty var that got created
284 $form->assign('dnc_groupTree', NULL);
285 }
286 // address custom data processing ends ..
287
288 if ($sharing) {
289 // shared address
290 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
291
292 // get the reserved for address
293 $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'shared_address', 'id', 'name');
294
295 if (!$profileId) {
296 CRM_Core_Error::fatal(ts('Your install is missing required "Shared Address" profile.'));
297 }
298
299 CRM_Contact_Form_NewContact::buildQuickForm($form, $blockId, array($profileId));
300 }
301 }
302
303 /**
304 * check for correct state / country mapping.
305 *
306 * @param array reference $fields - submitted form values.
307 * @param array reference $errors - if any errors found add to this array. please.
308 *
309 * @return true if no errors
310 * array of errors if any present.
311 *
312 * @access public
313 * @static
314 */
315 static function formRule($fields) {
316 $errors = array();
317 // check for state/county match if not report error to user.
318 if (CRM_Utils_Array::value('address', $fields) && is_array($fields['address'])) {
319 foreach ($fields['address'] as $instance => $addressValues) {
320 if (CRM_Utils_System::isNull($addressValues)) {
321 continue;
322 }
323
324 $countryId = CRM_Utils_Array::value('country_id', $addressValues);
325 $stateProvinceId = CRM_Utils_Array::value('state_province_id', $addressValues);
326
327 //do check for mismatch countries
328 if ($stateProvinceId && $countryId) {
329 $stateProvinceDAO = new CRM_Core_DAO_StateProvince();
330 $stateProvinceDAO->id = $stateProvinceId;
331 $stateProvinceDAO->find(TRUE);
332 if ($stateProvinceDAO->country_id != $countryId) {
333 // countries mismatch hence display error
334 $stateProvinces = CRM_Core_PseudoConstant::stateProvince();
335 $countries = CRM_Core_PseudoConstant::country();
336 $errors["address[$instance][state_province_id]"] = ts('State/Province %1 is not part of %2. It belongs to %3.',
337 array(
338 1 => $stateProvinces[$stateProvinceId],
339 2 => $countries[$countryId],
340 3 => $countries[$stateProvinceDAO->country_id]
341 )
342 );
343 }
344 }
345
346 $countyId = CRM_Utils_Array::value('county_id', $addressValues);
347
348 //state county validation
349 if ($stateProvinceId && $countyId) {
350 $countyDAO = new CRM_Core_DAO_County();
351 $countyDAO->id = $countyId;
352 $countyDAO->find(TRUE);
353 if ($countyDAO->state_province_id != $stateProvinceId) {
354 $counties = CRM_Core_PseudoConstant::county();
355 $errors["address[$instance][county_id]"] = ts('County %1 is not part of %2. It belongs to %3.',
356 array(
357 1 => $counties[$countyId],
358 2 => $stateProvinces[$stateProvinceId],
359 3 => $stateProvinces[$countyDAO->state_province_id]
360 )
361 );
362 }
363 }
364
365 if (CRM_Utils_Array::value('use_shared_address', $addressValues) && !CRM_Utils_Array::value('master_id', $addressValues)) {
366 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
367 }
368 }
369 }
370
371 return empty($errors) ? TRUE : $errors;
372 }
373
374 static function fixStateSelect(&$form,
375 $countryElementName,
376 $stateElementName,
377 $countyElementName,
378 $countryDefaultValue,
379 $stateDefaultValue = NULL
380 ) {
381 $countryID = $stateID = NULL;
382 if (isset($form->_elementIndex[$countryElementName])) {
383 //get the country id to load states -
384 //first check for submitted value,
385 //then check for user passed value.
386 //finally check for element default val.
387 $submittedVal = $form->getSubmitValue($countryElementName);
388 if ($submittedVal) {
389 $countryID = $submittedVal;
390 }
391 elseif ($countryDefaultValue) {
392 $countryID = $countryDefaultValue;
393 }
394 else {
395 $countryID = CRM_Utils_Array::value(0, $form->getElementValue($countryElementName));
396 }
397 }
6a488035
TO
398 $stateTitle = ts('State/Province');
399 if (isset($form->_fields[$stateElementName]['title'])) {
400 $stateTitle = $form->_fields[$stateElementName]['title'];
401 }
402
fe3098fd 403 if (isset($form->_elementIndex[$stateElementName])) {
6a488035
TO
404 $submittedValState = $form->getSubmitValue($stateElementName);
405 if ($submittedValState) {
406 $stateID = $submittedValState;
407 }
408 elseif ($stateDefaultValue) {
409 $stateID = $stateDefaultValue;
410 }
411 else {
412 $stateID = CRM_Utils_Array::value(0, $form->getElementValue($stateElementName));
413 }
fe3098fd
PJ
414 }
415 if ($countryID &&
416 isset($form->_elementIndex[$stateElementName])
417 ) {
6a488035
TO
418 $stateSelect = &$form->addElement('select',
419 $stateElementName,
420 $stateTitle,
421 array(
422 '' => ts('- select -')) +
423 CRM_Core_PseudoConstant::stateProvinceForCountry($countryID)
424 );
fe3098fd
PJ
425 }
426 if ($stateID &&
427 isset($form->_elementIndex[$stateElementName]) &&
428 isset($form->_elementIndex[$countyElementName])
429 ) {
430 $form->addElement('select',
431 $countyElementName,
432 ts('County'),
433 array(
434 '' => ts('- select -')) +
435 CRM_Core_PseudoConstant::countyForState($stateID)
436 );
437 }
6a488035 438
fe3098fd
PJ
439 // CRM-7296 freeze the select for state if address is shared with household
440 // CRM-9070 freeze the select for state if it is view only
441 if (isset($form->_fields) &&
442 CRM_Utils_Array::value($stateElementName, $form->_fields) &&
443 (CRM_Utils_Array::value('is_shared', $form->_fields[$stateElementName]) ||
444 CRM_Utils_Array::value('is_view', $form->_fields[$stateElementName]))
445 ) {
446 $stateSelect->freeze();
6a488035
TO
447 }
448 }
449
450 /**
451 * function to set default values for address block
452 *
453 * @param array $defaults defaults associated array
454 * @param object $form form object
455 *
456 * @static
457 * @access public
458 */
459 static function setDefaultValues( &$defaults, &$form ) {
460 $addressValues = array();
461 if (isset($defaults['address']) && is_array($defaults['address']) &&
462 !CRM_Utils_system::isNull($defaults['address'])
463 ) {
464
465 // start of contact shared adddress defaults
466 $sharedAddresses = array();
467 $masterAddress = array();
468
469 // get contact name of shared contact names
470 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils::getAddressShareContactNames($defaults['address']);
471
472 foreach ($defaults['address'] as $key => $addressValue) {
473 if (CRM_Utils_Array::value('master_id', $addressValue) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
474 $sharedAddresses[$key]['shared_address_display'] = array(
475 'address' => $addressValue['display'],
476 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
477 );
478 }
479 else {
480 $defaults['address'][$key]['use_shared_address'] = 0;
481 }
482
483 //check if any address is shared by any other contacts
484 $masterAddress[$key] = CRM_Core_BAO_Address::checkContactSharedAddress($addressValue['id']);
485 }
486
487 $form->assign('sharedAddresses', $sharedAddresses);
488 $form->assign('masterAddress', $masterAddress);
489 // end of shared address defaults
490
491 // start of parse address functionality
492 // build street address, CRM-5450.
493 if ($form->_parseStreetAddress) {
494 $parseFields = array('street_address', 'street_number', 'street_name', 'street_unit');
495 foreach ($defaults['address'] as $cnt => & $address) {
496 $streetAddress = NULL;
497 foreach (array(
498 'street_number', 'street_number_suffix', 'street_name', 'street_unit') as $fld) {
499 if (in_array($fld, array(
500 'street_name', 'street_unit'))) {
501 $streetAddress .= ' ';
502 }
503 $streetAddress .= CRM_Utils_Array::value($fld, $address);
504 }
505 $streetAddress = trim($streetAddress);
506 if (!empty($streetAddress)) {
507 $address['street_address'] = $streetAddress;
508 }
509 if (isset($address['street_number'])) {
510 $address['street_number'] .= CRM_Utils_Array::value('street_number_suffix', $address);
511 }
512
513 // build array for set default.
514 foreach ($parseFields as $field) {
515 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array::value($field, $address);
516 }
6a488035
TO
517 // don't load fields, use js to populate.
518 foreach (array('street_number', 'street_name', 'street_unit') as $f) {
519 if (isset($address[$f])) {
520 unset($address[$f]);
521 }
522 }
523 }
524 $form->assign('allAddressFieldValues', json_encode($addressValues));
525
526 //hack to handle show/hide address fields.
527 $parsedAddress = array();
528 if ($form->_contactId &&
529 CRM_Utils_Array::value('address', $_POST)
530 && is_array($_POST['address'])
531 ) {
532 foreach ($_POST['address'] as $cnt => $values) {
533 $showField = 'streetAddress';
534 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
535 if (CRM_Utils_Array::value($fld, $values)) {
536 $showField = 'addressElements';
537 break;
538 }
539 }
540 $parsedAddress[$cnt] = $showField;
541 }
542 }
543 $form->assign('showHideAddressFields', $parsedAddress);
544 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ? FALSE : TRUE);
545 }
546 // end of parse address functionality
547 }
548 }
549}
550