CRM-14621 Fixed rendering of date fields on On Behalf of profiles
[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_Type::escape($_GET['cid'], 'Integer');
50 if ($_GET['ufId']) {
51 $ufId = CRM_Utils_Type::escape($_GET['ufId'], 'Integer');
52 }
53 elseif ($_GET['relContact']) {
54 $relContact = CRM_Utils_Type::escape($_GET['relContact'], 'Integer');
55 }
56
57 $values = array();
58 $entityBlock = array('contact_id' => $cid);
59 $location = CRM_Core_BAO_Location::getValues($entityBlock);
60
61 $config = CRM_Core_Config::singleton();
62 $addressSequence = array_flip($config->addressSequence());
63
64
65 if (!empty($relContact)) {
66 $elements = array(
67 "phone_1_phone" =>
68 $location['phone'][1]['phone'],
69 "email_1_email" =>
70 $location['email'][1]['email'],
71 );
72
73 if (array_key_exists('street_address', $addressSequence)) {
74 $elements["address_1_street_address"] = $location['address'][1]['street_address'];
75 }
76 if (array_key_exists('supplemental_address_1', $addressSequence)) {
77 $elements['address_1_supplemental_address_1'] = $location['address'][1]['supplemental_address_1'];
78 }
79 if (array_key_exists('supplemental_address_2', $addressSequence)) {
80 $elements['address_1_supplemental_address_2'] = $location['address'][1]['supplemental_address_2'];
81 }
82 if (array_key_exists('city', $addressSequence)) {
83 $elements['address_1_city'] = $location['address'][1]['city'];
84 }
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'];
88 }
89 if (array_key_exists('country', $addressSequence)) {
90 $elements['address_1_country_id'] = $location['address'][1]['country_id'];
91 }
92 if (array_key_exists('state_province', $addressSequence)) {
93 $elements['address_1_state_province_id'] = $location['address'][1]['state_province_id'];
94 }
95 }
96 else {
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
99 );
100 $website = CRM_Core_BAO_Website::getValues($entityBlock, $values);
101
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(
107 'type' => 'Text',
108 'value' => $location[$fld][1][$fld],
109 );
110 unset($profileFields["{$fld}-{$locType}"]);
111 }
112 elseif ($fld == 'phone') {
113 $phoneTypeId = $values[1]['phone_type_id'];
114 $elements["onbehalf_{$fld}-{$locType}-{$phoneTypeId}"] = array(
115 'type' => 'Text',
116 'value' => $location[$fld][1][$fld],
117 );
118 unset($profileFields["{$fld}-{$locType}-{$phoneTypeId}"]);
119 }
120 elseif ($fld == 'im') {
121 $providerId = $values[1]['provider_id'];
122 $elements["onbehalf_{$fld}-{$locType}"] = array(
123 'type' => 'Text',
124 'value' => $location[$fld][1][$fld],
125 );
126 $elements["onbehalf_{$fld}-{$locType}provider_id"] = array(
127 'type' => 'Select',
128 'value' => $location[$fld][1]['provider_id'],
129 );
130 unset($profileFields["{$fld}-{$locType}-{$providerId}"]);
131 }
132 }
133 }
134
135 if (!empty($website)) {
136 foreach ($website as $key => $val) {
137 $websiteTypeId = $values[1]['website_type_id'];
138 $elements["onbehalf_url-1"] = array(
139 'type' => 'Text',
140 'value' => $website[1]['url'],
141 );
142 $elements["onbehalf_url-1-website_type_id"] = array(
143 'type' => 'Select',
144 'value' => $website[1]['website_type_id'],
145 );
146 unset($profileFields["url-1"]);
147 }
148 }
149
150 $locTypeId = isset($location['address'][1]) ? $location['address'][1]['location_type_id'] : NULL;
151 $addressFields = array(
152 'street_address',
153 'supplemental_address_1',
154 'supplemental_address_2',
155 'city',
156 'postal_code',
157 'country',
158 'state_province',
159 );
160
161 foreach ($addressFields as $field) {
162 if (array_key_exists($field, $addressSequence)) {
163 $addField = $field;
164 if (in_array($field, array(
165 'state_province', 'country'))) {
166 $addField = "{$field}_id";
167 }
168 $elements["onbehalf_{$field}-{$locTypeId}"] = array(
169 'type' => 'Text',
170 'value' => isset($location['address'][1]) ? $location['address'][1][$addField] : null,
171 );
172 unset($profileFields["{$field}-{$locTypeId}"]);
173 }
174 }
175
176 //set custom field defaults
177 $defaults = array();
178 CRM_Core_BAO_UFGroup::setProfileDefaults($cid, $profileFields, $defaults, TRUE, NULL, NULL, TRUE);
179
180 if (!empty($defaults)) {
181 foreach ($profileFields as $key => $val) {
182
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];
188 }
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;
193 }
194 }
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;
199 }
200 }
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"];
205 }
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];
210 }
211 else {
212 $elements["onbehalf_{$key}"]['type'] = $htmlType;
213 $elements["onbehalf_{$key}"]['value'] = $defaults[$key];
214 }
215 }
216 else {
217 $elements["onbehalf_{$key}"]['value'] = '';
218 }
219 }
220 }
221 }
222
223 echo json_encode($elements);
224 CRM_Utils_System::civiExit();
225 }
226
227 static function jqState() {
228 if (empty($_GET['_value'])) {
229 CRM_Utils_System::civiExit();
230 }
231
232 $result = CRM_Core_PseudoConstant::stateProvinceForCountry($_GET['_value']);
233
234 $elements = array(array(
235 'name' => $result ? ts('- select a state -') : ts('- N/A -'),
236 'value' => '',
237 ));
238 foreach ($result as $id => $name) {
239 $elements[] = array(
240 'name' => $name,
241 'value' => $id,
242 );
243 }
244
245 echo json_encode($elements);
246 CRM_Utils_System::civiExit();
247 }
248
249 static function jqCounty() {
250 if (!isset($_GET['_value']) || CRM_Utils_System::isNull($_GET['_value'])) {
251 $elements = array(
252 array('name' => ts('(choose state first)'), 'value' => '')
253 );
254 }
255 else {
256 $result = CRM_Core_PseudoConstant::countyForState($_GET['_value']);
257
258 $elements = array(array(
259 'name' => $result ? ts('- select -') : ts('- N/A -'),
260 'value' => '',
261 ));
262 foreach ($result as $id => $name) {
263 $elements[] = array(
264 'name' => $name,
265 'value' => $id,
266 );
267 }
268 }
269
270 echo json_encode($elements);
271 CRM_Utils_System::civiExit();
272 }
273
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);
280 }
281 // now lets use the event-id obtained above, to retrieve loc block information.
282 if ($eventId) {
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);
286 }
287
288 $result = array();
289 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
290 'address_options', TRUE, NULL, TRUE
291 );
292 // lets output only required fields.
293 foreach ($addressOptions as $element => $isSet) {
294 if ($isSet && (!in_array($element, array(
295 'im', 'openid')))) {
296 if (in_array($element, array(
297 'country', 'state_province', 'county'))) {
298 $element .= '_id';
299 }
300 elseif ($element == 'address_name') {
301 $element = 'name';
302 }
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;
308 }
309 }
310
311 foreach (array(
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;
320 }
321 }
322
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']);
325
326 echo json_encode($result);
327 CRM_Utils_System::civiExit();
328 }
329 }
330