Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | * |
33 | */ | |
34 | ||
35 | /** | |
36 | * This class contains all contact related functions that are called using AJAX (jQuery) | |
37 | */ | |
38 | class CRM_Contact_Page_AJAX { | |
272081ca TO |
39 | /** |
40 | * When a user chooses a username, CHECK_USERNAME_TTL | |
41 | * is the time window in which they can check usernames | |
42 | * (without reloading the overall form). | |
43 | */ | |
44 | const CHECK_USERNAME_TTL = 10800; // 3hr; 3*60*60 | |
45 | ||
b1dba111 TO |
46 | const AUTOCOMPLETE_TTL = 21600; // 6hr; 6*60*60 |
47 | ||
06508628 | 48 | /** |
06508628 CW |
49 | * Ajax callback for custom fields of type ContactReference |
50 | * | |
51 | * Todo: Migrate contact reference fields to use EntityRef | |
52 | */ | |
00be9182 | 53 | public static function contactReference() { |
06508628 | 54 | $name = CRM_Utils_Array::value('term', $_GET); |
6a488035 TO |
55 | $name = CRM_Utils_Type::escape($name, 'String'); |
56 | $cfID = CRM_Utils_Type::escape($_GET['id'], 'Positive'); | |
57 | ||
58 | // check that this is a valid, active custom field of Contact Reference type | |
06508628 | 59 | $params = array('id' => $cfID); |
6a488035 | 60 | $returnProperties = array('filter', 'data_type', 'is_active'); |
06508628 | 61 | $cf = array(); |
6a488035 | 62 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties); |
a4cce21a | 63 | if (!$cf['id'] || !$cf['is_active'] || $cf['data_type'] != 'ContactReference') { |
06508628 | 64 | CRM_Utils_System::civiExit('error'); |
6a488035 TO |
65 | } |
66 | ||
9624538c | 67 | if (!empty($cf['filter'])) { |
6a488035 TO |
68 | $filterParams = array(); |
69 | parse_str($cf['filter'], $filterParams); | |
70 | ||
71 | $action = CRM_Utils_Array::value('action', $filterParams); | |
72 | ||
73 | if (!empty($action) && | |
74 | !in_array($action, array('get', 'lookup')) | |
75 | ) { | |
06508628 | 76 | CRM_Utils_System::civiExit('error'); |
6a488035 TO |
77 | } |
78 | } | |
79 | ||
80 | $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
353ffa53 TO |
81 | 'contact_reference_options' |
82 | ), '1'); | |
6a488035 TO |
83 | |
84 | $return = array_unique(array_merge(array('sort_name'), $list)); | |
85 | ||
89595c92 | 86 | $limit = Civi::settings()->get('search_autocomplete_count'); |
6a488035 TO |
87 | |
88 | $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3); | |
89 | foreach ($return as $fld) { | |
90 | $params["return.{$fld}"] = 1; | |
91 | } | |
92 | ||
93 | if (!empty($action)) { | |
353ffa53 TO |
94 | $excludeGet = array( |
95 | 'reset', | |
96 | 'key', | |
97 | 'className', | |
98 | 'fnName', | |
99 | 'json', | |
100 | 'reset', | |
101 | 'context', | |
102 | 'timestamp', | |
103 | 'limit', | |
104 | 'id', | |
105 | 's', | |
106 | 'q', | |
408b79bf | 107 | 'action', |
353ffa53 | 108 | ); |
6a488035 TO |
109 | foreach ($_GET as $param => $val) { |
110 | if (empty($val) || | |
111 | in_array($param, $excludeGet) || | |
112 | strpos($param, 'return.') !== FALSE || | |
113 | strpos($param, 'api.') !== FALSE | |
114 | ) { | |
115 | continue; | |
116 | } | |
117 | $params[$param] = $val; | |
118 | } | |
119 | } | |
120 | ||
121 | if ($name) { | |
122 | $params['sort_name'] = $name; | |
123 | } | |
124 | ||
125 | $params['sort'] = 'sort_name'; | |
126 | ||
127 | // tell api to skip permission chk. dgg | |
128 | $params['check_permissions'] = 0; | |
129 | ||
130 | // add filter variable to params | |
131 | if (!empty($filterParams)) { | |
132 | $params = array_merge($params, $filterParams); | |
133 | } | |
134 | ||
135 | $contact = civicrm_api('Contact', 'Get', $params); | |
136 | ||
a7488080 | 137 | if (!empty($contact['is_error'])) { |
06508628 | 138 | CRM_Utils_System::civiExit('error'); |
6a488035 TO |
139 | } |
140 | ||
d6408252 | 141 | $contactList = array(); |
6a488035 TO |
142 | foreach ($contact['values'] as $value) { |
143 | $view = array(); | |
144 | foreach ($return as $fld) { | |
a7488080 | 145 | if (!empty($value[$fld])) { |
6a488035 TO |
146 | $view[] = $value[$fld]; |
147 | } | |
148 | } | |
06508628 | 149 | $contactList[] = array('id' => $value['id'], 'text' => implode(' :: ', $view)); |
6a488035 TO |
150 | } |
151 | ||
8be1a839 | 152 | CRM_Utils_JSON::output($contactList); |
6a488035 TO |
153 | } |
154 | ||
155 | /** | |
100fef9d | 156 | * Fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title |
6a488035 | 157 | */ |
00be9182 | 158 | public static function getPCPList() { |
7d86179d | 159 | $name = CRM_Utils_Array::value('term', $_GET); |
353ffa53 | 160 | $name = CRM_Utils_Type::escape($name, 'String'); |
89595c92 | 161 | $limit = $max = Civi::settings()->get('search_autocomplete_count'); |
6a488035 TO |
162 | |
163 | $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id'; | |
164 | ||
165 | $config = CRM_Core_Config::singleton(); | |
166 | if ($config->includeWildCardInName) { | |
167 | $strSearch = "%$name%"; | |
168 | } | |
169 | else { | |
170 | $strSearch = "$name%"; | |
171 | } | |
172 | $includeEmailFrom = $includeNickName = ''; | |
173 | if ($config->includeNickNameInName) { | |
174 | $includeNickName = " OR nick_name LIKE '$strSearch'"; | |
175 | } | |
176 | if ($config->includeEmailInName) { | |
177 | $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )"; | |
178 | $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
179 | } | |
180 | else { | |
181 | $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
182 | } | |
183 | ||
8da35492 | 184 | $offset = $count = 0; |
4feb1cad CW |
185 | if (!empty($_GET['page_num'])) { |
186 | $page = (int) $_GET['page_num']; | |
8da35492 CW |
187 | $offset = $limit * ($page - 1); |
188 | $limit++; | |
6a488035 TO |
189 | } |
190 | ||
191 | $select = 'cc.sort_name, pcp.title, cp.title'; | |
192 | $query = " | |
193 | SELECT id, data | |
194 | FROM ( | |
195 | SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name | |
196 | FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc | |
197 | {$includeEmailFrom} | |
900e6829 | 198 | {$whereClause} AND pcp.page_type = 'contribute' |
199 | UNION ALL | |
200 | SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name | |
201 | FROM civicrm_pcp pcp, civicrm_event cp, civicrm_contact cc | |
202 | {$includeEmailFrom} | |
203 | {$whereClause} AND pcp.page_type = 'event' | |
6a488035 TO |
204 | ) t |
205 | ORDER BY sort_name | |
8da35492 | 206 | LIMIT $offset, $limit |
6a488035 TO |
207 | "; |
208 | ||
209 | $dao = CRM_Core_DAO::executeQuery($query); | |
8da35492 | 210 | $output = array('results' => array(), 'more' => FALSE); |
6a488035 | 211 | while ($dao->fetch()) { |
8da35492 CW |
212 | if (++$count > $max) { |
213 | $output['more'] = TRUE; | |
214 | } | |
215 | else { | |
216 | $output['results'][] = array('id' => $dao->id, 'text' => $dao->data); | |
217 | } | |
6a488035 | 218 | } |
8da35492 | 219 | CRM_Utils_JSON::output($output); |
6a488035 TO |
220 | } |
221 | ||
3b1c37fe CW |
222 | /** |
223 | * @throws \CiviCRM_API3_Exception | |
224 | */ | |
00be9182 | 225 | public static function relationship() { |
3b1c37fe | 226 | $relType = CRM_Utils_Request::retrieve('rel_type', 'String', CRM_Core_DAO::$_nullObject, TRUE); |
c91df8b4 | 227 | $relContactID = CRM_Utils_Request::retrieve('rel_contact', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); |
3b1c37fe CW |
228 | $originalCid = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject); |
229 | $relationshipID = CRM_Utils_Request::retrieve('rel_id', 'Positive', CRM_Core_DAO::$_nullObject); | |
c91df8b4 | 230 | $caseID = CRM_Utils_Request::retrieve('case_id', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); |
6a488035 | 231 | |
3b1c37fe CW |
232 | if (!CRM_Case_BAO_Case::accessCase($caseID)) { |
233 | CRM_Utils_System::permissionDenied(); | |
234 | } | |
6a488035 | 235 | |
c91df8b4 CW |
236 | $ret = array('is_error' => 0); |
237 | ||
3b1c37fe | 238 | list($relTypeId, $b, $a) = explode('_', $relType); |
14a679f1 | 239 | |
3b1c37fe CW |
240 | if ($relationshipID && $originalCid) { |
241 | CRM_Case_BAO_Case::endCaseRole($caseID, $a, $originalCid, $relTypeId); | |
242 | } | |
14a679f1 | 243 | |
3b1c37fe | 244 | $clientList = CRM_Case_BAO_Case::getCaseClients($caseID); |
14a679f1 | 245 | |
3b1c37fe CW |
246 | // Loop through multiple case clients |
247 | foreach ($clientList as $i => $sourceContactID) { | |
248 | $result = civicrm_api3('relationship', 'create', array( | |
249 | 'case_id' => $caseID, | |
250 | 'relationship_type_id' => $relTypeId, | |
251 | "contact_id_$a" => $relContactID, | |
252 | "contact_id_$b" => $sourceContactID, | |
253 | 'start_date' => 'now', | |
254 | )); | |
255 | // Save activity only for the primary (first) client | |
256 | if ($i == 0 && empty($result['is_error'])) { | |
257 | CRM_Case_BAO_Case::createCaseRoleActivity($caseID, $result['id'], $relContactID); | |
c91df8b4 | 258 | } |
6a488035 | 259 | } |
6a488035 | 260 | |
ecdef330 | 261 | CRM_Utils_JSON::output($ret); |
6a488035 TO |
262 | } |
263 | ||
264 | /** | |
fe482240 | 265 | * Fetch the custom field help. |
6a488035 | 266 | */ |
00be9182 | 267 | public static function customField() { |
353ffa53 TO |
268 | $fieldId = CRM_Utils_Type::escape($_REQUEST['id'], 'Integer'); |
269 | $params = array('id' => $fieldId); | |
6a488035 | 270 | $returnProperties = array('help_pre', 'help_post'); |
353ffa53 | 271 | $values = array(); |
6a488035 TO |
272 | |
273 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties); | |
ecdef330 | 274 | CRM_Utils_JSON::output($values); |
6a488035 TO |
275 | } |
276 | ||
00be9182 | 277 | public static function groupTree() { |
d42a224c | 278 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
6a488035 TO |
279 | $gids = CRM_Utils_Type::escape($_GET['gids'], 'String'); |
280 | echo CRM_Contact_BAO_GroupNestingCache::json($gids); | |
281 | CRM_Utils_System::civiExit(); | |
282 | } | |
283 | ||
6a488035 | 284 | /** |
fe482240 | 285 | * Delete custom value. |
6a488035 | 286 | */ |
00be9182 | 287 | public static function deleteCustomValue() { |
d42a224c | 288 | CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain'); |
6a488035 TO |
289 | $customValueID = CRM_Utils_Type::escape($_REQUEST['valueID'], 'Positive'); |
290 | $customGroupID = CRM_Utils_Type::escape($_REQUEST['groupID'], 'Positive'); | |
015bf0da | 291 | $contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', CRM_Core_DAO::$_nullObject); |
6a488035 | 292 | CRM_Core_BAO_CustomValue::deleteCustomValue($customValueID, $customGroupID); |
a4cce21a | 293 | if ($contactId) { |
7fa9167d | 294 | echo CRM_Contact_BAO_Contact::getCountComponent('custom_' . $customGroupID, $contactId); |
6a488035 TO |
295 | } |
296 | ||
2b68a50c | 297 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
6a488035 TO |
298 | CRM_Utils_System::civiExit(); |
299 | } | |
300 | ||
6a488035 | 301 | /** |
fe482240 | 302 | * check the CMS username. |
ce80b209 | 303 | */ |
6a488035 | 304 | static public function checkUserName() { |
272081ca | 305 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), array('for', 'ts')); |
7fa9167d | 306 | $sig = CRM_Utils_Request::retrieve('sig', 'String', CRM_Core_DAO::$_nullObject); |
307 | $for = CRM_Utils_Request::retrieve('for', 'String', CRM_Core_DAO::$_nullObject); | |
272081ca TO |
308 | if ( |
309 | CRM_Utils_Time::getTimeRaw() > $_REQUEST['ts'] + self::CHECK_USERNAME_TTL | |
7fa9167d | 310 | || $for != 'civicrm/ajax/cmsuser' |
311 | || !$signer->validate($sig, $_REQUEST) | |
272081ca TO |
312 | ) { |
313 | $user = array('name' => 'error'); | |
8be1a839 | 314 | CRM_Utils_JSON::output($user); |
272081ca TO |
315 | } |
316 | ||
6a488035 | 317 | $config = CRM_Core_Config::singleton(); |
4c68cf7b | 318 | $username = trim(CRM_Utils_Array::value('cms_name', $_REQUEST)); |
6a488035 TO |
319 | |
320 | $params = array('name' => $username); | |
321 | ||
322 | $errors = array(); | |
323 | $config->userSystem->checkUserNameEmailExists($params, $errors); | |
324 | ||
325 | if (isset($errors['cms_name']) || isset($errors['name'])) { | |
b44e3f84 | 326 | //user name is not available |
6a488035 | 327 | $user = array('name' => 'no'); |
8be1a839 | 328 | CRM_Utils_JSON::output($user); |
6a488035 TO |
329 | } |
330 | else { | |
331 | //user name is available | |
332 | $user = array('name' => 'yes'); | |
8be1a839 | 333 | CRM_Utils_JSON::output($user); |
6a488035 | 334 | } |
8be1a839 TO |
335 | |
336 | // Not reachable: JSON::output() above exits. | |
6a488035 TO |
337 | CRM_Utils_System::civiExit(); |
338 | } | |
339 | ||
340 | /** | |
fe482240 | 341 | * Function to get email address of a contact. |
6a488035 | 342 | */ |
00be9182 | 343 | public static function getContactEmail() { |
a7488080 | 344 | if (!empty($_REQUEST['contact_id'])) { |
6a488035 | 345 | $contactID = CRM_Utils_Type::escape($_REQUEST['contact_id'], 'Positive'); |
8c0ea1d7 TO |
346 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) { |
347 | return; | |
348 | } | |
6a488035 TO |
349 | list($displayName, |
350 | $userEmail | |
353ffa53 | 351 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID); |
effdc2d9 | 352 | |
d42a224c | 353 | CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain'); |
6a488035 TO |
354 | if ($userEmail) { |
355 | echo $userEmail; | |
356 | } | |
357 | } | |
358 | else { | |
359 | $noemail = CRM_Utils_Array::value('noemail', $_GET); | |
360 | $queryString = NULL; | |
a4cce21a JM |
361 | $name = CRM_Utils_Array::value('name', $_GET); |
362 | if ($name) { | |
6a488035 TO |
363 | $name = CRM_Utils_Type::escape($name, 'String'); |
364 | if ($noemail) { | |
365 | $queryString = " cc.sort_name LIKE '%$name%'"; | |
366 | } | |
367 | else { | |
368 | $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) "; | |
369 | } | |
370 | } | |
a4cce21a | 371 | else { |
d75f2f47 EM |
372 | $cid = CRM_Utils_Array::value('cid', $_GET); |
373 | if ($cid) { | |
b44e3f84 | 374 | //check cid for integer |
a4cce21a JM |
375 | $contIDS = explode(',', $cid); |
376 | foreach ($contIDS as $contID) { | |
377 | CRM_Utils_Type::escape($contID, 'Integer'); | |
378 | } | |
379 | $queryString = " cc.id IN ( $cid )"; | |
d75f2f47 | 380 | } |
6a488035 TO |
381 | } |
382 | ||
383 | if ($queryString) { | |
384 | $offset = CRM_Utils_Array::value('offset', $_GET, 0); | |
89595c92 | 385 | $rowCount = Civi::settings()->get('search_autocomplete_count'); |
6a488035 | 386 | |
bf00d1b6 | 387 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
bf00d1b6 | 388 | |
6a488035 TO |
389 | // add acl clause here |
390 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
391 | if ($aclWhere) { | |
392 | $aclWhere = " AND $aclWhere"; | |
393 | } | |
394 | if ($noemail) { | |
395 | $query = " | |
396 | SELECT sort_name name, cc.id | |
397 | FROM civicrm_contact cc | |
398 | {$aclFrom} | |
399 | WHERE cc.is_deceased = 0 AND {$queryString} | |
400 | {$aclWhere} | |
401 | LIMIT {$offset}, {$rowCount} | |
402 | "; | |
403 | ||
404 | // send query to hook to be modified if needed | |
405 | CRM_Utils_Hook::contactListQuery($query, | |
406 | $name, | |
7fa9167d | 407 | CRM_Utils_Request::retrieve('context', 'String', CRM_Core_DAO::$_nullObject), |
408 | CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject) | |
6a488035 TO |
409 | ); |
410 | ||
411 | $dao = CRM_Core_DAO::executeQuery($query); | |
412 | while ($dao->fetch()) { | |
413 | $result[] = array( | |
6a488035 | 414 | 'id' => $dao->id, |
cac1236c | 415 | 'text' => $dao->name, |
6a488035 TO |
416 | ); |
417 | } | |
418 | } | |
419 | else { | |
420 | $query = " | |
421 | SELECT sort_name name, ce.email, cc.id | |
422 | FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id | |
423 | {$aclFrom} | |
424 | WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString} | |
425 | {$aclWhere} | |
426 | LIMIT {$offset}, {$rowCount} | |
427 | "; | |
428 | ||
429 | // send query to hook to be modified if needed | |
430 | CRM_Utils_Hook::contactListQuery($query, | |
431 | $name, | |
7fa9167d | 432 | CRM_Utils_Request::retrieve('context', 'String', CRM_Core_DAO::$_nullObject), |
015bf0da | 433 | CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject) |
6a488035 TO |
434 | ); |
435 | ||
6a488035 TO |
436 | $dao = CRM_Core_DAO::executeQuery($query); |
437 | ||
438 | while ($dao->fetch()) { | |
ce80b209 | 439 | //working here |
6a488035 | 440 | $result[] = array( |
cac1236c | 441 | 'text' => '"' . $dao->name . '" <' . $dao->email . '>', |
6a488035 TO |
442 | 'id' => (CRM_Utils_Array::value('id', $_GET)) ? "{$dao->id}::{$dao->email}" : '"' . $dao->name . '" <' . $dao->email . '>', |
443 | ); | |
444 | } | |
445 | } | |
6a488035 | 446 | if ($result) { |
8be1a839 | 447 | CRM_Utils_JSON::output($result); |
6a488035 TO |
448 | } |
449 | } | |
450 | } | |
451 | CRM_Utils_System::civiExit(); | |
452 | } | |
453 | ||
00be9182 | 454 | public static function getContactPhone() { |
6a488035 TO |
455 | |
456 | $queryString = NULL; | |
457 | //check for mobile type | |
458 | $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); | |
459 | $mobileType = CRM_Utils_Array::value('Mobile', $phoneTypes); | |
460 | ||
a4cce21a JM |
461 | $name = CRM_Utils_Array::value('name', $_GET); |
462 | if ($name) { | |
6a488035 TO |
463 | $name = CRM_Utils_Type::escape($name, 'String'); |
464 | $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) "; | |
465 | } | |
a4cce21a | 466 | else { |
d75f2f47 EM |
467 | $cid = CRM_Utils_Array::value('cid', $_GET); |
468 | if ($cid) { | |
b44e3f84 | 469 | //check cid for integer |
a4cce21a JM |
470 | $contIDS = explode(',', $cid); |
471 | foreach ($contIDS as $contID) { | |
472 | CRM_Utils_Type::escape($contID, 'Integer'); | |
473 | } | |
474 | $queryString = " cc.id IN ( $cid )"; | |
6a488035 | 475 | } |
6a488035 TO |
476 | } |
477 | ||
478 | if ($queryString) { | |
479 | $offset = CRM_Utils_Array::value('offset', $_GET, 0); | |
480 | $rowCount = CRM_Utils_Array::value('rowcount', $_GET, 20); | |
481 | ||
bf00d1b6 DL |
482 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
483 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
484 | ||
6a488035 TO |
485 | // add acl clause here |
486 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
487 | if ($aclWhere) { | |
488 | $aclWhere = " AND $aclWhere"; | |
489 | } | |
490 | ||
491 | $query = " | |
492 | SELECT sort_name name, cp.phone, cc.id | |
493 | FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id | |
494 | {$aclFrom} | |
495 | WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString} | |
496 | {$aclWhere} | |
497 | LIMIT {$offset}, {$rowCount} | |
498 | "; | |
499 | ||
500 | // send query to hook to be modified if needed | |
501 | CRM_Utils_Hook::contactListQuery($query, | |
502 | $name, | |
7fa9167d | 503 | CRM_Utils_Request::retrieve('context', 'String', CRM_Core_DAO::$_nullObject), |
015bf0da | 504 | CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject) |
6a488035 TO |
505 | ); |
506 | ||
507 | $dao = CRM_Core_DAO::executeQuery($query); | |
508 | ||
509 | while ($dao->fetch()) { | |
510 | $result[] = array( | |
b792e485 | 511 | 'text' => '"' . $dao->name . '" (' . $dao->phone . ')', |
6a488035 TO |
512 | 'id' => (CRM_Utils_Array::value('id', $_GET)) ? "{$dao->id}::{$dao->phone}" : '"' . $dao->name . '" <' . $dao->phone . '>', |
513 | ); | |
514 | } | |
515 | } | |
516 | ||
517 | if ($result) { | |
8be1a839 | 518 | CRM_Utils_JSON::output($result); |
6a488035 TO |
519 | } |
520 | CRM_Utils_System::civiExit(); | |
521 | } | |
522 | ||
523 | ||
00be9182 | 524 | public static function buildSubTypes() { |
7fa9167d | 525 | $parent = CRM_Utils_Request::retrieve('parentId', 'Positive', CRM_Core_DAO::$_nullObject); |
6a488035 TO |
526 | |
527 | switch ($parent) { | |
528 | case 1: | |
529 | $contactType = 'Individual'; | |
530 | break; | |
531 | ||
532 | case 2: | |
533 | $contactType = 'Household'; | |
534 | break; | |
535 | ||
536 | case 4: | |
537 | $contactType = 'Organization'; | |
538 | break; | |
539 | } | |
540 | ||
541 | $subTypes = CRM_Contact_BAO_ContactType::subTypePairs($contactType, FALSE, NULL); | |
542 | asort($subTypes); | |
ecdef330 | 543 | CRM_Utils_JSON::output($subTypes); |
6a488035 TO |
544 | } |
545 | ||
00be9182 | 546 | public static function buildDedupeRules() { |
015bf0da | 547 | $parent = CRM_Utils_Request::retrieve('parentId', 'Positive', CRM_Core_DAO::$_nullObject); |
6a488035 TO |
548 | |
549 | switch ($parent) { | |
550 | case 1: | |
551 | $contactType = 'Individual'; | |
552 | break; | |
553 | ||
554 | case 2: | |
555 | $contactType = 'Household'; | |
556 | break; | |
557 | ||
558 | case 4: | |
559 | $contactType = 'Organization'; | |
560 | break; | |
561 | } | |
562 | ||
563 | $dedupeRules = CRM_Dedupe_BAO_RuleGroup::getByType($contactType); | |
564 | ||
ecdef330 | 565 | CRM_Utils_JSON::output($dedupeRules); |
6a488035 TO |
566 | } |
567 | ||
568 | /** | |
fe482240 | 569 | * Function used for CiviCRM dashboard operations. |
6a488035 | 570 | */ |
00be9182 | 571 | public static function dashboard() { |
6a488035 TO |
572 | $operation = CRM_Utils_Type::escape($_REQUEST['op'], 'String'); |
573 | ||
574 | switch ($operation) { | |
575 | case 'get_widgets_by_column': | |
576 | // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration. | |
577 | // get contact id of logged in user | |
578 | ||
579 | $dashlets = CRM_Core_BAO_Dashboard::getContactDashlets(); | |
580 | break; | |
581 | ||
582 | case 'get_widget': | |
583 | $dashletID = CRM_Utils_Type::escape($_GET['id'], 'Positive'); | |
584 | ||
585 | $dashlets = CRM_Core_BAO_Dashboard::getDashletInfo($dashletID); | |
586 | break; | |
587 | ||
588 | case 'save_columns': | |
589 | CRM_Core_BAO_Dashboard::saveDashletChanges($_REQUEST['columns']); | |
590 | CRM_Utils_System::civiExit(); | |
591 | case 'delete_dashlet': | |
592 | $dashletID = CRM_Utils_Type::escape($_REQUEST['dashlet_id'], 'Positive'); | |
593 | CRM_Core_BAO_Dashboard::deleteDashlet($dashletID); | |
594 | CRM_Utils_System::civiExit(); | |
595 | } | |
596 | ||
ecdef330 | 597 | CRM_Utils_JSON::output($dashlets); |
6a488035 TO |
598 | } |
599 | ||
600 | /** | |
fe482240 | 601 | * Retrieve signature based on email id. |
6a488035 | 602 | */ |
00be9182 | 603 | public static function getSignature() { |
6a488035 | 604 | $emailID = CRM_Utils_Type::escape($_REQUEST['emailID'], 'Positive'); |
353ffa53 TO |
605 | $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}"; |
606 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
607 | |
608 | $signatures = array(); | |
609 | while ($dao->fetch()) { | |
610 | $signatures = array( | |
611 | 'signature_text' => $dao->signature_text, | |
612 | 'signature_html' => $dao->signature_html, | |
613 | ); | |
614 | } | |
615 | ||
ecdef330 | 616 | CRM_Utils_JSON::output($signatures); |
6a488035 TO |
617 | } |
618 | ||
619 | /** | |
100fef9d | 620 | * Process dupes. |
6a488035 | 621 | */ |
00be9182 | 622 | public static function processDupes() { |
6a488035 | 623 | $oper = CRM_Utils_Type::escape($_REQUEST['op'], 'String'); |
353ffa53 TO |
624 | $cid = CRM_Utils_Type::escape($_REQUEST['cid'], 'Positive'); |
625 | $oid = CRM_Utils_Type::escape($_REQUEST['oid'], 'Positive'); | |
6a488035 TO |
626 | |
627 | if (!$oper || !$cid || !$oid) { | |
628 | return; | |
629 | } | |
630 | ||
631 | $exception = new CRM_Dedupe_DAO_Exception(); | |
632 | $exception->contact_id1 = $cid; | |
633 | $exception->contact_id2 = $oid; | |
634 | //make sure contact2 > contact1. | |
635 | if ($cid > $oid) { | |
636 | $exception->contact_id1 = $oid; | |
637 | $exception->contact_id2 = $cid; | |
638 | } | |
639 | $exception->find(TRUE); | |
640 | $status = NULL; | |
641 | if ($oper == 'dupe-nondupe') { | |
642 | $status = $exception->save(); | |
643 | } | |
644 | if ($oper == 'nondupe-dupe') { | |
645 | $status = $exception->delete(); | |
646 | } | |
647 | ||
ecdef330 | 648 | CRM_Utils_JSON::output(array('status' => ($status) ? $oper : $status)); |
6a488035 TO |
649 | } |
650 | ||
183ec330 | 651 | /** |
652 | * Retrieve list of duplicate pairs from cache table. | |
653 | */ | |
00be9182 | 654 | public static function getDedupes() { |
63ef778e | 655 | $offset = isset($_REQUEST['start']) ? CRM_Utils_Type::escape($_REQUEST['start'], 'Integer') : 0; |
656 | $rowCount = isset($_REQUEST['length']) ? CRM_Utils_Type::escape($_REQUEST['length'], 'Integer') : 25; | |
6a488035 | 657 | |
63ef778e | 658 | $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type::escape($_REQUEST['gid'], 'Integer') : 0; |
659 | $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type::escape($_REQUEST['rgid'], 'Integer') : 0; | |
660 | $selected = isset($_REQUEST['selected']) ? CRM_Utils_Type::escape($_REQUEST['selected'], 'Integer') : 0; | |
661 | if ($rowCount < 0) { | |
662 | $rowCount = 0; | |
663 | } | |
6a488035 | 664 | |
1f51ef4e | 665 | $whereClause = $orderByClause = ''; |
e23e26ec | 666 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); |
63ef778e | 667 | $searchRows = array(); |
6a488035 | 668 | |
ed92673b | 669 | $searchParams = self::getSearchOptionsFromRequest(); |
670 | $queryParams = array(); | |
671 | ||
63ef778e | 672 | $join = ''; |
673 | $where = array(); | |
579ea9bc | 674 | |
ed92673b | 675 | $isOrQuery = self::isOrQuery(); |
676 | ||
677 | $nextParamKey = 3; | |
678 | $mappings = array( | |
679 | 'src' => 'cc1.display_name', | |
680 | 'dst' => 'cc2.display_name', | |
681 | 'src_email' => 'ce1.email', | |
682 | 'dst_email' => 'ce2.email', | |
683 | 'src_postcode' => 'ca1.postal_code', | |
684 | 'dst_postcode' => 'ca2.postal_code', | |
685 | 'src_street' => 'ca1.street', | |
686 | 'dst_street' => 'ca2.street', | |
687 | ); | |
688 | ||
689 | foreach ($mappings as $key => $dbName) { | |
690 | if (!empty($searchParams[$key])) { | |
691 | $queryParams[$nextParamKey] = array('%' . $searchParams[$key] . '%', 'String'); | |
692 | $where[] = $dbName . " LIKE %{$nextParamKey} "; | |
693 | $nextParamKey++; | |
694 | } | |
63ef778e | 695 | } |
ed92673b | 696 | |
697 | if ($isOrQuery) { | |
f931b74c | 698 | $whereClause = ' ( ' . implode(' OR ', $where) . ' ) '; |
63ef778e | 699 | } |
700 | else { | |
701 | if (!empty($where)) { | |
702 | $whereClause = implode(' AND ', $where); | |
703 | } | |
f931b74c | 704 | } |
63ef778e | 705 | $whereClause .= $whereClause ? ' AND de.id IS NULL' : ' de.id IS NULL'; |
6a488035 | 706 | |
63ef778e | 707 | if ($selected) { |
708 | $whereClause .= ' AND pn.is_selected = 1'; | |
709 | } | |
2988f5c7 | 710 | $join .= CRM_Dedupe_Merger::getJoinOnDedupeTable(); |
63ef778e | 711 | |
63ef778e | 712 | $select = array( |
f931b74c | 713 | 'cc1.contact_type' => 'src_contact_type', |
714 | 'cc1.display_name' => 'src_display_name', | |
715 | 'cc1.contact_sub_type' => 'src_contact_sub_type', | |
716 | 'cc2.contact_type' => 'dst_contact_type', | |
717 | 'cc2.display_name' => 'dst_display_name', | |
718 | 'cc2.contact_sub_type' => 'dst_contact_sub_type', | |
719 | 'ce1.email' => 'src_email', | |
720 | 'ce2.email' => 'dst_email', | |
721 | 'ca1.postal_code' => 'src_postcode', | |
722 | 'ca2.postal_code' => 'dst_postcode', | |
723 | 'ca1.street_address' => 'src_street', | |
724 | 'ca2.street_address' => 'dst_street', | |
63ef778e | 725 | ); |
726 | ||
f931b74c | 727 | if ($select) { |
63ef778e | 728 | $join .= " INNER JOIN civicrm_contact cc1 ON cc1.id = pn.entity_id1"; |
729 | $join .= " INNER JOIN civicrm_contact cc2 ON cc2.id = pn.entity_id2"; | |
730 | $join .= " LEFT JOIN civicrm_email ce1 ON (ce1.contact_id = pn.entity_id1 AND ce1.is_primary = 1 )"; | |
731 | $join .= " LEFT JOIN civicrm_email ce2 ON (ce2.contact_id = pn.entity_id2 AND ce2.is_primary = 1 )"; | |
732 | $join .= " LEFT JOIN civicrm_address ca1 ON (ca1.contact_id = pn.entity_id1 AND ca1.is_primary = 1 )"; | |
733 | $join .= " LEFT JOIN civicrm_address ca2 ON (ca2.contact_id = pn.entity_id2 AND ca2.is_primary = 1 )"; | |
734 | } | |
ed92673b | 735 | $iTotal = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, $join, $whereClause, '=', $queryParams); |
1f51ef4e | 736 | if (!empty($_REQUEST['order'])) { |
737 | foreach ($_REQUEST['order'] as $orderInfo) { | |
738 | if (!empty($orderInfo['column'])) { | |
739 | $orderColumnNumber = $orderInfo['column']; | |
740 | $dir = $orderInfo['dir']; | |
741 | } | |
63ef778e | 742 | } |
1f51ef4e | 743 | $columnDetails = CRM_Utils_Array::value($orderColumnNumber, $_REQUEST['columns']); |
63ef778e | 744 | } |
f931b74c | 745 | if (!empty($columnDetails)) { |
63ef778e | 746 | switch ($columnDetails['data']) { |
f931b74c | 747 | case 'src': |
239ed377 | 748 | $orderByClause = " ORDER BY cc1.display_name {$dir}"; |
f931b74c | 749 | break; |
750 | ||
751 | case 'src_email': | |
239ed377 | 752 | $orderByClause = " ORDER BY ce1.email {$dir}"; |
f931b74c | 753 | break; |
754 | ||
755 | case 'src_street': | |
239ed377 | 756 | $orderByClause = " ORDER BY ca1.street_address {$dir}"; |
f931b74c | 757 | break; |
758 | ||
759 | case 'src_postcode': | |
239ed377 | 760 | $orderByClause = " ORDER BY ca1.postal_code {$dir}"; |
f931b74c | 761 | break; |
762 | ||
763 | case 'dst': | |
239ed377 | 764 | $orderByClause = " ORDER BY cc2.display_name {$dir}"; |
f931b74c | 765 | break; |
766 | ||
767 | case 'dst_email': | |
239ed377 | 768 | $orderByClause = " ORDER BY ce2.email {$dir}"; |
f931b74c | 769 | break; |
770 | ||
771 | case 'dst_street': | |
239ed377 | 772 | $orderByClause = " ORDER BY ca2.street_address {$dir}"; |
f931b74c | 773 | break; |
774 | ||
775 | case 'dst_postcode': | |
239ed377 | 776 | $orderByClause = " ORDER BY ca2.postal_code {$dir}"; |
f931b74c | 777 | break; |
778 | ||
779 | default: | |
780 | break; | |
63ef778e | 781 | } |
782 | } | |
6a488035 | 783 | |
ed92673b | 784 | $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $whereClause, $offset, $rowCount, $select, $orderByClause, TRUE, $queryParams); |
63ef778e | 785 | $iFilteredTotal = CRM_Core_DAO::singleValueQuery("SELECT FOUND_ROWS()"); |
6a488035 | 786 | |
63ef778e | 787 | $count = 0; |
788 | foreach ($dupePairs as $key => $pairInfo) { | |
789 | $pair =& $pairInfo['data']; | |
790 | $srcContactSubType = CRM_Utils_Array::value('src_contact_sub_type', $pairInfo); | |
791 | $dstContactSubType = CRM_Utils_Array::value('dst_contact_sub_type', $pairInfo); | |
792 | $srcTypeImage = CRM_Contact_BAO_Contact_Utils::getImage($srcContactSubType ? | |
793 | $srcContactSubType : $pairInfo['src_contact_type'], | |
794 | FALSE, | |
fd630ef9 | 795 | $pairInfo['entity_id1'] |
63ef778e | 796 | ); |
797 | $dstTypeImage = CRM_Contact_BAO_Contact_Utils::getImage($dstContactSubType ? | |
798 | $dstContactSubType : $pairInfo['dst_contact_type'], | |
799 | FALSE, | |
fd630ef9 | 800 | $pairInfo['entity_id2'] |
63ef778e | 801 | ); |
6a488035 | 802 | |
63ef778e | 803 | $searchRows[$count]['is_selected'] = $pairInfo['is_selected']; |
804 | $searchRows[$count]['is_selected_input'] = "<input type='checkbox' class='crm-dedupe-select' name='pnid_{$pairInfo['prevnext_id']}' value='{$pairInfo['is_selected']}' onclick='toggleDedupeSelect(this)'>"; | |
805 | $searchRows[$count]['src_image'] = $srcTypeImage; | |
fd630ef9 | 806 | $searchRows[$count]['src'] = CRM_Utils_System::href($pair['srcName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id1']}"); |
63ef778e | 807 | $searchRows[$count]['src_email'] = CRM_Utils_Array::value('src_email', $pairInfo); |
808 | $searchRows[$count]['src_street'] = CRM_Utils_Array::value('src_street', $pairInfo); | |
809 | $searchRows[$count]['src_postcode'] = CRM_Utils_Array::value('src_postcode', $pairInfo); | |
810 | $searchRows[$count]['dst_image'] = $dstTypeImage; | |
fd630ef9 | 811 | $searchRows[$count]['dst'] = CRM_Utils_System::href($pair['dstName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id2']}"); |
63ef778e | 812 | $searchRows[$count]['dst_email'] = CRM_Utils_Array::value('dst_email', $pairInfo); |
813 | $searchRows[$count]['dst_street'] = CRM_Utils_Array::value('dst_street', $pairInfo); | |
814 | $searchRows[$count]['dst_postcode'] = CRM_Utils_Array::value('dst_postcode', $pairInfo); | |
da3afd4a | 815 | $searchRows[$count]['conflicts'] = str_replace("',", "',<br/>", CRM_Utils_Array::value('conflicts', $pair)); |
63ef778e | 816 | $searchRows[$count]['weight'] = CRM_Utils_Array::value('weight', $pair); |
817 | ||
fd630ef9 | 818 | if (!empty($pairInfo['data']['canMerge'])) { |
819 | $mergeParams = "reset=1&cid={$pairInfo['entity_id1']}&oid={$pairInfo['entity_id2']}&action=update&rgid={$rgid}"; | |
6a488035 TO |
820 | if ($gid) { |
821 | $mergeParams .= "&gid={$gid}"; | |
822 | } | |
823 | ||
fd630ef9 | 824 | $searchRows[$count]['actions'] = "<a class='crm-dedupe-flip' href='#' data-pnid={$pairInfo['prevnext_id']}>" . ts('flip') . "</a> | "; |
da3afd4a | 825 | $searchRows[$count]['actions'] .= CRM_Utils_System::href(ts('merge'), 'civicrm/contact/merge', $mergeParams); |
fd630ef9 | 826 | $searchRows[$count]['actions'] .= " | <a id='notDuplicate' href='#' onClick=\"processDupes( {$pairInfo['entity_id1']}, {$pairInfo['entity_id2']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>"; |
6a488035 TO |
827 | } |
828 | else { | |
63ef778e | 829 | $searchRows[$count]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>'; |
6a488035 | 830 | } |
63ef778e | 831 | $count++; |
6a488035 TO |
832 | } |
833 | ||
22b232f3 | 834 | $dupePairs = array( |
835 | 'data' => $searchRows, | |
836 | 'recordsTotal' => $iTotal, | |
837 | 'recordsFiltered' => $iFilteredTotal, | |
838 | ); | |
1f51ef4e | 839 | if (!empty($_REQUEST['is_unit_test'])) { |
840 | return $dupePairs; | |
841 | } | |
22b232f3 | 842 | CRM_Utils_JSON::output($dupePairs); |
6a488035 TO |
843 | } |
844 | ||
ed92673b | 845 | /** |
846 | * Get the searchable options from the request. | |
847 | * | |
848 | * @return array | |
849 | */ | |
850 | public static function getSearchOptionsFromRequest() { | |
851 | $searchParams = array(); | |
852 | $searchData = CRM_Utils_Array::value('search', $_REQUEST); | |
853 | $searchData['value'] = CRM_Utils_Type::escape($searchData['value'], 'String'); | |
854 | $selectorElements = array( | |
855 | 'is_selected', | |
856 | 'is_selected_input', | |
857 | 'src_image', | |
858 | 'src', | |
859 | 'src_email', | |
860 | 'src_street', | |
861 | 'src_postcode', | |
862 | 'dst_image', | |
863 | 'dst', | |
864 | 'dst_email', | |
865 | 'dst_street', | |
866 | 'dst_postcode', | |
867 | 'conflicts', | |
868 | 'weight', | |
869 | 'actions', | |
870 | ); | |
871 | $columns = $_REQUEST['columns']; | |
872 | ||
873 | foreach ($columns as $column) { | |
874 | if (!empty($column['search']['value']) && in_array($column['data'], $selectorElements)) { | |
875 | $searchParams[$column['data']] = CRM_Utils_Type::escape($column['search']['value'], 'String'); | |
876 | } | |
877 | elseif (!empty($searchData['value'])) { | |
878 | $searchParams[$column['data']] = $searchData['value']; | |
879 | } | |
880 | } | |
881 | return $searchParams; | |
882 | } | |
883 | ||
884 | /** | |
885 | * Is the query an OR query. | |
886 | * | |
887 | * If a generic search value is passed in - ie. $_REQUEST['search']['value'] = 'abc' | |
888 | * then all fields are searched for this. | |
889 | * | |
890 | * It is unclear if there is any code that still passes this in or whether is is just legacy. It | |
891 | * could cause a server-killing query on a large site so it probably is NOT in use if we haven't | |
892 | * had complaints. | |
893 | * | |
894 | * @return bool | |
895 | */ | |
896 | public static function isOrQuery() { | |
897 | $searchData = CRM_Utils_Array::value('search', $_REQUEST); | |
898 | return !empty($searchData['value']); | |
899 | } | |
900 | ||
6a488035 | 901 | /** |
fe482240 | 902 | * Retrieve a PDF Page Format for the PDF Letter form. |
6a488035 | 903 | */ |
00be9182 | 904 | public function pdfFormat() { |
6a488035 TO |
905 | $formatId = CRM_Utils_Type::escape($_REQUEST['formatId'], 'Integer'); |
906 | ||
907 | $pdfFormat = CRM_Core_BAO_PdfFormat::getById($formatId); | |
908 | ||
ecdef330 | 909 | CRM_Utils_JSON::output($pdfFormat); |
6a488035 TO |
910 | } |
911 | ||
912 | /** | |
fe482240 | 913 | * Retrieve Paper Size dimensions. |
6a488035 | 914 | */ |
00be9182 | 915 | public static function paperSize() { |
6a488035 TO |
916 | $paperSizeName = CRM_Utils_Type::escape($_REQUEST['paperSizeName'], 'String'); |
917 | ||
918 | $paperSize = CRM_Core_BAO_PaperSize::getByName($paperSizeName); | |
919 | ||
ecdef330 | 920 | CRM_Utils_JSON::output($paperSize); |
6a488035 TO |
921 | } |
922 | ||
183ec330 | 923 | /** |
924 | * Swap contacts in a dupe pair i.e main with duplicate contact. | |
ea3ddccf | 925 | * |
926 | * @param int $prevNextId | |
183ec330 | 927 | */ |
f931b74c | 928 | public static function flipDupePairs($prevNextId = NULL) { |
fd630ef9 | 929 | if (!$prevNextId) { |
808c05a9 | 930 | // @todo figure out if this is always POST & specify that rather than inexact GET |
931 | $prevNextId = CRM_Utils_Request::retrieve('pnid', 'Integer'); | |
fd630ef9 | 932 | } |
808c05a9 | 933 | |
934 | $onlySelected = FALSE; | |
fd630ef9 | 935 | if (is_array($prevNextId) && !CRM_Utils_Array::crmIsEmptyArray($prevNextId)) { |
808c05a9 | 936 | $onlySelected = TRUE; |
fd630ef9 | 937 | } |
808c05a9 | 938 | $prevNextId = CRM_Utils_Type::escapeAll((array) $prevNextId, 'Positive'); |
939 | CRM_Core_BAO_PrevNextCache::flipPair($prevNextId, $onlySelected); | |
fd630ef9 | 940 | CRM_Utils_JSON::output(); |
941 | } | |
942 | ||
aeb97cc1 CW |
943 | /** |
944 | * Used to store selected contacts across multiple pages in advanced search. | |
945 | */ | |
00be9182 | 946 | public static function selectUnselectContacts() { |
353ffa53 TO |
947 | $name = CRM_Utils_Array::value('name', $_REQUEST); |
948 | $cacheKey = CRM_Utils_Array::value('qfKey', $_REQUEST); | |
949 | $state = CRM_Utils_Array::value('state', $_REQUEST, 'checked'); | |
6a488035 TO |
950 | $variableType = CRM_Utils_Array::value('variableType', $_REQUEST, 'single'); |
951 | ||
952 | $actionToPerform = CRM_Utils_Array::value('action', $_REQUEST, 'select'); | |
953 | ||
954 | if ($variableType == 'multiple') { | |
955 | // action post value only works with multiple type variable | |
956 | if ($name) { | |
957 | //multiple names like mark_x_1-mark_x_2 where 1,2 are cids | |
958 | $elements = explode('-', $name); | |
959 | foreach ($elements as $key => $element) { | |
960 | $elements[$key] = self::_convertToId($element); | |
961 | } | |
91209206 | 962 | CRM_Utils_Type::escapeAll($elements, 'Integer'); |
6a488035 TO |
963 | CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform, $elements); |
964 | } | |
965 | else { | |
966 | CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform); | |
967 | } | |
968 | } | |
969 | elseif ($variableType == 'single') { | |
970 | $cId = self::_convertToId($name); | |
91209206 | 971 | CRM_Utils_Type::escape($cId, 'Integer'); |
6a488035 TO |
972 | $action = ($state == 'checked') ? 'select' : 'unselect'; |
973 | CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $action, $cId); | |
974 | } | |
975 | $contactIds = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey); | |
976 | $countSelectionCids = count($contactIds[$cacheKey]); | |
977 | ||
978 | $arrRet = array('getCount' => $countSelectionCids); | |
ecdef330 | 979 | CRM_Utils_JSON::output($arrRet); |
6a488035 TO |
980 | } |
981 | ||
4319322b | 982 | /** |
100fef9d | 983 | * @param string $name |
4319322b EM |
984 | * |
985 | * @return string | |
986 | */ | |
00be9182 | 987 | public static function _convertToId($name) { |
6a488035 TO |
988 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { |
989 | $cId = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
990 | } | |
991 | return $cId; | |
992 | } | |
993 | ||
00be9182 | 994 | public static function getAddressDisplay() { |
7fa9167d | 995 | $contactId = CRM_Utils_Request::retrieve('contact_id', 'Positive', CRM_Core_DAO::$_nullObject); |
6a488035 TO |
996 | if (!$contactId) { |
997 | $addressVal["error_message"] = "no contact id found"; | |
998 | } | |
999 | else { | |
408b79bf | 1000 | $entityBlock = array( |
1001 | 'contact_id' => $contactId, | |
1002 | 'entity_id' => $contactId, | |
1003 | ); | |
6a488035 TO |
1004 | $addressVal = CRM_Core_BAO_Address::getValues($entityBlock); |
1005 | } | |
1006 | ||
ecdef330 | 1007 | CRM_Utils_JSON::output($addressVal); |
6a488035 | 1008 | } |
40458f6c | 1009 | |
183ec330 | 1010 | /** |
1011 | * Mark dupe pairs as selected from un-selected state or vice-versa, in dupe cache table. | |
1012 | */ | |
f931b74c | 1013 | public static function toggleDedupeSelect() { |
63ef778e | 1014 | $rgid = CRM_Utils_Type::escape($_REQUEST['rgid'], 'Integer'); |
1015 | $gid = CRM_Utils_Type::escape($_REQUEST['gid'], 'Integer'); | |
1016 | $pnid = $_REQUEST['pnid']; | |
1017 | $isSelected = CRM_Utils_Type::escape($_REQUEST['is_selected'], 'Boolean'); | |
1018 | ||
2ae26001 | 1019 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); |
63ef778e | 1020 | |
1021 | $params = array( | |
1022 | 1 => array($isSelected, 'Boolean'), | |
f931b74c | 1023 | 3 => array("$cacheKeyString%", 'String'), // using % to address rows with conflicts as well |
63ef778e | 1024 | ); |
1025 | ||
1026 | //check pnid is_array or integer | |
1027 | $whereClause = NULL; | |
1028 | if (is_array($pnid) && !CRM_Utils_Array::crmIsEmptyArray($pnid)) { | |
13c42e60 | 1029 | CRM_Utils_Type::escapeAll($pnid, 'Positive'); |
63ef778e | 1030 | $pnid = implode(', ', $pnid); |
63ef778e | 1031 | $whereClause = " id IN ( {$pnid} ) "; |
1032 | } | |
1033 | else { | |
1034 | $pnid = CRM_Utils_Type::escape($pnid, 'Integer'); | |
1035 | $whereClause = " id = %2"; | |
1036 | $params[2] = array($pnid, 'Integer'); | |
1037 | } | |
1038 | ||
1039 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = %1 WHERE {$whereClause} AND cacheKey LIKE %3"; | |
1040 | CRM_Core_DAO::executeQuery($sql, $params); | |
1041 | ||
1042 | CRM_Utils_System::civiExit(); | |
1043 | } | |
1044 | ||
40458f6c | 1045 | /** |
fe482240 | 1046 | * Retrieve contact relationships. |
40458f6c | 1047 | */ |
1048 | public static function getContactRelationships() { | |
1049 | $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); | |
1050 | $context = CRM_Utils_Type::escape($_GET['context'], 'String'); | |
7d12de7f | 1051 | $relationship_type_id = CRM_Utils_Type::escape(CRM_Utils_Array::value('relationship_type_id', $_GET), 'Integer', FALSE); |
40458f6c | 1052 | |
b0266403 CW |
1053 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactID)) { |
1054 | return CRM_Utils_System::permissionDenied(); | |
1055 | } | |
1056 | ||
00f11506 | 1057 | $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); |
40458f6c | 1058 | |
1059 | $params['contact_id'] = $contactID; | |
1060 | $params['context'] = $context; | |
4c5f31d0 | 1061 | if ($relationship_type_id) { |
cdee9432 TM |
1062 | $params['relationship_type_id'] = $relationship_type_id; |
1063 | } | |
40458f6c | 1064 | |
1065 | // get the contact relationships | |
1066 | $relationships = CRM_Contact_BAO_Relationship::getContactRelationshipSelector($params); | |
1067 | ||
7d12de7f | 1068 | CRM_Utils_JSON::output($relationships); |
40458f6c | 1069 | } |
96025800 | 1070 | |
6a488035 | 1071 | } |