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