3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * This class is used to build address block.
37 class CRM_Contact_Form_Edit_Address
{
40 * Build form for address input fields.
42 * @param CRM_Core_Form $form
43 * @param int $addressBlockCount
44 * The index of the address array (if multiple addresses on a page).
45 * @param bool $sharing
46 * False, if we want to skip the address sharing features.
47 * @param bool $inlineEdit
48 * True when edit used in inline edit.
50 public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
51 // passing this via the session is AWFUL. we need to fix this
52 if (!$addressBlockCount) {
53 $blockId = ($form->get('Address_Block_Count')) ?
$form->get('Address_Block_Count') : 1;
56 $blockId = $addressBlockCount;
59 $form->applyFilter('__ALL__', 'trim');
63 $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL);
66 //make location type required for inline edit
67 $form->addField("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight', 'option_url' => NULL) +
$js, $inlineEdit);
69 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
73 "address[$blockId][is_primary]", array(
74 'entity' => 'address',
75 'label' => ts('Primary location for this contact'),
76 'text' => ts('Primary location for this contact')) +
$js);
79 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
83 "address[$blockId][is_billing]", array(
84 'entity' => 'address',
85 'label' => ts('Billing location for this contact'),
86 'text' => ts('Billing location for this contact')) +
$js);
88 // hidden element to store master address id
89 $form->addField("address[$blockId][master_id]", array('entity' => 'address', 'type' => 'hidden'));
90 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
91 'address_options', TRUE, NULL, TRUE
97 'supplemental_address_1',
98 'supplemental_address_2',
99 'supplemental_address_3',
102 'postal_code_suffix',
113 foreach ($elements as $name) {
114 //Remove id from name, to allow comparison against enabled addressOptions.
115 $nameWithoutID = strpos($name, '_id') !== FALSE ?
substr($name, 0, -3) : $name;
116 // Skip fields which are not enabled in the address options.
117 if (empty($addressOptions[$nameWithoutID])) {
119 //Don't skip street parsed fields when parsing is enabled.
120 if (in_array($nameWithoutID, array(
124 )) && !empty($addressOptions['street_address_parsing'])
132 if ($name == 'address_name') {
136 $params = array('entity' => 'address');
138 if ($name == 'postal_code_suffix') {
139 $params['label'] = ts('Suffix');
142 $form->addField("address[$blockId][$name]", $params);
146 if (!empty($form->_values
['address']) && !empty($form->_values
['address'][$blockId])) {
147 $entityId = $form->_values
['address'][$blockId]['id'];
150 // CRM-11665 geocode override option
152 if (CRM_Utils_GeocodeProvider
::getUsableClassName()) {
154 $form->addElement('checkbox',
155 "address[$blockId][manual_geo_code]",
156 ts('Override automatic geocoding')
159 $form->assign('geoCode', $geoCode);
161 self
::addCustomDataToForm($form, $entityId, $blockId);
165 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
167 // Override the default profile links to add address form
168 $profileLinks = CRM_Contact_BAO_Contact
::getEntityRefCreateLinks('shared_address');
169 $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), ['create' => $profileLinks, 'api' => ['extra' => ['contact_type']]]);
171 // do we want to update employer for shared address
172 $form->addElement('checkbox', "address[$blockId][update_current_employer]", NULL, ts('Set this organization as current employer'));
177 * Check for correct state / country mapping.
179 * @param array $fields
180 * @param array $files
181 * @param CRM_Core_Form $self
186 public static function formRule($fields, $files = array(), $self = NULL) {
189 $customDataRequiredFields = array();
190 if ($self && property_exists($self, '_addressRequireOmission')) {
191 $customDataRequiredFields = explode(',', $self->_addressRequireOmission
);
194 if (!empty($fields['address']) && is_array($fields['address'])) {
195 foreach ($fields['address'] as $instance => $addressValues) {
197 if (CRM_Utils_System
::isNull($addressValues)) {
198 // DETACH 'required' form rule error to
199 // custom data only if address data not exists upon submission
200 if (!empty($customDataRequiredFields)) {
201 foreach ($customDataRequiredFields as $customElementName) {
202 $elementName = "address[$instance][$customElementName]";
203 if ($self->getElementError($elementName)) {
204 // set element error to none
205 $self->setElementError($elementName, NULL);
212 // DETACH 'required' form rule error to
213 // custom data if address data not exists upon submission
214 // or if master address is selected
215 if (!empty($customDataRequiredFields) && (!CRM_Core_BAO_Address
::dataExists($addressValues) ||
!empty($addressValues['master_id']))) {
216 foreach ($customDataRequiredFields as $customElementName) {
217 $elementName = "address[$instance][$customElementName]";
218 if ($self->getElementError($elementName)) {
219 // set element error to none
220 $self->setElementError($elementName, NULL);
225 if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) {
226 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
231 return empty($errors) ?
TRUE : $errors;
235 * Set default values for address block.
237 * @param array $defaults
238 * Defaults associated array.
239 * @param CRM_Core_Form $form
242 public static function setDefaultValues(&$defaults, &$form) {
243 $addressValues = array();
244 if (isset($defaults['address']) && is_array($defaults['address']) &&
245 !CRM_Utils_System
::isNull($defaults['address'])
248 // start of contact shared adddress defaults
249 $sharedAddresses = array();
250 $masterAddress = array();
252 // get contact name of shared contact names
253 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils
::getAddressShareContactNames($defaults['address']);
255 foreach ($defaults['address'] as $key => $addressValue) {
256 if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
257 $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id'];
258 $sharedAddresses[$key]['shared_address_display'] = array(
259 'address' => $addressValue['display'],
260 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
261 'options' => CRM_Core_BAO_Address
::getValues(array(
262 'entity_id' => $master_cid,
263 'contact_id' => $master_cid,
265 'master_id' => $addressValue['master_id'],
267 $defaults['address'][$key]['master_contact_id'] = $master_cid;
270 $defaults['address'][$key]['use_shared_address'] = 0;
273 //check if any address is shared by any other contacts
274 $masterAddress[$key] = CRM_Core_BAO_Address
::checkContactSharedAddress($addressValue['id']);
277 $form->assign('sharedAddresses', $sharedAddresses);
278 $form->assign('masterAddress', $masterAddress);
279 // end of shared address defaults
281 // start of parse address functionality
282 // build street address, CRM-5450.
283 if ($form->_parseStreetAddress
) {
284 $parseFields = array('street_address', 'street_number', 'street_name', 'street_unit');
285 foreach ($defaults['address'] as $cnt => & $address) {
286 $streetAddress = NULL;
289 'street_number_suffix',
293 if (in_array($fld, array(
297 $streetAddress .= ' ';
299 // CRM-17619 - if the street number suffix begins with a number, add a space
300 $numsuffix = CRM_Utils_Array
::value($fld, $address);
301 if ($fld === 'street_number_suffix' && !empty($numsuffix)) {
302 if (ctype_digit(substr($numsuffix, 0, 1))) {
303 $streetAddress .= ' ';
306 $streetAddress .= CRM_Utils_Array
::value($fld, $address);
308 $streetAddress = trim($streetAddress);
309 if (!empty($streetAddress)) {
310 $address['street_address'] = $streetAddress;
312 if (isset($address['street_number'])) {
313 // CRM-17619 - if the street number suffix begins with a number, add a space
314 $thesuffix = CRM_Utils_Array
::value('street_number_suffix', $address);
316 if (ctype_digit(substr($thesuffix, 0, 1))) {
317 $address['street_number'] .= " ";
320 $address['street_number'] .= $thesuffix;
322 // build array for set default.
323 foreach ($parseFields as $field) {
324 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array
::value($field, $address);
326 // don't load fields, use js to populate.
327 foreach (array('street_number', 'street_name', 'street_unit') as $f) {
328 if (isset($address[$f])) {
333 $form->assign('allAddressFieldValues', json_encode($addressValues));
335 //hack to handle show/hide address fields.
336 $parsedAddress = array();
337 if ($form->_contactId
&& !empty($_POST['address']) && is_array($_POST['address'])
339 foreach ($_POST['address'] as $cnt => $values) {
340 $showField = 'streetAddress';
341 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
342 if (!empty($values[$fld])) {
343 $showField = 'addressElements';
347 $parsedAddress[$cnt] = $showField;
350 $form->assign('showHideAddressFields', $parsedAddress);
351 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ?
FALSE : TRUE);
353 // end of parse address functionality
358 * Store required custom data info.
360 * @param CRM_Core_Form $form
361 * @param array $groupTree
363 public static function storeRequiredCustomDataInfo(&$form, $groupTree) {
364 if (in_array(CRM_Utils_System
::getClassName($form), array('CRM_Contact_Form_Contact', 'CRM_Contact_Form_Inline_Address'))) {
365 $requireOmission = NULL;
366 foreach ($groupTree as $csId => $csVal) {
367 // only process Address entity fields
368 if ($csVal['extends'] != 'Address') {
372 foreach ($csVal['fields'] as $cdId => $cdVal) {
373 if ($cdVal['is_required']) {
374 $elementName = $cdVal['element_name'];
375 if (in_array($elementName, $form->_required
)) {
376 // store the omitted rule for a element, to be used later on
377 $requireOmission .= $cdVal['element_custom_name'] . ',';
383 $form->_addressRequireOmission
= rtrim($requireOmission, ',');
388 * Add custom data to the form.
390 * @param CRM_Core_Form $form
391 * @param int $entityId
392 * @param int $blockId
394 protected static function addCustomDataToForm(&$form, $entityId, $blockId) {
395 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Address', NULL, $entityId);
397 if (isset($groupTree) && is_array($groupTree)) {
398 // use simplified formatted groupTree
399 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
401 // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
402 foreach ($groupTree as $id => $group) {
403 foreach ($group['fields'] as $fldId => $field) {
404 $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
405 $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]";
410 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
412 // since we change element name for address custom data, we need to format the setdefault values
413 $addressDefaults = array();
414 foreach ($defaults as $key => $val) {
419 // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
420 // this works for all types custom data
421 $keyValues = explode('[', str_replace(']', '', $key));
422 $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
425 $form->setDefaults($addressDefaults);
427 // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
428 // And we can't set it to 'address_' because we want to set it in a slightly different format.
429 CRM_Core_BAO_CustomGroup
::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
431 // during contact editing : if no address is filled
432 // required custom data must not produce 'required' form rule error
433 // more handling done in formRule func
434 CRM_Contact_Form_Edit_Address
::storeRequiredCustomDataInfo($form, $groupTree);
436 $tplGroupTree = CRM_Core_Smarty
::singleton()
437 ->get_template_vars('address_groupTree');
438 $tplGroupTree = empty($tplGroupTree) ?
array() : $tplGroupTree;
440 $form->assign('address_groupTree', $tplGroupTree +
array($blockId => $groupTree));
441 // unset the temp smarty var that got created
442 $form->assign('dnc_groupTree', NULL);
444 // address custom data processing ends ..