Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | ||
06508628 | 86 | $limit = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10); |
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'); |
8da35492 | 161 | $limit = $max = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10); |
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 | ||
00be9182 | 222 | public static function relationship() { |
c91df8b4 CW |
223 | $relType = CRM_Utils_Request::retrieve('rel_type', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); |
224 | $relContactID = CRM_Utils_Request::retrieve('rel_contact', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); | |
225 | $relationshipID = CRM_Utils_Array::value('rel_id', $_REQUEST); // this used only to determine add or update mode | |
226 | $caseID = CRM_Utils_Request::retrieve('case_id', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); | |
6a488035 | 227 | |
14a679f1 KJ |
228 | // check if there are multiple clients for this case, if so then we need create |
229 | // relationship and also activities for each contacts | |
6a488035 | 230 | |
14a679f1 KJ |
231 | // get case client list |
232 | $clientList = CRM_Case_BAO_Case::getCaseClients($caseID); | |
6a488035 | 233 | |
c91df8b4 CW |
234 | $ret = array('is_error' => 0); |
235 | ||
22e263ad | 236 | foreach ($clientList as $sourceContactID) { |
14a679f1 KJ |
237 | $relationParams = array( |
238 | 'relationship_type_id' => $relType . '_a_b', | |
239 | 'contact_check' => array($relContactID => 1), | |
240 | 'is_active' => 1, | |
241 | 'case_id' => $caseID, | |
242 | 'start_date' => date("Ymd"), | |
6a488035 TO |
243 | ); |
244 | ||
14a679f1 KJ |
245 | $relationIds = array('contact' => $sourceContactID); |
246 | ||
247 | // check if we are editing/updating existing relationship | |
248 | if ($relationshipID && $relationshipID != 'null') { | |
249 | // here we need to retrieve appropriate relationshipID based on client id and relationship type id | |
250 | $caseRelationships = new CRM_Contact_DAO_Relationship(); | |
251 | $caseRelationships->case_id = $caseID; | |
252 | $caseRelationships->relationship_type_id = $relType; | |
253 | $caseRelationships->contact_id_a = $sourceContactID; | |
254 | $caseRelationships->find(); | |
255 | ||
22e263ad | 256 | while ($caseRelationships->fetch()) { |
14a679f1 KJ |
257 | $relationIds['relationship'] = $caseRelationships->id; |
258 | $relationIds['contactTarget'] = $relContactID; | |
259 | } | |
260 | $caseRelationships->free(); | |
261 | } | |
262 | ||
263 | // create new or update existing relationship | |
2da59b29 | 264 | $return = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds); |
14a679f1 | 265 | |
a7488080 | 266 | if (!empty($return[4][0])) { |
14a679f1 | 267 | $relationshipID = $return[4][0]; |
14a679f1 KJ |
268 | |
269 | //create an activity for case role assignment.CRM-4480 | |
270 | CRM_Case_BAO_Case::createCaseRoleActivity($caseID, $relationshipID, $relContactID); | |
271 | } | |
c91df8b4 CW |
272 | else { |
273 | $ret = array( | |
274 | 'is_error' => 1, | |
275 | 'error_message' => ts('The relationship type definition for the case role is not valid for the client and / or staff contact types. You can review and edit relationship types at <a href="%1">Administer >> Option Lists >> Relationship Types</a>.', | |
21dfd5f5 | 276 | array(1 => CRM_Utils_System::url('civicrm/admin/reltype', 'reset=1'))), |
c91df8b4 CW |
277 | ); |
278 | } | |
6a488035 | 279 | } |
6a488035 | 280 | |
ecdef330 | 281 | CRM_Utils_JSON::output($ret); |
6a488035 TO |
282 | } |
283 | ||
284 | /** | |
fe482240 | 285 | * Fetch the custom field help. |
6a488035 | 286 | */ |
00be9182 | 287 | public static function customField() { |
353ffa53 TO |
288 | $fieldId = CRM_Utils_Type::escape($_REQUEST['id'], 'Integer'); |
289 | $params = array('id' => $fieldId); | |
6a488035 | 290 | $returnProperties = array('help_pre', 'help_post'); |
353ffa53 | 291 | $values = array(); |
6a488035 TO |
292 | |
293 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties); | |
ecdef330 | 294 | CRM_Utils_JSON::output($values); |
6a488035 TO |
295 | } |
296 | ||
00be9182 | 297 | public static function groupTree() { |
8be1a839 | 298 | header('Content-Type: application/json'); |
6a488035 TO |
299 | $gids = CRM_Utils_Type::escape($_GET['gids'], 'String'); |
300 | echo CRM_Contact_BAO_GroupNestingCache::json($gids); | |
301 | CRM_Utils_System::civiExit(); | |
302 | } | |
303 | ||
6a488035 | 304 | /** |
fe482240 | 305 | * Delete custom value. |
6a488035 | 306 | */ |
00be9182 | 307 | public static function deleteCustomValue() { |
effdc2d9 | 308 | header('Content-Type: text/plain'); |
6a488035 TO |
309 | $customValueID = CRM_Utils_Type::escape($_REQUEST['valueID'], 'Positive'); |
310 | $customGroupID = CRM_Utils_Type::escape($_REQUEST['groupID'], 'Positive'); | |
311 | ||
312 | CRM_Core_BAO_CustomValue::deleteCustomValue($customValueID, $customGroupID); | |
a4cce21a JM |
313 | $contactId = CRM_Utils_Array::value('contactId', $_REQUEST); |
314 | if ($contactId) { | |
6a488035 TO |
315 | echo CRM_Contact_BAO_Contact::getCountComponent('custom_' . $_REQUEST['groupID'], $contactId); |
316 | } | |
317 | ||
318 | // reset the group contact cache for this group | |
319 | CRM_Contact_BAO_GroupContactCache::remove(); | |
320 | CRM_Utils_System::civiExit(); | |
321 | } | |
322 | ||
6a488035 | 323 | /** |
fe482240 | 324 | * check the CMS username. |
ce80b209 | 325 | */ |
6a488035 | 326 | static public function checkUserName() { |
272081ca TO |
327 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), array('for', 'ts')); |
328 | if ( | |
329 | CRM_Utils_Time::getTimeRaw() > $_REQUEST['ts'] + self::CHECK_USERNAME_TTL | |
330 | || $_REQUEST['for'] != 'civicrm/ajax/cmsuser' | |
331 | || !$signer->validate($_REQUEST['sig'], $_REQUEST) | |
332 | ) { | |
333 | $user = array('name' => 'error'); | |
8be1a839 | 334 | CRM_Utils_JSON::output($user); |
272081ca TO |
335 | } |
336 | ||
6a488035 TO |
337 | $config = CRM_Core_Config::singleton(); |
338 | $username = trim($_REQUEST['cms_name']); | |
339 | ||
340 | $params = array('name' => $username); | |
341 | ||
342 | $errors = array(); | |
343 | $config->userSystem->checkUserNameEmailExists($params, $errors); | |
344 | ||
345 | if (isset($errors['cms_name']) || isset($errors['name'])) { | |
346 | //user name is not availble | |
347 | $user = array('name' => 'no'); | |
8be1a839 | 348 | CRM_Utils_JSON::output($user); |
6a488035 TO |
349 | } |
350 | else { | |
351 | //user name is available | |
352 | $user = array('name' => 'yes'); | |
8be1a839 | 353 | CRM_Utils_JSON::output($user); |
6a488035 | 354 | } |
8be1a839 TO |
355 | |
356 | // Not reachable: JSON::output() above exits. | |
6a488035 TO |
357 | CRM_Utils_System::civiExit(); |
358 | } | |
359 | ||
360 | /** | |
fe482240 | 361 | * Function to get email address of a contact. |
6a488035 | 362 | */ |
00be9182 | 363 | public static function getContactEmail() { |
a7488080 | 364 | if (!empty($_REQUEST['contact_id'])) { |
6a488035 | 365 | $contactID = CRM_Utils_Type::escape($_REQUEST['contact_id'], 'Positive'); |
8c0ea1d7 TO |
366 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) { |
367 | return; | |
368 | } | |
6a488035 TO |
369 | list($displayName, |
370 | $userEmail | |
353ffa53 | 371 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID); |
effdc2d9 TO |
372 | |
373 | header('Content-Type: text/plain'); | |
6a488035 TO |
374 | if ($userEmail) { |
375 | echo $userEmail; | |
376 | } | |
377 | } | |
378 | else { | |
379 | $noemail = CRM_Utils_Array::value('noemail', $_GET); | |
380 | $queryString = NULL; | |
a4cce21a JM |
381 | $name = CRM_Utils_Array::value('name', $_GET); |
382 | if ($name) { | |
6a488035 TO |
383 | $name = CRM_Utils_Type::escape($name, 'String'); |
384 | if ($noemail) { | |
385 | $queryString = " cc.sort_name LIKE '%$name%'"; | |
386 | } | |
387 | else { | |
388 | $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) "; | |
389 | } | |
390 | } | |
a4cce21a | 391 | else { |
d75f2f47 EM |
392 | $cid = CRM_Utils_Array::value('cid', $_GET); |
393 | if ($cid) { | |
a4cce21a JM |
394 | //check cid for interger |
395 | $contIDS = explode(',', $cid); | |
396 | foreach ($contIDS as $contID) { | |
397 | CRM_Utils_Type::escape($contID, 'Integer'); | |
398 | } | |
399 | $queryString = " cc.id IN ( $cid )"; | |
d75f2f47 | 400 | } |
6a488035 TO |
401 | } |
402 | ||
403 | if ($queryString) { | |
404 | $offset = CRM_Utils_Array::value('offset', $_GET, 0); | |
cac1236c | 405 | $rowCount = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10); |
6a488035 | 406 | |
bf00d1b6 | 407 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
bf00d1b6 | 408 | |
6a488035 TO |
409 | // add acl clause here |
410 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
411 | if ($aclWhere) { | |
412 | $aclWhere = " AND $aclWhere"; | |
413 | } | |
414 | if ($noemail) { | |
415 | $query = " | |
416 | SELECT sort_name name, cc.id | |
417 | FROM civicrm_contact cc | |
418 | {$aclFrom} | |
419 | WHERE cc.is_deceased = 0 AND {$queryString} | |
420 | {$aclWhere} | |
421 | LIMIT {$offset}, {$rowCount} | |
422 | "; | |
423 | ||
424 | // send query to hook to be modified if needed | |
425 | CRM_Utils_Hook::contactListQuery($query, | |
426 | $name, | |
427 | CRM_Utils_Array::value('context', $_GET), | |
428 | CRM_Utils_Array::value('cid', $_GET) | |
429 | ); | |
430 | ||
431 | $dao = CRM_Core_DAO::executeQuery($query); | |
432 | while ($dao->fetch()) { | |
433 | $result[] = array( | |
6a488035 | 434 | 'id' => $dao->id, |
cac1236c | 435 | 'text' => $dao->name, |
6a488035 TO |
436 | ); |
437 | } | |
438 | } | |
439 | else { | |
440 | $query = " | |
441 | SELECT sort_name name, ce.email, cc.id | |
442 | FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id | |
443 | {$aclFrom} | |
444 | WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString} | |
445 | {$aclWhere} | |
446 | LIMIT {$offset}, {$rowCount} | |
447 | "; | |
448 | ||
449 | // send query to hook to be modified if needed | |
450 | CRM_Utils_Hook::contactListQuery($query, | |
451 | $name, | |
452 | CRM_Utils_Array::value('context', $_GET), | |
453 | CRM_Utils_Array::value('cid', $_GET) | |
454 | ); | |
455 | ||
6a488035 TO |
456 | $dao = CRM_Core_DAO::executeQuery($query); |
457 | ||
458 | while ($dao->fetch()) { | |
ce80b209 | 459 | //working here |
6a488035 | 460 | $result[] = array( |
cac1236c | 461 | 'text' => '"' . $dao->name . '" <' . $dao->email . '>', |
6a488035 TO |
462 | 'id' => (CRM_Utils_Array::value('id', $_GET)) ? "{$dao->id}::{$dao->email}" : '"' . $dao->name . '" <' . $dao->email . '>', |
463 | ); | |
464 | } | |
465 | } | |
6a488035 | 466 | if ($result) { |
8be1a839 | 467 | CRM_Utils_JSON::output($result); |
6a488035 TO |
468 | } |
469 | } | |
470 | } | |
471 | CRM_Utils_System::civiExit(); | |
472 | } | |
473 | ||
00be9182 | 474 | public static function getContactPhone() { |
6a488035 TO |
475 | |
476 | $queryString = NULL; | |
477 | //check for mobile type | |
478 | $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); | |
479 | $mobileType = CRM_Utils_Array::value('Mobile', $phoneTypes); | |
480 | ||
a4cce21a JM |
481 | $name = CRM_Utils_Array::value('name', $_GET); |
482 | if ($name) { | |
6a488035 TO |
483 | $name = CRM_Utils_Type::escape($name, 'String'); |
484 | $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) "; | |
485 | } | |
a4cce21a | 486 | else { |
d75f2f47 EM |
487 | $cid = CRM_Utils_Array::value('cid', $_GET); |
488 | if ($cid) { | |
a4cce21a JM |
489 | //check cid for interger |
490 | $contIDS = explode(',', $cid); | |
491 | foreach ($contIDS as $contID) { | |
492 | CRM_Utils_Type::escape($contID, 'Integer'); | |
493 | } | |
494 | $queryString = " cc.id IN ( $cid )"; | |
6a488035 | 495 | } |
6a488035 TO |
496 | } |
497 | ||
498 | if ($queryString) { | |
499 | $offset = CRM_Utils_Array::value('offset', $_GET, 0); | |
500 | $rowCount = CRM_Utils_Array::value('rowcount', $_GET, 20); | |
501 | ||
bf00d1b6 DL |
502 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
503 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
504 | ||
6a488035 TO |
505 | // add acl clause here |
506 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
507 | if ($aclWhere) { | |
508 | $aclWhere = " AND $aclWhere"; | |
509 | } | |
510 | ||
511 | $query = " | |
512 | SELECT sort_name name, cp.phone, cc.id | |
513 | FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id | |
514 | {$aclFrom} | |
515 | WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString} | |
516 | {$aclWhere} | |
517 | LIMIT {$offset}, {$rowCount} | |
518 | "; | |
519 | ||
520 | // send query to hook to be modified if needed | |
521 | CRM_Utils_Hook::contactListQuery($query, | |
522 | $name, | |
523 | CRM_Utils_Array::value('context', $_GET), | |
524 | CRM_Utils_Array::value('cid', $_GET) | |
525 | ); | |
526 | ||
527 | $dao = CRM_Core_DAO::executeQuery($query); | |
528 | ||
529 | while ($dao->fetch()) { | |
530 | $result[] = array( | |
b792e485 | 531 | 'text' => '"' . $dao->name . '" (' . $dao->phone . ')', |
6a488035 TO |
532 | 'id' => (CRM_Utils_Array::value('id', $_GET)) ? "{$dao->id}::{$dao->phone}" : '"' . $dao->name . '" <' . $dao->phone . '>', |
533 | ); | |
534 | } | |
535 | } | |
536 | ||
537 | if ($result) { | |
8be1a839 | 538 | CRM_Utils_JSON::output($result); |
6a488035 TO |
539 | } |
540 | CRM_Utils_System::civiExit(); | |
541 | } | |
542 | ||
543 | ||
00be9182 | 544 | public static function buildSubTypes() { |
6a488035 TO |
545 | $parent = CRM_Utils_Array::value('parentId', $_REQUEST); |
546 | ||
547 | switch ($parent) { | |
548 | case 1: | |
549 | $contactType = 'Individual'; | |
550 | break; | |
551 | ||
552 | case 2: | |
553 | $contactType = 'Household'; | |
554 | break; | |
555 | ||
556 | case 4: | |
557 | $contactType = 'Organization'; | |
558 | break; | |
559 | } | |
560 | ||
561 | $subTypes = CRM_Contact_BAO_ContactType::subTypePairs($contactType, FALSE, NULL); | |
562 | asort($subTypes); | |
ecdef330 | 563 | CRM_Utils_JSON::output($subTypes); |
6a488035 TO |
564 | } |
565 | ||
00be9182 | 566 | public static function buildDedupeRules() { |
6a488035 TO |
567 | $parent = CRM_Utils_Array::value('parentId', $_REQUEST); |
568 | ||
569 | switch ($parent) { | |
570 | case 1: | |
571 | $contactType = 'Individual'; | |
572 | break; | |
573 | ||
574 | case 2: | |
575 | $contactType = 'Household'; | |
576 | break; | |
577 | ||
578 | case 4: | |
579 | $contactType = 'Organization'; | |
580 | break; | |
581 | } | |
582 | ||
583 | $dedupeRules = CRM_Dedupe_BAO_RuleGroup::getByType($contactType); | |
584 | ||
ecdef330 | 585 | CRM_Utils_JSON::output($dedupeRules); |
6a488035 TO |
586 | } |
587 | ||
588 | /** | |
fe482240 | 589 | * Function used for CiviCRM dashboard operations. |
6a488035 | 590 | */ |
00be9182 | 591 | public static function dashboard() { |
6a488035 TO |
592 | $operation = CRM_Utils_Type::escape($_REQUEST['op'], 'String'); |
593 | ||
594 | switch ($operation) { | |
595 | case 'get_widgets_by_column': | |
596 | // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration. | |
597 | // get contact id of logged in user | |
598 | ||
599 | $dashlets = CRM_Core_BAO_Dashboard::getContactDashlets(); | |
600 | break; | |
601 | ||
602 | case 'get_widget': | |
603 | $dashletID = CRM_Utils_Type::escape($_GET['id'], 'Positive'); | |
604 | ||
605 | $dashlets = CRM_Core_BAO_Dashboard::getDashletInfo($dashletID); | |
606 | break; | |
607 | ||
608 | case 'save_columns': | |
609 | CRM_Core_BAO_Dashboard::saveDashletChanges($_REQUEST['columns']); | |
610 | CRM_Utils_System::civiExit(); | |
611 | case 'delete_dashlet': | |
612 | $dashletID = CRM_Utils_Type::escape($_REQUEST['dashlet_id'], 'Positive'); | |
613 | CRM_Core_BAO_Dashboard::deleteDashlet($dashletID); | |
614 | CRM_Utils_System::civiExit(); | |
615 | } | |
616 | ||
ecdef330 | 617 | CRM_Utils_JSON::output($dashlets); |
6a488035 TO |
618 | } |
619 | ||
620 | /** | |
fe482240 | 621 | * Retrieve signature based on email id. |
6a488035 | 622 | */ |
00be9182 | 623 | public static function getSignature() { |
6a488035 | 624 | $emailID = CRM_Utils_Type::escape($_REQUEST['emailID'], 'Positive'); |
353ffa53 TO |
625 | $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}"; |
626 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
627 | |
628 | $signatures = array(); | |
629 | while ($dao->fetch()) { | |
630 | $signatures = array( | |
631 | 'signature_text' => $dao->signature_text, | |
632 | 'signature_html' => $dao->signature_html, | |
633 | ); | |
634 | } | |
635 | ||
ecdef330 | 636 | CRM_Utils_JSON::output($signatures); |
6a488035 TO |
637 | } |
638 | ||
639 | /** | |
100fef9d | 640 | * Process dupes. |
6a488035 | 641 | */ |
00be9182 | 642 | public static function processDupes() { |
6a488035 | 643 | $oper = CRM_Utils_Type::escape($_REQUEST['op'], 'String'); |
353ffa53 TO |
644 | $cid = CRM_Utils_Type::escape($_REQUEST['cid'], 'Positive'); |
645 | $oid = CRM_Utils_Type::escape($_REQUEST['oid'], 'Positive'); | |
6a488035 TO |
646 | |
647 | if (!$oper || !$cid || !$oid) { | |
648 | return; | |
649 | } | |
650 | ||
651 | $exception = new CRM_Dedupe_DAO_Exception(); | |
652 | $exception->contact_id1 = $cid; | |
653 | $exception->contact_id2 = $oid; | |
654 | //make sure contact2 > contact1. | |
655 | if ($cid > $oid) { | |
656 | $exception->contact_id1 = $oid; | |
657 | $exception->contact_id2 = $cid; | |
658 | } | |
659 | $exception->find(TRUE); | |
660 | $status = NULL; | |
661 | if ($oper == 'dupe-nondupe') { | |
662 | $status = $exception->save(); | |
663 | } | |
664 | if ($oper == 'nondupe-dupe') { | |
665 | $status = $exception->delete(); | |
666 | } | |
667 | ||
ecdef330 | 668 | CRM_Utils_JSON::output(array('status' => ($status) ? $oper : $status)); |
6a488035 TO |
669 | } |
670 | ||
00be9182 | 671 | public static function getDedupes() { |
6a488035 | 672 | |
353ffa53 TO |
673 | $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); |
674 | $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0; | |
675 | $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25; | |
676 | $sort = 'sort_name'; | |
6a488035 TO |
677 | $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc'; |
678 | ||
353ffa53 TO |
679 | $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type::escape($_REQUEST['gid'], 'Integer') : 0; |
680 | $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type::escape($_REQUEST['rgid'], 'Integer') : 0; | |
6a488035 TO |
681 | $contactType = ''; |
682 | if ($rgid) { | |
683 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type'); | |
684 | } | |
685 | ||
353ffa53 TO |
686 | $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}"; |
687 | $searchRows = array(); | |
6a488035 TO |
688 | $selectorElements = array('src', 'dst', 'weight', 'actions'); |
689 | ||
6a488035 TO |
690 | $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND |
691 | pn.entity_id2 = de.contact_id2 )"; | |
692 | $where = "de.id IS NULL"; | |
693 | ||
694 | $iFilteredTotal = $iTotal = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, $join, $where); | |
695 | $mainContacts = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where, $offset, $rowCount); | |
696 | ||
697 | foreach ($mainContacts as $mainId => $main) { | |
698 | $searchRows[$mainId]['src'] = CRM_Utils_System::href($main['srcName'], 'civicrm/contact/view', "reset=1&cid={$main['srcID']}"); | |
699 | $searchRows[$mainId]['dst'] = CRM_Utils_System::href($main['dstName'], 'civicrm/contact/view', "reset=1&cid={$main['dstID']}"); | |
700 | $searchRows[$mainId]['weight'] = CRM_Utils_Array::value('weight', $main); | |
701 | ||
a7488080 | 702 | if (!empty($main['canMerge'])) { |
6a488035 TO |
703 | $mergeParams = "reset=1&cid={$main['srcID']}&oid={$main['dstID']}&action=update&rgid={$rgid}"; |
704 | if ($gid) { | |
705 | $mergeParams .= "&gid={$gid}"; | |
706 | } | |
707 | ||
0d75c29c | 708 | $searchRows[$mainId]['actions'] = '<a class="action-item crm-hover-button" href="' . CRM_Utils_System::url('civicrm/contact/merge', $mergeParams) . '">' . ts('merge') . '</a>'; |
aeb97cc1 | 709 | $searchRows[$mainId]['actions'] .= "<a class='action-item crm-hover-button crm-notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>"; |
6a488035 TO |
710 | } |
711 | else { | |
712 | $searchRows[$mainId]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>'; | |
713 | } | |
714 | } | |
715 | ||
70dd31e2 | 716 | header('Content-Type: application/json'); |
6a488035 TO |
717 | echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
718 | ||
719 | CRM_Utils_System::civiExit(); | |
720 | } | |
721 | ||
722 | /** | |
fe482240 | 723 | * Retrieve a PDF Page Format for the PDF Letter form. |
6a488035 | 724 | */ |
00be9182 | 725 | public function pdfFormat() { |
6a488035 TO |
726 | $formatId = CRM_Utils_Type::escape($_REQUEST['formatId'], 'Integer'); |
727 | ||
728 | $pdfFormat = CRM_Core_BAO_PdfFormat::getById($formatId); | |
729 | ||
ecdef330 | 730 | CRM_Utils_JSON::output($pdfFormat); |
6a488035 TO |
731 | } |
732 | ||
733 | /** | |
fe482240 | 734 | * Retrieve Paper Size dimensions. |
6a488035 | 735 | */ |
00be9182 | 736 | public static function paperSize() { |
6a488035 TO |
737 | $paperSizeName = CRM_Utils_Type::escape($_REQUEST['paperSizeName'], 'String'); |
738 | ||
739 | $paperSize = CRM_Core_BAO_PaperSize::getByName($paperSizeName); | |
740 | ||
ecdef330 | 741 | CRM_Utils_JSON::output($paperSize); |
6a488035 TO |
742 | } |
743 | ||
aeb97cc1 CW |
744 | /** |
745 | * Used to store selected contacts across multiple pages in advanced search. | |
746 | */ | |
00be9182 | 747 | public static function selectUnselectContacts() { |
353ffa53 TO |
748 | $name = CRM_Utils_Array::value('name', $_REQUEST); |
749 | $cacheKey = CRM_Utils_Array::value('qfKey', $_REQUEST); | |
750 | $state = CRM_Utils_Array::value('state', $_REQUEST, 'checked'); | |
6a488035 TO |
751 | $variableType = CRM_Utils_Array::value('variableType', $_REQUEST, 'single'); |
752 | ||
753 | $actionToPerform = CRM_Utils_Array::value('action', $_REQUEST, 'select'); | |
754 | ||
755 | if ($variableType == 'multiple') { | |
756 | // action post value only works with multiple type variable | |
757 | if ($name) { | |
758 | //multiple names like mark_x_1-mark_x_2 where 1,2 are cids | |
759 | $elements = explode('-', $name); | |
760 | foreach ($elements as $key => $element) { | |
761 | $elements[$key] = self::_convertToId($element); | |
762 | } | |
763 | CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform, $elements); | |
764 | } | |
765 | else { | |
766 | CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform); | |
767 | } | |
768 | } | |
769 | elseif ($variableType == 'single') { | |
770 | $cId = self::_convertToId($name); | |
771 | $action = ($state == 'checked') ? 'select' : 'unselect'; | |
772 | CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $action, $cId); | |
773 | } | |
774 | $contactIds = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey); | |
775 | $countSelectionCids = count($contactIds[$cacheKey]); | |
776 | ||
777 | $arrRet = array('getCount' => $countSelectionCids); | |
ecdef330 | 778 | CRM_Utils_JSON::output($arrRet); |
6a488035 TO |
779 | } |
780 | ||
4319322b | 781 | /** |
100fef9d | 782 | * @param string $name |
4319322b EM |
783 | * |
784 | * @return string | |
785 | */ | |
00be9182 | 786 | public static function _convertToId($name) { |
6a488035 TO |
787 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { |
788 | $cId = substr($name, CRM_Core_Form::CB_PREFIX_LEN); | |
789 | } | |
790 | return $cId; | |
791 | } | |
792 | ||
00be9182 | 793 | public static function getAddressDisplay() { |
6a488035 TO |
794 | $contactId = CRM_Utils_Array::value('contact_id', $_REQUEST); |
795 | if (!$contactId) { | |
796 | $addressVal["error_message"] = "no contact id found"; | |
797 | } | |
798 | else { | |
408b79bf | 799 | $entityBlock = array( |
800 | 'contact_id' => $contactId, | |
801 | 'entity_id' => $contactId, | |
802 | ); | |
6a488035 TO |
803 | $addressVal = CRM_Core_BAO_Address::getValues($entityBlock); |
804 | } | |
805 | ||
ecdef330 | 806 | CRM_Utils_JSON::output($addressVal); |
6a488035 | 807 | } |
40458f6c | 808 | |
809 | /** | |
fe482240 | 810 | * Retrieve contact relationships. |
40458f6c | 811 | */ |
812 | public static function getContactRelationships() { | |
813 | $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); | |
814 | $context = CRM_Utils_Type::escape($_GET['context'], 'String'); | |
1b09aa03 EM |
815 | $relationship_type_id = CRM_Utils_Type::escape(CRM_Utils_Array::value('relationship_type_id', $_GET), 'Integer', |
816 | FALSE); | |
40458f6c | 817 | |
b0266403 CW |
818 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactID)) { |
819 | return CRM_Utils_System::permissionDenied(); | |
820 | } | |
821 | ||
40458f6c | 822 | $sortMapper = array( |
823 | 0 => 'relation', | |
824 | 1 => 'sort_name', | |
825 | 2 => 'start_date', | |
826 | 3 => 'end_date', | |
827 | 4 => 'city', | |
828 | 5 => 'state', | |
829 | 6 => 'email', | |
830 | 7 => 'phone', | |
831 | 8 => 'links', | |
e1cad725 | 832 | 9 => '', |
f1321272 | 833 | 10 => '', |
40458f6c | 834 | ); |
835 | ||
353ffa53 TO |
836 | $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); |
837 | $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0; | |
838 | $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25; | |
839 | $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL; | |
40458f6c | 840 | $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc'; |
841 | ||
842 | $params = $_POST; | |
843 | if ($sort && $sortOrder) { | |
844 | $params['sortBy'] = $sort . ' ' . $sortOrder; | |
845 | } | |
846 | ||
847 | $params['page'] = ($offset / $rowCount) + 1; | |
848 | $params['rp'] = $rowCount; | |
849 | ||
850 | $params['contact_id'] = $contactID; | |
851 | $params['context'] = $context; | |
4c5f31d0 | 852 | if ($relationship_type_id) { |
cdee9432 TM |
853 | $params['relationship_type_id'] = $relationship_type_id; |
854 | } | |
40458f6c | 855 | |
856 | // get the contact relationships | |
857 | $relationships = CRM_Contact_BAO_Relationship::getContactRelationshipSelector($params); | |
858 | ||
859 | $iFilteredTotal = $iTotal = $params['total']; | |
860 | $selectorElements = array( | |
861 | 'relation', | |
862 | 'name', | |
863 | 'start_date', | |
864 | 'end_date', | |
865 | 'city', | |
866 | 'state', | |
867 | 'email', | |
868 | 'phone', | |
869 | 'links', | |
e1cad725 | 870 | 'id', |
f1321272 | 871 | 'is_active', |
40458f6c | 872 | ); |
873 | ||
70dd31e2 | 874 | header('Content-Type: application/json'); |
40458f6c | 875 | echo CRM_Utils_JSON::encodeDataTableSelector($relationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
876 | CRM_Utils_System::civiExit(); | |
877 | } | |
96025800 | 878 | |
6a488035 | 879 | } |