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