3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
::locationType(),
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('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),
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 $form->addElement('select',
209 "address[$blockId][$name]",
211 array('' => ts('- select -')) + CRM_Core_PseudoConstant
::$select()
216 CRM_Core_BAO_Address
::addStateCountryMap($stateCountryMap);
219 if (!empty($form->_values
['address']) && CRM_Utils_Array
::value($blockId, $form->_values
['address'])) {
220 $entityId = $form->_values
['address'][$blockId]['id'];
223 // CRM-11665 geocode override option
225 if (!empty($config->geocodeMethod
)) {
227 $form->addElement('checkbox',
228 "address[$blockId][manual_geo_code]",
229 ts('Override automatic geocoding')
232 $form->assign('geoCode', $geoCode);
234 // Process any address custom data -
235 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Address',
240 if (isset($groupTree) && is_array($groupTree)) {
241 // use simplified formatted groupTree
242 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
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']}]";
253 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
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 ) ) {
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;
268 $form->setDefaults($addressDefaults);
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_');
274 $template = CRM_Core_Smarty
::singleton();
275 $tplGroupTree = $template->get_template_vars('address_groupTree');
276 $tplGroupTree = empty($tplGroupTree) ?
array(
279 $form->assign('address_groupTree', $tplGroupTree +
array($blockId => $groupTree));
280 // unset the temp smarty var that got created
281 $form->assign('dnc_groupTree', NULL);
283 // address custom data processing ends ..
287 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
289 // get the reserved for address
290 $profileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', 'shared_address', 'id', 'name');
293 CRM_Core_Error
::fatal(ts('Your install is missing required "Shared Address" profile.'));
296 CRM_Contact_Form_NewContact
::buildQuickForm($form, $blockId, array($profileId));
301 * check for correct state / country mapping.
303 * @param array reference $fields - submitted form values.
304 * @param array reference $errors - if any errors found add to this array. please.
306 * @return true if no errors
307 * array of errors if any present.
312 static function formRule($fields) {
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)) {
321 $countryId = CRM_Utils_Array
::value('country_id', $addressValues);
322 $stateProvinceId = CRM_Utils_Array
::value('state_province_id', $addressValues);
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.',
335 1 => $stateProvinces[$stateProvinceId],
336 2 => $countries[$countryId],
337 3 => $countries[$stateProvinceDAO->country_id
]
343 $countyId = CRM_Utils_Array
::value('county_id', $addressValues);
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.',
354 1 => $counties[$countyId],
355 2 => $stateProvinces[$stateProvinceId],
356 3 => $stateProvinces[$countyDAO->state_province_id
]
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.');
368 return empty($errors) ?
TRUE : $errors;
371 static function fixStateSelect(&$form,
375 $countryDefaultValue,
376 $stateDefaultValue = NULL
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);
386 $countryID = $submittedVal;
388 elseif ($countryDefaultValue) {
389 $countryID = $countryDefaultValue;
392 $countryID = CRM_Utils_Array
::value(0, $form->getElementValue($countryElementName));
396 $stateTitle = ts('State/Province');
397 if (isset($form->_fields
[$stateElementName]['title'])) {
398 $stateTitle = $form->_fields
[$stateElementName]['title'];
402 isset($form->_elementIndex
[$stateElementName])
405 $submittedValState = $form->getSubmitValue($stateElementName);
406 if ($submittedValState) {
407 $stateID = $submittedValState;
409 elseif ($stateDefaultValue) {
410 $stateID = $stateDefaultValue;
413 $stateID = CRM_Utils_Array
::value(0, $form->getElementValue($stateElementName));
416 $stateSelect = &$form->addElement('select',
420 '' => ts('- select -')) +
421 CRM_Core_PseudoConstant
::stateProvinceForCountry($countryID)
426 isset($form->_elementIndex
[$stateElementName]) &&
427 isset($form->_elementIndex
[$countyElementName])
429 $form->addElement('select',
433 '' => ts('- select -')) +
434 CRM_Core_PseudoConstant
::countyForState($stateID)
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]))
445 $stateSelect->freeze();
451 * function to set default values for address block
453 * @param array $defaults defaults associated array
454 * @param object $form form object
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'])
465 // start of contact shared adddress defaults
466 $sharedAddresses = array();
467 $masterAddress = array();
469 // get contact name of shared contact names
470 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils
::getAddressShareContactNames($defaults['address']);
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'],
480 $defaults['address'][$key]['use_shared_address'] = 0;
483 //check if any address is shared by any other contacts
484 $masterAddress[$key] = CRM_Core_BAO_Address
::checkContactSharedAddress($addressValue['id']);
487 $form->assign('sharedAddresses', $sharedAddresses);
488 $form->assign('masterAddress', $masterAddress);
489 // end of shared address defaults
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;
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 .= ' ';
503 $streetAddress .= CRM_Utils_Array
::value($fld, $address);
505 $streetAddress = trim($streetAddress);
506 if (!empty($streetAddress)) {
507 $address['street_address'] = $streetAddress;
509 if (isset($address['street_number'])) {
510 $address['street_number'] .= CRM_Utils_Array
::value('street_number_suffix', $address);
513 // build array for set default.
514 foreach ($parseFields as $field) {
515 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array
::value($field, $address);
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])) {
525 $form->assign('allAddressFieldValues', json_encode($addressValues));
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'])
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';
541 $parsedAddress[$cnt] = $showField;
544 $form->assign('showHideAddressFields', $parsedAddress);
545 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ?
FALSE : TRUE);
547 // end of parse address functionality