Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 contains all the function that are called using AJAX | |
38 | */ | |
39 | class CRM_Core_Page_AJAX_Location { | |
40 | ||
41 | /** | |
42 | * FIXME: we should make this method like getLocBlock() OR use the same method and | |
43 | * remove this one. | |
44 | * | |
45 | * Function to obtain the location of given contact-id. | |
46 | * This method is used by on-behalf-of form to dynamically generate poulate the | |
47 | * location field values for selected permissioned contact. | |
48 | */ | |
49 | function getPermissionedLocation() { | |
50 | $cid = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); | |
51 | if ($_GET['ufId']) { | |
52 | $ufId = CRM_Utils_Type::escape($_GET['ufId'], 'Integer'); | |
53 | } | |
54 | elseif ($_GET['relContact']) { | |
55 | $relContact = CRM_Utils_Type::escape($_GET['relContact'], 'Integer'); | |
56 | } | |
57 | ||
58 | $values = array(); | |
59 | $entityBlock = array('contact_id' => $cid); | |
60 | $location = CRM_Core_BAO_Location::getValues($entityBlock); | |
61 | ||
62 | $config = CRM_Core_Config::singleton(); | |
63 | $addressSequence = array_flip($config->addressSequence()); | |
64 | ||
65 | ||
66 | if ($relContact) { | |
67 | $elements = array( | |
68 | "phone_1_phone" => | |
69 | $location['phone'][1]['phone'], | |
70 | "email_1_email" => | |
71 | $location['email'][1]['email'], | |
72 | ); | |
73 | ||
74 | if (array_key_exists('street_address', $addressSequence)) { | |
75 | $elements["address_1_street_address"] = $location['address'][1]['street_address']; | |
76 | } | |
77 | if (array_key_exists('supplemental_address_1', $addressSequence)) { | |
78 | $elements['address_1_supplemental_address_1'] = $location['address'][1]['supplemental_address_1']; | |
79 | } | |
80 | if (array_key_exists('supplemental_address_2', $addressSequence)) { | |
81 | $elements['address_1_supplemental_address_2'] = $location['address'][1]['supplemental_address_2']; | |
82 | } | |
83 | if (array_key_exists('city', $addressSequence)) { | |
84 | $elements['address_1_city'] = $location['address'][1]['city']; | |
85 | } | |
86 | if (array_key_exists('postal_code', $addressSequence)) { | |
87 | $elements['address_1_postal_code'] = $location['address'][1]['postal_code']; | |
88 | $elements['address_1_postal_code_suffix'] = $location['address'][1]['postal_code_suffix']; | |
89 | } | |
90 | if (array_key_exists('country', $addressSequence)) { | |
91 | $elements['address_1_country_id'] = $location['address'][1]['country_id']; | |
92 | } | |
93 | if (array_key_exists('state_province', $addressSequence)) { | |
94 | $elements['address_1_state_province_id'] = $location['address'][1]['state_province_id']; | |
95 | } | |
96 | } | |
97 | else { | |
98 | $profileFields = CRM_Core_BAO_UFGroup::getFields($ufId, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, | |
99 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL | |
100 | ); | |
101 | $website = CRM_Core_BAO_Website::getValues($entityBlock, $values); | |
102 | ||
103 | foreach ($location as $fld => $values) { | |
104 | if (is_array($values) && !empty($values)) { | |
105 | $locType = $values[1]['location_type_id']; | |
106 | if ($fld == 'email') { | |
107 | $elements["onbehalf_{$fld}-{$locType}"] = array( | |
108 | 'type' => 'Text', | |
109 | 'value' => $location[$fld][1][$fld], | |
110 | ); | |
111 | unset($profileFields["{$fld}-{$locType}"]); | |
112 | } | |
113 | elseif ($fld == 'phone') { | |
114 | $phoneTypeId = $values[1]['phone_type_id']; | |
115 | $elements["onbehalf_{$fld}-{$locType}-{$phoneTypeId}"] = array( | |
116 | 'type' => 'Text', | |
117 | 'value' => $location[$fld][1][$fld], | |
118 | ); | |
119 | unset($profileFields["{$fld}-{$locType}-{$phoneTypeId}"]); | |
120 | } | |
121 | elseif ($fld == 'im') { | |
122 | $providerId = $values[1]['provider_id']; | |
123 | $elements["onbehalf_{$fld}-{$locType}"] = array( | |
124 | 'type' => 'Text', | |
125 | 'value' => $location[$fld][1][$fld], | |
126 | ); | |
127 | $elements["onbehalf_{$fld}-{$locType}provider_id"] = array( | |
128 | 'type' => 'Select', | |
129 | 'value' => $location[$fld][1]['provider_id'], | |
130 | ); | |
131 | unset($profileFields["{$fld}-{$locType}-{$providerId}"]); | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | if (!empty($website)) { | |
137 | foreach ($website as $key => $val) { | |
138 | $websiteTypeId = $values[1]['website_type_id']; | |
139 | $elements["onbehalf_url-1"] = array( | |
140 | 'type' => 'Text', | |
141 | 'value' => $website[1]['url'], | |
142 | ); | |
143 | $elements["onbehalf_url-1-website_type_id"] = array( | |
144 | 'type' => 'Select', | |
145 | 'value' => $website[1]['website_type_id'], | |
146 | ); | |
147 | unset($profileFields["url-1"]); | |
148 | } | |
149 | } | |
150 | ||
151 | $locTypeId = $location['address'][1]['location_type_id']; | |
152 | $addressFields = array( | |
153 | 'street_address', | |
154 | 'supplemental_address_1', | |
155 | 'supplemental_address_2', | |
156 | 'city', | |
157 | 'postal_code', | |
158 | 'country', | |
159 | 'state_province', | |
160 | ); | |
161 | ||
162 | foreach ($addressFields as $field) { | |
163 | if (array_key_exists($field, $addressSequence)) { | |
164 | $addField = $field; | |
165 | if (in_array($field, array( | |
166 | 'state_province', 'country'))) { | |
167 | $addField = "{$field}_id"; | |
168 | } | |
169 | $elements["onbehalf_{$field}-{$locTypeId}"] = array( | |
170 | 'type' => 'Text', | |
171 | 'value' => $location['address'][1][$addField], | |
172 | ); | |
173 | unset($profileFields["{$field}-{$locTypeId}"]); | |
174 | } | |
175 | } | |
176 | ||
177 | //set custom field defaults | |
178 | $defaults = array(); | |
179 | CRM_Core_BAO_UFGroup::setProfileDefaults($cid, $profileFields, $defaults, TRUE, NULL, NULL, TRUE); | |
180 | ||
181 | if (!empty($defaults)) { | |
182 | foreach ($profileFields as $key => $val) { | |
183 | ||
184 | if (array_key_exists($key, $defaults)) { | |
185 | $htmlType = CRM_Utils_Array::value('html_type', $val); | |
186 | if ($htmlType == 'Radio') { | |
187 | $elements["onbehalf[{$key}]"]['type'] = $htmlType; | |
188 | $elements["onbehalf[{$key}]"]['value'] = $defaults[$key]; | |
189 | } | |
190 | elseif ($htmlType == 'CheckBox') { | |
191 | foreach ($defaults[$key] as $k => $v) { | |
192 | $elements["onbehalf[{$key}][{$k}]"]['type'] = $htmlType; | |
193 | $elements["onbehalf[{$key}][{$k}]"]['value'] = $v; | |
194 | } | |
195 | } | |
196 | elseif ($htmlType == 'Multi-Select') { | |
197 | foreach ($defaults[$key] as $k => $v) { | |
198 | $elements["onbehalf_{$key}"]['type'] = $htmlType; | |
199 | $elements["onbehalf_{$key}"]['value'][$k] = $v; | |
200 | } | |
201 | } | |
202 | elseif ($htmlType == 'Autocomplete-Select') { | |
203 | $elements["onbehalf_{$key}"]['type'] = $htmlType; | |
204 | $elements["onbehalf_{$key}"]['value'] = $defaults[$key]; | |
205 | $elements["onbehalf_{$key}"]['id'] = $defaults["{$key}_id"]; | |
206 | } | |
207 | else { | |
208 | $elements["onbehalf_{$key}"]['type'] = $htmlType; | |
209 | $elements["onbehalf_{$key}"]['value'] = $defaults[$key]; | |
210 | } | |
211 | } | |
212 | else { | |
213 | $elements["onbehalf_{$key}"]['value'] = ''; | |
214 | } | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | echo json_encode($elements); | |
220 | CRM_Utils_System::civiExit(); | |
221 | } | |
222 | ||
223 | function jqState($config) { | |
224 | if (!isset($_GET['_value']) || | |
225 | empty($_GET['_value']) | |
226 | ) { | |
227 | CRM_Utils_System::civiExit(); | |
228 | } | |
229 | ||
230 | $result = CRM_Core_PseudoConstant::stateProvinceForCountry($_GET['_value']); | |
231 | ||
232 | $elements = array(array('name' => ts('- select a state -'), | |
233 | 'value' => '', | |
234 | )); | |
235 | foreach ($result as $id => $name) { | |
236 | $elements[] = array( | |
237 | 'name' => $name, | |
238 | 'value' => $id, | |
239 | ); | |
240 | } | |
241 | ||
242 | echo json_encode($elements); | |
243 | CRM_Utils_System::civiExit(); | |
244 | } | |
245 | ||
246 | function jqCounty($config) { | |
247 | if (CRM_Utils_System::isNull($_GET['_value'])) { | |
248 | $elements = array(array('name' => ts('- select state -'), | |
249 | 'value' => '', | |
250 | )); | |
251 | } | |
252 | else { | |
253 | ||
254 | $result = CRM_Core_PseudoConstant::countyForState($_GET['_value']); | |
255 | ||
256 | $elements = array(array('name' => ts('- select -'), | |
257 | 'value' => '', | |
258 | )); | |
259 | foreach ($result as $id => $name) { | |
260 | $elements[] = array( | |
261 | 'name' => $name, | |
262 | 'value' => $id, | |
263 | ); | |
264 | } | |
265 | ||
266 | if ($elements == array( | |
267 | array('name' => ts('- select -'), 'value' => ''))) { | |
268 | $elements = array(array('name' => ts('- no counties -'), | |
269 | 'value' => '', | |
270 | )); | |
271 | } | |
272 | } | |
273 | ||
274 | echo json_encode($elements); | |
275 | CRM_Utils_System::civiExit(); | |
276 | } | |
277 | ||
278 | function getLocBlock() { | |
279 | // i wish i could retrieve loc block info based on loc_block_id, | |
280 | // Anyway, lets retrieve an event which has loc_block_id set to 'lbid'. | |
281 | if ($_POST['lbid']) { | |
282 | $params = array('1' => array($_POST['lbid'], 'Integer')); | |
283 | $eventId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_event WHERE loc_block_id=%1 LIMIT 1', $params); | |
284 | } | |
285 | // now lets use the event-id obtained above, to retrieve loc block information. | |
286 | if ($eventId) { | |
287 | $params = array('entity_id' => $eventId, 'entity_table' => 'civicrm_event'); | |
288 | // second parameter is of no use, but since required, lets use the same variable. | |
289 | $location = CRM_Core_BAO_Location::getValues($params, $params); | |
290 | } | |
291 | ||
292 | $result = array(); | |
293 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
294 | 'address_options', TRUE, NULL, TRUE | |
295 | ); | |
296 | // lets output only required fields. | |
297 | foreach ($addressOptions as $element => $isSet) { | |
298 | if ($isSet && (!in_array($element, array( | |
299 | 'im', 'openid')))) { | |
300 | if (in_array($element, array( | |
301 | 'country', 'state_province', 'county'))) { | |
302 | $element .= '_id'; | |
303 | } | |
304 | elseif ($element == 'address_name') { | |
305 | $element = 'name'; | |
306 | } | |
307 | $fld = "address[1][{$element}]"; | |
308 | $value = CRM_Utils_Array::value($element, $location['address'][1]); | |
309 | $value = $value ? $value : ""; | |
310 | $result[str_replace(array( | |
311 | '][', '[', "]"), array('_', '_', ''), $fld)] = $value; | |
312 | } | |
313 | } | |
314 | ||
315 | foreach (array( | |
316 | 'email', 'phone_type_id', 'phone') as $element) { | |
317 | $block = ($element == 'phone_type_id') ? 'phone' : $element; | |
318 | for ($i = 1; $i < 3; $i++) { | |
319 | $fld = "{$block}[{$i}][{$element}]"; | |
320 | $value = CRM_Utils_Array::value($element, $location[$block][$i]); | |
321 | $value = $value ? $value : ""; | |
322 | $result[str_replace(array( | |
323 | '][', '[', "]"), array('_', '_', ''), $fld)] = $value; | |
324 | } | |
325 | } | |
326 | ||
327 | // set the message if loc block is being used by more than one event. | |
328 | $result['count_loc_used'] = CRM_Event_BAO_Event::countEventsUsingLocBlockId($_POST['lbid']); | |
329 | ||
330 | echo json_encode($result); | |
331 | CRM_Utils_System::civiExit(); | |
332 | } | |
333 | } | |
334 |