commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / tools / bin / scripts / ajax.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * @param $config
68 */
69 function states(&$config) {
70 $elements = array();
71 if (isset($_GET['node1'])) {
72 require_once 'CRM/Utils/Type.php';
73 $countryId = CRM_Utils_Type::escape($_GET['node1'], 'String');
74
75 if ($countryId) {
76 $stateName = NULL;
77 if (isset($_GET['name'])) {
78 $stateName = trim(CRM_Utils_Type::escape($_GET['name'], 'String'));
79 $stateName = str_replace('*', '%', $stateName);
80 }
81
82 $default = NULL;
83 if (isset($_GET['default'])) {
84 $default = trim(CRM_Utils_Type::escape($_GET['default'], 'Boolean'));
85 }
86
87 $stateId = NULL;
88 if (isset($_GET['id'])) {
89 $stateId = CRM_Utils_Type::escape($_GET['id'], 'Positive', FALSE);
90 }
91
92 $query = "
93 SELECT civicrm_state_province.name name, civicrm_state_province.id id
94 FROM civicrm_state_province
95 WHERE civicrm_state_province.country_id={$countryId}
96 AND civicrm_state_province.name LIKE LOWER('$stateName%')
97 ORDER BY name";
98
99 $nullArray = array();
100 $dao = CRM_Core_DAO::executeQuery($query, $nullArray);
101
102 if ($default) {
103 while ($dao->fetch()) {
104 $elements[] = array('name' => ts($dao->name),
105 'value' => $dao->id,
106 );
107 }
108 }
109 elseif ($stateId) {
110 while ($dao->fetch()) {
111 if ($dao->id == $stateId) {
112 $elements[] = array('name' => ts($dao->name),
113 'value' => $dao->id,
114 );
115 }
116 }
117 }
118 else {
119 $count = 0;
120 while ($dao->fetch() && $count < 5) {
121 $elements[] = array('name' => ts($dao->name),
122 'value' => $dao->id,
123 );
124 $count++;
125 }
126 }
127
128 if (empty($elements)) {
129 if ($stateName != '- type first letter(s) -') {
130 $label = '- state n/a -';
131 }
132 else {
133 $label = '- type first letter(s) -';
134 }
135 $elements[] = array('name' => $label,
136 'value' => '',
137 );
138 }
139 elseif (!$default && !$stateId && (!$stateName || $stateName == '- type first letter(s) -')) {
140 $elements = array();
141 $elements[] = array('name' => '- type first letter(s) -',
142 'value' => '',
143 );
144 }
145 }
146 else {
147 $elements[] = array('name' => '- state n/a -',
148 'value' => '',
149 );
150 }
151 }
152
153 require_once "CRM/Utils/JSON.php";
154 echo CRM_Utils_JSON::encode($elements, 'value');
155 }
156
157 /**
158 * Test Function used for new hs-widget.
159 * @param $config
160 */
161 function countries(&$config) {
162 //get the country limit and restrict the combo select options
163 $limitCodes = $config->countryLimit();
164 if (!is_array($limitCodes)) {
165 $limitCodes = array($config->countryLimit => 1);
166 }
167
168 $limitCodes = array_intersect(CRM_Core_PseudoConstant::countryIsoCode(), $limitCodes);
169 // added for testing purpose
170 //$limitCodes['1101'] = 'IN';
171 if (count($limitCodes)) {
172 $whereClause = " iso_code IN ('" . implode("', '", $limitCodes) . "')";
173 }
174 else {
175 $whereClause = " 1";
176 }
177
178 $elements = array();
179 require_once 'CRM/Utils/Type.php';
180
181 $name = NULL;
182 if (isset($_GET['name'])) {
183 $name = CRM_Utils_Type::escape($_GET['name'], 'String');
184 }
185
186 $countryId = NULL;
187 if (isset($_GET['id'])) {
188 $countryId = CRM_Utils_Type::escape($_GET['id'], 'Positive', FALSE);
189 }
190
191 //temporary fix to handle locales other than default US,
192 // CRM-2653
193 if (!$countryId && $name && $config->lcMessages != 'en_US') {
194 $countries = CRM_Core_PseudoConstant::country();
195
196 // get the country name in en_US, since db has this locale
197 $countryName = array_search($name, $countries);
198
199 if ($countryName) {
200 $countryId = $countryName;
201 }
202 }
203
204 $validValue = TRUE;
205 if (!$name && !$countryId) {
206 $validValue = FALSE;
207 }
208
209 if ($validValue) {
210 if (!$countryId) {
211 $name = str_replace('*', '%', $name);
212 $countryClause = " civicrm_country.name LIKE LOWER('$name%') ";
213 }
214 else {
215 $countryClause = " civicrm_country.id = {$countryId} ";
216 }
217
218 $query = "
219 SELECT id, name
220 FROM civicrm_country
221 WHERE {$countryClause}
222 AND {$whereClause}
223 ORDER BY name";
224
225 $nullArray = array();
226 $dao = CRM_Core_DAO::executeQuery($query, $nullArray);
227
228 $count = 0;
229 while ($dao->fetch() && $count < 5) {
230 $elements[] = array('name' => ts($dao->name),
231 'value' => $dao->id,
232 );
233 $count++;
234 }
235 }
236
237 if (empty($elements)) {
238 if (isset($_GET['id'])) {
239 $name = $_GET['id'];
240 }
241
242 $elements[] = array('name' => trim($name, "%"),
243 'value' => trim($name, "%"),
244 );
245 }
246
247 require_once "CRM/Utils/JSON.php";
248 echo CRM_Utils_JSON::encode($elements, 'value');
249 }
250