/** * 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)); } }
/** * 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)); } }
/** * Set defaults for the form. * * @return array */ public function setDefaultValues() { $defaults = $this->_values; $config = CRM_Core_Config::singleton(); //set address block defaults if (!empty($defaults['address'])) { CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this); } else { // get the default location type $locationType = CRM_Core_BAO_LocationType::getDefault(); if ($this->_locBlockNo == 1) { $address['is_primary'] = TRUE; $address['location_type_id'] = $locationType->id; } $address['country_id'] = $config->defaultContactCountry; $defaults['address'][$this->_locBlockNo] = $address; } return $defaults; }
/** * set defaults for the form * * @return void * @access public */ public function setDefaultValues() { $defaults = $this->_values; $config = CRM_Core_Config::singleton(); //set address block defaults if (CRM_Utils_Array::value('address', $defaults)) { CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this); } else { // get the default location type $locationType = CRM_Core_BAO_LocationType::getDefault(); if ($this->_locBlockNo == 1) { $address['is_primary'] = TRUE; $address['location_type_id'] = $locationType->id; } $address['country_id'] = $config->defaultContactCountry; $defaults['address'][$this->_locBlockNo] = $address; } $values = $defaults['address'][$this->_locBlockNo]; CRM_Contact_Form_Edit_Address::fixStateSelect($this, "address[{$this->_locBlockNo}][country_id]", "address[{$this->_locBlockNo}][state_province_id]", "address[{$this->_locBlockNo}][county_id]", CRM_Utils_Array::value('country_id', $values, $config->defaultContactCountry), CRM_Utils_Array::value('state_province_id', $values)); return $defaults; }
/** * Build form for related contacts / on behalf of organization. * * @param CRM_Core_Form $form * @param string $contactType * contact type. * @param int $countryID * @param int $stateID * @param string $title * fieldset title. * */ public static function buildOnBehalfForm(&$form, $contactType, $countryID, $stateID, $title) { $config = CRM_Core_Config::singleton(); $form->assign('contact_type', $contactType); $form->assign('fieldSetTitle', $title); $form->assign('contactEditMode', TRUE); $attributes = CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact'); if ($form->_contactId) { $form->assign('orgId', $form->_contactId); } switch ($contactType) { case 'Organization': $form->add('text', 'organization_name', ts('Organization Name'), $attributes['organization_name'], TRUE); break; case 'Household': $form->add('text', 'household_name', ts('Household Name'), $attributes['household_name']); break; default: // individual $form->addElement('select', 'prefix_id', ts('Prefix'), array('' => ts('- prefix -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')); $form->addElement('text', 'first_name', ts('First Name'), $attributes['first_name']); $form->addElement('text', 'middle_name', ts('Middle Name'), $attributes['middle_name']); $form->addElement('text', 'last_name', ts('Last Name'), $attributes['last_name']); $form->addElement('select', 'suffix_id', ts('Suffix'), array('' => ts('- suffix -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')); } $addressSequence = $config->addressSequence(); $form->assign('addressSequence', array_fill_keys($addressSequence, 1)); //Primary Phone $form->addElement('text', 'phone[1][phone]', ts('Primary Phone'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone')); //Primary Email $form->addElement('text', 'email[1][email]', ts('Primary Email'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email')); //build the address block CRM_Contact_Form_Edit_Address::buildQuickForm($form); }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule($fields) { // check for state/country mapping $errors = CRM_Contact_Form_Edit_Address::formRule($fields); return empty($errors) ? TRUE : $errors; }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule(&$fields) { $errors = array(); // check for state/country mapping CRM_Contact_Form_Edit_Address::formRule($fields, $errors); //fix for CRM-3552, //as we use "fromName"<emailaddresss> format for domain email. if (strpos($fields['email_name'], '"') !== false) { $errors['email_name'] = ts('Double quotes are not allow in from name.'); } return empty($errors) ? true : $errors; }
/** * global form rule * * @param array $fields the input form values * @param array $files the uploaded files if any * @param array $options additional user data * * @return true if no errors, else array of errors * @access public * @static */ static function formRule(&$fields, &$files, $contactID = null) { $errors = array(); //FIXME if (CRM_Utils_Array::value('state_province_id', $fields['address'][1]) == 'undefined') { $fields['address'][1]['state_province_id'] = ''; } $primaryID = CRM_Contact_Form_Contact::formRule($fields, $errors, $contactID); // check for state/country mapping require_once 'CRM/Contact/Form/Edit/Address.php'; CRM_Contact_Form_Edit_Address::formRule($fields, $errors); // make sure that firstName and lastName or a primary OpenID is set if (!$primaryID && (!CRM_Utils_Array::value('first_name', $fields) || !CRM_Utils_Array::value('last_name', $fields))) { $errors['_qf_default'] = ts('First Name and Last Name OR an email OR an OpenID in the Primary Location should be set.'); } //check for duplicate - dedupe rules CRM_Contact_Form_Contact::checkDuplicateContacts($fields, $errors, $contactID, 'Individual'); // if use_household_address option is checked, make sure 'valid household_name' is also present. if (CRM_Utils_Array::value('use_household_address', $fields) && !CRM_Utils_Array::value('shared_household_id', $fields)) { $errors["shared_household"] = ts("Please select a household from the 'Select Household' list"); } return empty($errors) ? true : $errors; }
/** * Global validation rules for the form. * * @param array $fields * Posted values of the form. * * @return array * list of errors to be posted back to the form */ public static function formRule($fields) { // check for state/country mapping $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject); // $errors === TRUE means no errors from above formRule excution, // so declaring $errors to array for further processing if ($errors === TRUE) { $errors = array(); } //fix for CRM-3552, //as we use "fromName"<emailaddresss> format for domain email. if (strpos($fields['email_name'], '"') !== FALSE) { $errors['email_name'] = ts('Double quotes are not allow in from name.'); } // Check for default from email address and organization (domain) name. Force them to change it. if ($fields['email_address'] == '*****@*****.**') { $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.'); } if ($fields['name'] == 'Default Domain Name') { $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.'); } return empty($errors) ? TRUE : $errors; }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule($fields) { // check for state/country mapping $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject); return empty($errors) ? TRUE : $errors; }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule(&$fields) { $errors = array(); // check for state/country mapping CRM_Contact_Form_Edit_Address::formRule($fields, $errors); return empty($errors) ? true : $errors; }
static function fixAllStateSelects(&$form, $defaults, $batchFieldNames = false) { $config = CRM_Core_Config::singleton(); $map = null; if (is_array($batchFieldNames)) { $map = $batchFieldNames; } elseif (!empty($config->stateCountryMap)) { $map = $config->stateCountryMap; } if (!empty($map)) { foreach ($map as $index => $match) { if (array_key_exists('state_province', $match) || array_key_exists('country', $match) || array_key_exists('county', $match)) { $countryElementName = CRM_Utils_Array::value('country', $match); $stateProvinceElementName = CRM_Utils_Array::value('state_province', $match); $countyElementName = CRM_Utils_Array::value('county', $match); CRM_Contact_Form_Edit_Address::fixStateSelect($form, $countryElementName, $stateProvinceElementName, $countyElementName, CRM_Utils_Array::value($countryElementName, $defaults), CRM_Utils_Array::value($stateProvinceElementName, $defaults)); } else { unset($config->stateCountryMap[$index]); } } } }
static function fixAllStateSelects(&$form, &$defaults) { $config = CRM_Core_Config::singleton(); if (!empty($config->stateCountryMap)) { foreach ($config->stateCountryMap as $index => $match) { if (array_key_exists('state_province', $match) && array_key_exists('country', $match)) { CRM_Contact_Form_Edit_Address::fixStateSelect($form, $match['country'], $match['state_province'], CRM_Utils_Array::value('county', $match), CRM_Utils_Array::value($match['country'], $defaults), CRM_Utils_Array::value($match['state_province'], $defaults)); } else { unset($config->stateCountryMap[$index]); } } } }
/** * do the set default related to location type id, * primary location, default country * */ function blockSetDefaults(&$defaults) { $locationTypeKeys = array_filter(array_keys(CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id')), 'is_int'); sort($locationTypeKeys); // get the default location type $locationType = CRM_Core_BAO_LocationType::getDefault(); // unset primary location type $primaryLocationTypeIdKey = CRM_Utils_Array::key($locationType->id, $locationTypeKeys); unset($locationTypeKeys[$primaryLocationTypeIdKey]); // reset the array sequence $locationTypeKeys = array_values($locationTypeKeys); // get default phone and im provider id. $defPhoneTypeId = key(CRM_Core_OptionGroup::values('phone_type', FALSE, FALSE, FALSE, ' AND is_default = 1')); $defIMProviderId = key(CRM_Core_OptionGroup::values('instant_messenger_service', FALSE, FALSE, FALSE, ' AND is_default = 1')); $allBlocks = $this->_blocks; if (array_key_exists('Address', $this->_editOptions)) { $allBlocks['Address'] = $this->_editOptions['Address']; } $config = CRM_Core_Config::singleton(); foreach ($allBlocks as $blockName => $label) { $name = strtolower($blockName); $hasPrimary = $updateMode = FALSE; // user is in update mode. if (array_key_exists($name, $defaults) && !CRM_Utils_System::isNull($defaults[$name])) { $updateMode = TRUE; } for ($instance = 1; $instance <= $this->get($blockName . '_Block_Count'); $instance++) { // make we require one primary block, CRM-5505 if ($updateMode) { if (!$hasPrimary) { $hasPrimary = CRM_Utils_Array::value('is_primary', CRM_Utils_Array::value($instance, $defaults[$name])); } continue; } //set location to primary for first one. if ($instance == 1) { $hasPrimary = TRUE; $defaults[$name][$instance]['is_primary'] = TRUE; $defaults[$name][$instance]['location_type_id'] = $locationType->id; } else { $locTypeId = isset($locationTypeKeys[$instance - 1]) ? $locationTypeKeys[$instance - 1] : $locationType->id; $defaults[$name][$instance]['location_type_id'] = $locTypeId; } //set default country if ($name == 'address' && $config->defaultContactCountry) { $defaults[$name][$instance]['country_id'] = $config->defaultContactCountry; } //set default state/province if ($name == 'address' && $config->defaultContactStateProvince) { $defaults[$name][$instance]['state_province_id'] = $config->defaultContactStateProvince; } //set default phone type. if ($name == 'phone' && $defPhoneTypeId) { $defaults[$name][$instance]['phone_type_id'] = $defPhoneTypeId; } //set default im provider. if ($name == 'im' && $defIMProviderId) { $defaults[$name][$instance]['provider_id'] = $defIMProviderId; } } if (!$hasPrimary) { $defaults[$name][1]['is_primary'] = TRUE; } } // set defaults for country-state widget if (CRM_Utils_Array::value('address', $defaults) && is_array($defaults['address'])) { foreach ($defaults['address'] as $blockId => $values) { CRM_Contact_Form_Edit_Address::fixStateSelect($this, "address[{$blockId}][country_id]", "address[{$blockId}][state_province_id]", "address[{$blockId}][county_id]", CRM_Utils_Array::value('country_id', $values, $config->defaultContactCountry), CRM_Utils_Array::value('state_province_id', $values, $config->defaultContactStateProvince)); } } }
function blockSetDefaults(&$defaults) { $locationTypeKeys = array_filter(array_keys(CRM_Core_PseudoConstant::locationType()), 'is_int'); sort($locationTypeKeys); // get the default location type require_once 'CRM/Core/BAO/LocationType.php'; $locationType = CRM_Core_BAO_LocationType::getDefault(); // unset primary location type $primaryLocationTypeIdKey = CRM_Utils_Array::key($locationType->id, $locationTypeKeys); unset($locationTypeKeys[$primaryLocationTypeIdKey]); // reset the array sequence $locationTypeKeys = array_values($locationTypeKeys); // get default phone and im provider id. require_once 'CRM/Core/OptionGroup.php'; $defPhoneTypeId = key(CRM_Core_OptionGroup::values('phone_type', false, false, false, ' AND is_default = 1')); $defIMProviderId = key(CRM_Core_OptionGroup::values('instant_messenger_service', false, false, false, ' AND is_default = 1')); $allBlocks = $this->_blocks; if (array_key_exists('Address', $this->_editOptions)) { $allBlocks['Address'] = $this->_editOptions['Address']; } $config =& CRM_Core_Config::singleton(); foreach ($allBlocks as $blockName => $label) { $name = strtolower($blockName); if (array_key_exists($name, $defaults) && !CRM_Utils_System::isNull($defaults[$name])) { continue; } for ($instance = 1; $instance <= $this->get($blockName . "_Block_Count"); $instance++) { //set location to primary for first one. if ($instance == 1) { $defaults[$name][$instance]['is_primary'] = true; $defaults[$name][$instance]['location_type_id'] = $locationType->id; } else { $locTypeId = isset($locationTypeKeys[$instance - 1]) ? $locationTypeKeys[$instance - 1] : $locationType->id; $defaults[$name][$instance]['location_type_id'] = $locTypeId; } //set default country if ($name == 'address' && $config->defaultContactCountry) { $defaults[$name][$instance]['country_id'] = $config->defaultContactCountry; } //set default phone type. if ($name == 'phone' && $defPhoneTypeId) { $defaults[$name][$instance]['phone_type_id'] = $defPhoneTypeId; } //set default im provider. if ($name == 'im' && $defIMProviderId) { $defaults[$name][$instance]['provider_id'] = $defIMProviderId; } } } // set defaults for country-state widget if (CRM_Utils_Array::value('address', $defaults) && is_array($defaults['address'])) { require_once 'CRM/Contact/Form/Edit/Address.php'; foreach ($defaults['address'] as $blockId => $values) { CRM_Contact_Form_Edit_Address::fixStateSelect($this, "address[{$blockId}][country_id]", "address[{$blockId}][state_province_id]", CRM_Utils_Array::value('country_id', $values, $config->defaultContactCountry)); } } }
/** * Function to build form for related contacts / on behalf of organization. * * @param $form object invoking Object * @param $contactType string contact type * @param $title string fieldset title * @param $maxLocationBlocks int number of location blocks * * @static * */ static function buildOnBehalfForm(&$form, $contactType = 'Individual', $countryID = null, $stateID = null, $title = 'Contact Information', $contactEditMode = false, $maxLocationBlocks = 1) { if ($title == 'Contact Information') { $title = ts('Contact Information'); } require_once 'CRM/Contact/Form/Location.php'; $config = CRM_Core_Config::singleton(); $form->assign('contact_type', $contactType); $form->assign('fieldSetTitle', $title); $form->assign('contactEditMode', $contactEditMode); $attributes = CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact'); switch ($contactType) { case 'Organization': $session = CRM_Core_Session::singleton(); $contactID = $session->get('userID'); if ($contactID) { require_once 'CRM/Contact/BAO/Relationship.php'; $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($contactID); } if (!$contactEditMode && $contactID && count($employers) >= 1) { $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', "cid=", false, null, false); $form->assign('locDataURL', $locDataURL); $dataURL = CRM_Utils_System::url('civicrm/ajax/employer', "cid=" . $contactID, false, null, false); $form->assign('employerDataURL', $dataURL); $form->add('text', 'organization_id', ts('Select an existing related Organization OR Enter a new one')); $form->add('hidden', 'onbehalfof_id', '', array('id' => 'onbehalfof_id')); $orgOptions = array('0' => ts('Create new organization'), '1' => ts('Select existing organization')); $orgOptionExtra = array('onclick' => "showHideByValue('org_option','true','select_org','table-row','radio',true);showHideByValue('org_option','true','create_org','table-row','radio',false);"); $form->addRadio('org_option', ts('options'), $orgOptions, $orgOptionExtra); $form->assign('relatedOrganizationFound', true); } $isRequired = false; if (CRM_Utils_Array::value('is_for_organization', $form->_values) == 2) { $isRequired = true; } $form->add('text', 'organization_name', ts('Organization Name'), $attributes['organization_name'], $isRequired); break; case 'Household': $form->add('text', 'household_name', ts('Household Name'), $attributes['household_name']); break; default: // individual $form->addElement('select', 'prefix_id', ts('Prefix'), array('' => ts('- prefix -')) + CRM_Core_PseudoConstant::individualPrefix()); $form->addElement('text', 'first_name', ts('First Name'), $attributes['first_name']); $form->addElement('text', 'middle_name', ts('Middle Name'), $attributes['middle_name']); $form->addElement('text', 'last_name', ts('Last Name'), $attributes['last_name']); $form->addElement('select', 'suffix_id', ts('Suffix'), array('' => ts('- suffix -')) + CRM_Core_PseudoConstant::individualSuffix()); } $addressSequence = $config->addressSequence(); $form->assign('addressSequence', array_fill_keys($addressSequence, 1)); //Primary Phone $form->addElement('text', "phone[1][phone]", ts('Primary Phone'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone')); //Primary Email $form->addElement('text', "email[1][email]", ts('Primary Email'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email')); //build the address block require_once 'CRM/Contact/Form/Edit/Address.php'; CRM_Contact_Form_Edit_Address::buildQuickForm($form); // also fix the state country selector CRM_Contact_Form_Edit_Address::fixStateSelect($form, "address[1][country_id]", "address[1][state_province_id]", $countryID); }
/** * Set default values for the form. * * Note that in edit/view mode the default values are retrieved from the database */ public function setDefaultValues() { $defaults = $this->_values; if ($this->_action & CRM_Core_Action::ADD) { if (array_key_exists('TagsAndGroups', $this->_editOptions)) { // set group and tag defaults if any if ($this->_gid) { $defaults['group'][] = $this->_gid; } if ($this->_tid) { $defaults['tag'][$this->_tid] = 1; } } if ($this->_contactSubType) { $defaults['contact_sub_type'] = $this->_contactSubType; } } else { foreach ($defaults['email'] as $dontCare => &$val) { if (isset($val['signature_text'])) { $val['signature_text_hidden'] = $val['signature_text']; } if (isset($val['signature_html'])) { $val['signature_html_hidden'] = $val['signature_html']; } } if (!empty($defaults['contact_sub_type'])) { $defaults['contact_sub_type'] = $this->_oldSubtypes; } } // set defaults for blocks ( custom data, address, communication preference, notes, tags and groups ) foreach ($this->_editOptions as $name => $label) { if (!in_array($name, array('Address', 'Notes'))) { $className = 'CRM_Contact_Form_Edit_' . $name; $className::setDefaultValues($this, $defaults); } } //set address block defaults CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this); if (!empty($defaults['image_URL'])) { list($imageWidth, $imageHeight) = getimagesize(CRM_Utils_String::unstupifyUrl($defaults['image_URL'])); list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); $this->assign('imageWidth', $imageWidth); $this->assign('imageHeight', $imageHeight); $this->assign('imageThumbWidth', $imageThumbWidth); $this->assign('imageThumbHeight', $imageThumbHeight); $this->assign('imageURL', $defaults['image_URL']); } //set location type and country to default for each block $this->blockSetDefaults($defaults); $this->_preEditValues = $defaults; return $defaults; }
/** * global validation rules for the form * * @param array $fields posted values of the form * * @return array list of errors to be posted back to the form * @static * @access public */ static function formRule($fields) { $errors = array(); // check for state/country mapping CRM_Contact_Form_Edit_Address::formRule($fields, $errors); //fix for CRM-3552, //as we use "fromName"<emailaddresss> format for domain email. if (strpos($fields['email_name'], '"') !== FALSE) { $errors['email_name'] = ts('Double quotes are not allow in from name.'); } // Check for default from email address and organization (domain) name. Force them to change it. if ($fields['email_address'] == '*****@*****.**') { $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.'); } if ($fields['name'] == 'Default Domain Name') { $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.'); } return empty($errors) ? TRUE : $errors; }