Merge pull request #3151 from pradpnayak/CRM-9288
[civicrm-core.git] / CRM / Core / Page / AJAX / Location.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * 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.
47 */
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);
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 (!$user || !CRM_Contact_BAO_Contact_Permission::validateChecksumContact($user, CRM_Core_DAO::$_nullObject, FALSE)) {
55 CRM_Utils_System::civiExit();
56 }
57
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();
62 }
63
64 $values = array();
65 $entityBlock = array('contact_id' => $cid);
66 $location = CRM_Core_BAO_Location::getValues($entityBlock);
67
68 $config = CRM_Core_Config::singleton();
69 $addressSequence = array_flip($config->addressSequence());
70
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 'country',
133 'state_province',
134 );
135
136 foreach ($addressFields as $field) {
137 if (array_key_exists($field, $addressSequence)) {
138 $addField = $field;
139 if (in_array($field, array(
140 'state_province', 'country'))) {
141 $addField = "{$field}_id";
142 }
143 $elements["onbehalf_{$field}-{$locTypeId}"] = array(
144 'type' => 'Text',
145 'value' => isset($location['address'][1]) ? $location['address'][1][$addField] : null,
146 );
147 unset($profileFields["{$field}-{$locTypeId}"]);
148 }
149 }
150
151 //set custom field defaults
152 $defaults = array();
153 CRM_Core_BAO_UFGroup::setProfileDefaults($cid, $profileFields, $defaults, TRUE, NULL, NULL, TRUE);
154
155 if (!empty($defaults)) {
156 foreach ($profileFields as $key => $val) {
157
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];
163 }
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;
168 }
169 }
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;
174 }
175 }
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"];
180 }
181 else {
182 $elements["onbehalf_{$key}"]['type'] = $htmlType;
183 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
184 }
185 }
186 else {
187 $elements["onbehalf_{$key}"]['value'] = '';
188 }
189 }
190 }
191
192 echo json_encode($elements);
193 CRM_Utils_System::civiExit();
194 }
195
196 static function jqState() {
197 if (empty($_GET['_value'])) {
198 CRM_Utils_System::civiExit();
199 }
200
201 $result = CRM_Core_PseudoConstant::stateProvinceForCountry($_GET['_value']);
202
203 $elements = array(array(
204 'name' => $result ? ts('- select a state -') : ts('- N/A -'),
205 'value' => '',
206 ));
207 foreach ($result as $id => $name) {
208 $elements[] = array(
209 'name' => $name,
210 'value' => $id,
211 );
212 }
213
214 echo json_encode($elements);
215 CRM_Utils_System::civiExit();
216 }
217
218 static function jqCounty() {
219 if (!isset($_GET['_value']) || CRM_Utils_System::isNull($_GET['_value'])) {
220 $elements = array(
221 array('name' => ts('(choose state first)'), 'value' => '')
222 );
223 }
224 else {
225 $result = CRM_Core_PseudoConstant::countyForState($_GET['_value']);
226
227 $elements = array(array(
228 'name' => $result ? ts('- select -') : ts('- N/A -'),
229 'value' => '',
230 ));
231 foreach ($result as $id => $name) {
232 $elements[] = array(
233 'name' => $name,
234 'value' => $id,
235 );
236 }
237 }
238
239 echo json_encode($elements);
240 CRM_Utils_System::civiExit();
241 }
242
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);
249 }
250 // now lets use the event-id obtained above, to retrieve loc block information.
251 if ($eventId) {
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);
255 }
256
257 $result = array();
258 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
259 'address_options', TRUE, NULL, TRUE
260 );
261 // lets output only required fields.
262 foreach ($addressOptions as $element => $isSet) {
263 if ($isSet && (!in_array($element, array(
264 'im', 'openid')))) {
265 if (in_array($element, array(
266 'country', 'state_province', 'county'))) {
267 $element .= '_id';
268 }
269 elseif ($element == 'address_name') {
270 $element = 'name';
271 }
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;
277 }
278 }
279
280 foreach (array(
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;
289 }
290 }
291
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']);
294
295 echo json_encode($result);
296 CRM_Utils_System::civiExit();
297 }
298 }
299