static function fixStateSelect(&$form, $countryElementName, $stateElementName, $countyElementName, $countryDefaultValue, $stateDefaultValue = NULL) { $countryID = $stateID = NULL; if (isset($form->_elementIndex[$countryElementName])) { //get the country id to load states - //first check for submitted value, //then check for user passed value. //finally check for element default val. $submittedVal = $form->getSubmitValue($countryElementName); if ($submittedVal) { $countryID = $submittedVal; } elseif ($countryDefaultValue) { $countryID = $countryDefaultValue; } else { $countryID = CRM_Utils_Array::value(0, $form->getElementValue($countryElementName)); } } $stateTitle = ts('State/Province'); if (isset($form->_fields[$stateElementName]['title'])) { $stateTitle = $form->_fields[$stateElementName]['title']; } if (isset($form->_elementIndex[$stateElementName])) { $submittedValState = $form->getSubmitValue($stateElementName); if ($submittedValState) { $stateID = $submittedValState; } elseif ($stateDefaultValue) { $stateID = $stateDefaultValue; } else { $stateID = CRM_Utils_Array::value(0, $form->getElementValue($stateElementName)); } } if (isset($form->_elementIndex[$stateElementName])) { if ($countryID) { $stateProvinces = CRM_Core_PseudoConstant::stateProvinceForCountry($countryID); } else { $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); } $stateSelect =& $form->addElement('select', $stateElementName, $stateTitle, array('' => ts('- select -')) + $stateProvinces); } if (isset($form->_elementIndex[$stateElementName]) && isset($form->_elementIndex[$countyElementName])) { if ($stateID) { $counties = CRM_Core_PseudoConstant::countyForState($stateID); } else { $counties = CRM_Core_PseudoConstant::county(); } $form->addElement('select', $countyElementName, ts('County'), array('' => ts('- select -')) + $counties); } // CRM-7296 freeze the select for state if address is shared with household // CRM-9070 freeze the select for state if it is view only if (isset($form->_fields) && !empty($form->_fields[$stateElementName]) && (!empty($form->_fields[$stateElementName]['is_shared']) || !empty($form->_fields[$stateElementName]['is_view']))) { $stateSelect->freeze(); } }
/** * Get the values for pseudoconstants for name->value and reverse. * * @param array $defaults * (reference) the default values, some of which need to be resolved. * @param bool $reverse * True if we want to resolve the values in the reverse direction (value -> name). */ public static function resolveDefaults(&$defaults, $reverse = FALSE) { // Hack for birth_date. if (!empty($defaults['birth_date'])) { if (is_array($defaults['birth_date'])) { $defaults['birth_date'] = CRM_Utils_Date::format($defaults['birth_date'], '-'); } } CRM_Utils_Array::lookupValue($defaults, 'prefix', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'), $reverse); CRM_Utils_Array::lookupValue($defaults, 'suffix', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'), $reverse); CRM_Utils_Array::lookupValue($defaults, 'gender', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), $reverse); CRM_Utils_Array::lookupValue($defaults, 'communication_style', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'communication_style_id'), $reverse); //lookup value of email/postal greeting, addressee, CRM-4575 foreach (self::$_greetingTypes as $greeting) { $filterCondition = array('contact_type' => CRM_Utils_Array::value('contact_type', $defaults), 'greeting_type' => $greeting); CRM_Utils_Array::lookupValue($defaults, $greeting, CRM_Core_PseudoConstant::greeting($filterCondition), $reverse); } $blocks = array('address', 'im', 'phone'); foreach ($blocks as $name) { if (!array_key_exists($name, $defaults) || !is_array($defaults[$name])) { continue; } foreach ($defaults[$name] as $count => &$values) { //get location type id. CRM_Utils_Array::lookupValue($values, 'location_type', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), $reverse); if ($name == 'address') { // FIXME: lookupValue doesn't work for vcard_name if (!empty($values['location_type_id'])) { $vcardNames = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array('labelColumn' => 'vcard_name')); $values['vcard_name'] = $vcardNames[$values['location_type_id']]; } if (!CRM_Utils_Array::lookupValue($values, 'country', CRM_Core_PseudoConstant::country(), $reverse) && $reverse) { CRM_Utils_Array::lookupValue($values, 'country', CRM_Core_PseudoConstant::countryIsoCode(), $reverse); } // CRM-7597 // if we find a country id above, we need to restrict it to that country // rather than the list of all countries if (!empty($values['country_id'])) { $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id']); } else { $stateProvinceList = CRM_Core_PseudoConstant::stateProvince(); } if (!CRM_Utils_Array::lookupValue($values, 'state_province', $stateProvinceList, $reverse) && $reverse) { if (!empty($values['country_id'])) { $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id'], 'abbreviation'); } else { $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceAbbreviation(); } CRM_Utils_Array::lookupValue($values, 'state_province', $stateProvinceList, $reverse); } if (!empty($values['state_province_id'])) { $countyList = CRM_Core_PseudoConstant::countyForState($values['state_province_id']); } else { $countyList = CRM_Core_PseudoConstant::county(); } CRM_Utils_Array::lookupValue($values, 'county', $countyList, $reverse); } if ($name == 'im') { CRM_Utils_Array::lookupValue($values, 'provider', CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), $reverse); } if ($name == 'phone') { CRM_Utils_Array::lookupValue($values, 'phone_type', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), $reverse); } // Kill the reference. unset($values); } } }
static function location(&$form) { // Build location criteria based on _submitValues if // available; otherwise, use $form->_formValues. $formValues = $form->_submitValues; if (empty($formValues) && !empty($form->_formValues)) { $formValues = $form->_formValues; } $form->addElement('hidden', 'hidden_location', 1); $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('street_address' => array(ts('Street Address'), $attributes['street_address'], NULL, NULL), 'city' => array(ts('City'), $attributes['city'], NULL, NULL), 'postal_code' => array(ts('Zip / Postal Code'), $attributes['postal_code'], NULL, NULL), 'county' => array(ts('County'), $attributes['county_id'], 'county', TRUE), 'state_province' => array(ts('State / Province'), $attributes['state_province_id'], 'stateProvince', TRUE), 'country' => array(ts('Country'), $attributes['country_id'], 'country', FALSE), 'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL, NULL), 'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL, NULL), 'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL, NULL), 'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL, NULL)); $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', $addressOptions, 0); $form->assign('parseStreetAddress', $parseStreetAddress); foreach ($elements as $name => $v) { list($title, $attributes, $select, $multiSelect) = $v; if (in_array($name, array('street_number', 'street_name', 'street_unit'))) { if (!$parseStreetAddress) { continue; } } elseif (!$addressOptions[$name]) { continue; } if (!$attributes) { $attributes = $attributes[$name]; } if ($select) { $config = CRM_Core_Config::singleton(); $countryDefault = $config->defaultContactCountry; $stateCountryMap[] = array('state_province' => 'state_province', 'country' => 'country', 'county' => 'county'); if ($select == 'stateProvince') { if ($countryDefault && !isset($formValues['country'])) { $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($countryDefault); } elseif ($formValues['country']) { $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($formValues['country']); } else { //if not setdefault any country $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select(); } $element = $form->addElement('select', $name, $title, $selectElements); } elseif ($select == 'country') { if ($countryDefault) { //for setdefault country $defaultValues = array(); $defaultValues[$name] = $countryDefault; $form->setDefaults($defaultValues); } $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select(); $element = $form->addElement('select', $name, $title, $selectElements); } elseif ($select == 'county') { if (array_key_exists('state_province', $formValues) && !CRM_Utils_System::isNull($formValues['state_province'])) { $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::countyForState($formValues['state_province']); } else { $selectElements = array('' => ts('- select a state -')); } $element = $form->addElement('select', $name, $title, $selectElements); } else { $selectElements = array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select(); $element = $form->addElement('select', $name, $title, $selectElements); } if ($multiSelect) { $element->setMultiple(TRUE); } } else { $form->addElement('text', $name, $title, $attributes); } if ($addressOptions['postal_code']) { $form->addElement('text', 'postal_code_low', ts('Range-From'), CRM_Utils_Array::value('postal_code', $attributes)); $form->addElement('text', 'postal_code_high', ts('To'), CRM_Utils_Array::value('postal_code', $attributes)); } } // extend addresses with proximity search $form->addElement('text', 'prox_distance', ts('Find contacts within')); $form->addElement('select', 'prox_distance_unit', NULL, array('miles' => ts('Miles'), 'kilos' => ts('Kilometers'))); // is there another form rule that does decimals besides money ? ... $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric'); CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap); $worldRegions = array('' => ts('- any region -')) + CRM_Core_PseudoConstant::worldRegion(); $form->addElement('select', 'world_region', ts('World Region'), $worldRegions); // checkboxes for location type $location_type = array(); $locationType = CRM_Core_PseudoConstant::locationType(); foreach ($locationType as $locationTypeID => $locationTypeName) { $location_type[] = $form->createElement('checkbox', $locationTypeID, NULL, $locationTypeName); } $form->addGroup($location_type, 'location_type', ts('Location Types'), ' '); // custom data extending addresses - $extends = array('Address'); $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends); if ($groupDetails) { $form->assign('addressGroupTree', $groupDetails); foreach ($groupDetails as $group) { foreach ($group['fields'] as $field) { $elementName = 'custom_' . $field['id']; CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $field['id'], FALSE, FALSE, TRUE); } } } }
static function jqCounty($config) { if (CRM_Utils_System::isNull($_GET['_value'])) { $elements = array(array('name' => ts('- select state -'), 'value' => '')); } else { $result = CRM_Core_PseudoConstant::countyForState($_GET['_value']); $elements = array(array('name' => ts('- select -'), 'value' => '')); foreach ($result as $id => $name) { $elements[] = array('name' => $name, 'value' => $id); } if ($elements == array(array('name' => ts('- select -'), 'value' => ''))) { $elements = array(array('name' => ts('- no counties -'), 'value' => '')); } } echo json_encode($elements); CRM_Utils_System::civiExit(); }
static function jqCounty() { if (!isset($_GET['_value']) || CRM_Utils_System::isNull($_GET['_value'])) { $elements = array(array('name' => ts('(choose state first)'), 'value' => '')); } else { $result = CRM_Core_PseudoConstant::countyForState($_GET['_value']); $elements = array(array('name' => $result ? ts('- select -') : ts('- N/A -'), 'value' => '')); foreach ($result as $id => $name) { $elements[] = array('name' => $name, 'value' => $id); } } echo json_encode($elements); CRM_Utils_System::civiExit(); }