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