Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Contact / Form / Edit / Address.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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(
77 '' => ts('- select -')) + CRM_Core_PseudoConstant::locationType(),
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('Addt\'l Address 1'), $attributes['supplemental_address_1'], NULL),
121 'supplemental_address_2' => array(ts('Addt\'l 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 $form->addElement('select',
209 "address[$blockId][$name]",
210 $title,
211 array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select()
212 );
213 }
214 }
215
216 CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
217
218 $entityId = NULL;
219 if (!empty($form->_values['address']) && CRM_Utils_Array::value($blockId, $form->_values['address'])) {
220 $entityId = $form->_values['address'][$blockId]['id'];
221 }
222
223 // CRM-11665 geocode override option
224 $geoCode = FALSE;
225 if (!empty($config->geocodeMethod)) {
226 $geoCode = TRUE;
227 $form->addElement('checkbox',
228 "address[$blockId][manual_geo_code]",
229 ts('Override automatic geocoding')
230 );
231 }
232 $form->assign('geoCode', $geoCode);
233
234 // Process any address custom data -
235 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address',
236 $form,
237 $entityId
238 );
239
240 if (isset($groupTree) && is_array($groupTree)) {
241 // use simplified formatted groupTree
242 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
243
244 // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
245 foreach ($groupTree as $id => $group) {
246 foreach ($group['fields'] as $fldId => $field) {
247 $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
248 $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]";
249 }
250 }
251
252 $defaults = array();
253 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
254
255 // since we change element name for address custom data, we need to format the setdefault values
256 $addressDefaults = array();
257 foreach ($defaults as $key => $val) {
258 if ( empty( $val ) ) {
259 continue;
260 }
261
262 // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
263 // this works for all types custom data
264 $keyValues = explode('[', str_replace(']', '', $key));
265 $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
266 }
267
268 $form->setDefaults($addressDefaults);
269
270 // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
271 // And we can't set it to 'address_' because we want to set it in a slightly different format.
272 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 1, 'dnc_');
273
274 $template = CRM_Core_Smarty::singleton();
275 $tplGroupTree = $template->get_template_vars('address_groupTree');
276 $tplGroupTree = empty($tplGroupTree) ? array(
277 ) : $tplGroupTree;
278
279 $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
280 // unset the temp smarty var that got created
281 $form->assign('dnc_groupTree', NULL);
282 }
283 // address custom data processing ends ..
284
285 if ($sharing) {
286 // shared address
287 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
288
289 // get the reserved for address
290 $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'shared_address', 'id', 'name');
291
292 if (!$profileId) {
293 CRM_Core_Error::fatal(ts('Your install is missing required "Shared Address" profile.'));
294 }
295
296 CRM_Contact_Form_NewContact::buildQuickForm($form, $blockId, array($profileId));
297 }
298 }
299
300 /**
301 * check for correct state / country mapping.
302 *
303 * @param array reference $fields - submitted form values.
304 * @param array reference $errors - if any errors found add to this array. please.
305 *
306 * @return true if no errors
307 * array of errors if any present.
308 *
309 * @access public
310 * @static
311 */
312 static function formRule($fields) {
313 $errors = array();
314 // check for state/county match if not report error to user.
315 if (CRM_Utils_Array::value('address', $fields) && is_array($fields['address'])) {
316 foreach ($fields['address'] as $instance => $addressValues) {
317 if (CRM_Utils_System::isNull($addressValues)) {
318 continue;
319 }
320
321 $countryId = CRM_Utils_Array::value('country_id', $addressValues);
322 $stateProvinceId = CRM_Utils_Array::value('state_province_id', $addressValues);
323
324 //do check for mismatch countries
325 if ($stateProvinceId && $countryId) {
326 $stateProvinceDAO = new CRM_Core_DAO_StateProvince();
327 $stateProvinceDAO->id = $stateProvinceId;
328 $stateProvinceDAO->find(TRUE);
329 if ($stateProvinceDAO->country_id != $countryId) {
330 // countries mismatch hence display error
331 $stateProvinces = CRM_Core_PseudoConstant::stateProvince();
332 $countries = CRM_Core_PseudoConstant::country();
333 $errors["address[$instance][state_province_id]"] = ts('State/Province %1 is not part of %2. It belongs to %3.',
334 array(
335 1 => $stateProvinces[$stateProvinceId],
336 2 => $countries[$countryId],
337 3 => $countries[$stateProvinceDAO->country_id]
338 )
339 );
340 }
341 }
342
343 $countyId = CRM_Utils_Array::value('county_id', $addressValues);
344
345 //state county validation
346 if ($stateProvinceId && $countyId) {
347 $countyDAO = new CRM_Core_DAO_County();
348 $countyDAO->id = $countyId;
349 $countyDAO->find(TRUE);
350 if ($countyDAO->state_province_id != $stateProvinceId) {
351 $counties = CRM_Core_PseudoConstant::county();
352 $errors["address[$instance][county_id]"] = ts('County %1 is not part of %2. It belongs to %3.',
353 array(
354 1 => $counties[$countyId],
355 2 => $stateProvinces[$stateProvinceId],
356 3 => $stateProvinces[$countyDAO->state_province_id]
357 )
358 );
359 }
360 }
361
362 if (CRM_Utils_Array::value('use_shared_address', $addressValues) && !CRM_Utils_Array::value('master_id', $addressValues)) {
363 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
364 }
365 }
366 }
367
368 return empty($errors) ? TRUE : $errors;
369 }
370
371 static function fixStateSelect(&$form,
372 $countryElementName,
373 $stateElementName,
374 $countyElementName,
375 $countryDefaultValue,
376 $stateDefaultValue = NULL
377 ) {
378 $countryID = $stateID = NULL;
379 if (isset($form->_elementIndex[$countryElementName])) {
380 //get the country id to load states -
381 //first check for submitted value,
382 //then check for user passed value.
383 //finally check for element default val.
384 $submittedVal = $form->getSubmitValue($countryElementName);
385 if ($submittedVal) {
386 $countryID = $submittedVal;
387 }
388 elseif ($countryDefaultValue) {
389 $countryID = $countryDefaultValue;
390 }
391 else {
392 $countryID = CRM_Utils_Array::value(0, $form->getElementValue($countryElementName));
393 }
394 }
395
396 $stateTitle = ts('State/Province');
397 if (isset($form->_fields[$stateElementName]['title'])) {
398 $stateTitle = $form->_fields[$stateElementName]['title'];
399 }
400
401 if ($countryID &&
402 isset($form->_elementIndex[$stateElementName])
403 ) {
404
405 $submittedValState = $form->getSubmitValue($stateElementName);
406 if ($submittedValState) {
407 $stateID = $submittedValState;
408 }
409 elseif ($stateDefaultValue) {
410 $stateID = $stateDefaultValue;
411 }
412 else {
413 $stateID = CRM_Utils_Array::value(0, $form->getElementValue($stateElementName));
414 }
415
416 $stateSelect = &$form->addElement('select',
417 $stateElementName,
418 $stateTitle,
419 array(
420 '' => ts('- select -')) +
421 CRM_Core_PseudoConstant::stateProvinceForCountry($countryID)
422 );
423
424
425 if ($stateID &&
426 isset($form->_elementIndex[$stateElementName]) &&
427 isset($form->_elementIndex[$countyElementName])
428 ) {
429 $form->addElement('select',
430 $countyElementName,
431 ts('County'),
432 array(
433 '' => ts('- select -')) +
434 CRM_Core_PseudoConstant::countyForState($stateID)
435 );
436 }
437
438 // CRM-7296 freeze the select for state if address is shared with household
439 // CRM-9070 freeze the select for state if it is view only
440 if (isset($form->_fields) &&
441 CRM_Utils_Array::value($stateElementName, $form->_fields) &&
442 (CRM_Utils_Array::value('is_shared', $form->_fields[$stateElementName]) ||
443 CRM_Utils_Array::value('is_view', $form->_fields[$stateElementName]))
444 ) {
445 $stateSelect->freeze();
446 }
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 }
517
518 // don't load fields, use js to populate.
519 foreach (array('street_number', 'street_name', 'street_unit') as $f) {
520 if (isset($address[$f])) {
521 unset($address[$f]);
522 }
523 }
524 }
525 $form->assign('allAddressFieldValues', json_encode($addressValues));
526
527 //hack to handle show/hide address fields.
528 $parsedAddress = array();
529 if ($form->_contactId &&
530 CRM_Utils_Array::value('address', $_POST)
531 && is_array($_POST['address'])
532 ) {
533 foreach ($_POST['address'] as $cnt => $values) {
534 $showField = 'streetAddress';
535 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
536 if (CRM_Utils_Array::value($fld, $values)) {
537 $showField = 'addressElements';
538 break;
539 }
540 }
541 $parsedAddress[$cnt] = $showField;
542 }
543 }
544 $form->assign('showHideAddressFields', $parsedAddress);
545 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ? FALSE : TRUE);
546 }
547 // end of parse address functionality
548 }
549 }
550}
551