3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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.
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;
61 $blockId = $addressBlockCount;
64 $config = CRM_Core_Config
::singleton();
65 $countryDefault = $config->defaultContactCountry
;
67 $form->applyFilter('__ALL__', 'trim');
71 $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL);
74 //make location type required for inline edit
75 $form->addField("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') +
$js, $inlineEdit);
78 $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
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);
88 $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
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);
97 // hidden element to store master address id
98 $form->addField("address[$blockId][master_id]", array('entity' => 'address', 'type' => 'hidden'));
99 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
100 'address_options', TRUE, NULL, TRUE
102 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Address');
107 'supplemental_address_1',
108 'supplemental_address_2',
111 'postal_code_suffix',
122 foreach ($elements as $name) {
123 //Remove id from name, to allow comparison against enabled addressOtions.
124 $nameWithoutID = strpos($name, '_id') !== FALSE ?
substr($name, 0, -3) : $name;
125 // Skip fields which are not enabled in the address options.
126 if (empty($addressOptions[$nameWithoutID])) {
128 //Don't skip street parsed fields when parsing is enabled.
129 if (in_array($nameWithoutID, array(
133 )) && !empty($addressOptions['street_address_parsing'])
141 if ($name == 'address_name') {
145 $form->addField("address[$blockId][$name]", array('entity' => 'address'));
149 if (!empty($form->_values
['address']) && !empty($form->_values
['address'][$blockId])) {
150 $entityId = $form->_values
['address'][$blockId]['id'];
153 // CRM-11665 geocode override option
155 if (!empty($config->geocodeMethod
)) {
157 $form->addElement('checkbox',
158 "address[$blockId][manual_geo_code]",
159 ts('Override automatic geocoding')
162 $form->assign('geoCode', $geoCode);
164 // Process any address custom data -
165 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Address',
170 if (isset($groupTree) && is_array($groupTree)) {
171 // use simplified formatted groupTree
172 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
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']}]";
183 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
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) {
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;
198 $form->setDefaults($addressDefaults);
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.
202 CRM_Core_BAO_CustomGroup
::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
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
208 CRM_Contact_Form_Edit_Address
::storeRequiredCustomDataInfo($form, $groupTree);
211 $template = CRM_Core_Smarty
::singleton();
212 $tplGroupTree = $template->get_template_vars('address_groupTree');
213 $tplGroupTree = empty($tplGroupTree) ?
array() : $tplGroupTree;
215 $form->assign('address_groupTree', $tplGroupTree +
array($blockId => $groupTree));
216 // unset the temp smarty var that got created
217 $form->assign('dnc_groupTree', NULL);
219 // address custom data processing ends ..
223 $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
225 // Override the default profile links to add address form
226 $profileLinks = CRM_Core_BAO_UFGroup
::getCreateLinks(array(
230 ), 'shared_address');
231 $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
236 * Check for correct state / country mapping.
246 public static function formRule($fields, $files, $self) {
249 $customDataRequiredFields = array();
250 if ($self && property_exists($self, '_addressRequireOmission')) {
251 $customDataRequiredFields = explode(',', $self->_addressRequireOmission
);
254 if (!empty($fields['address']) && is_array($fields['address'])) {
255 foreach ($fields['address'] as $instance => $addressValues) {
257 if (CRM_Utils_System
::isNull($addressValues)) {
258 // DETACH 'required' form rule error to
259 // custom data only if address data not exists upon submission
260 if (!empty($customDataRequiredFields)) {
261 foreach ($customDataRequiredFields as $customElementName) {
262 $elementName = "address[$instance][$customElementName]";
263 if ($self->getElementError($elementName)) {
264 // set element error to none
265 $self->setElementError($elementName, NULL);
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)) {
275 foreach ($customDataRequiredFields as $customElementName) {
276 $elementName = "address[$instance][$customElementName]";
277 if ($self->getElementError($elementName)) {
278 // set element error to none
279 $self->setElementError($elementName, NULL);
284 if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) {
285 $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
290 return empty($errors) ?
TRUE : $errors;
294 * Set default values for address block.
296 * @param array $defaults
297 * Defaults associated array.
298 * @param CRM_Core_Form $form
302 public static function setDefaultValues(&$defaults, &$form) {
303 $addressValues = array();
304 if (isset($defaults['address']) && is_array($defaults['address']) &&
305 !CRM_Utils_System
::isNull($defaults['address'])
308 // start of contact shared adddress defaults
309 $sharedAddresses = array();
310 $masterAddress = array();
312 // get contact name of shared contact names
313 $shareAddressContactNames = CRM_Contact_BAO_Contact_Utils
::getAddressShareContactNames($defaults['address']);
315 foreach ($defaults['address'] as $key => $addressValue) {
316 if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
317 $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id'];
318 $sharedAddresses[$key]['shared_address_display'] = array(
319 'address' => $addressValue['display'],
320 'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
321 'options' => CRM_Core_BAO_Address
::getValues(array(
322 'entity_id' => $master_cid,
323 'contact_id' => $master_cid,
325 'master_id' => $addressValue['master_id'],
327 $defaults['address'][$key]['master_contact_id'] = $master_cid;
330 $defaults['address'][$key]['use_shared_address'] = 0;
333 //check if any address is shared by any other contacts
334 $masterAddress[$key] = CRM_Core_BAO_Address
::checkContactSharedAddress($addressValue['id']);
337 $form->assign('sharedAddresses', $sharedAddresses);
338 $form->assign('masterAddress', $masterAddress);
339 // end of shared address defaults
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;
349 'street_number_suffix',
353 if (in_array($fld, array(
357 $streetAddress .= ' ';
359 $streetAddress .= CRM_Utils_Array
::value($fld, $address);
361 $streetAddress = trim($streetAddress);
362 if (!empty($streetAddress)) {
363 $address['street_address'] = $streetAddress;
365 if (isset($address['street_number'])) {
366 $address['street_number'] .= CRM_Utils_Array
::value('street_number_suffix', $address);
369 // build array for set default.
370 foreach ($parseFields as $field) {
371 $addressValues["{$field}_{$cnt}"] = CRM_Utils_Array
::value($field, $address);
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])) {
380 $form->assign('allAddressFieldValues', json_encode($addressValues));
382 //hack to handle show/hide address fields.
383 $parsedAddress = array();
384 if ($form->_contactId
&& !empty($_POST['address']) && is_array($_POST['address'])
386 foreach ($_POST['address'] as $cnt => $values) {
387 $showField = 'streetAddress';
388 foreach (array('street_number', 'street_name', 'street_unit') as $fld) {
389 if (!empty($values[$fld])) {
390 $showField = 'addressElements';
394 $parsedAddress[$cnt] = $showField;
397 $form->assign('showHideAddressFields', $parsedAddress);
398 $form->assign('loadShowHideAddressFields', empty($parsedAddress) ?
FALSE : TRUE);
400 // end of parse address functionality
406 * @param CRM_Core_Form $form
409 public static function storeRequiredCustomDataInfo(&$form, $groupTree) {
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') {
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'] . ',';
429 $form->_addressRequireOmission
= rtrim($requireOmission, ',');