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