3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class is used to build address block
39 class CRM_Contact_Form_Edit_Address
{
42 * build form for address input fields
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
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;
60 $blockId = $addressBlockCount;
63 $config = CRM_Core_Config
::singleton();
64 $countryDefault = $config->defaultContactCountry
;
66 $form->applyFilter('__ALL__', 'trim');
70 $js = array('onChange' => 'checkLocation( this.id );');
73 $form->addElement('select',
74 "address[$blockId][location_type_id]",
77 '' => ts('- select -')) + CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id'),
82 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
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');
91 "address[$blockId][is_primary]",
92 ts('Primary location for this contact'),
93 ts('Primary location for this contact'),
98 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
103 "address[$blockId][is_billing]",
104 ts('Billing location for this contact'),
105 ts('Billing location for this contact'),
109 // hidden element to store master address id
110 $form->addElement('hidden', "address[$blockId][master_id]");
112 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
113 'address_options', TRUE, NULL, TRUE
115 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Address');
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),
135 $stateCountryMap = array();
136 foreach ($elements as $name => $v) {
137 list($title, $attributes, $select) = $v;
139 $nameWithoutID = strpos($name, '_id') !== FALSE ?
substr($name, 0, -3) : $name;
140 if (!CRM_Utils_Array
::value($nameWithoutID, $addressOptions)) {
142 if (in_array($nameWithoutID, array(
143 'street_number', 'street_name', 'street_unit')) &&
144 CRM_Utils_Array
::value('street_address_parsing', $addressOptions)
154 $attributes = $attributes[$name];
157 //build normal select if country is not present in address block
158 if ($name == 'state_province_id' && !$addressOptions['country']) {
159 $select = 'stateProvince';
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();
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);
174 $selectOptions = array('' => ts('- select a country -'));
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]"));
182 elseif ($form->getSubmitValue("address[{$blockId}][county_id]")) {
183 $selectOptions = array('' => ts('- select a state -')) + CRM_Core_PseudoConstant
::county();
186 $selectOptions = array('' => ts('- select a state -'));
189 $form->addElement('select',
190 "address[$blockId][$name]",
196 if ($name == 'address_name') {
200 $form->addElement('text',
201 "address[$blockId][$name]",
208 if ($name == 'state_province_id') {
209 $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
211 $form->addElement('select',
212 "address[$blockId][$name]",
214 array('' => ts('- select -')) + CRM_Core_PseudoConstant
::$select()
219 CRM_Core_BAO_Address
::addStateCountryMap($stateCountryMap);
222 if (!empty($form->_values
['address']) && CRM_Utils_Array
::value($blockId, $form->_values
['address'])) {
223 $entityId = $form->_values
['address'][$blockId]['id'];
226 // CRM-11665 geocode override option
228 if (!empty($config->geocodeMethod
)) {
230 $form->addElement('checkbox',
231 "address[$blockId][manual_geo_code]",
232 ts('Override automatic geocoding')
235 $form->assign('geoCode', $geoCode);
237 // Process any address custom data -
238 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Address',
243 if (isset($groupTree) && is_array($groupTree)) {
244 // use simplified formatted groupTree
245 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
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']}]";
256 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
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 ) ) {
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;
271 $form->setDefaults($addressDefaults);
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_');
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
281 CRM_Contact_Form_Edit_Address
::storeRequiredCustomDataInfo($form, $groupTree);
284 $template = CRM_Core_Smarty
::singleton();
285 $tplGroupTree = $template->get_template_vars('address_groupTree');
286 $tplGroupTree = empty($tplGroupTree) ?
array(
289 $form->assign('address_groupTree', $tplGroupTree +
array($blockId => $groupTree));
290 // unset the temp smarty var that got created
291 $form->assign('dnc_groupTree', NULL);
293 // address custom data processing ends ..
297 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
299 // get the reserved for address
300 $profileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', 'shared_address', 'id', 'name');
303 CRM_Core_Error
::fatal(ts('Your install is missing required "Shared Address" profile.'));
306 CRM_Contact_Form_NewContact
::buildQuickForm($form, $blockId, array($profileId));
311 * check for correct state / country mapping.
313 * @param array reference $fields - submitted form values.
314 * @param array reference $errors - if any errors found add to this array. please.
316 * @return true if no errors
317 * array of errors if any present.
322 static function formRule($fields, $files, $self) {
325 $customDataRequiredFields = array();
326 if ($self && property_exists($self, '_addressRequireOmission')) {
327 $customDataRequiredFields = explode(',', $self->_addressRequireOmission
);
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) {
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);
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);
361 $countryId = CRM_Utils_Array
::value('country_id', $addressValues);
363 $stateProvinceId = CRM_Utils_Array
::value('state_province_id', $addressValues);
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.',
376 1 => $stateProvinces[$stateProvinceId],
377 2 => $countries[$countryId],
378 3 => $countries[$stateProvinceDAO->country_id
]
384 $countyId = CRM_Utils_Array
::value('county_id', $addressValues);
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.',
395 1 => $counties[$countyId],
396 2 => $stateProvinces[$stateProvinceId],
397 3 => $stateProvinces[$countyDAO->state_province_id
]
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.');
409 return empty($errors) ?
TRUE : $errors;
412 static function fixStateSelect(&$form,
416 $countryDefaultValue,
417 $stateDefaultValue = NULL
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);
427 $countryID = $submittedVal;
429 elseif ($countryDefaultValue) {
430 $countryID = $countryDefaultValue;
433 $countryID = CRM_Utils_Array
::value(0, $form->getElementValue($countryElementName));
436 $stateTitle = ts('State/Province');
437 if (isset($form->_fields
[$stateElementName]['title'])) {
438 $stateTitle = $form->_fields
[$stateElementName]['title'];
441 if (isset($form->_elementIndex
[$stateElementName])) {
442 $submittedValState = $form->getSubmitValue($stateElementName);
443 if ($submittedValState) {
444 $stateID = $submittedValState;
446 elseif ($stateDefaultValue) {
447 $stateID = $stateDefaultValue;
450 $stateID = CRM_Utils_Array
::value(0, $form->getElementValue($stateElementName));
454 if (isset($form->_elementIndex
[$stateElementName])) {
456 $stateProvinces = CRM_Core_PseudoConstant
::stateProvinceForCountry($countryID);
459 $stateProvinces = CRM_Core_PseudoConstant
::stateProvince();
462 $stateSelect = & $form->addElement('select', $stateElementName, $stateTitle,
463 array('' => ts('- select -')) +
$stateProvinces);
466 if (isset($form->_elementIndex
[$stateElementName]) && isset($form->_elementIndex
[$countyElementName])) {
468 $counties = CRM_Core_PseudoConstant
::countyForState($stateID);
471 $counties = CRM_Core_PseudoConstant
::country();
474 $form->addElement('select', $countyElementName, ts('County'), array('' => ts('- select -')) +
$counties);
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]))
484 $stateSelect->freeze();
489 * function to set default values for address block
491 * @param array $defaults defaults associated array
492 * @param object $form form object
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'])
503 // start of contact shared adddress defaults
504 $sharedAddresses = array();
505 $masterAddress = array();
507 // get contact name of shared contact names
508 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils
::getAddressShareContactNames($defaults['address']);
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'],
518 $defaults['address'][$key]['use_shared_address'] = 0;
521 //check if any address is shared by any other contacts
522 $masterAddress[$key] = CRM_Core_BAO_Address
::checkContactSharedAddress($addressValue['id']);
525 $form->assign('sharedAddresses', $sharedAddresses);
526 $form->assign('masterAddress', $masterAddress);
527 // end of shared address defaults
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;
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 .= ' ';
541 $streetAddress .= CRM_Utils_Array
::value($fld, $address);
543 $streetAddress = trim($streetAddress);
544 if (!empty($streetAddress)) {
545 $address['street_address'] = $streetAddress;
547 if (isset($address['street_number'])) {
548 $address['street_number'] .= CRM_Utils_Array
::value('street_number_suffix', $address);
551 // build array for set default.
552 foreach ($parseFields as $field) {
553 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array
::value($field, $address);
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])) {
562 $form->assign('allAddressFieldValues', json_encode($addressValues));
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'])
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';
578 $parsedAddress[$cnt] = $showField;
581 $form->assign('showHideAddressFields', $parsedAddress);
582 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ?
FALSE : TRUE);
584 // end of parse address functionality
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') {
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'] . ',';
609 $form->_addressRequireOmission
= rtrim($requireOmission, ',');