| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 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 CRM_Core_Form $form |
| 45 | * @param int $addressBlockCount |
| 46 | * The index of the address array (if multiple addresses on a page). |
| 47 | * @param bool $sharing |
| 48 | * False, if we want to skip the address sharing features. |
| 49 | * @param bool $inlineEdit |
| 50 | * True when edit used in inline edit. |
| 51 | * |
| 52 | * @return void |
| 53 | * |
| 54 | * @static |
| 55 | */ |
| 56 | public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) { |
| 57 | // passing this via the session is AWFUL. we need to fix this |
| 58 | if (!$addressBlockCount) { |
| 59 | $blockId = ($form->get('Address_Block_Count')) ? $form->get('Address_Block_Count') : 1; |
| 60 | } |
| 61 | else { |
| 62 | $blockId = $addressBlockCount; |
| 63 | } |
| 64 | |
| 65 | $config = CRM_Core_Config::singleton(); |
| 66 | $countryDefault = $config->defaultContactCountry; |
| 67 | |
| 68 | $form->applyFilter('__ALL__', 'trim'); |
| 69 | |
| 70 | $js = array(); |
| 71 | if (!$inlineEdit) { |
| 72 | $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL); |
| 73 | } |
| 74 | |
| 75 | //make location type required for inline edit |
| 76 | $form->addSelect("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') + $js, $inlineEdit); |
| 77 | |
| 78 | if (!$inlineEdit) { |
| 79 | $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );'); |
| 80 | } |
| 81 | |
| 82 | $form->addElement( |
| 83 | 'checkbox', |
| 84 | "address[$blockId][is_primary]", |
| 85 | ts('Primary location for this contact'), |
| 86 | ts('Primary location for this contact'), |
| 87 | $js |
| 88 | ); |
| 89 | |
| 90 | if (!$inlineEdit) { |
| 91 | $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );'); |
| 92 | } |
| 93 | |
| 94 | $form->addElement( |
| 95 | 'checkbox', |
| 96 | "address[$blockId][is_billing]", |
| 97 | ts('Billing location for this contact'), |
| 98 | ts('Billing location for this contact'), |
| 99 | $js |
| 100 | ); |
| 101 | |
| 102 | // hidden element to store master address id |
| 103 | $form->addElement('hidden', "address[$blockId][master_id]"); |
| 104 | |
| 105 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 106 | 'address_options', TRUE, NULL, TRUE |
| 107 | ); |
| 108 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address'); |
| 109 | |
| 110 | $elements = array( |
| 111 | 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL), |
| 112 | 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL), |
| 113 | 'supplemental_address_1' => array(ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL), |
| 114 | 'supplemental_address_2' => array(ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL), |
| 115 | 'city' => array(ts('City'), $attributes['city'], NULL), |
| 116 | 'postal_code' => array(ts('Zip / Postal Code'), array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')), NULL), |
| 117 | 'postal_code_suffix' => array(ts('Postal Code Suffix'), array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'), NULL), |
| 118 | 'country_id' => array(ts('Country'), $attributes['country_id'], 'country'), |
| 119 | 'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL), |
| 120 | 'county_id' => array(ts('County'), $attributes['county_id'], NULL), |
| 121 | 'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 11), NULL), |
| 122 | 'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 11), NULL), |
| 123 | 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL), |
| 124 | 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL), |
| 125 | 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL), |
| 126 | ); |
| 127 | |
| 128 | foreach ($elements as $name => $v) { |
| 129 | list($title, $attributes, $select) = $v; |
| 130 | |
| 131 | $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name; |
| 132 | if (empty($addressOptions[$nameWithoutID])) { |
| 133 | $continue = TRUE; |
| 134 | if (in_array($nameWithoutID, array( |
| 135 | 'street_number', 'street_name', 'street_unit')) && !empty($addressOptions['street_address_parsing'])) { |
| 136 | $continue = FALSE; |
| 137 | } |
| 138 | if ($continue) { |
| 139 | continue; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | //build normal select if country is not present in address block |
| 144 | if ($name == 'state_province_id' && !$addressOptions['country']) { |
| 145 | $select = 'stateProvince'; |
| 146 | } |
| 147 | |
| 148 | if (!$select) { |
| 149 | if ($name == 'state_province_id' || $name == 'county_id') { |
| 150 | $form->addChainSelect("address[$blockId][$name]"); |
| 151 | } |
| 152 | else { |
| 153 | if ($name == 'address_name') { |
| 154 | $name = 'name'; |
| 155 | } |
| 156 | |
| 157 | $form->addElement('text', |
| 158 | "address[$blockId][$name]", |
| 159 | $title, |
| 160 | $attributes |
| 161 | ); |
| 162 | } |
| 163 | } |
| 164 | else { |
| 165 | $form->addElement('select', |
| 166 | "address[$blockId][$name]", |
| 167 | $title, |
| 168 | array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select() |
| 169 | ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | $entityId = NULL; |
| 174 | if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) { |
| 175 | $entityId = $form->_values['address'][$blockId]['id']; |
| 176 | } |
| 177 | |
| 178 | // CRM-11665 geocode override option |
| 179 | $geoCode = FALSE; |
| 180 | if (!empty($config->geocodeMethod)) { |
| 181 | $geoCode = TRUE; |
| 182 | $form->addElement('checkbox', |
| 183 | "address[$blockId][manual_geo_code]", |
| 184 | ts('Override automatic geocoding') |
| 185 | ); |
| 186 | } |
| 187 | $form->assign('geoCode', $geoCode); |
| 188 | |
| 189 | // Process any address custom data - |
| 190 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', |
| 191 | $form, |
| 192 | $entityId |
| 193 | ); |
| 194 | |
| 195 | if (isset($groupTree) && is_array($groupTree)) { |
| 196 | // use simplified formatted groupTree |
| 197 | $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form); |
| 198 | |
| 199 | // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]' |
| 200 | foreach ($groupTree as $id => $group) { |
| 201 | foreach ($group['fields'] as $fldId => $field) { |
| 202 | $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name']; |
| 203 | $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]"; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | $defaults = array(); |
| 208 | CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults); |
| 209 | |
| 210 | // since we change element name for address custom data, we need to format the setdefault values |
| 211 | $addressDefaults = array(); |
| 212 | foreach ($defaults as $key => $val) { |
| 213 | if ( empty( $val ) ) { |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array |
| 218 | // this works for all types custom data |
| 219 | $keyValues = explode('[', str_replace(']', '', $key)); |
| 220 | $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val; |
| 221 | } |
| 222 | |
| 223 | $form->setDefaults($addressDefaults); |
| 224 | |
| 225 | // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var. |
| 226 | // And we can't set it to 'address_' because we want to set it in a slightly different format. |
| 227 | CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); |
| 228 | |
| 229 | // during contact editing : if no address is filled |
| 230 | // required custom data must not produce 'required' form rule error |
| 231 | // more handling done in formRule func |
| 232 | if (!$inlineEdit) { |
| 233 | CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree); |
| 234 | } |
| 235 | |
| 236 | $template = CRM_Core_Smarty::singleton(); |
| 237 | $tplGroupTree = $template->get_template_vars('address_groupTree'); |
| 238 | $tplGroupTree = empty($tplGroupTree) ? array( |
| 239 | ) : $tplGroupTree; |
| 240 | |
| 241 | $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree)); |
| 242 | // unset the temp smarty var that got created |
| 243 | $form->assign('dnc_groupTree', NULL); |
| 244 | } |
| 245 | // address custom data processing ends .. |
| 246 | |
| 247 | if ($sharing) { |
| 248 | // shared address |
| 249 | $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address')); |
| 250 | |
| 251 | // Override the default profile links to add address form |
| 252 | $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array('new_individual', 'new_organization', 'new_household'), 'shared_address'); |
| 253 | $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Check for correct state / country mapping. |
| 259 | * |
| 260 | * @param $fields |
| 261 | * @param $files |
| 262 | * @param $self |
| 263 | * |
| 264 | * @return array|bool if no errors |
| 265 | * |
| 266 | * @static |
| 267 | */ |
| 268 | public static function formRule($fields, $files, $self) { |
| 269 | $errors = array(); |
| 270 | |
| 271 | $customDataRequiredFields = array(); |
| 272 | if ($self && property_exists($self, '_addressRequireOmission')) { |
| 273 | $customDataRequiredFields = explode(',', $self->_addressRequireOmission); |
| 274 | } |
| 275 | |
| 276 | if (!empty($fields['address']) && is_array($fields['address'])) { |
| 277 | foreach ($fields['address'] as $instance => $addressValues) { |
| 278 | |
| 279 | if (CRM_Utils_System::isNull($addressValues)) { |
| 280 | // DETACH 'required' form rule error to |
| 281 | // custom data only if address data not exists upon submission |
| 282 | if (!empty($customDataRequiredFields)) { |
| 283 | foreach($customDataRequiredFields as $customElementName) { |
| 284 | $elementName = "address[$instance][$customElementName]"; |
| 285 | if ($self->getElementError($elementName)) { |
| 286 | // set element error to none |
| 287 | $self->setElementError($elementName, NULL); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | // DETACH 'required' form rule error to |
| 295 | // custom data only if address data not exists upon submission |
| 296 | if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address::dataExists($addressValues)) { |
| 297 | foreach($customDataRequiredFields as $customElementName) { |
| 298 | $elementName = "address[$instance][$customElementName]"; |
| 299 | if ($self->getElementError($elementName)) { |
| 300 | // set element error to none |
| 301 | $self->setElementError($elementName, NULL); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) { |
| 307 | $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.'); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | return empty($errors) ? TRUE : $errors; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Set default values for address block |
| 317 | * |
| 318 | * @param array $defaults |
| 319 | * Defaults associated array. |
| 320 | * @param CRM_Core_Form $form |
| 321 | * Form object. |
| 322 | * |
| 323 | * @static |
| 324 | */ |
| 325 | public static function setDefaultValues( &$defaults, &$form ) { |
| 326 | $addressValues = array(); |
| 327 | if (isset($defaults['address']) && is_array($defaults['address']) && |
| 328 | !CRM_Utils_System::isNull($defaults['address']) |
| 329 | ) { |
| 330 | |
| 331 | // start of contact shared adddress defaults |
| 332 | $sharedAddresses = array(); |
| 333 | $masterAddress = array(); |
| 334 | |
| 335 | // get contact name of shared contact names |
| 336 | $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils::getAddressShareContactNames($defaults['address']); |
| 337 | |
| 338 | foreach ($defaults['address'] as $key => $addressValue) { |
| 339 | if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) { |
| 340 | $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id']; |
| 341 | $sharedAddresses[$key]['shared_address_display'] = array( |
| 342 | 'address' => $addressValue['display'], |
| 343 | 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'], |
| 344 | 'options' => CRM_Core_BAO_Address::getValues(array('entity_id' => $master_cid, 'contact_id' => $master_cid)), |
| 345 | 'master_id' => $addressValue['master_id'], |
| 346 | ); |
| 347 | $defaults['address'][$key]['master_contact_id'] = $master_cid; |
| 348 | } |
| 349 | else { |
| 350 | $defaults['address'][$key]['use_shared_address'] = 0; |
| 351 | } |
| 352 | |
| 353 | //check if any address is shared by any other contacts |
| 354 | $masterAddress[$key] = CRM_Core_BAO_Address::checkContactSharedAddress($addressValue['id']); |
| 355 | } |
| 356 | |
| 357 | $form->assign('sharedAddresses', $sharedAddresses); |
| 358 | $form->assign('masterAddress', $masterAddress); |
| 359 | // end of shared address defaults |
| 360 | |
| 361 | // start of parse address functionality |
| 362 | // build street address, CRM-5450. |
| 363 | if ($form->_parseStreetAddress) { |
| 364 | $parseFields = array('street_address', 'street_number', 'street_name', 'street_unit'); |
| 365 | foreach ($defaults['address'] as $cnt => & $address) { |
| 366 | $streetAddress = NULL; |
| 367 | foreach (array( |
| 368 | 'street_number', 'street_number_suffix', 'street_name', 'street_unit') as $fld) { |
| 369 | if (in_array($fld, array( |
| 370 | 'street_name', 'street_unit'))) { |
| 371 | $streetAddress .= ' '; |
| 372 | } |
| 373 | $streetAddress .= CRM_Utils_Array::value($fld, $address); |
| 374 | } |
| 375 | $streetAddress = trim($streetAddress); |
| 376 | if (!empty($streetAddress)) { |
| 377 | $address['street_address'] = $streetAddress; |
| 378 | } |
| 379 | if (isset($address['street_number'])) { |
| 380 | $address['street_number'] .= CRM_Utils_Array::value('street_number_suffix', $address); |
| 381 | } |
| 382 | |
| 383 | // build array for set default. |
| 384 | foreach ($parseFields as $field) { |
| 385 | $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array::value($field, $address); |
| 386 | } |
| 387 | // don't load fields, use js to populate. |
| 388 | foreach (array('street_number', 'street_name', 'street_unit') as $f) { |
| 389 | if (isset($address[$f])) { |
| 390 | unset($address[$f]); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | $form->assign('allAddressFieldValues', json_encode($addressValues)); |
| 395 | |
| 396 | //hack to handle show/hide address fields. |
| 397 | $parsedAddress = array(); |
| 398 | if ($form->_contactId && !empty($_POST['address']) && is_array($_POST['address']) |
| 399 | ) { |
| 400 | foreach ($_POST['address'] as $cnt => $values) { |
| 401 | $showField = 'streetAddress'; |
| 402 | foreach (array('street_number', 'street_name', 'street_unit') as $fld) { |
| 403 | if (!empty($values[$fld])) { |
| 404 | $showField = 'addressElements'; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | $parsedAddress[$cnt] = $showField; |
| 409 | } |
| 410 | } |
| 411 | $form->assign('showHideAddressFields', $parsedAddress); |
| 412 | $form->assign('loadShowHideAddressFields', empty($parsedAddress) ? FALSE : TRUE); |
| 413 | } |
| 414 | // end of parse address functionality |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * @param CRM_Core_Form $form |
| 421 | * @param $groupTree |
| 422 | */ |
| 423 | public static function storeRequiredCustomDataInfo(&$form, $groupTree) { |
| 424 | if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') { |
| 425 | $requireOmission = NULL; |
| 426 | foreach ($groupTree as $csId => $csVal) { |
| 427 | // only process Address entity fields |
| 428 | if ($csVal['extends'] != 'Address') { |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | foreach ($csVal['fields'] as $cdId => $cdVal) { |
| 433 | if ($cdVal['is_required']) { |
| 434 | $elementName = $cdVal['element_name']; |
| 435 | if (in_array($elementName, $form->_required)) { |
| 436 | // store the omitted rule for a element, to be used later on |
| 437 | $requireOmission .= $cdVal['element_custom_name'] . ','; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | $form->_addressRequireOmission = rtrim($requireOmission, ','); |
| 444 | } |
| 445 | } |
| 446 | } |