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