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