3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 object $form - CRM_Core_Form (or subclass)
45 * @param int $addressBlockCount - the index of the address array (if multiple addresses on a page)
46 * @param boolean $sharing - false, if we want to skip the address sharing features
47 * @param boolean $inlineEdit true when edit used in inline edit
54 static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
55 // passing this via the session is AWFUL. we need to fix this
56 if (!$addressBlockCount) {
57 $blockId = ($form->get('Address_Block_Count')) ?
$form->get('Address_Block_Count') : 1;
60 $blockId = $addressBlockCount;
63 $config = CRM_Core_Config
::singleton();
64 $countryDefault = $config->defaultContactCountry
;
66 $form->applyFilter('__ALL__', 'trim');
70 $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL);
73 //make location type required for inline edit
74 $form->addSelect("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') +
$js, $inlineEdit);
77 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
82 "address[$blockId][is_primary]",
83 ts('Primary location for this contact'),
84 ts('Primary location for this contact'),
89 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
94 "address[$blockId][is_billing]",
95 ts('Billing location for this contact'),
96 ts('Billing location for this contact'),
100 // hidden element to store master address id
101 $form->addElement('hidden', "address[$blockId][master_id]");
103 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
104 'address_options', TRUE, NULL, TRUE
106 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Address');
109 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL),
110 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL),
111 'supplemental_address_1' => array(ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL),
112 'supplemental_address_2' => array(ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL),
113 'city' => array(ts('City'), $attributes['city'], NULL),
114 'postal_code' => array(ts('Zip / Postal Code'), array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')), NULL),
115 'postal_code_suffix' => array(ts('Postal Code Suffix'), array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'), NULL),
116 'country_id' => array(ts('Country'), $attributes['country_id'], 'country'),
117 'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL),
118 'county_id' => array(ts('County'), $attributes['county_id'], NULL),
119 'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 11), NULL),
120 'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 11), NULL),
121 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL),
122 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL),
123 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL),
126 foreach ($elements as $name => $v) {
127 list($title, $attributes, $select) = $v;
129 $nameWithoutID = strpos($name, '_id') !== FALSE ?
substr($name, 0, -3) : $name;
130 if (empty($addressOptions[$nameWithoutID])) {
132 if (in_array($nameWithoutID, array(
133 'street_number', 'street_name', 'street_unit')) && !empty($addressOptions['street_address_parsing'])) {
141 //build normal select if country is not present in address block
142 if ($name == 'state_province_id' && !$addressOptions['country']) {
143 $select = 'stateProvince';
147 if ($name == 'state_province_id' ||
$name == 'county_id') {
148 $form->addChainSelect("address[$blockId][$name]");
151 if ($name == 'address_name') {
155 $form->addElement('text',
156 "address[$blockId][$name]",
163 $form->addElement('select',
164 "address[$blockId][$name]",
166 array('' => ts('- select -')) + CRM_Core_PseudoConstant
::$select()
172 if (!empty($form->_values
['address']) && !empty($form->_values
['address'][$blockId])) {
173 $entityId = $form->_values
['address'][$blockId]['id'];
176 // CRM-11665 geocode override option
178 if (!empty($config->geocodeMethod
)) {
180 $form->addElement('checkbox',
181 "address[$blockId][manual_geo_code]",
182 ts('Override automatic geocoding')
185 $form->assign('geoCode', $geoCode);
187 // Process any address custom data -
188 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Address',
193 if (isset($groupTree) && is_array($groupTree)) {
194 // use simplified formatted groupTree
195 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
197 // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
198 foreach ($groupTree as $id => $group) {
199 foreach ($group['fields'] as $fldId => $field) {
200 $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
201 $groupTree[$id]['fields'][$fldId]['element_name'] = "address[$blockId][{$field['element_name']}]";
206 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
208 // since we change element name for address custom data, we need to format the setdefault values
209 $addressDefaults = array();
210 foreach ($defaults as $key => $val) {
211 if ( empty( $val ) ) {
215 // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
216 // this works for all types custom data
217 $keyValues = explode('[', str_replace(']', '', $key));
218 $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
221 $form->setDefaults($addressDefaults);
223 // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
224 // And we can't set it to 'address_' because we want to set it in a slightly different format.
225 CRM_Core_BAO_CustomGroup
::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
227 // during contact editing : if no address is filled
228 // required custom data must not produce 'required' form rule error
229 // more handling done in formRule func
231 CRM_Contact_Form_Edit_Address
::storeRequiredCustomDataInfo($form, $groupTree);
234 $template = CRM_Core_Smarty
::singleton();
235 $tplGroupTree = $template->get_template_vars('address_groupTree');
236 $tplGroupTree = empty($tplGroupTree) ?
array(
239 $form->assign('address_groupTree', $tplGroupTree +
array($blockId => $groupTree));
240 // unset the temp smarty var that got created
241 $form->assign('dnc_groupTree', NULL);
243 // address custom data processing ends ..
247 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
249 // Override the default profile links to add address form
250 $profileLinks = CRM_Core_BAO_UFGroup
::getCreateLinks(array('new_individual', 'new_organization', 'new_household'), 'shared_address');
251 $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
256 * check for correct state / country mapping.
262 * @internal param \reference $array $fields - submitted form values.
263 * @internal param \reference $array $errors - if any errors found add to this array. please.
265 * @return true if no errors
266 * array of errors if any present.
271 static function formRule($fields, $files, $self) {
274 $customDataRequiredFields = array();
275 if ($self && property_exists($self, '_addressRequireOmission')) {
276 $customDataRequiredFields = explode(',', $self->_addressRequireOmission
);
279 if (!empty($fields['address']) && is_array($fields['address'])) {
280 foreach ($fields['address'] as $instance => $addressValues) {
282 if (CRM_Utils_System
::isNull($addressValues)) {
283 // DETACH 'required' form rule error to
284 // custom data only if address data not exists upon submission
285 if (!empty($customDataRequiredFields)) {
286 foreach($customDataRequiredFields as $customElementName) {
287 $elementName = "address[$instance][$customElementName]";
288 if ($self->getElementError($elementName)) {
289 // set element error to none
290 $self->setElementError($elementName, NULL);
297 // DETACH 'required' form rule error to
298 // custom data only if address data not exists upon submission
299 if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address
::dataExists($addressValues)) {
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);
309 if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) {
310 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
315 return empty($errors) ?
TRUE : $errors;
319 * function to set default values for address block
321 * @param array $defaults defaults associated array
322 * @param object $form form object
327 static function setDefaultValues( &$defaults, &$form ) {
328 $addressValues = array();
329 if (isset($defaults['address']) && is_array($defaults['address']) &&
330 !CRM_Utils_System
::isNull($defaults['address'])
333 // start of contact shared adddress defaults
334 $sharedAddresses = array();
335 $masterAddress = array();
337 // get contact name of shared contact names
338 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils
::getAddressShareContactNames($defaults['address']);
340 foreach ($defaults['address'] as $key => $addressValue) {
341 if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
342 $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id'];
343 $sharedAddresses[$key]['shared_address_display'] = array(
344 'address' => $addressValue['display'],
345 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
346 'options' => CRM_Core_BAO_Address
::getValues(array('entity_id' => $master_cid, 'contact_id' => $master_cid)),
347 'master_id' => $addressValue['master_id'],
349 $defaults['address'][$key]['master_contact_id'] = $master_cid;
352 $defaults['address'][$key]['use_shared_address'] = 0;
355 //check if any address is shared by any other contacts
356 $masterAddress[$key] = CRM_Core_BAO_Address
::checkContactSharedAddress($addressValue['id']);
359 $form->assign('sharedAddresses', $sharedAddresses);
360 $form->assign('masterAddress', $masterAddress);
361 // end of shared address defaults
363 // start of parse address functionality
364 // build street address, CRM-5450.
365 if ($form->_parseStreetAddress
) {
366 $parseFields = array('street_address', 'street_number', 'street_name', 'street_unit');
367 foreach ($defaults['address'] as $cnt => & $address) {
368 $streetAddress = NULL;
370 'street_number', 'street_number_suffix', 'street_name', 'street_unit') as $fld) {
371 if (in_array($fld, array(
372 'street_name', 'street_unit'))) {
373 $streetAddress .= ' ';
375 $streetAddress .= CRM_Utils_Array
::value($fld, $address);
377 $streetAddress = trim($streetAddress);
378 if (!empty($streetAddress)) {
379 $address['street_address'] = $streetAddress;
381 if (isset($address['street_number'])) {
382 $address['street_number'] .= CRM_Utils_Array
::value('street_number_suffix', $address);
385 // build array for set default.
386 foreach ($parseFields as $field) {
387 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array
::value($field, $address);
389 // don't load fields, use js to populate.
390 foreach (array('street_number', 'street_name', 'street_unit') as $f) {
391 if (isset($address[$f])) {
396 $form->assign('allAddressFieldValues', json_encode($addressValues));
398 //hack to handle show/hide address fields.
399 $parsedAddress = array();
400 if ($form->_contactId
&& !empty($_POST['address']) && is_array($_POST['address'])
402 foreach ($_POST['address'] as $cnt => $values) {
403 $showField = 'streetAddress';
404 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
405 if (!empty($values[$fld])) {
406 $showField = 'addressElements';
410 $parsedAddress[$cnt] = $showField;
413 $form->assign('showHideAddressFields', $parsedAddress);
414 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ?
FALSE : TRUE);
416 // end of parse address functionality
425 static function storeRequiredCustomDataInfo(&$form, $groupTree) {
426 if (CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Contact') {
427 $requireOmission = NULL;
428 foreach ($groupTree as $csId => $csVal) {
429 // only process Address entity fields
430 if ($csVal['extends'] != 'Address') {
434 foreach ($csVal['fields'] as $cdId => $cdVal) {
435 if ($cdVal['is_required']) {
436 $elementName = $cdVal['element_name'];
437 if (in_array($elementName, $form->_required
)) {
438 // store the omitted rule for a element, to be used later on
439 $requireOmission .= $cdVal['element_custom_name'] . ',';
445 $form->_addressRequireOmission
= rtrim($requireOmission, ',');