3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class is used to build address block
39 class CRM_Contact_Form_Edit_Address
{
42 * Build form for address input fields
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.
56 public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
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;
62 $blockId = $addressBlockCount;
65 $config = CRM_Core_Config
::singleton();
66 $countryDefault = $config->defaultContactCountry
;
68 $form->applyFilter('__ALL__', 'trim');
72 $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL);
75 //make location type required for inline edit
76 $form->addSelect("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') +
$js, $inlineEdit);
79 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
84 "address[$blockId][is_primary]",
85 ts('Primary location for this contact'),
86 ts('Primary location for this contact'),
91 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
96 "address[$blockId][is_billing]",
97 ts('Billing location for this contact'),
98 ts('Billing location for this contact'),
102 // hidden element to store master address id
103 $form->addElement('hidden', "address[$blockId][master_id]");
105 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
106 'address_options', TRUE, NULL, TRUE
108 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Address');
111 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL),
112 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL),
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),
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),
118 'country_id' => array(ts('Country'), $attributes['country_id'], 'country'),
119 'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL),
120 'county_id' => array(ts('County'), $attributes['county_id'], NULL),
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),
128 foreach ($elements as $name => $v) {
129 list($title, $attributes, $select) = $v;
131 $nameWithoutID = strpos($name, '_id') !== FALSE ?
substr($name, 0, -3) : $name;
132 if (empty($addressOptions[$nameWithoutID])) {
134 if (in_array($nameWithoutID, array(
135 'street_number', 'street_name', 'street_unit')) && !empty($addressOptions['street_address_parsing'])) {
143 //build normal select if country is not present in address block
144 if ($name == 'state_province_id' && !$addressOptions['country']) {
145 $select = 'stateProvince';
149 if ($name == 'state_province_id' ||
$name == 'county_id') {
150 $form->addChainSelect("address[$blockId][$name]");
153 if ($name == 'address_name') {
157 $form->addElement('text',
158 "address[$blockId][$name]",
165 $form->addElement('select',
166 "address[$blockId][$name]",
168 array('' => ts('- select -')) + CRM_Core_PseudoConstant
::$select()
174 if (!empty($form->_values
['address']) && !empty($form->_values
['address'][$blockId])) {
175 $entityId = $form->_values
['address'][$blockId]['id'];
178 // CRM-11665 geocode override option
180 if (!empty($config->geocodeMethod
)) {
182 $form->addElement('checkbox',
183 "address[$blockId][manual_geo_code]",
184 ts('Override automatic geocoding')
187 $form->assign('geoCode', $geoCode);
189 // Process any address custom data -
190 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Address',
195 if (isset($groupTree) && is_array($groupTree)) {
196 // use simplified formatted groupTree
197 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
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']}]";
208 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
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) {
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;
223 $form->setDefaults($addressDefaults);
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.
227 CRM_Core_BAO_CustomGroup
::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
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
233 CRM_Contact_Form_Edit_Address
::storeRequiredCustomDataInfo($form, $groupTree);
236 $template = CRM_Core_Smarty
::singleton();
237 $tplGroupTree = $template->get_template_vars('address_groupTree');
238 $tplGroupTree = empty($tplGroupTree) ?
array() : $tplGroupTree;
240 $form->assign('address_groupTree', $tplGroupTree +
array($blockId => $groupTree));
241 // unset the temp smarty var that got created
242 $form->assign('dnc_groupTree', NULL);
244 // address custom data processing ends ..
248 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
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));
257 * Check for correct state / country mapping.
263 * @return array|bool if no errors
267 public static function formRule($fields, $files, $self) {
270 $customDataRequiredFields = array();
271 if ($self && property_exists($self, '_addressRequireOmission')) {
272 $customDataRequiredFields = explode(',', $self->_addressRequireOmission
);
275 if (!empty($fields['address']) && is_array($fields['address'])) {
276 foreach ($fields['address'] as $instance => $addressValues) {
278 if (CRM_Utils_System
::isNull($addressValues)) {
279 // DETACH 'required' form rule error to
280 // custom data only if address data not exists upon submission
281 if (!empty($customDataRequiredFields)) {
282 foreach ($customDataRequiredFields as $customElementName) {
283 $elementName = "address[$instance][$customElementName]";
284 if ($self->getElementError($elementName)) {
285 // set element error to none
286 $self->setElementError($elementName, NULL);
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)) {
296 foreach ($customDataRequiredFields as $customElementName) {
297 $elementName = "address[$instance][$customElementName]";
298 if ($self->getElementError($elementName)) {
299 // set element error to none
300 $self->setElementError($elementName, NULL);
305 if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) {
306 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
311 return empty($errors) ?
TRUE : $errors;
315 * Set default values for address block
317 * @param array $defaults
318 * Defaults associated array.
319 * @param CRM_Core_Form $form
324 public static function setDefaultValues(&$defaults, &$form) {
325 $addressValues = array();
326 if (isset($defaults['address']) && is_array($defaults['address']) &&
327 !CRM_Utils_System
::isNull($defaults['address'])
330 // start of contact shared adddress defaults
331 $sharedAddresses = array();
332 $masterAddress = array();
334 // get contact name of shared contact names
335 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils
::getAddressShareContactNames($defaults['address']);
337 foreach ($defaults['address'] as $key => $addressValue) {
338 if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
339 $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id'];
340 $sharedAddresses[$key]['shared_address_display'] = array(
341 'address' => $addressValue['display'],
342 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
343 'options' => CRM_Core_BAO_Address
::getValues(array('entity_id' => $master_cid, 'contact_id' => $master_cid)),
344 'master_id' => $addressValue['master_id'],
346 $defaults['address'][$key]['master_contact_id'] = $master_cid;
349 $defaults['address'][$key]['use_shared_address'] = 0;
352 //check if any address is shared by any other contacts
353 $masterAddress[$key] = CRM_Core_BAO_Address
::checkContactSharedAddress($addressValue['id']);
356 $form->assign('sharedAddresses', $sharedAddresses);
357 $form->assign('masterAddress', $masterAddress);
358 // end of shared address defaults
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;
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 .= ' ';
372 $streetAddress .= CRM_Utils_Array
::value($fld, $address);
374 $streetAddress = trim($streetAddress);
375 if (!empty($streetAddress)) {
376 $address['street_address'] = $streetAddress;
378 if (isset($address['street_number'])) {
379 $address['street_number'] .= CRM_Utils_Array
::value('street_number_suffix', $address);
382 // build array for set default.
383 foreach ($parseFields as $field) {
384 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array
::value($field, $address);
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])) {
393 $form->assign('allAddressFieldValues', json_encode($addressValues));
395 //hack to handle show/hide address fields.
396 $parsedAddress = array();
397 if ($form->_contactId
&& !empty($_POST['address']) && is_array($_POST['address'])
399 foreach ($_POST['address'] as $cnt => $values) {
400 $showField = 'streetAddress';
401 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
402 if (!empty($values[$fld])) {
403 $showField = 'addressElements';
407 $parsedAddress[$cnt] = $showField;
410 $form->assign('showHideAddressFields', $parsedAddress);
411 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ?
FALSE : TRUE);
413 // end of parse address functionality
419 * @param CRM_Core_Form $form
422 public static function storeRequiredCustomDataInfo(&$form, $groupTree) {
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') {
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'] . ',';
442 $form->_addressRequireOmission
= rtrim($requireOmission, ',');