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