3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 * This class contains all the function that are called using AJAX
38 class CRM_Core_Page_AJAX_Location
{
41 * FIXME: we should make this method like getLocBlock() OR use the same method and
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.
48 static function getPermissionedLocation() {
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);
53 $user = CRM_Utils_Request
::retrieve('uid', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, CRM_Core_Session
::singleton()->get('userID'));
54 if (!$user ||
!CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($user, CRM_Core_DAO
::$_nullObject, FALSE)) {
55 CRM_Utils_System
::civiExit();
58 // Verify user permission on related contact
59 $employers = CRM_Contact_BAO_Relationship
::getPermissionedEmployer($user);
60 if (!isset($employers[$cid])) {
61 CRM_Utils_System
::civiExit();
65 $entityBlock = array('contact_id' => $cid);
66 $location = CRM_Core_BAO_Location
::getValues($entityBlock);
68 $config = CRM_Core_Config
::singleton();
69 $addressSequence = array_flip($config->addressSequence());
72 $profileFields = CRM_Core_BAO_UFGroup
::getFields($ufId, FALSE, CRM_Core_Action
::VIEW
, NULL, NULL, FALSE,
73 NULL, FALSE, NULL, CRM_Core_Permission
::CREATE
, NULL
75 $website = CRM_Core_BAO_Website
::getValues($entityBlock, $values);
77 foreach ($location as $fld => $values) {
78 if (is_array($values) && !empty($values)) {
79 $locType = $values[1]['location_type_id'];
80 if ($fld == 'email') {
81 $elements["onbehalf_{$fld}-{$locType}"] = array(
83 'value' => $location[$fld][1][$fld],
85 unset($profileFields["{$fld}-{$locType}"]);
87 elseif ($fld == 'phone') {
88 $phoneTypeId = $values[1]['phone_type_id'];
89 $elements["onbehalf_{$fld}-{$locType}-{$phoneTypeId}"] = array(
91 'value' => $location[$fld][1][$fld],
93 unset($profileFields["{$fld}-{$locType}-{$phoneTypeId}"]);
95 elseif ($fld == 'im') {
96 $providerId = $values[1]['provider_id'];
97 $elements["onbehalf_{$fld}-{$locType}"] = array(
99 'value' => $location[$fld][1][$fld],
101 $elements["onbehalf_{$fld}-{$locType}provider_id"] = array(
103 'value' => $location[$fld][1]['provider_id'],
105 unset($profileFields["{$fld}-{$locType}-{$providerId}"]);
110 if (!empty($website)) {
111 foreach ($website as $key => $val) {
112 $websiteTypeId = $values[1]['website_type_id'];
113 $elements["onbehalf_url-1"] = array(
115 'value' => $website[1]['url'],
117 $elements["onbehalf_url-1-website_type_id"] = array(
119 'value' => $website[1]['website_type_id'],
121 unset($profileFields["url-1"]);
125 $locTypeId = isset($location['address'][1]) ?
$location['address'][1]['location_type_id'] : NULL;
126 $addressFields = array(
128 'supplemental_address_1',
129 'supplemental_address_2',
136 foreach ($addressFields as $field) {
137 if (array_key_exists($field, $addressSequence)) {
139 if (in_array($field, array(
140 'state_province', 'country'))) {
141 $addField = "{$field}_id";
143 $elements["onbehalf_{$field}-{$locTypeId}"] = array(
145 'value' => isset($location['address'][1]) ?
$location['address'][1][$addField] : null,
147 unset($profileFields["{$field}-{$locTypeId}"]);
151 //set custom field defaults
153 CRM_Core_BAO_UFGroup
::setProfileDefaults($cid, $profileFields, $defaults, TRUE, NULL, NULL, TRUE);
155 if (!empty($defaults)) {
156 foreach ($profileFields as $key => $val) {
158 if (array_key_exists($key, $defaults)) {
159 $htmlType = CRM_Utils_Array
::value('html_type', $val);
160 if ($htmlType == 'Radio') {
161 $elements["onbehalf[{$key}]"]['type'] = $htmlType;
162 $elements["onbehalf[{$key}]"]['value'] = $defaults[$key];
164 elseif ($htmlType == 'CheckBox') {
165 foreach ($defaults[$key] as $k => $v) {
166 $elements["onbehalf[{$key}][{$k}]"]['type'] = $htmlType;
167 $elements["onbehalf[{$key}][{$k}]"]['value'] = $v;
170 elseif ($htmlType == 'Multi-Select') {
171 foreach ($defaults[$key] as $k => $v) {
172 $elements["onbehalf_{$key}"]['type'] = $htmlType;
173 $elements["onbehalf_{$key}"]['value'][$k] = $v;
176 elseif ($htmlType == 'Autocomplete-Select') {
177 $elements["onbehalf_{$key}"]['type'] = $htmlType;
178 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
179 $elements["onbehalf_{$key}"]['id'] = $defaults["{$key}_id"];
182 $elements["onbehalf_{$key}"]['type'] = $htmlType;
183 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
187 $elements["onbehalf_{$key}"]['value'] = '';
192 echo json_encode($elements);
193 CRM_Utils_System
::civiExit();
196 static function jqState() {
197 if (empty($_GET['_value'])) {
198 CRM_Utils_System
::civiExit();
201 $result = CRM_Core_PseudoConstant
::stateProvinceForCountry($_GET['_value']);
203 $elements = array(array(
204 'name' => $result ?
ts('- select a state -') : ts('- N/A -'),
207 foreach ($result as $id => $name) {
214 echo json_encode($elements);
215 CRM_Utils_System
::civiExit();
218 static function jqCounty() {
219 if (!isset($_GET['_value']) || CRM_Utils_System
::isNull($_GET['_value'])) {
221 array('name' => ts('(choose state first)'), 'value' => '')
225 $result = CRM_Core_PseudoConstant
::countyForState($_GET['_value']);
227 $elements = array(array(
228 'name' => $result ?
ts('- select -') : ts('- N/A -'),
231 foreach ($result as $id => $name) {
239 echo json_encode($elements);
240 CRM_Utils_System
::civiExit();
243 static function getLocBlock() {
244 // i wish i could retrieve loc block info based on loc_block_id,
245 // Anyway, lets retrieve an event which has loc_block_id set to 'lbid'.
246 if ($_POST['lbid']) {
247 $params = array('1' => array($_POST['lbid'], 'Integer'));
248 $eventId = CRM_Core_DAO
::singleValueQuery('SELECT id FROM civicrm_event WHERE loc_block_id=%1 LIMIT 1', $params);
250 // now lets use the event-id obtained above, to retrieve loc block information.
252 $params = array('entity_id' => $eventId, 'entity_table' => 'civicrm_event');
253 // second parameter is of no use, but since required, lets use the same variable.
254 $location = CRM_Core_BAO_Location
::getValues($params, $params);
258 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
259 'address_options', TRUE, NULL, TRUE
261 // lets output only required fields.
262 foreach ($addressOptions as $element => $isSet) {
263 if ($isSet && (!in_array($element, array(
265 if (in_array($element, array(
266 'country', 'state_province', 'county'))) {
269 elseif ($element == 'address_name') {
272 $fld = "address[1][{$element}]";
273 $value = CRM_Utils_Array
::value($element, $location['address'][1]);
274 $value = $value ?
$value : "";
275 $result[str_replace(array(
276 '][', '[', "]"), array('_', '_', ''), $fld)] = $value;
281 'email', 'phone_type_id', 'phone') as $element) {
282 $block = ($element == 'phone_type_id') ?
'phone' : $element;
283 for ($i = 1; $i < 3; $i++
) {
284 $fld = "{$block}[{$i}][{$element}]";
285 $value = CRM_Utils_Array
::value($element, $location[$block][$i]);
286 $value = $value ?
$value : "";
287 $result[str_replace(array(
288 '][', '[', "]"), array('_', '_', ''), $fld)] = $value;
292 // set the message if loc block is being used by more than one event.
293 $result['count_loc_used'] = CRM_Event_BAO_Event
::countEventsUsingLocBlockId($_POST['lbid']);
295 echo json_encode($result);
296 CRM_Utils_System
::civiExit();