Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | /** | |
100fef9d | 42 | * Build form for address input fields |
6a488035 | 43 | * |
c490a46a | 44 | * @param CRM_Core_Form $form |
77c5b619 TO |
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. | |
6a488035 | 51 | * |
355ba699 | 52 | * @return void |
6a488035 | 53 | * |
6a488035 | 54 | */ |
00be9182 | 55 | public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) { |
6a488035 TO |
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(); | |
0bdc50a1 | 70 | if (!$inlineEdit) { |
dd7240b1 | 71 | $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL); |
6a488035 TO |
72 | } |
73 | ||
dd7240b1 | 74 | //make location type required for inline edit |
353ffa53 TO |
75 | $form->addSelect("address[$blockId][location_type_id]", array( |
76 | 'entity' => 'address', | |
77 | 'class' => 'eight' | |
78 | ) + $js, $inlineEdit); | |
6a488035 | 79 | |
0bdc50a1 CW |
80 | if (!$inlineEdit) { |
81 | $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );'); | |
6a488035 | 82 | } |
6a488035 TO |
83 | |
84 | $form->addElement( | |
85 | 'checkbox', | |
86 | "address[$blockId][is_primary]", | |
87 | ts('Primary location for this contact'), | |
88 | ts('Primary location for this contact'), | |
89 | $js | |
90 | ); | |
91 | ||
dd7240b1 CW |
92 | if (!$inlineEdit) { |
93 | $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );'); | |
6a488035 TO |
94 | } |
95 | ||
96 | $form->addElement( | |
97 | 'checkbox', | |
98 | "address[$blockId][is_billing]", | |
99 | ts('Billing location for this contact'), | |
100 | ts('Billing location for this contact'), | |
101 | $js | |
102 | ); | |
103 | ||
104 | // hidden element to store master address id | |
105 | $form->addElement('hidden', "address[$blockId][master_id]"); | |
106 | ||
107 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
108 | 'address_options', TRUE, NULL, TRUE | |
109 | ); | |
110 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address'); | |
111 | ||
112 | $elements = array( | |
113 | 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL), | |
114 | 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL), | |
2dc3fbdc DG |
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), | |
6a488035 | 117 | 'city' => array(ts('City'), $attributes['city'], NULL), |
353ffa53 TO |
118 | 'postal_code' => array( |
119 | ts('Zip / Postal Code'), | |
120 | array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')), | |
121 | NULL | |
122 | ), | |
123 | 'postal_code_suffix' => array( | |
124 | ts('Postal Code Suffix'), | |
125 | array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'), | |
126 | NULL | |
127 | ), | |
c927c151 | 128 | 'country_id' => array(ts('Country'), $attributes['country_id'], 'country'), |
757069de | 129 | 'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL), |
c927c151 | 130 | 'county_id' => array(ts('County'), $attributes['county_id'], NULL), |
6a488035 TO |
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), | |
136 | ); | |
137 | ||
6a488035 TO |
138 | foreach ($elements as $name => $v) { |
139 | list($title, $attributes, $select) = $v; | |
140 | ||
141 | $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name; | |
a7488080 | 142 | if (empty($addressOptions[$nameWithoutID])) { |
6a488035 TO |
143 | $continue = TRUE; |
144 | if (in_array($nameWithoutID, array( | |
353ffa53 TO |
145 | 'street_number', |
146 | 'street_name', | |
147 | 'street_unit' | |
148 | )) && !empty($addressOptions['street_address_parsing']) | |
149 | ) { | |
6a488035 TO |
150 | $continue = FALSE; |
151 | } | |
152 | if ($continue) { | |
153 | continue; | |
154 | } | |
155 | } | |
156 | ||
6a488035 TO |
157 | //build normal select if country is not present in address block |
158 | if ($name == 'state_province_id' && !$addressOptions['country']) { | |
159 | $select = 'stateProvince'; | |
160 | } | |
161 | ||
162 | if (!$select) { | |
c927c151 CW |
163 | if ($name == 'state_province_id' || $name == 'county_id') { |
164 | $form->addChainSelect("address[$blockId][$name]"); | |
6a488035 TO |
165 | } |
166 | else { | |
167 | if ($name == 'address_name') { | |
168 | $name = 'name'; | |
169 | } | |
170 | ||
171 | $form->addElement('text', | |
172 | "address[$blockId][$name]", | |
173 | $title, | |
174 | $attributes | |
175 | ); | |
176 | } | |
177 | } | |
178 | else { | |
179 | $form->addElement('select', | |
180 | "address[$blockId][$name]", | |
181 | $title, | |
182 | array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select() | |
183 | ); | |
184 | } | |
185 | } | |
186 | ||
6a488035 | 187 | $entityId = NULL; |
8cc574cf | 188 | if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) { |
6a488035 TO |
189 | $entityId = $form->_values['address'][$blockId]['id']; |
190 | } | |
191 | ||
192 | // CRM-11665 geocode override option | |
193 | $geoCode = FALSE; | |
194 | if (!empty($config->geocodeMethod)) { | |
195 | $geoCode = TRUE; | |
196 | $form->addElement('checkbox', | |
197 | "address[$blockId][manual_geo_code]", | |
198 | ts('Override automatic geocoding') | |
199 | ); | |
200 | } | |
201 | $form->assign('geoCode', $geoCode); | |
202 | ||
203 | // Process any address custom data - | |
204 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', | |
205 | $form, | |
206 | $entityId | |
207 | ); | |
208 | ||
209 | if (isset($groupTree) && is_array($groupTree)) { | |
210 | // use simplified formatted groupTree | |
211 | $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form); | |
212 | ||
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']}]"; | |
218 | } | |
219 | } | |
220 | ||
221 | $defaults = array(); | |
222 | CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults); | |
223 | ||
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) { | |
481a74f4 | 227 | if (empty($val)) { |
6a488035 TO |
228 | continue; |
229 | } | |
230 | ||
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; | |
235 | } | |
236 | ||
237 | $form->setDefaults($addressDefaults); | |
238 | ||
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. | |
7b226831 | 241 | CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); |
6a488035 | 242 | |
ac79e2f5 PJ |
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 | |
246 | if (!$inlineEdit) { | |
1d961366 | 247 | CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree); |
ac79e2f5 PJ |
248 | } |
249 | ||
353ffa53 | 250 | $template = CRM_Core_Smarty::singleton(); |
6a488035 | 251 | $tplGroupTree = $template->get_template_vars('address_groupTree'); |
d3e86119 | 252 | $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree; |
6a488035 TO |
253 | |
254 | $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree)); | |
255 | // unset the temp smarty var that got created | |
256 | $form->assign('dnc_groupTree', NULL); | |
257 | } | |
258 | // address custom data processing ends .. | |
259 | ||
260 | if ($sharing) { | |
261 | // shared address | |
262 | $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address')); | |
263 | ||
1a90e0dd | 264 | // Override the default profile links to add address form |
353ffa53 TO |
265 | $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array( |
266 | 'new_individual', | |
267 | 'new_organization', | |
268 | 'new_household' | |
269 | ), 'shared_address'); | |
1a90e0dd | 270 | $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks)); |
6a488035 TO |
271 | } |
272 | } | |
273 | ||
274 | /** | |
100fef9d | 275 | * Check for correct state / country mapping. |
6a488035 | 276 | * |
dd244018 EM |
277 | * @param $fields |
278 | * @param $files | |
279 | * @param $self | |
280 | * | |
72b3a70c CW |
281 | * @return array|bool |
282 | * if no errors | |
6a488035 | 283 | * |
6a488035 | 284 | */ |
00be9182 | 285 | public static function formRule($fields, $files, $self) { |
6a488035 | 286 | $errors = array(); |
ac79e2f5 PJ |
287 | |
288 | $customDataRequiredFields = array(); | |
233e6285 | 289 | if ($self && property_exists($self, '_addressRequireOmission')) { |
ac79e2f5 PJ |
290 | $customDataRequiredFields = explode(',', $self->_addressRequireOmission); |
291 | } | |
292 | ||
a7488080 | 293 | if (!empty($fields['address']) && is_array($fields['address'])) { |
6a488035 | 294 | foreach ($fields['address'] as $instance => $addressValues) { |
1d961366 | 295 | |
6a488035 | 296 | if (CRM_Utils_System::isNull($addressValues)) { |
1d961366 PJ |
297 | // DETACH 'required' form rule error to |
298 | // custom data only if address data not exists upon submission | |
299 | if (!empty($customDataRequiredFields)) { | |
22e263ad | 300 | foreach ($customDataRequiredFields as $customElementName) { |
1d961366 PJ |
301 | $elementName = "address[$instance][$customElementName]"; |
302 | if ($self->getElementError($elementName)) { | |
303 | // set element error to none | |
304 | $self->setElementError($elementName, NULL); | |
305 | } | |
306 | } | |
307 | } | |
6a488035 TO |
308 | continue; |
309 | } | |
310 | ||
1d961366 PJ |
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)) { | |
22e263ad | 314 | foreach ($customDataRequiredFields as $customElementName) { |
1d961366 PJ |
315 | $elementName = "address[$instance][$customElementName]"; |
316 | if ($self->getElementError($elementName)) { | |
317 | // set element error to none | |
318 | $self->setElementError($elementName, NULL); | |
ac79e2f5 PJ |
319 | } |
320 | } | |
321 | } | |
322 | ||
8cc574cf | 323 | if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) { |
6a488035 TO |
324 | $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.'); |
325 | } | |
326 | } | |
327 | } | |
328 | ||
329 | return empty($errors) ? TRUE : $errors; | |
330 | } | |
331 | ||
6a488035 | 332 | /** |
100fef9d | 333 | * Set default values for address block |
6a488035 | 334 | * |
77c5b619 TO |
335 | * @param array $defaults |
336 | * Defaults associated array. | |
337 | * @param CRM_Core_Form $form | |
338 | * Form object. | |
6a488035 | 339 | * |
6a488035 | 340 | */ |
481a74f4 | 341 | public static function setDefaultValues(&$defaults, &$form) { |
6a488035 TO |
342 | $addressValues = array(); |
343 | if (isset($defaults['address']) && is_array($defaults['address']) && | |
395d8dc6 | 344 | !CRM_Utils_System::isNull($defaults['address']) |
6a488035 TO |
345 | ) { |
346 | ||
347 | // start of contact shared adddress defaults | |
348 | $sharedAddresses = array(); | |
349 | $masterAddress = array(); | |
350 | ||
351 | // get contact name of shared contact names | |
352 | $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils::getAddressShareContactNames($defaults['address']); | |
353 | ||
354 | foreach ($defaults['address'] as $key => $addressValue) { | |
a7488080 | 355 | if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) { |
88820b18 | 356 | $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id']; |
6a488035 TO |
357 | $sharedAddresses[$key]['shared_address_display'] = array( |
358 | 'address' => $addressValue['display'], | |
359 | 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'], | |
353ffa53 TO |
360 | 'options' => CRM_Core_BAO_Address::getValues(array( |
361 | 'entity_id' => $master_cid, | |
362 | 'contact_id' => $master_cid | |
363 | )), | |
88820b18 | 364 | 'master_id' => $addressValue['master_id'], |
6a488035 | 365 | ); |
88820b18 | 366 | $defaults['address'][$key]['master_contact_id'] = $master_cid; |
6a488035 TO |
367 | } |
368 | else { | |
369 | $defaults['address'][$key]['use_shared_address'] = 0; | |
370 | } | |
371 | ||
372 | //check if any address is shared by any other contacts | |
373 | $masterAddress[$key] = CRM_Core_BAO_Address::checkContactSharedAddress($addressValue['id']); | |
374 | } | |
375 | ||
376 | $form->assign('sharedAddresses', $sharedAddresses); | |
377 | $form->assign('masterAddress', $masterAddress); | |
378 | // end of shared address defaults | |
379 | ||
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; | |
386 | foreach (array( | |
353ffa53 TO |
387 | 'street_number', |
388 | 'street_number_suffix', | |
389 | 'street_name', | |
390 | 'street_unit' | |
391 | ) as $fld) { | |
6a488035 | 392 | if (in_array($fld, array( |
353ffa53 TO |
393 | 'street_name', |
394 | 'street_unit' | |
395 | ))) { | |
6a488035 TO |
396 | $streetAddress .= ' '; |
397 | } | |
398 | $streetAddress .= CRM_Utils_Array::value($fld, $address); | |
399 | } | |
400 | $streetAddress = trim($streetAddress); | |
401 | if (!empty($streetAddress)) { | |
402 | $address['street_address'] = $streetAddress; | |
403 | } | |
404 | if (isset($address['street_number'])) { | |
405 | $address['street_number'] .= CRM_Utils_Array::value('street_number_suffix', $address); | |
406 | } | |
407 | ||
408 | // build array for set default. | |
409 | foreach ($parseFields as $field) { | |
410 | $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array::value($field, $address); | |
411 | } | |
6a488035 TO |
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])) { | |
415 | unset($address[$f]); | |
416 | } | |
417 | } | |
418 | } | |
419 | $form->assign('allAddressFieldValues', json_encode($addressValues)); | |
420 | ||
421 | //hack to handle show/hide address fields. | |
422 | $parsedAddress = array(); | |
8cc574cf | 423 | if ($form->_contactId && !empty($_POST['address']) && is_array($_POST['address']) |
6a488035 TO |
424 | ) { |
425 | foreach ($_POST['address'] as $cnt => $values) { | |
426 | $showField = 'streetAddress'; | |
427 | foreach (array('street_number', 'street_name', 'street_unit') as $fld) { | |
a7488080 | 428 | if (!empty($values[$fld])) { |
6a488035 TO |
429 | $showField = 'addressElements'; |
430 | break; | |
431 | } | |
432 | } | |
433 | $parsedAddress[$cnt] = $showField; | |
434 | } | |
435 | } | |
436 | $form->assign('showHideAddressFields', $parsedAddress); | |
437 | $form->assign('loadShowHideAddressFields', empty($parsedAddress) ? FALSE : TRUE); | |
438 | } | |
439 | // end of parse address functionality | |
440 | } | |
441 | } | |
6a488035 | 442 | |
1d961366 | 443 | |
86538308 | 444 | /** |
c490a46a | 445 | * @param CRM_Core_Form $form |
86538308 EM |
446 | * @param $groupTree |
447 | */ | |
00be9182 | 448 | public static function storeRequiredCustomDataInfo(&$form, $groupTree) { |
1d961366 PJ |
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') { | |
454 | continue; | |
455 | } | |
456 | ||
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'] . ','; | |
463 | } | |
464 | } | |
465 | } | |
466 | } | |
467 | ||
468 | $form->_addressRequireOmission = rtrim($requireOmission, ','); | |
469 | } | |
470 | } | |
355ba699 | 471 | } |