3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 * This class provides the functionality to support Proximity Searches.
37 class CRM_Contact_Form_Task_ProximityCommon
{
40 * The context that we are working on.
47 * The groupId retrieved from the GET vars.
54 * The title of the group.
61 * Build the form object.
63 * @param CRM_Core_Form $form
64 * @param int $proxSearch
66 static public function buildQuickForm($form, $proxSearch) {
67 // is proximity search required (2) or optional (1)?
68 $proxRequired = ($proxSearch == 2 ?
TRUE : FALSE);
69 $form->assign('proximity_search', TRUE);
71 $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE);
73 $form->add('text', 'prox_city', ts('City'), NULL, FALSE);
75 $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE);
77 $form->addChainSelect('prox_state_province_id', array('required' => $proxRequired));
79 $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant
::country();
80 $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired);
82 $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired);
84 $proxUnits = array('km' => ts('km'), 'miles' => ts('miles'));
85 $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired);
88 $form->addFormRule(array('CRM_Contact_Form_Task_ProximityCommon', 'formRule'), $form);
94 * @param array $fields
95 * The input form values.
97 * The uploaded files if any.
98 * @param CRM_Core_Form $form
101 * true if no errors, else array of errors
103 public static function formRule($fields, $files, $form) {
105 // If Distance is present, make sure state, country and city or postal code are populated.
106 if (!empty($fields['prox_distance'])) {
107 if (empty($fields['prox_state_province_id']) ||
empty($fields['prox_country_id'])) {
108 $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance.");
110 if (!CRM_Utils_Array
::value('prox_postal_code', $fields) AND
111 !CRM_Utils_Array
::value('prox_city', $fields)
113 $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance.");
117 return empty($errors) ?
TRUE : $errors;
121 * Set the default form values.
123 * @param CRM_Core_Form $form
126 * the default array reference
128 static public function setDefaultValues($form) {
130 $config = CRM_Core_Config
::singleton();
131 $countryDefault = $config->defaultContactCountry
;
133 if ($countryDefault) {
134 $defaults['prox_country_id'] = $countryDefault;
135 if ($countryDefault == '1228') {
136 $defaults['prox_distance_unit'] = 'miles';
139 $defaults['prox_distance_unit'] = 'km';
142 $form->setDefaults($defaults);