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