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