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_Type
::escape($_GET['cid'], 'Integer');
51 $ufId = CRM_Utils_Type
::escape($_GET['ufId'], 'Integer');
53 elseif ($_GET['relContact']) {
54 $relContact = CRM_Utils_Type
::escape($_GET['relContact'], 'Integer');
58 $entityBlock = array('contact_id' => $cid);
59 $location = CRM_Core_BAO_Location
::getValues($entityBlock);
61 $config = CRM_Core_Config
::singleton();
62 $addressSequence = array_flip($config->addressSequence());
65 if (!empty($relContact)) {
68 $location['phone'][1]['phone'],
70 $location['email'][1]['email'],
73 if (array_key_exists('street_address', $addressSequence)) {
74 $elements["address_1_street_address"] = $location['address'][1]['street_address'];
76 if (array_key_exists('supplemental_address_1', $addressSequence)) {
77 $elements['address_1_supplemental_address_1'] = $location['address'][1]['supplemental_address_1'];
79 if (array_key_exists('supplemental_address_2', $addressSequence)) {
80 $elements['address_1_supplemental_address_2'] = $location['address'][1]['supplemental_address_2'];
82 if (array_key_exists('city', $addressSequence)) {
83 $elements['address_1_city'] = $location['address'][1]['city'];
85 if (array_key_exists('postal_code', $addressSequence)) {
86 $elements['address_1_postal_code'] = $location['address'][1]['postal_code'];
87 $elements['address_1_postal_code_suffix'] = $location['address'][1]['postal_code_suffix'];
89 if (array_key_exists('country', $addressSequence)) {
90 $elements['address_1_country_id'] = $location['address'][1]['country_id'];
92 if (array_key_exists('state_province', $addressSequence)) {
93 $elements['address_1_state_province_id'] = $location['address'][1]['state_province_id'];
97 $profileFields = CRM_Core_BAO_UFGroup
::getFields($ufId, FALSE, CRM_Core_Action
::VIEW
, NULL, NULL, FALSE,
98 NULL, FALSE, NULL, CRM_Core_Permission
::CREATE
, NULL
100 $website = CRM_Core_BAO_Website
::getValues($entityBlock, $values);
102 foreach ($location as $fld => $values) {
103 if (is_array($values) && !empty($values)) {
104 $locType = $values[1]['location_type_id'];
105 if ($fld == 'email') {
106 $elements["onbehalf_{$fld}-{$locType}"] = array(
108 'value' => $location[$fld][1][$fld],
110 unset($profileFields["{$fld}-{$locType}"]);
112 elseif ($fld == 'phone') {
113 $phoneTypeId = $values[1]['phone_type_id'];
114 $elements["onbehalf_{$fld}-{$locType}-{$phoneTypeId}"] = array(
116 'value' => $location[$fld][1][$fld],
118 unset($profileFields["{$fld}-{$locType}-{$phoneTypeId}"]);
120 elseif ($fld == 'im') {
121 $providerId = $values[1]['provider_id'];
122 $elements["onbehalf_{$fld}-{$locType}"] = array(
124 'value' => $location[$fld][1][$fld],
126 $elements["onbehalf_{$fld}-{$locType}provider_id"] = array(
128 'value' => $location[$fld][1]['provider_id'],
130 unset($profileFields["{$fld}-{$locType}-{$providerId}"]);
135 if (!empty($website)) {
136 foreach ($website as $key => $val) {
137 $websiteTypeId = $values[1]['website_type_id'];
138 $elements["onbehalf_url-1"] = array(
140 'value' => $website[1]['url'],
142 $elements["onbehalf_url-1-website_type_id"] = array(
144 'value' => $website[1]['website_type_id'],
146 unset($profileFields["url-1"]);
150 $locTypeId = isset($location['address'][1]) ?
$location['address'][1]['location_type_id'] : NULL;
151 $addressFields = array(
153 'supplemental_address_1',
154 'supplemental_address_2',
161 foreach ($addressFields as $field) {
162 if (array_key_exists($field, $addressSequence)) {
164 if (in_array($field, array(
165 'state_province', 'country'))) {
166 $addField = "{$field}_id";
168 $elements["onbehalf_{$field}-{$locTypeId}"] = array(
170 'value' => isset($location['address'][1]) ?
$location['address'][1][$addField] : null,
172 unset($profileFields["{$field}-{$locTypeId}"]);
176 //set custom field defaults
178 CRM_Core_BAO_UFGroup
::setProfileDefaults($cid, $profileFields, $defaults, TRUE, NULL, NULL, TRUE);
180 if (!empty($defaults)) {
181 foreach ($profileFields as $key => $val) {
183 if (array_key_exists($key, $defaults)) {
184 $htmlType = CRM_Utils_Array
::value('html_type', $val);
185 if ($htmlType == 'Radio') {
186 $elements["onbehalf[{$key}]"]['type'] = $htmlType;
187 $elements["onbehalf[{$key}]"]['value'] = $defaults[$key];
189 elseif ($htmlType == 'CheckBox') {
190 foreach ($defaults[$key] as $k => $v) {
191 $elements["onbehalf[{$key}][{$k}]"]['type'] = $htmlType;
192 $elements["onbehalf[{$key}][{$k}]"]['value'] = $v;
195 elseif ($htmlType == 'Multi-Select') {
196 foreach ($defaults[$key] as $k => $v) {
197 $elements["onbehalf_{$key}"]['type'] = $htmlType;
198 $elements["onbehalf_{$key}"]['value'][$k] = $v;
201 elseif ($htmlType == 'Autocomplete-Select') {
202 $elements["onbehalf_{$key}"]['type'] = $htmlType;
203 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
204 $elements["onbehalf_{$key}"]['id'] = $defaults["{$key}_id"];
206 elseif ($htmlType == 'Select Date') {
207 $elements["onbehalf_{$key}"]['type'] = $htmlType;
208 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
209 $elements["onbehalf_{$key}_display"]['value'] = $defaults[$key];
212 $elements["onbehalf_{$key}"]['type'] = $htmlType;
213 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
217 $elements["onbehalf_{$key}"]['value'] = '';
223 echo json_encode($elements);
224 CRM_Utils_System
::civiExit();
227 static function jqState() {
228 if (empty($_GET['_value'])) {
229 CRM_Utils_System
::civiExit();
232 $result = CRM_Core_PseudoConstant
::stateProvinceForCountry($_GET['_value']);
234 $elements = array(array(
235 'name' => $result ?
ts('- select a state -') : ts('- N/A -'),
238 foreach ($result as $id => $name) {
245 echo json_encode($elements);
246 CRM_Utils_System
::civiExit();
249 static function jqCounty() {
250 if (!isset($_GET['_value']) || CRM_Utils_System
::isNull($_GET['_value'])) {
252 array('name' => ts('(choose state first)'), 'value' => '')
256 $result = CRM_Core_PseudoConstant
::countyForState($_GET['_value']);
258 $elements = array(array(
259 'name' => $result ?
ts('- select -') : ts('- N/A -'),
262 foreach ($result as $id => $name) {
270 echo json_encode($elements);
271 CRM_Utils_System
::civiExit();
274 static function getLocBlock() {
275 // i wish i could retrieve loc block info based on loc_block_id,
276 // Anyway, lets retrieve an event which has loc_block_id set to 'lbid'.
277 if ($_POST['lbid']) {
278 $params = array('1' => array($_POST['lbid'], 'Integer'));
279 $eventId = CRM_Core_DAO
::singleValueQuery('SELECT id FROM civicrm_event WHERE loc_block_id=%1 LIMIT 1', $params);
281 // now lets use the event-id obtained above, to retrieve loc block information.
283 $params = array('entity_id' => $eventId, 'entity_table' => 'civicrm_event');
284 // second parameter is of no use, but since required, lets use the same variable.
285 $location = CRM_Core_BAO_Location
::getValues($params, $params);
289 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
290 'address_options', TRUE, NULL, TRUE
292 // lets output only required fields.
293 foreach ($addressOptions as $element => $isSet) {
294 if ($isSet && (!in_array($element, array(
296 if (in_array($element, array(
297 'country', 'state_province', 'county'))) {
300 elseif ($element == 'address_name') {
303 $fld = "address[1][{$element}]";
304 $value = CRM_Utils_Array
::value($element, $location['address'][1]);
305 $value = $value ?
$value : "";
306 $result[str_replace(array(
307 '][', '[', "]"), array('_', '_', ''), $fld)] = $value;
312 'email', 'phone_type_id', 'phone') as $element) {
313 $block = ($element == 'phone_type_id') ?
'phone' : $element;
314 for ($i = 1; $i < 3; $i++
) {
315 $fld = "{$block}[{$i}][{$element}]";
316 $value = CRM_Utils_Array
::value($element, $location[$block][$i]);
317 $value = $value ?
$value : "";
318 $result[str_replace(array(
319 '][', '[', "]"), array('_', '_', ''), $fld)] = $value;
323 // set the message if loc block is being used by more than one event.
324 $result['count_loc_used'] = CRM_Event_BAO_Event
::countEventsUsingLocBlockId($_POST['lbid']);
326 echo json_encode($result);
327 CRM_Utils_System
::civiExit();