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