INFRA-132 - Copyright header - Drupal.WhiteSpace.ScopeIndent.IncorrectExact
[civicrm-core.git] / CRM / Contact / Form / Edit / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
42 * Build form for address input fields
43 *
44 * @param CRM_Core_Form $form
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.
51 *
52 * @return void
53 *
54 */
55 public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
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();
70 if (!$inlineEdit) {
71 $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL);
72 }
73
74 //make location type required for inline edit
75 $form->addSelect("address[$blockId][location_type_id]", array(
76 'entity' => 'address',
77 'class' => 'eight',
78 ) + $js, $inlineEdit);
79
80 if (!$inlineEdit) {
81 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
82 }
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
92 if (!$inlineEdit) {
93 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
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),
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),
117 'city' => array(ts('City'), $attributes['city'], NULL),
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 ),
128 'country_id' => array(ts('Country'), $attributes['country_id'], 'country'),
129 'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL),
130 'county_id' => array(ts('County'), $attributes['county_id'], NULL),
131 'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 12), NULL),
132 'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 12), 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
138 foreach ($elements as $name => $v) {
139 list($title, $attributes, $select) = $v;
140
141 $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name;
142 if (empty($addressOptions[$nameWithoutID])) {
143 $continue = TRUE;
144 if (in_array($nameWithoutID, array(
145 'street_number',
146 'street_name',
147 'street_unit',
148 )) && !empty($addressOptions['street_address_parsing'])
149 ) {
150 $continue = FALSE;
151 }
152 if ($continue) {
153 continue;
154 }
155 }
156
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) {
163 if ($name == 'state_province_id' || $name == 'county_id') {
164 $form->addChainSelect("address[$blockId][$name]");
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
187 $entityId = NULL;
188 if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) {
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) {
227 if (empty($val)) {
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.
241 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
242
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) {
247 CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree);
248 }
249
250 $template = CRM_Core_Smarty::singleton();
251 $tplGroupTree = $template->get_template_vars('address_groupTree');
252 $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree;
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
264 // Override the default profile links to add address form
265 $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array(
266 'new_individual',
267 'new_organization',
268 'new_household',
269 ), 'shared_address');
270 $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
271 }
272 }
273
274 /**
275 * Check for correct state / country mapping.
276 *
277 * @param $fields
278 * @param $files
279 * @param $self
280 *
281 * @return array|bool
282 * if no errors
283 *
284 */
285 public static function formRule($fields, $files, $self) {
286 $errors = array();
287
288 $customDataRequiredFields = array();
289 if ($self && property_exists($self, '_addressRequireOmission')) {
290 $customDataRequiredFields = explode(',', $self->_addressRequireOmission);
291 }
292
293 if (!empty($fields['address']) && is_array($fields['address'])) {
294 foreach ($fields['address'] as $instance => $addressValues) {
295
296 if (CRM_Utils_System::isNull($addressValues)) {
297 // DETACH 'required' form rule error to
298 // custom data only if address data not exists upon submission
299 if (!empty($customDataRequiredFields)) {
300 foreach ($customDataRequiredFields as $customElementName) {
301 $elementName = "address[$instance][$customElementName]";
302 if ($self->getElementError($elementName)) {
303 // set element error to none
304 $self->setElementError($elementName, NULL);
305 }
306 }
307 }
308 continue;
309 }
310
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)) {
314 foreach ($customDataRequiredFields as $customElementName) {
315 $elementName = "address[$instance][$customElementName]";
316 if ($self->getElementError($elementName)) {
317 // set element error to none
318 $self->setElementError($elementName, NULL);
319 }
320 }
321 }
322
323 if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) {
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
332 /**
333 * Set default values for address block
334 *
335 * @param array $defaults
336 * Defaults associated array.
337 * @param CRM_Core_Form $form
338 * Form object.
339 *
340 */
341 public static function setDefaultValues(&$defaults, &$form) {
342 $addressValues = array();
343 if (isset($defaults['address']) && is_array($defaults['address']) &&
344 !CRM_Utils_System::isNull($defaults['address'])
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) {
355 if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
356 $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id'];
357 $sharedAddresses[$key]['shared_address_display'] = array(
358 'address' => $addressValue['display'],
359 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
360 'options' => CRM_Core_BAO_Address::getValues(array(
361 'entity_id' => $master_cid,
362 'contact_id' => $master_cid,
363 )),
364 'master_id' => $addressValue['master_id'],
365 );
366 $defaults['address'][$key]['master_contact_id'] = $master_cid;
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(
387 'street_number',
388 'street_number_suffix',
389 'street_name',
390 'street_unit',
391 ) as $fld) {
392 if (in_array($fld, array(
393 'street_name',
394 'street_unit',
395 ))) {
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 }
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();
423 if ($form->_contactId && !empty($_POST['address']) && is_array($_POST['address'])
424 ) {
425 foreach ($_POST['address'] as $cnt => $values) {
426 $showField = 'streetAddress';
427 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
428 if (!empty($values[$fld])) {
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 }
442
443
444 /**
445 * @param CRM_Core_Form $form
446 * @param $groupTree
447 */
448 public static function storeRequiredCustomDataInfo(&$form, $groupTree) {
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 }
471
472 }