Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality to support Proximity Searches | |
38 | */ | |
39 | class CRM_Contact_Form_Task_ProximityCommon extends CRM_Contact_Form_Task { | |
40 | ||
41 | /** | |
42 | * The context that we are working on | |
43 | * | |
44 | * @var string | |
45 | */ | |
46 | protected $_context; | |
47 | ||
48 | /** | |
49 | * the groupId retrieved from the GET vars | |
50 | * | |
51 | * @var int | |
52 | */ | |
53 | protected $_id; | |
54 | ||
55 | /** | |
56 | * the title of the group | |
57 | * | |
58 | * @var string | |
59 | */ | |
60 | protected $_title; | |
61 | ||
62 | /** | |
63 | * build all the data structures needed to build the form | |
64 | * | |
65 | * @return void | |
66 | * @access public | |
67 | */ | |
68 | function preProcess() { | |
69 | /* | |
70 | * initialize the task and row fields | |
71 | */ | |
72 | parent::preProcess(); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Build the form | |
77 | * | |
78 | * @access public | |
79 | * | |
80 | * @return void | |
81 | */ | |
82 | function buildQuickForm($form, $proxSearch) { | |
83 | // is proximity search required (2) or optional (1)? | |
84 | $proxRequired = ($proxSearch == 2 ? TRUE : FALSE); | |
85 | $form->assign('proximity_search', TRUE); | |
86 | ||
87 | $form->add('text', 'prox_street_address', ts('Street Address'), NULL, FALSE); | |
88 | ||
89 | $form->add('text', 'prox_city', ts('City'), NULL, FALSE); | |
90 | ||
91 | $form->add('text', 'prox_postal_code', ts('Postal Code'), NULL, FALSE); | |
92 | ||
93 | $defaults = self::setDefaultValues($form); | |
94 | if (CRM_Utils_Array::value('prox_country_id', $defaults)) { | |
95 | $stateProvince = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($defaults['prox_country_id']); | |
96 | } | |
97 | else { | |
98 | $stateProvince = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvince(); | |
99 | } | |
100 | $form->add('select', 'prox_state_province_id', ts('State/Province'), $stateProvince, $proxRequired); | |
101 | ||
102 | $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country(); | |
103 | $form->add('select', 'prox_country_id', ts('Country'), $country, $proxRequired); | |
104 | ||
105 | $form->add('text', 'prox_distance', ts('Distance'), NULL, $proxRequired); | |
106 | ||
107 | $proxUnits = array('km' => ts('km'), 'miles' => ts('miles')); | |
108 | $form->add('select', 'prox_distance_unit', ts('Units'), $proxUnits, $proxRequired); | |
109 | // prox_distance_unit | |
110 | ||
111 | // state country js, CRM-5233 | |
112 | $stateCountryMap = array(); | |
113 | $stateCountryMap[] = array( | |
114 | 'state_province' => 'prox_state_province_id', | |
115 | 'country' => 'prox_country_id', | |
116 | ); | |
117 | CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap); | |
118 | CRM_Core_BAO_Address::fixAllStateSelects($this, $defaults); | |
119 | $form->addFormRule(array('CRM_Contact_Form_Task_ProximityCommon', 'formRule'), $form); | |
120 | } | |
121 | ||
122 | /** | |
123 | * global form rule | |
124 | * | |
125 | * @param array $fields the input form values | |
126 | * @param array $files the uploaded files if any | |
127 | * @param array $options additional user data | |
128 | * | |
129 | * @return true if no errors, else array of errors | |
130 | * @access public | |
131 | * @static | |
132 | */ | |
133 | static function formRule($fields, $files, $form) { | |
134 | $errors = array(); | |
135 | // If Distance is present, make sure state, country and city or postal code are populated. | |
136 | if (CRM_Utils_Array::value('prox_distance', $fields)) { | |
137 | if (!CRM_Utils_Array::value('prox_state_province_id', $fields) || | |
138 | !CRM_Utils_Array::value('prox_country_id', $fields) | |
139 | ) { | |
140 | $errors["prox_state_province_id"] = ts("Country AND State/Province are required to search by distance."); | |
141 | } | |
142 | if (!CRM_Utils_Array::value('prox_postal_code', $fields) AND | |
143 | !CRM_Utils_Array::value('prox_city', $fields) | |
144 | ) { | |
145 | $errors["prox_distance"] = ts("City OR Postal Code are required to search by distance."); | |
146 | } | |
147 | } | |
148 | ||
149 | return empty($errors) ? TRUE : $errors; | |
150 | } | |
151 | ||
152 | /** | |
153 | * Set the default form values | |
154 | * | |
155 | * @access protected | |
156 | * | |
157 | * @return array the default array reference | |
158 | */ | |
159 | function setDefaultValues($form) { | |
160 | $defaults = array(); | |
161 | $config = CRM_Core_Config::singleton(); | |
162 | $countryDefault = $config->defaultContactCountry; | |
163 | ||
164 | if ($countryDefault) { | |
165 | $defaults['prox_country_id'] = $countryDefault; | |
166 | if ($countryDefault == '1228') { | |
167 | $defaults['prox_distance_unit'] = 'miles'; | |
168 | } | |
169 | else { | |
170 | $defaults['prox_distance_unit'] = 'km'; | |
171 | } | |
172 | } | |
173 | $form->setDefaults($defaults); | |
174 | return $defaults; | |
175 | } | |
176 | } | |
177 |