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