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 provides the functionality to support Proximity Searches
39 class CRM_Contact_Form_Task_ProximityCommon
extends CRM_Contact_Form_Task
{
42 * The context that we are working on
49 * the groupId retrieved from the GET vars
56 * the title of the group
63 * build all the data structures needed to build the form
68 function preProcess() {
70 * initialize the task and row fields
85 function buildQuickForm($form, $proxSearch) {
86 // is proximity search required (2) or optional (1)?
87 $proxRequired = ($proxSearch == 2 ?
TRUE : FALSE);
88 $form->assign('proximity_search', TRUE);
90 $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE);
92 $form->add('text', 'prox_city', ts('City'), NULL, FALSE);
94 $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE);
96 $defaults = self
::setDefaultValues($form);
97 if (!empty($defaults['prox_country_id'])) {
98 $stateProvince = array('' => ts('- select -')) + CRM_Core_PseudoConstant
::stateProvinceForCountry($defaults['prox_country_id']);
101 $stateProvince = array('' => ts('- select -')) + CRM_Core_PseudoConstant
::stateProvince();
103 $form->add('select', 'prox_state_province_id', ts('State/Province'), $stateProvince, $proxRequired);
105 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant
::country();
106 $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired);
108 $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired);
110 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
111 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired);
112 // prox_distance_unit
114 // state country js, CRM-5233
115 $stateCountryMap = array();
116 $stateCountryMap[] = array(
117 'state_province' => 'prox_state_province_id',
118 'country' => 'prox_country_id',
120 CRM_Core_BAO_Address
::addStateCountryMap($stateCountryMap);
121 CRM_Core_BAO_Address
::fixAllStateSelects($this, $defaults);
122 $form->addFormRule(array('CRM_Contact_Form_Task_ProximityCommon', 'formRule'), $form);
128 * @param array $fields the input form values
129 * @param array $files the uploaded files if any
132 * @internal param array $options additional user data
134 * @return true if no errors, else array of errors
138 static function formRule($fields, $files, $form) {
140 // If Distance is present, make sure state, country and city or postal code are populated.
141 if (!empty($fields['prox_distance'])) {
142 if (empty($fields['prox_state_province_id']) ||
empty($fields['prox_country_id'])) {
143 $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance.");
145 if (!CRM_Utils_Array
::value('prox_postal_code', $fields) AND
146 !CRM_Utils_Array
::value('prox_city', $fields)
148 $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance.");
152 return empty($errors) ?
TRUE : $errors;
156 * Set the default form values
162 * @return array the default array reference
164 function setDefaultValues($form) {
166 $config = CRM_Core_Config
::singleton();
167 $countryDefault = $config->defaultContactCountry
;
169 if ($countryDefault) {
170 $defaults['prox_country_id'] = $countryDefault;
171 if ($countryDefault == '1228') {
172 $defaults['prox_distance_unit'] = 'miles';
175 $defaults['prox_distance_unit'] = 'km';
178 $form->setDefaults($defaults);