Esempio n. 1
0
 /**
  * Build form for address input fields.
  *
  * @param CRM_Core_Form $form
  * @param int $addressBlockCount
  *   The index of the address array (if multiple addresses on a page).
  * @param bool $sharing
  *   False, if we want to skip the address sharing features.
  * @param bool $inlineEdit
  *   True when edit used in inline edit.
  */
 public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE)
 {
     // passing this via the session is AWFUL. we need to fix this
     if (!$addressBlockCount) {
         $blockId = $form->get('Address_Block_Count') ? $form->get('Address_Block_Count') : 1;
     } else {
         $blockId = $addressBlockCount;
     }
     $config = CRM_Core_Config::singleton();
     $countryDefault = $config->defaultContactCountry;
     $form->applyFilter('__ALL__', 'trim');
     $js = array();
     if (!$inlineEdit) {
         $js = array('onChange' => 'checkLocation( this.id );', 'placeholder' => NULL);
     }
     //make location type required for inline edit
     $form->addField("address[{$blockId}][location_type_id]", array('entity' => 'address', 'class' => 'eight') + $js, $inlineEdit);
     if (!$inlineEdit) {
         $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
     }
     $form->addField("address[{$blockId}][is_primary]", array('entity' => 'address', 'label' => ts('Primary location for this contact'), 'text' => ts('Primary location for this contact')) + $js);
     if (!$inlineEdit) {
         $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
     }
     $form->addField("address[{$blockId}][is_billing]", array('entity' => 'address', 'label' => ts('Primary location for this contact'), 'text' => ts('Primary location for this contact')) + $js);
     // hidden element to store master address id
     $form->addField("address[{$blockId}][master_id]", array('entity' => 'address', 'type' => 'hidden'));
     $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options', TRUE, NULL, TRUE);
     $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address');
     $elements = array('address_name', 'street_address', 'supplemental_address_1', 'supplemental_address_2', 'city', 'postal_code', 'postal_code_suffix', 'country_id', 'state_province_id', 'county_id', 'geo_code_1', 'geo_code_2', 'street_number', 'street_name', 'street_unit');
     foreach ($elements as $name) {
         //Remove id from name, to allow comparison against enabled addressOtions.
         $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name;
         // Skip fields which are not enabled in the address options.
         if (empty($addressOptions[$nameWithoutID])) {
             $continue = TRUE;
             //Don't skip street parsed fields when parsing is enabled.
             if (in_array($nameWithoutID, array('street_number', 'street_name', 'street_unit')) && !empty($addressOptions['street_address_parsing'])) {
                 $continue = FALSE;
             }
             if ($continue) {
                 continue;
             }
         }
         if ($name == 'address_name') {
             $name = 'name';
         }
         $params = array('entity' => 'address');
         if ($name == 'postal_code_suffix') {
             $params['label'] = ts('Suffix');
         }
         $form->addField("address[{$blockId}][{$name}]", $params);
     }
     $entityId = NULL;
     if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) {
         $entityId = $form->_values['address'][$blockId]['id'];
     }
     // CRM-11665 geocode override option
     $geoCode = FALSE;
     if (!empty($config->geocodeMethod)) {
         $geoCode = TRUE;
         $form->addElement('checkbox', "address[{$blockId}][manual_geo_code]", ts('Override automatic geocoding'));
     }
     $form->assign('geoCode', $geoCode);
     // Process any address custom data -
     $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', $form, $entityId);
     if (isset($groupTree) && is_array($groupTree)) {
         // use simplified formatted groupTree
         $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
         // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
         foreach ($groupTree as $id => $group) {
             foreach ($group['fields'] as $fldId => $field) {
                 $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
                 $groupTree[$id]['fields'][$fldId]['element_name'] = "address[{$blockId}][{$field['element_name']}]";
             }
         }
         $defaults = array();
         CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
         // since we change element name for address custom data, we need to format the setdefault values
         $addressDefaults = array();
         foreach ($defaults as $key => $val) {
             if (empty($val)) {
                 continue;
             }
             // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
             // this works for all types custom data
             $keyValues = explode('[', str_replace(']', '', $key));
             $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
         }
         $form->setDefaults($addressDefaults);
         // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
         // And we can't set it to 'address_' because we want to set it in a slightly different format.
         CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
         // during contact editing : if no address is filled
         // required custom data must not produce 'required' form rule error
         // more handling done in formRule func
         if (!$inlineEdit) {
             CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree);
         }
         $template = CRM_Core_Smarty::singleton();
         $tplGroupTree = $template->get_template_vars('address_groupTree');
         $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree;
         $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
         // unset the temp smarty var that got created
         $form->assign('dnc_groupTree', NULL);
     }
     // address custom data processing ends ..
     if ($sharing) {
         // shared address
         $form->addElement('checkbox', "address[{$blockId}][use_shared_address]", NULL, ts('Use another contact\'s address'));
         // Override the default profile links to add address form
         $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array('new_individual', 'new_organization', 'new_household'), 'shared_address');
         $form->addEntityRef("address[{$blockId}][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
     }
 }
Esempio n. 2
0
 /**
  * build form for address input fields
  *
  * @param object  $form - CRM_Core_Form (or subclass)
  * @param int     $addressBlockCount - the index of the address array (if multiple addresses on a page)
  * @param boolean $sharing - false, if we want to skip the address sharing features
  * @param boolean $inlineEdit true when edit used in inline edit
  *
  * @return void
  *
  * @access public
  * @static
  */
 static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE)
 {
     // passing this via the session is AWFUL. we need to fix this
     if (!$addressBlockCount) {
         $blockId = $form->get('Address_Block_Count') ? $form->get('Address_Block_Count') : 1;
     } else {
         $blockId = $addressBlockCount;
     }
     $config = CRM_Core_Config::singleton();
     $countryDefault = $config->defaultContactCountry;
     $form->applyFilter('__ALL__', 'trim');
     $js = array();
     if (!$inlineEdit) {
         $js = array('onChange' => 'checkLocation( this.id );');
     }
     $form->addSelect("address[{$blockId}][location_type_id]", array('entity' => 'address', 'class' => 'eight') + $js);
     if (!$inlineEdit) {
         $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
     } else {
         //make location type required for inline edit
         $form->addRule("address[{$blockId}][location_type_id]", ts('%1 is a required field.', array(1 => ts('Location Type'))), 'required');
     }
     $form->addElement('checkbox', "address[{$blockId}][is_primary]", ts('Primary location for this contact'), ts('Primary location for this contact'), $js);
     if (!$inlineEdit) {
         $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
     }
     $form->addElement('checkbox', "address[{$blockId}][is_billing]", ts('Billing location for this contact'), ts('Billing location for this contact'), $js);
     // hidden element to store master address id
     $form->addElement('hidden', "address[{$blockId}][master_id]");
     $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options', TRUE, NULL, TRUE);
     $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address');
     $elements = array('address_name' => array(ts('Address Name'), $attributes['address_name'], NULL), 'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL), 'supplemental_address_1' => array(ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL), 'supplemental_address_2' => array(ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL), 'city' => array(ts('City'), $attributes['city'], NULL), 'postal_code' => array(ts('Zip / Postal Code'), array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')), NULL), 'postal_code_suffix' => array(ts('Postal Code Suffix'), array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'), NULL), 'county_id' => array(ts('County'), $attributes['county_id'], NULL), 'state_province_id' => array(ts('State / Province'), $attributes['state_province_id'], NULL), 'country_id' => array(ts('Country'), $attributes['country_id'], NULL), 'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 11), NULL), 'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 11), NULL), 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL), 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL), 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL));
     $stateCountryMap = array();
     foreach ($elements as $name => $v) {
         list($title, $attributes, $select) = $v;
         $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name;
         if (empty($addressOptions[$nameWithoutID])) {
             $continue = TRUE;
             if (in_array($nameWithoutID, array('street_number', 'street_name', 'street_unit')) && !empty($addressOptions['street_address_parsing'])) {
                 $continue = FALSE;
             }
             if ($continue) {
                 continue;
             }
         }
         if (!$attributes) {
             $attributes = $attributes[$name];
         }
         //build normal select if country is not present in address block
         if ($name == 'state_province_id' && !$addressOptions['country']) {
             $select = 'stateProvince';
         }
         if (!$select) {
             if ($name == 'country_id' || $name == 'state_province_id' || $name == 'county_id') {
                 if ($name == 'country_id') {
                     $stateCountryMap[$blockId]['country'] = "address_{$blockId}_{$name}";
                     $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country();
                 } elseif ($name == 'state_province_id') {
                     $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
                     if ($countryDefault) {
                         $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($countryDefault);
                     } else {
                         $selectOptions = array('' => ts('- select a country -'));
                     }
                 } elseif ($name == 'county_id') {
                     $stateCountryMap[$blockId]['county'] = "address_{$blockId}_{$name}";
                     if ($form->getSubmitValue("address[{$blockId}][state_province_id]")) {
                         $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::countyForState($form->getSubmitValue("address[{$blockId}][state_province_id]"));
                     } elseif ($form->getSubmitValue("address[{$blockId}][county_id]")) {
                         $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::county();
                     } else {
                         $selectOptions = array('' => ts('- select a state -'));
                     }
                 }
                 $form->addElement('select', "address[{$blockId}][{$name}]", $title, $selectOptions);
             } else {
                 if ($name == 'address_name') {
                     $name = 'name';
                 }
                 $form->addElement('text', "address[{$blockId}][{$name}]", $title, $attributes);
             }
         } else {
             if ($name == 'state_province_id') {
                 $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
             }
             $form->addElement('select', "address[{$blockId}][{$name}]", $title, array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select());
         }
     }
     CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
     $entityId = NULL;
     if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) {
         $entityId = $form->_values['address'][$blockId]['id'];
     }
     // CRM-11665 geocode override option
     $geoCode = FALSE;
     if (!empty($config->geocodeMethod)) {
         $geoCode = TRUE;
         $form->addElement('checkbox', "address[{$blockId}][manual_geo_code]", ts('Override automatic geocoding'));
     }
     $form->assign('geoCode', $geoCode);
     // Process any address custom data -
     $groupTree = CRM_Core_BAO_CustomGroup::getTree('Address', $form, $entityId);
     if (isset($groupTree) && is_array($groupTree)) {
         // use simplified formatted groupTree
         $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
         // make sure custom fields are added /w element-name in the format - 'address[$blockId][custom-X]'
         foreach ($groupTree as $id => $group) {
             foreach ($group['fields'] as $fldId => $field) {
                 $groupTree[$id]['fields'][$fldId]['element_custom_name'] = $field['element_name'];
                 $groupTree[$id]['fields'][$fldId]['element_name'] = "address[{$blockId}][{$field['element_name']}]";
             }
         }
         $defaults = array();
         CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
         // since we change element name for address custom data, we need to format the setdefault values
         $addressDefaults = array();
         foreach ($defaults as $key => $val) {
             if (empty($val)) {
                 continue;
             }
             // inorder to set correct defaults for checkbox custom data, we need to converted flat key to array
             // this works for all types custom data
             $keyValues = explode('[', str_replace(']', '', $key));
             $addressDefaults[$keyValues[0]][$keyValues[1]][$keyValues[2]] = $val;
         }
         $form->setDefaults($addressDefaults);
         // we setting the prefix to 'dnc_' below, so that we don't overwrite smarty's grouptree var.
         // And we can't set it to 'address_' because we want to set it in a slightly different format.
         CRM_Core_BAO_CustomGroup::buildQuickForm($form, $groupTree, FALSE, 'dnc_');
         // during contact editing : if no address is filled
         // required custom data must not produce 'required' form rule error
         // more handling done in formRule func
         if (!$inlineEdit) {
             CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree);
         }
         $template = CRM_Core_Smarty::singleton();
         $tplGroupTree = $template->get_template_vars('address_groupTree');
         $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree;
         $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
         // unset the temp smarty var that got created
         $form->assign('dnc_groupTree', NULL);
     }
     // address custom data processing ends ..
     if ($sharing) {
         // shared address
         $form->addElement('checkbox', "address[{$blockId}][use_shared_address]", NULL, ts('Use another contact\'s address'));
         // get the reserved for address
         $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'shared_address', 'id', 'name');
         if (!$profileId) {
             CRM_Core_Error::fatal(ts('Your install is missing required "Shared Address" profile.'));
         }
         CRM_Contact_Form_NewContact::buildQuickForm($form, $blockId, array($profileId));
     }
 }