3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class contains all the function that are called using AJAX
21 class CRM_Core_Page_AJAX_Location
{
24 * FIXME: we should make this method like getLocBlock() OR use the same method and
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.
31 * @throws \CRM_Core_Exception
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);
38 $user = CRM_Utils_Request
::retrieve('uid', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, CRM_Core_Session
::singleton()
40 if (empty($user) ||
(CRM_Utils_Request
::retrieve('cs', 'String', $form, FALSE) && !CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($user, CRM_Core_DAO
::$_nullObject, FALSE))
42 CRM_Utils_System
::civiExit();
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();
52 $entityBlock = ['contact_id' => $cid];
53 $location = CRM_Core_BAO_Location
::getValues($entityBlock);
55 $addressSequence = array_flip(CRM_Utils_Address
::sequence(\Civi
::settings()->get('address_format')));
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
60 $website = CRM_Core_BAO_Website
::getValues($entityBlock, $values);
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}"] = [
68 'value' => $location[$fld][1][$fld],
70 unset($profileFields["{$fld}-{$locType}"]);
72 elseif ($fld == 'phone') {
73 $phoneTypeId = $values[1]['phone_type_id'];
74 $elements["onbehalf_{$fld}-{$locType}-{$phoneTypeId}"] = [
76 'value' => $location[$fld][1][$fld],
78 unset($profileFields["{$fld}-{$locType}-{$phoneTypeId}"]);
80 elseif ($fld == 'im') {
81 $providerId = $values[1]['provider_id'];
82 $elements["onbehalf_{$fld}-{$locType}"] = [
84 'value' => $location[$fld][1][$fld],
86 $elements["onbehalf_{$fld}-{$locType}provider_id"] = [
88 'value' => $location[$fld][1]['provider_id'],
90 unset($profileFields["{$fld}-{$locType}-{$providerId}"]);
95 if (!empty($website)) {
96 foreach ($website as $key => $val) {
97 $websiteTypeId = $values[1]['website_type_id'];
98 $elements["onbehalf_url-1"] = [
100 'value' => $website[1]['url'],
102 $elements["onbehalf_url-1-website_type_id"] = [
104 'value' => $website[1]['website_type_id'],
106 unset($profileFields["url-1"]);
110 $locTypeId = isset($location['address'][1]) ?
$location['address'][1]['location_type_id'] : NULL;
113 'supplemental_address_1',
114 'supplemental_address_2',
115 'supplemental_address_3',
123 foreach ($addressFields as $field) {
124 if (array_key_exists($field, $addressSequence)) {
127 if (in_array($field, ['state_province', 'country', 'county'])) {
128 $addField = "{$field}_id";
131 $elements["onbehalf_{$field}-{$locTypeId}"] = [
133 'value' => isset($location['address'][1]) ? CRM_Utils_Array
::value($addField,
134 $location['address'][1]) : NULL,
136 unset($profileFields["{$field}-{$locTypeId}"]);
140 //set custom field defaults
142 CRM_Core_BAO_UFGroup
::setProfileDefaults($cid, $profileFields, $defaults, TRUE, NULL, NULL, TRUE);
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];
152 elseif ($htmlType == 'CheckBox') {
153 $elements["onbehalf_{$key}"]['type'] = $htmlType;
154 foreach ($defaults[$key] as $k => $v) {
155 $elements["onbehalf_{$key}"]['value'][$k] = $v;
158 elseif (strstr($htmlType, 'Multi-Select')) {
159 $elements["onbehalf_{$key}"]['type'] = 'Multi-Select';
160 $elements["onbehalf_{$key}"]['value'] = array_values($defaults[$key]);
162 elseif ($htmlType == 'Autocomplete-Select') {
163 $elements["onbehalf_{$key}"]['type'] = $htmlType;
164 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
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');
172 $elements["onbehalf_{$key}"]['type'] = $htmlType;
173 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
177 $elements["onbehalf_{$key}"]['value'] = '';
182 CRM_Utils_JSON
::output($elements);
185 public static function jqState() {
186 CRM_Utils_JSON
::output(CRM_Core_BAO_Location
::getChainSelectValues($_GET['_value'], 'country'));
189 public static function jqCounty() {
190 CRM_Utils_JSON
::output(CRM_Core_BAO_Location
::getChainSelectValues($_GET['_value'], 'stateProvince'));
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);
200 // now lets use the event-id obtained above, to retrieve loc block information.
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);
208 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
209 'address_options', TRUE, NULL, TRUE
211 // lets output only required fields.
212 foreach ($addressOptions as $element => $isSet) {
213 if ($isSet && (!in_array($element, [
217 if (in_array($element, [
224 elseif ($element == 'address_name') {
227 $fld = "address[1][{$element}]";
228 $value = $location['address'][1][$element] ??
NULL;
229 $value = $value ?
$value : "";
230 $result[str_replace([
234 ], ['_', '_', ''], $fld)] = $value;
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([
252 ], ['_', '_', ''], $fld)] = $value;
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']);
259 CRM_Utils_JSON
::output($result);