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