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 | ||
999ab5e1 | 145 | $form->addField("address[$blockId][$name]", array('entity' => 'address')); |
6a488035 TO |
146 | } |
147 | ||
6a488035 | 148 | $entityId = NULL; |
8cc574cf | 149 | if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) { |
6a488035 TO |
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) { | |
481a74f4 | 188 | if (empty($val)) { |
6a488035 TO |
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. | |
7b226831 | 202 | CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_'); |
6a488035 | 203 | |
ac79e2f5 PJ |
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) { | |
1d961366 | 208 | CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree); |
ac79e2f5 PJ |
209 | } |
210 | ||
353ffa53 | 211 | $template = CRM_Core_Smarty::singleton(); |
6a488035 | 212 | $tplGroupTree = $template->get_template_vars('address_groupTree'); |
d3e86119 | 213 | $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree; |
6a488035 TO |
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 | ||
1a90e0dd | 225 | // Override the default profile links to add address form |
353ffa53 TO |
226 | $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array( |
227 | 'new_individual', | |
228 | 'new_organization', | |
7c550ca0 | 229 | 'new_household', |
353ffa53 | 230 | ), 'shared_address'); |
1a90e0dd | 231 | $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks)); |
6a488035 TO |
232 | } |
233 | } | |
234 | ||
235 | /** | |
100fef9d | 236 | * Check for correct state / country mapping. |
6a488035 | 237 | * |
dd244018 EM |
238 | * @param $fields |
239 | * @param $files | |
240 | * @param $self | |
241 | * | |
72b3a70c CW |
242 | * @return array|bool |
243 | * if no errors | |
6a488035 | 244 | * |
6a488035 | 245 | */ |
00be9182 | 246 | public static function formRule($fields, $files, $self) { |
6a488035 | 247 | $errors = array(); |
ac79e2f5 PJ |
248 | |
249 | $customDataRequiredFields = array(); | |
233e6285 | 250 | if ($self && property_exists($self, '_addressRequireOmission')) { |
ac79e2f5 PJ |
251 | $customDataRequiredFields = explode(',', $self->_addressRequireOmission); |
252 | } | |
253 | ||
a7488080 | 254 | if (!empty($fields['address']) && is_array($fields['address'])) { |
6a488035 | 255 | foreach ($fields['address'] as $instance => $addressValues) { |
1d961366 | 256 | |
6a488035 | 257 | if (CRM_Utils_System::isNull($addressValues)) { |
1d961366 PJ |
258 | // DETACH 'required' form rule error to |
259 | // custom data only if address data not exists upon submission | |
260 | if (!empty($customDataRequiredFields)) { | |
22e263ad | 261 | foreach ($customDataRequiredFields as $customElementName) { |
1d961366 PJ |
262 | $elementName = "address[$instance][$customElementName]"; |
263 | if ($self->getElementError($elementName)) { | |
264 | // set element error to none | |
265 | $self->setElementError($elementName, NULL); | |
266 | } | |
267 | } | |
268 | } | |
6a488035 TO |
269 | continue; |
270 | } | |
271 | ||
1d961366 PJ |
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)) { | |
22e263ad | 275 | foreach ($customDataRequiredFields as $customElementName) { |
1d961366 PJ |
276 | $elementName = "address[$instance][$customElementName]"; |
277 | if ($self->getElementError($elementName)) { | |
278 | // set element error to none | |
279 | $self->setElementError($elementName, NULL); | |
ac79e2f5 PJ |
280 | } |
281 | } | |
282 | } | |
283 | ||
8cc574cf | 284 | if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) { |
6a488035 TO |
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 | ||
6a488035 | 293 | /** |
fe482240 | 294 | * Set default values for address block. |
6a488035 | 295 | * |
77c5b619 TO |
296 | * @param array $defaults |
297 | * Defaults associated array. | |
298 | * @param CRM_Core_Form $form | |
299 | * Form object. | |
6a488035 | 300 | * |
6a488035 | 301 | */ |
481a74f4 | 302 | public static function setDefaultValues(&$defaults, &$form) { |
6a488035 TO |
303 | $addressValues = array(); |
304 | if (isset($defaults['address']) && is_array($defaults['address']) && | |
395d8dc6 | 305 | !CRM_Utils_System::isNull($defaults['address']) |
6a488035 TO |
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) { | |
a7488080 | 316 | if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) { |
88820b18 | 317 | $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id']; |
6a488035 TO |
318 | $sharedAddresses[$key]['shared_address_display'] = array( |
319 | 'address' => $addressValue['display'], | |
320 | 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'], | |
353ffa53 TO |
321 | 'options' => CRM_Core_BAO_Address::getValues(array( |
322 | 'entity_id' => $master_cid, | |
7c550ca0 | 323 | 'contact_id' => $master_cid, |
353ffa53 | 324 | )), |
88820b18 | 325 | 'master_id' => $addressValue['master_id'], |
6a488035 | 326 | ); |
88820b18 | 327 | $defaults['address'][$key]['master_contact_id'] = $master_cid; |
6a488035 TO |
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( | |
353ffa53 TO |
348 | 'street_number', |
349 | 'street_number_suffix', | |
350 | 'street_name', | |
7c550ca0 | 351 | 'street_unit', |
353ffa53 | 352 | ) as $fld) { |
6a488035 | 353 | if (in_array($fld, array( |
353ffa53 | 354 | 'street_name', |
7c550ca0 | 355 | 'street_unit', |
353ffa53 | 356 | ))) { |
6a488035 TO |
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 | } | |
6a488035 TO |
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(); | |
8cc574cf | 384 | if ($form->_contactId && !empty($_POST['address']) && is_array($_POST['address']) |
6a488035 TO |
385 | ) { |
386 | foreach ($_POST['address'] as $cnt => $values) { | |
387 | $showField = 'streetAddress'; | |
388 | foreach (array('street_number', 'street_name', 'street_unit') as $fld) { | |
a7488080 | 389 | if (!empty($values[$fld])) { |
6a488035 TO |
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 | } | |
6a488035 | 403 | |
1d961366 | 404 | |
86538308 | 405 | /** |
c490a46a | 406 | * @param CRM_Core_Form $form |
86538308 EM |
407 | * @param $groupTree |
408 | */ | |
00be9182 | 409 | public static function storeRequiredCustomDataInfo(&$form, $groupTree) { |
1d961366 PJ |
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 | } | |
96025800 | 432 | |
355ba699 | 433 | } |