Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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 | * | |
c490a46a | 44 | * obtain the location of given contact-id. |
6a488035 TO |
45 | * This method is used by on-behalf-of form to dynamically generate poulate the |
46 | * location field values for selected permissioned contact. | |
47 | */ | |
00be9182 | 48 | public 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 | |
353ffa53 | 53 | $user = CRM_Utils_Request::retrieve('uid', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, CRM_Core_Session::singleton() |
518fa0ee | 54 | ->get('userID')); |
08b83340 | 55 | if (empty($user) || (CRM_Utils_Request::retrieve('cs', 'String', $form, FALSE) && !CRM_Contact_BAO_Contact_Permission::validateChecksumContact($user, CRM_Core_DAO::$_nullObject, FALSE)) |
3255c209 | 56 | ) { |
d6d56b6b | 57 | CRM_Utils_System::civiExit(); |
6a488035 | 58 | } |
3719c6dd CW |
59 | |
60 | // Verify user permission on related contact | |
33260076 | 61 | $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($user, NULL, NULL, 'Organization'); |
62 | if (!isset($organizations[$cid])) { | |
3719c6dd | 63 | CRM_Utils_System::civiExit(); |
6a488035 TO |
64 | } |
65 | ||
be2fb01f CW |
66 | $values = []; |
67 | $entityBlock = ['contact_id' => $cid]; | |
353ffa53 | 68 | $location = CRM_Core_BAO_Location::getValues($entityBlock); |
6a488035 | 69 | |
9b118398 | 70 | $addressSequence = array_flip(CRM_Utils_Address::sequence(\Civi::settings()->get('address_format'))); |
6a488035 | 71 | |
2aa397bc | 72 | $profileFields = CRM_Core_BAO_UFGroup::getFields($ufId, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, |
353ffa53 TO |
73 | NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL |
74 | ); | |
2aa397bc | 75 | $website = CRM_Core_BAO_Website::getValues($entityBlock, $values); |
6a488035 | 76 | |
2aa397bc TO |
77 | foreach ($location as $fld => $values) { |
78 | if (is_array($values) && !empty($values)) { | |
79 | $locType = $values[1]['location_type_id']; | |
80 | if ($fld == 'email') { | |
be2fb01f | 81 | $elements["onbehalf_{$fld}-{$locType}"] = [ |
353ffa53 TO |
82 | 'type' => 'Text', |
83 | 'value' => $location[$fld][1][$fld], | |
be2fb01f | 84 | ]; |
2aa397bc TO |
85 | unset($profileFields["{$fld}-{$locType}"]); |
86 | } | |
87 | elseif ($fld == 'phone') { | |
88 | $phoneTypeId = $values[1]['phone_type_id']; | |
be2fb01f | 89 | $elements["onbehalf_{$fld}-{$locType}-{$phoneTypeId}"] = [ |
353ffa53 TO |
90 | 'type' => 'Text', |
91 | 'value' => $location[$fld][1][$fld], | |
be2fb01f | 92 | ]; |
2aa397bc TO |
93 | unset($profileFields["{$fld}-{$locType}-{$phoneTypeId}"]); |
94 | } | |
95 | elseif ($fld == 'im') { | |
96 | $providerId = $values[1]['provider_id']; | |
be2fb01f | 97 | $elements["onbehalf_{$fld}-{$locType}"] = [ |
353ffa53 TO |
98 | 'type' => 'Text', |
99 | 'value' => $location[$fld][1][$fld], | |
be2fb01f CW |
100 | ]; |
101 | $elements["onbehalf_{$fld}-{$locType}provider_id"] = [ | |
353ffa53 TO |
102 | 'type' => 'Select', |
103 | 'value' => $location[$fld][1]['provider_id'], | |
be2fb01f | 104 | ]; |
2aa397bc | 105 | unset($profileFields["{$fld}-{$locType}-{$providerId}"]); |
6a488035 TO |
106 | } |
107 | } | |
2aa397bc | 108 | } |
6a488035 | 109 | |
2aa397bc TO |
110 | if (!empty($website)) { |
111 | foreach ($website as $key => $val) { | |
112 | $websiteTypeId = $values[1]['website_type_id']; | |
be2fb01f | 113 | $elements["onbehalf_url-1"] = [ |
353ffa53 TO |
114 | 'type' => 'Text', |
115 | 'value' => $website[1]['url'], | |
be2fb01f CW |
116 | ]; |
117 | $elements["onbehalf_url-1-website_type_id"] = [ | |
353ffa53 TO |
118 | 'type' => 'Select', |
119 | 'value' => $website[1]['website_type_id'], | |
be2fb01f | 120 | ]; |
2aa397bc | 121 | unset($profileFields["url-1"]); |
6a488035 | 122 | } |
2aa397bc | 123 | } |
6a488035 | 124 | |
2aa397bc | 125 | $locTypeId = isset($location['address'][1]) ? $location['address'][1]['location_type_id'] : NULL; |
be2fb01f | 126 | $addressFields = [ |
353ffa53 TO |
127 | 'street_address', |
128 | 'supplemental_address_1', | |
129 | 'supplemental_address_2', | |
207f62c6 | 130 | 'supplemental_address_3', |
353ffa53 TO |
131 | 'city', |
132 | 'postal_code', | |
133 | 'county', | |
134 | 'state_province', | |
135 | 'country', | |
be2fb01f | 136 | ]; |
6a488035 | 137 | |
2aa397bc TO |
138 | foreach ($addressFields as $field) { |
139 | if (array_key_exists($field, $addressSequence)) { | |
140 | $addField = $field; | |
141 | $type = 'Text'; | |
be2fb01f | 142 | if (in_array($field, ['state_province', 'country', 'county'])) { |
2aa397bc TO |
143 | $addField = "{$field}_id"; |
144 | $type = 'Select'; | |
145 | } | |
be2fb01f | 146 | $elements["onbehalf_{$field}-{$locTypeId}"] = [ |
353ffa53 | 147 | 'type' => $type, |
d6c75f5a | 148 | 'value' => isset($location['address'][1]) ? CRM_Utils_Array::value($addField, |
5c9fb05f | 149 | $location['address'][1]) : NULL, |
be2fb01f | 150 | ]; |
2aa397bc | 151 | unset($profileFields["{$field}-{$locTypeId}"]); |
6a488035 | 152 | } |
2aa397bc | 153 | } |
6a488035 | 154 | |
2aa397bc | 155 | //set custom field defaults |
be2fb01f | 156 | $defaults = []; |
2aa397bc TO |
157 | CRM_Core_BAO_UFGroup::setProfileDefaults($cid, $profileFields, $defaults, TRUE, NULL, NULL, TRUE); |
158 | ||
159 | if (!empty($defaults)) { | |
160 | foreach ($profileFields as $key => $val) { | |
161 | if (array_key_exists($key, $defaults)) { | |
162 | $htmlType = CRM_Utils_Array::value('html_type', $val); | |
163 | if ($htmlType == 'Radio') { | |
164 | $elements["onbehalf_{$key}"]['type'] = $htmlType; | |
165 | $elements["onbehalf_{$key}"]['value'] = $defaults[$key]; | |
166 | } | |
167 | elseif ($htmlType == 'CheckBox') { | |
168 | $elements["onbehalf_{$key}"]['type'] = $htmlType; | |
169 | foreach ($defaults[$key] as $k => $v) { | |
170 | $elements["onbehalf_{$key}"]['value'][$k] = $v; | |
31e83de7 | 171 | } |
2aa397bc | 172 | } |
6cc845ad | 173 | elseif (strstr($htmlType, 'Multi-Select')) { |
9c3a8754 CW |
174 | $elements["onbehalf_{$key}"]['type'] = 'Multi-Select'; |
175 | $elements["onbehalf_{$key}"]['value'] = array_values($defaults[$key]); | |
6a488035 | 176 | } |
2aa397bc TO |
177 | elseif ($htmlType == 'Autocomplete-Select') { |
178 | $elements["onbehalf_{$key}"]['type'] = $htmlType; | |
179 | $elements["onbehalf_{$key}"]['value'] = $defaults[$key]; | |
2aa397bc TO |
180 | } |
181 | elseif ($htmlType == 'Select Date') { | |
182 | $elements["onbehalf_{$key}"]['type'] = $htmlType; | |
5942e66f | 183 | //CRM-18349, date value must be ISO formatted before being set as a default value for crmDatepicker custom field |
184 | $elements["onbehalf_{$key}"]['value'] = CRM_Utils_Date::processDate($defaults[$key], NULL, FALSE, 'Y-m-d G:i:s'); | |
2aa397bc | 185 | } |
6a488035 | 186 | else { |
2aa397bc TO |
187 | $elements["onbehalf_{$key}"]['type'] = $htmlType; |
188 | $elements["onbehalf_{$key}"]['value'] = $defaults[$key]; | |
6a488035 TO |
189 | } |
190 | } | |
2aa397bc TO |
191 | else { |
192 | $elements["onbehalf_{$key}"]['value'] = ''; | |
193 | } | |
6a488035 | 194 | } |
2aa397bc | 195 | } |
6a488035 | 196 | |
ecdef330 | 197 | CRM_Utils_JSON::output($elements); |
6a488035 TO |
198 | } |
199 | ||
00be9182 | 200 | public static function jqState() { |
1d07e7ab | 201 | CRM_Utils_JSON::output(CRM_Core_BAO_Location::getChainSelectValues($_GET['_value'], 'country')); |
6a488035 TO |
202 | } |
203 | ||
00be9182 | 204 | public static function jqCounty() { |
1d07e7ab | 205 | CRM_Utils_JSON::output(CRM_Core_BAO_Location::getChainSelectValues($_GET['_value'], 'stateProvince')); |
6a488035 TO |
206 | } |
207 | ||
00be9182 | 208 | public static function getLocBlock() { |
6a488035 TO |
209 | // i wish i could retrieve loc block info based on loc_block_id, |
210 | // Anyway, lets retrieve an event which has loc_block_id set to 'lbid'. | |
62a0f5a1 | 211 | if ($_REQUEST['lbid']) { |
be2fb01f | 212 | $params = ['1' => [$_REQUEST['lbid'], 'Integer']]; |
6a488035 TO |
213 | $eventId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_event WHERE loc_block_id=%1 LIMIT 1', $params); |
214 | } | |
215 | // now lets use the event-id obtained above, to retrieve loc block information. | |
216 | if ($eventId) { | |
be2fb01f | 217 | $params = ['entity_id' => $eventId, 'entity_table' => 'civicrm_event']; |
6a488035 TO |
218 | // second parameter is of no use, but since required, lets use the same variable. |
219 | $location = CRM_Core_BAO_Location::getValues($params, $params); | |
220 | } | |
221 | ||
be2fb01f | 222 | $result = []; |
6a488035 TO |
223 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
224 | 'address_options', TRUE, NULL, TRUE | |
225 | ); | |
226 | // lets output only required fields. | |
227 | foreach ($addressOptions as $element => $isSet) { | |
be2fb01f | 228 | if ($isSet && (!in_array($element, [ |
518fa0ee SL |
229 | 'im', |
230 | 'openid', | |
231 | ]))) { | |
be2fb01f | 232 | if (in_array($element, [ |
353ffa53 TO |
233 | 'country', |
234 | 'state_province', | |
af9b09df | 235 | 'county', |
be2fb01f | 236 | ])) { |
6a488035 TO |
237 | $element .= '_id'; |
238 | } | |
239 | elseif ($element == 'address_name') { | |
240 | $element = 'name'; | |
241 | } | |
242 | $fld = "address[1][{$element}]"; | |
243 | $value = CRM_Utils_Array::value($element, $location['address'][1]); | |
244 | $value = $value ? $value : ""; | |
be2fb01f | 245 | $result[str_replace([ |
353ffa53 TO |
246 | '][', |
247 | '[', | |
af9b09df | 248 | "]", |
be2fb01f | 249 | ], ['_', '_', ''], $fld)] = $value; |
6a488035 TO |
250 | } |
251 | } | |
252 | ||
be2fb01f | 253 | foreach ([ |
518fa0ee SL |
254 | 'email', |
255 | 'phone_type_id', | |
256 | 'phone', | |
257 | ] as $element) { | |
6a488035 TO |
258 | $block = ($element == 'phone_type_id') ? 'phone' : $element; |
259 | for ($i = 1; $i < 3; $i++) { | |
260 | $fld = "{$block}[{$i}][{$element}]"; | |
261 | $value = CRM_Utils_Array::value($element, $location[$block][$i]); | |
262 | $value = $value ? $value : ""; | |
be2fb01f | 263 | $result[str_replace([ |
353ffa53 TO |
264 | '][', |
265 | '[', | |
af9b09df | 266 | "]", |
be2fb01f | 267 | ], ['_', '_', ''], $fld)] = $value; |
6a488035 TO |
268 | } |
269 | } | |
270 | ||
271 | // set the message if loc block is being used by more than one event. | |
62a0f5a1 | 272 | $result['count_loc_used'] = CRM_Event_BAO_Event::countEventsUsingLocBlockId($_REQUEST['lbid']); |
6a488035 | 273 | |
ecdef330 | 274 | CRM_Utils_JSON::output($result); |
6a488035 | 275 | } |
96025800 | 276 | |
6a488035 | 277 | } |