Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.1 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2011 | | |
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. | | |
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 along with this program; if not, contact CiviCRM LLC | | |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
25 | */ | |
26 | ||
27 | /** | |
28 | * This file returns permissioned data required by dojo hierselect widget. | |
29 | * | |
30 | */ | |
31 | ||
32 | /** | |
33 | * call to invoke function | |
34 | * | |
35 | */ | |
36 | ||
37 | invoke(); | |
38 | exit(); | |
39 | ||
40 | /** | |
41 | * Invoke function that redirects to respective functions | |
42 | */ | |
43 | function invoke() { | |
44 | if (!isset($_GET['return'])) { | |
45 | return; | |
46 | } | |
47 | ||
48 | // intialize the system | |
49 | require_once '../civicrm.config.php'; | |
50 | require_once 'CRM/Core/Config.php'; | |
51 | $config = &CRM_Core_Config::singleton(); | |
52 | ||
53 | switch ($_GET['return']) { | |
54 | case 'states': | |
55 | return states($config); | |
56 | ||
57 | case 'countries': | |
58 | return countries($config); | |
59 | ||
60 | default: | |
61 | return; | |
62 | } | |
63 | } | |
64 | ||
65 | /** | |
66 | * Test Function used for new hs-widget. | |
67 | */ | |
68 | function states(&$config) { | |
69 | $elements = array(); | |
70 | if (isset($_GET['node1'])) { | |
71 | require_once 'CRM/Utils/Type.php'; | |
72 | $countryId = CRM_Utils_Type::escape($_GET['node1'], 'String'); | |
73 | ||
74 | if ($countryId) { | |
75 | $stateName = NULL; | |
76 | if (isset($_GET['name'])) { | |
77 | $stateName = trim(CRM_Utils_Type::escape($_GET['name'], 'String')); | |
78 | $stateName = str_replace('*', '%', $stateName); | |
79 | } | |
80 | ||
81 | $default = NULL; | |
82 | if (isset($_GET['default'])) { | |
83 | $default = trim(CRM_Utils_Type::escape($_GET['default'], 'Boolean')); | |
84 | } | |
85 | ||
86 | $stateId = NULL; | |
87 | if (isset($_GET['id'])) { | |
88 | $stateId = CRM_Utils_Type::escape($_GET['id'], 'Positive', FALSE); | |
89 | } | |
90 | ||
91 | $query = " | |
92 | SELECT civicrm_state_province.name name, civicrm_state_province.id id | |
93 | FROM civicrm_state_province | |
94 | WHERE civicrm_state_province.country_id={$countryId} | |
95 | AND civicrm_state_province.name LIKE LOWER('$stateName%') | |
96 | ORDER BY name"; | |
97 | ||
98 | $nullArray = array(); | |
99 | $dao = CRM_Core_DAO::executeQuery($query, $nullArray); | |
100 | ||
101 | if ($default) { | |
102 | while ($dao->fetch()) { | |
103 | $elements[] = array('name' => ts($dao->name), | |
104 | 'value' => $dao->id, | |
105 | ); | |
106 | } | |
107 | } | |
108 | elseif ($stateId) { | |
109 | while ($dao->fetch()) { | |
110 | if ($dao->id == $stateId) { | |
111 | $elements[] = array('name' => ts($dao->name), | |
112 | 'value' => $dao->id, | |
113 | ); | |
114 | } | |
115 | } | |
116 | } | |
117 | else { | |
118 | $count = 0; | |
119 | while ($dao->fetch() && $count < 5) { | |
120 | $elements[] = array('name' => ts($dao->name), | |
121 | 'value' => $dao->id, | |
122 | ); | |
123 | $count++; | |
124 | } | |
125 | } | |
126 | ||
127 | if (empty($elements)) { | |
128 | if ($stateName != '- type first letter(s) -') { | |
129 | $label = '- state n/a -'; | |
130 | } | |
131 | else { | |
132 | $label = '- type first letter(s) -'; | |
133 | } | |
134 | $elements[] = array('name' => $label, | |
135 | 'value' => '', | |
136 | ); | |
137 | } | |
138 | elseif (!$default && !$stateId && (!$stateName || $stateName == '- type first letter(s) -')) { | |
139 | $elements = array(); | |
140 | $elements[] = array('name' => '- type first letter(s) -', | |
141 | 'value' => '', | |
142 | ); | |
143 | } | |
144 | } | |
145 | else { | |
146 | $elements[] = array('name' => '- state n/a -', | |
147 | 'value' => '', | |
148 | ); | |
149 | } | |
150 | } | |
151 | ||
152 | require_once "CRM/Utils/JSON.php"; | |
153 | echo CRM_Utils_JSON::encode($elements, 'value'); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Test Function used for new hs-widget. | |
158 | */ | |
159 | function countries(&$config) { | |
160 | //get the country limit and restrict the combo select options | |
161 | $limitCodes = $config->countryLimit(); | |
162 | if (!is_array($limitCodes)) { | |
163 | $limitCodes = array($config->countryLimit => 1); | |
164 | } | |
165 | ||
166 | $limitCodes = array_intersect(CRM_Core_PseudoConstant::countryIsoCode(), $limitCodes); | |
167 | // added for testing purpose | |
168 | //$limitCodes['1101'] = 'IN'; | |
169 | if (count($limitCodes)) { | |
170 | $whereClause = " iso_code IN ('" . implode("', '", $limitCodes) . "')"; | |
171 | } | |
172 | else { | |
173 | $whereClause = " 1"; | |
174 | } | |
175 | ||
176 | $elements = array(); | |
177 | require_once 'CRM/Utils/Type.php'; | |
178 | ||
179 | $name = NULL; | |
180 | if (isset($_GET['name'])) { | |
181 | $name = CRM_Utils_Type::escape($_GET['name'], 'String'); | |
182 | } | |
183 | ||
184 | $countryId = NULL; | |
185 | if (isset($_GET['id'])) { | |
186 | $countryId = CRM_Utils_Type::escape($_GET['id'], 'Positive', FALSE); | |
187 | } | |
188 | ||
189 | //temporary fix to handle locales other than default US, | |
190 | // CRM-2653 | |
191 | if (!$countryId && $name && $config->lcMessages != 'en_US') { | |
192 | $countries = CRM_Core_PseudoConstant::country(); | |
193 | ||
194 | // get the country name in en_US, since db has this locale | |
195 | $countryName = array_search($name, $countries); | |
196 | ||
197 | if ($countryName) { | |
198 | $countryId = $countryName; | |
199 | } | |
200 | } | |
201 | ||
202 | $validValue = TRUE; | |
203 | if (!$name && !$countryId) { | |
204 | $validValue = FALSE; | |
205 | } | |
206 | ||
207 | if ($validValue) { | |
208 | if (!$countryId) { | |
209 | $name = str_replace('*', '%', $name); | |
210 | $countryClause = " civicrm_country.name LIKE LOWER('$name%') "; | |
211 | } | |
212 | else { | |
213 | $countryClause = " civicrm_country.id = {$countryId} "; | |
214 | } | |
215 | ||
216 | $query = " | |
217 | SELECT id, name | |
218 | FROM civicrm_country | |
219 | WHERE {$countryClause} | |
220 | AND {$whereClause} | |
221 | ORDER BY name"; | |
222 | ||
223 | $nullArray = array(); | |
224 | $dao = CRM_Core_DAO::executeQuery($query, $nullArray); | |
225 | ||
226 | $count = 0; | |
227 | while ($dao->fetch() && $count < 5) { | |
228 | $elements[] = array('name' => ts($dao->name), | |
229 | 'value' => $dao->id, | |
230 | ); | |
231 | $count++; | |
232 | } | |
233 | } | |
234 | ||
235 | if (empty($elements)) { | |
236 | if (isset($_GET['id'])) { | |
237 | $name = $_GET['id']; | |
238 | } | |
239 | ||
240 | $elements[] = array('name' => trim($name, "%"), | |
241 | 'value' => trim($name, "%"), | |
242 | ); | |
243 | } | |
244 | ||
245 | require_once "CRM/Utils/JSON.php"; | |
246 | echo CRM_Utils_JSON::encode($elements, 'value'); | |
247 | } | |
248 |