3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 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.
55 public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
56 // passing this via the session is AWFUL. we need to fix this
57 if (!$addressBlockCount) {
58 $blockId = ($form->get('Address_Block_Count')) ?
$form->get('Address_Block_Count') : 1;
61 $blockId = $addressBlockCount;
64 $config = CRM_Core_Config
::singleton();
65 $countryDefault = $config->defaultContactCountry
;
67 $form->applyFilter('__ALL__', 'trim');
71 $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL);
74 //make location type required for inline edit
75 $form->addSelect("address[$blockId][location_type_id]", array(
76 'entity' => 'address',
78 ) +
$js, $inlineEdit);
81 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
86 "address[$blockId][is_primary]",
87 ts('Primary location for this contact'),
88 ts('Primary location for this contact'),
93 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
98 "address[$blockId][is_billing]",
99 ts('Billing location for this contact'),
100 ts('Billing location for this contact'),
104 // hidden element to store master address id
105 $form->addElement('hidden', "address[$blockId][master_id]");
107 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
108 'address_options', TRUE, NULL, TRUE
110 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Address');
113 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL),
114 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL),
115 'supplemental_address_1' => array(ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL),
116 'supplemental_address_2' => array(ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL),
117 'city' => array(ts('City'), $attributes['city'], NULL),
118 'postal_code' => array(
119 ts('Zip / Postal Code'),
120 array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')),
123 'postal_code_suffix' => array(
124 ts('Postal Code Suffix'),
125 array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'),
128 'country_id' => array(ts('Country'), $attributes['country_id'], 'country'),
129 'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL),
130 'county_id' => array(ts('County'), $attributes['county_id'], NULL),
131 'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 11), NULL),
132 'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 11), NULL),
133 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL),
134 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL),
135 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL),
138 foreach ($elements as $name => $v) {
139 list($title, $attributes, $select) = $v;
141 $nameWithoutID = strpos($name, '_id') !== FALSE ?
substr($name, 0, -3) : $name;
142 if (empty($addressOptions[$nameWithoutID])) {
144 if (in_array($nameWithoutID, array(
148 )) && !empty($addressOptions['street_address_parsing'])
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 == 'state_province_id' ||
$name == 'county_id') {
164 $form->addChainSelect("address[$blockId][$name]");
167 if ($name == 'address_name') {
171 $form->addElement('text',
172 "address[$blockId][$name]",
179 $form->addElement('select',
180 "address[$blockId][$name]",
182 array('' => ts('- select -')) + CRM_Core_PseudoConstant
::$select()
188 if (!empty($form->_values
['address']) && !empty($form->_values
['address'][$blockId])) {
189 $entityId = $form->_values
['address'][$blockId]['id'];
192 // CRM-11665 geocode override option
194 if (!empty($config->geocodeMethod
)) {
196 $form->addElement('checkbox',
197 "address[$blockId][manual_geo_code]",
198 ts('Override automatic geocoding')
201 $form->assign('geoCode', $geoCode);
203 // Process any address custom data -
204 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Address',
209 if (isset($groupTree) && is_array($groupTree)) {
210 // use simplified formatted groupTree
211 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
213 // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
214 foreach ($groupTree as $id => $group) {
215 foreach ($group['fields'] as $fldId => $field) {
216 $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
217 $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]";
222 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
224 // since we change element name for address custom data, we need to format the setdefault values
225 $addressDefaults = array();
226 foreach ($defaults as $key => $val) {
231 // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
232 // this works for all types custom data
233 $keyValues = explode('[', str_replace(']', '', $key));
234 $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
237 $form->setDefaults($addressDefaults);
239 // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
240 // And we can't set it to 'address_' because we want to set it in a slightly different format.
241 CRM_Core_BAO_CustomGroup
::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
243 // during contact editing : if no address is filled
244 // required custom data must not produce 'required' form rule error
245 // more handling done in formRule func
247 CRM_Contact_Form_Edit_Address
::storeRequiredCustomDataInfo($form, $groupTree);
250 $template = CRM_Core_Smarty
::singleton();
251 $tplGroupTree = $template->get_template_vars('address_groupTree');
252 $tplGroupTree = empty($tplGroupTree) ?
array() : $tplGroupTree;
254 $form->assign('address_groupTree', $tplGroupTree +
array($blockId => $groupTree));
255 // unset the temp smarty var that got created
256 $form->assign('dnc_groupTree', NULL);
258 // address custom data processing ends ..
262 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
264 // Override the default profile links to add address form
265 $profileLinks = CRM_Core_BAO_UFGroup
::getCreateLinks(array(
269 ), 'shared_address');
270 $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
275 * Check for correct state / country mapping.
285 public static function formRule($fields, $files, $self) {
288 $customDataRequiredFields = array();
289 if ($self && property_exists($self, '_addressRequireOmission')) {
290 $customDataRequiredFields = explode(',', $self->_addressRequireOmission
);
293 if (!empty($fields['address']) && is_array($fields['address'])) {
294 foreach ($fields['address'] as $instance => $addressValues) {
296 if (CRM_Utils_System
::isNull($addressValues)) {
297 // DETACH 'required' form rule error to
298 // custom data only if address data not exists upon submission
299 if (!empty($customDataRequiredFields)) {
300 foreach ($customDataRequiredFields as $customElementName) {
301 $elementName = "address[$instance][$customElementName]";
302 if ($self->getElementError($elementName)) {
303 // set element error to none
304 $self->setElementError($elementName, NULL);
311 // DETACH 'required' form rule error to
312 // custom data only if address data not exists upon submission
313 if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address
::dataExists($addressValues)) {
314 foreach ($customDataRequiredFields as $customElementName) {
315 $elementName = "address[$instance][$customElementName]";
316 if ($self->getElementError($elementName)) {
317 // set element error to none
318 $self->setElementError($elementName, NULL);
323 if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) {
324 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
329 return empty($errors) ?
TRUE : $errors;
333 * Set default values for address block
335 * @param array $defaults
336 * Defaults associated array.
337 * @param CRM_Core_Form $form
341 public static function setDefaultValues(&$defaults, &$form) {
342 $addressValues = array();
343 if (isset($defaults['address']) && is_array($defaults['address']) &&
344 !CRM_Utils_System
::isNull($defaults['address'])
347 // start of contact shared adddress defaults
348 $sharedAddresses = array();
349 $masterAddress = array();
351 // get contact name of shared contact names
352 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils
::getAddressShareContactNames($defaults['address']);
354 foreach ($defaults['address'] as $key => $addressValue) {
355 if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
356 $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id'];
357 $sharedAddresses[$key]['shared_address_display'] = array(
358 'address' => $addressValue['display'],
359 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
360 'options' => CRM_Core_BAO_Address
::getValues(array(
361 'entity_id' => $master_cid,
362 'contact_id' => $master_cid
364 'master_id' => $addressValue['master_id'],
366 $defaults['address'][$key]['master_contact_id'] = $master_cid;
369 $defaults['address'][$key]['use_shared_address'] = 0;
372 //check if any address is shared by any other contacts
373 $masterAddress[$key] = CRM_Core_BAO_Address
::checkContactSharedAddress($addressValue['id']);
376 $form->assign('sharedAddresses', $sharedAddresses);
377 $form->assign('masterAddress', $masterAddress);
378 // end of shared address defaults
380 // start of parse address functionality
381 // build street address, CRM-5450.
382 if ($form->_parseStreetAddress
) {
383 $parseFields = array('street_address', 'street_number', 'street_name', 'street_unit');
384 foreach ($defaults['address'] as $cnt => & $address) {
385 $streetAddress = NULL;
388 'street_number_suffix',
392 if (in_array($fld, array(
396 $streetAddress .= ' ';
398 $streetAddress .= CRM_Utils_Array
::value($fld, $address);
400 $streetAddress = trim($streetAddress);
401 if (!empty($streetAddress)) {
402 $address['street_address'] = $streetAddress;
404 if (isset($address['street_number'])) {
405 $address['street_number'] .= CRM_Utils_Array
::value('street_number_suffix', $address);
408 // build array for set default.
409 foreach ($parseFields as $field) {
410 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array
::value($field, $address);
412 // don't load fields, use js to populate.
413 foreach (array('street_number', 'street_name', 'street_unit') as $f) {
414 if (isset($address[$f])) {
419 $form->assign('allAddressFieldValues', json_encode($addressValues));
421 //hack to handle show/hide address fields.
422 $parsedAddress = array();
423 if ($form->_contactId
&& !empty($_POST['address']) && is_array($_POST['address'])
425 foreach ($_POST['address'] as $cnt => $values) {
426 $showField = 'streetAddress';
427 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
428 if (!empty($values[$fld])) {
429 $showField = 'addressElements';
433 $parsedAddress[$cnt] = $showField;
436 $form->assign('showHideAddressFields', $parsedAddress);
437 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ?
FALSE : TRUE);
439 // end of parse address functionality
445 * @param CRM_Core_Form $form
448 public static function storeRequiredCustomDataInfo(&$form, $groupTree) {
449 if (CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Contact') {
450 $requireOmission = NULL;
451 foreach ($groupTree as $csId => $csVal) {
452 // only process Address entity fields
453 if ($csVal['extends'] != 'Address') {
457 foreach ($csVal['fields'] as $cdId => $cdVal) {
458 if ($cdVal['is_required']) {
459 $elementName = $cdVal['element_name'];
460 if (in_array($elementName, $form->_required
)) {
461 // store the omitted rule for a element, to be used later on
462 $requireOmission .= $cdVal['element_custom_name'] . ',';
468 $form->_addressRequireOmission
= rtrim($requireOmission, ',');