3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2020
35 * This class is used to retrieve and display a range of contacts that match the given criteria.
37 class CRM_Case_Selector_Search
extends CRM_Core_Selector_Base
{
40 * This defines two actions- View and Edit.
44 public static $_links = NULL;
47 * The action links that we need to display for the browse screen.
51 private static $_actionLinks;
54 * We use desc to remind us what that column is, name is used in the tpl
58 public static $_columnHeaders;
61 * Properties of contact we're interested in displaying
64 public static $_properties = [
80 * Are we restricting ourselves to a single contact
84 protected $_single = FALSE;
87 * Are we restricting ourselves to a single contact
91 protected $_limit = NULL;
94 * What context are we being invoked from
98 protected $_context = NULL;
101 * QueryParams is the array returned by exportValues called on
102 * the HTML_QuickForm_Controller for that page.
106 public $_queryParams;
109 * Represent the type of selector
116 * The additional clause that we restrict the search with
120 protected $_additionalClause = NULL;
132 * @param array $queryParams
133 * Array of parameters for query.
134 * @param \const|int $action - action of search basic or advanced.
135 * @param string $additionalClause
136 * If the caller wants to further restrict the search (used in participations).
137 * @param bool $single
138 * Are we dealing only with one contact?.
140 * How many signers do we want returned.
142 * @param string $context
144 * @return \CRM_Case_Selector_Search
146 public function __construct(
148 $action = CRM_Core_Action
::NONE
,
149 $additionalClause = NULL,
154 // submitted form values
155 $this->_queryParams
= &$queryParams;
157 $this->_single
= $single;
158 $this->_limit
= $limit;
159 $this->_context
= $context;
161 $this->_additionalClause
= $additionalClause;
164 $this->_action
= $action;
166 $this->_query
= new CRM_Contact_BAO_Query($this->_queryParams
,
167 CRM_Case_BAO_Query
::defaultReturnProperties(CRM_Contact_BAO_Query
::MODE_CASE
,
171 CRM_Contact_BAO_Query
::MODE_CASE
174 $this->_query
->_distinctComponentClause
= " civicrm_case.id ";
175 $this->_query
->_groupByComponentClause
= " GROUP BY civicrm_case.id ";
179 * This method returns the links that are given for each search row.
180 * currently the links added for each row are
185 * @param bool $isDeleted
190 public static function &links($isDeleted = FALSE, $key = NULL) {
191 $extraParams = ($key) ?
"&key={$key}" : NULL;
195 CRM_Core_Action
::RENEW
=> [
196 'name' => ts('Restore'),
197 'url' => 'civicrm/contact/view/case',
198 'qs' => 'reset=1&action=renew&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
199 'ref' => 'restore-case',
200 'title' => ts('Restore Case'),
206 CRM_Core_Action
::VIEW
=> [
207 'name' => ts('Manage'),
208 'url' => 'civicrm/contact/view/case',
209 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=case' . $extraParams,
210 'ref' => 'manage-case',
211 'class' => 'no-popup',
212 'title' => ts('Manage Case'),
214 CRM_Core_Action
::DELETE
=> [
215 'name' => ts('Delete'),
216 'url' => 'civicrm/contact/view/case',
217 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
218 'ref' => 'delete-case',
219 'title' => ts('Delete Case'),
221 CRM_Core_Action
::UPDATE
=> [
222 'name' => ts('Assign to Another Client'),
223 'url' => 'civicrm/contact/view/case/editClient',
224 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
226 'class' => 'medium-popup',
227 'title' => ts('Assign to Another Client'),
233 foreach (self
::$_links as $key => $value) {
234 $actionLinks['primaryActions'][$key] = $value;
241 * Getter for array of the parameters required for creating pager.
244 * @param array $params
246 public function getPagerParams($action, &$params) {
247 $params['status'] = ts('Case') . ' %%StatusMessage%%';
248 $params['csvString'] = NULL;
250 $params['rowCount'] = $this->_limit
;
253 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
256 $params['buttonTop'] = 'PagerTopButton';
257 $params['buttonBottom'] = 'PagerBottomButton';
261 * Returns total number of rows for the query.
266 * Total number of rows
268 public function getTotalCount($action) {
269 return $this->_query
->searchQuery(0, 0, NULL,
273 $this->_additionalClause
278 * Returns all the rows in the given offset and rowCount.
280 * @param string $action
281 * The action being performed.
283 * The row number to start from.
284 * @param int $rowCount
285 * The number of rows to return.
286 * @param string $sort
287 * The sql string that describes the sort order.
288 * @param string $output
289 * What should the result set include (web/email/csv).
292 * the total number of rows for this action
294 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
295 $result = $this->_query
->searchQuery($offset, $rowCount, $sort,
299 $this->_additionalClause
301 // process the result of the query
304 //CRM-4418 check for view, edit, delete
305 $permissions = [CRM_Core_Permission
::VIEW
];
306 if (CRM_Core_Permission
::check('access all cases and activities')
307 || CRM_Core_Permission
::check('access my cases and activities')
309 $permissions[] = CRM_Core_Permission
::EDIT
;
311 if (CRM_Core_Permission
::check('delete in CiviCase')) {
312 $permissions[] = CRM_Core_Permission
::DELETE
;
314 $mask = CRM_Core_Action
::mask($permissions);
316 $caseStatus = CRM_Core_OptionGroup
::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' ");
320 while ($result->fetch()) {
322 // the columns we are interested in
323 foreach (self
::$_properties as $property) {
324 if (isset($result->$property)) {
325 $row[$property] = $result->$property;
330 if ($result->case_deleted
) {
332 $row['case_status_id'] = empty($row['case_status_id']) ?
"" : $row['case_status_id'] . '<br />' . ts('(deleted)');
335 $scheduledInfo['case_id'][] = $result->case_id
;
336 $scheduledInfo['contact_id'][] = $result->contact_id
;
337 $scheduledInfo['case_deleted'] = $result->case_deleted
;
338 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->case_id
;
340 $links = self
::links($isDeleted, $this->_key
);
341 $row['action'] = CRM_Core_Action
::formLink($links['primaryActions'],
343 'id' => $result->case_id
,
344 'cid' => $result->contact_id
,
345 'cxt' => $this->_context
,
349 'case.selector.actions',
354 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
357 //adding case manager to case selector.CRM-4510.
358 $caseType = CRM_Case_BAO_Case
::getCaseType($result->case_id
, 'name');
359 $row['casemanager'] = CRM_Case_BAO_Case
::getCaseManagerContact($caseType, $result->case_id
);
361 if (isset($result->case_status_id
) &&
362 array_key_exists($result->case_status_id
, $caseStatus)
364 $row['class'] = "status-urgent";
367 $row['class'] = "status-normal";
370 $rows[$result->case_id
] = $row;
373 //retrive the scheduled & recent Activity type and date for selector
374 if (!empty($scheduledInfo)) {
375 $schdeduledActivity = CRM_Case_BAO_Case
::getNextScheduledActivity($scheduledInfo, 'upcoming');
376 foreach ($schdeduledActivity as $key => $value) {
377 $rows[$key]['case_scheduled_activity_date'] = $value['date'];
378 $rows[$key]['case_scheduled_activity_type'] = $value['type'];
380 $recentActivity = CRM_Case_BAO_Case
::getNextScheduledActivity($scheduledInfo, 'recent');
381 foreach ($recentActivity as $key => $value) {
382 $rows[$key]['case_recent_activity_date'] = $value['date'];
383 $rows[$key]['case_recent_activity_type'] = $value['type'];
392 public function getQILL() {
393 return $this->_query
->qill();
397 * Returns the column headers as an array of tuples:
398 * (name, sortName (key to the sort array))
400 * @param string $action
401 * The action being performed.
402 * @param string $output
403 * What should the result set include (web/email/csv).
406 * the column headers that need to be displayed
408 public function &getColumnHeaders($action = NULL, $output = NULL) {
409 if (!isset(self
::$_columnHeaders)) {
410 self
::$_columnHeaders = [
412 'name' => ts('Subject'),
413 'direction' => CRM_Utils_Sort
::DONTCARE
,
416 'name' => ts('Status'),
417 'sort' => 'case_status',
418 'direction' => CRM_Utils_Sort
::DONTCARE
,
421 'name' => ts('Case Type'),
422 'sort' => 'case_type',
423 'direction' => CRM_Utils_Sort
::DONTCARE
,
426 'name' => ts('My Role'),
427 'sort' => 'case_role',
428 'direction' => CRM_Utils_Sort
::DONTCARE
,
431 'name' => ts('Case Manager'),
432 'direction' => CRM_Utils_Sort
::DONTCARE
,
435 'name' => ts('Most Recent'),
436 'sort' => 'case_recent_activity_date',
437 'direction' => CRM_Utils_Sort
::DONTCARE
,
440 'name' => ts('Next Sched.'),
441 'sort' => 'case_scheduled_activity_date',
442 'direction' => CRM_Utils_Sort
::DONTCARE
,
444 ['name' => ts('Actions')],
447 if (!$this->_single
) {
450 'name' => ts('Client'),
451 'sort' => 'sort_name',
452 'direction' => CRM_Utils_Sort
::ASCENDING
,
456 self
::$_columnHeaders = array_merge($pre, self
::$_columnHeaders);
459 return self
::$_columnHeaders;
465 public function alphabetQuery() {
466 return $this->_query
->alphabetQuery();
472 public function &getQuery() {
473 return $this->_query
;
477 * Name of export file.
479 * @param string $output
485 public function getExportFileName($output = 'csv') {
486 return ts('Case Search');
490 * Add the set of "actionLinks" to the case activity
493 * @param int $contactID
495 * @param string $context
496 * @param \CRM_Activity_BAO_Activity $dao
497 * @param bool $allowView
499 * @return string $linksMarkup
501 public static function addCaseActivityLinks($caseID, $contactID, $userID, $context, $dao, $allowView = TRUE) {
502 $caseDeleted = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted');
503 $actionLinks = self
::actionLinks();
504 // Check logged in user for permission.
505 if (CRM_Case_BAO_Case
::checkPermission($dao->id
, 'view', $dao->activity_type_id
, $userID)) {
506 $permissions[] = CRM_Core_Permission
::VIEW
;
509 unset($actionLinks[CRM_Core_Action
::VIEW
]);
511 if (!$dao->deleted
) {
512 // Activity is not deleted, allow user to edit/delete if they have permission
513 // hide Edit link if:
514 // 1. User does not have edit permission.
515 // 2. Activity type is NOT editable (special case activities).CRM-5871
516 if (CRM_Case_BAO_Case
::checkPermission($dao->id
, 'edit', $dao->activity_type_id
, $userID)) {
517 $permissions[] = CRM_Core_Permission
::EDIT
;
519 if (in_array($dao->activity_type_id
, CRM_Activity_BAO_Activity
::getViewOnlyActivityTypeIDs())) {
520 unset($actionLinks[CRM_Core_Action
::UPDATE
]);
522 if (CRM_Case_BAO_Case
::checkPermission($dao->id
, 'delete', $dao->activity_type_id
, $userID)) {
523 $permissions[] = CRM_Core_Permission
::DELETE
;
525 unset($actionLinks[CRM_Core_Action
::RENEW
]);
528 if ($dao->deleted
&& !$caseDeleted
529 && (CRM_Case_BAO_Case
::checkPermission($dao->id
, 'delete', $dao->activity_type_id
, $userID))) {
530 // Case is not deleted but activity is.
531 // Allow user to restore activity if they have delete permissions
532 unset($actionLinks[CRM_Core_Action
::DELETE
]);
533 $extraMask = CRM_Core_Action
::RENEW
;
535 if (!CRM_Case_BAO_Case
::checkPermission($dao->id
, 'Move To Case', $dao->activity_type_id
)) {
536 unset($actionLinks[CRM_Core_Action
::DETACH
]);
538 if (!CRM_Case_BAO_Case
::checkPermission($dao->id
, 'Copy To Case', $dao->activity_type_id
)) {
539 unset($actionLinks[CRM_Core_Action
::COPY
]);
541 $actionMask = CRM_Core_Action
::mask($permissions) |
$extraMask;
545 'cxt' => empty($context) ?
'' : "&context={$context}",
548 $linksMarkup = CRM_Core_Action
::formLink($actionLinks,
557 // if there are file attachments we will return how many and, if only one, add a link to it
558 if (!empty($dao->attachment_ids
)) {
559 $linksMarkup .= implode(' ', CRM_Core_BAO_File
::paperIconAttachment('civicrm_activity', $dao->id
));
566 * @param int $contactID
568 * @param string $context
569 * @param int $activityTypeID
570 * @param int $activityDeleted
571 * @param int $activityID
572 * @param bool $allowView
576 public static function permissionedActionLinks($caseID, $contactID, $userID, $context, $activityTypeID, $activityDeleted, $activityID, $allowView = TRUE) {
577 $caseDeleted = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted');
579 'aid' => $activityID,
581 'cxt' => empty($context) ?
'' : "&context={$context}",
584 $actionLinks = self
::actionLinks();
586 // Check logged in user for permission.
587 if (CRM_Case_BAO_Case
::checkPermission($activityID, 'view', $activityTypeID, $userID)) {
588 $permissions[] = CRM_Core_Permission
::VIEW
;
591 unset($actionLinks[CRM_Core_Action
::VIEW
]);
593 if (!$activityDeleted) {
594 // Activity is not deleted, allow user to edit/delete if they have permission
596 // hide Edit link if:
597 // 1. User does not have edit permission.
598 // 2. Activity type is NOT editable (special case activities).CRM-5871
599 if (CRM_Case_BAO_Case
::checkPermission($activityID, 'edit', $activityTypeID, $userID)) {
600 $permissions[] = CRM_Core_Permission
::EDIT
;
602 if (in_array($activityTypeID, CRM_Activity_BAO_Activity
::getViewOnlyActivityTypeIDs())) {
603 unset($actionLinks[CRM_Core_Action
::UPDATE
]);
605 if (CRM_Case_BAO_Case
::checkPermission($activityID, 'delete', $activityTypeID, $userID)) {
606 $permissions[] = CRM_Core_Permission
::DELETE
;
608 unset($actionLinks[CRM_Core_Action
::RENEW
]);
611 if ($activityDeleted && !$caseDeleted
612 && (CRM_Case_BAO_Case
::checkPermission($activityID, 'delete', $activityTypeID, $userID))) {
613 // Case is not deleted but activity is.
614 // Allow user to restore activity if they have delete permissions
615 unset($actionLinks[CRM_Core_Action
::DELETE
]);
616 $extraMask = CRM_Core_Action
::RENEW
;
618 if (!CRM_Case_BAO_Case
::checkPermission($activityID, 'Move To Case', $activityTypeID)) {
619 unset($actionLinks[CRM_Core_Action
::DETACH
]);
621 if (!CRM_Case_BAO_Case
::checkPermission($activityID, 'Copy To Case', $activityTypeID)) {
622 unset($actionLinks[CRM_Core_Action
::COPY
]);
625 $actionMask = CRM_Core_Action
::mask($permissions) |
$extraMask;
626 return CRM_Core_Action
::filterLinks($actionLinks, $actionMask, $values, 'case.activity', 'Activity', $activityID);
630 * Get the action links for this page.
634 public static function actionLinks() {
635 // check if variable _actionsLinks is populated
636 if (!isset(self
::$_actionLinks)) {
637 self
::$_actionLinks = [
638 CRM_Core_Action
::VIEW
=> [
639 'name' => ts('View'),
640 'url' => 'civicrm/case/activity/view',
641 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&aid=%%aid%%',
642 'title' => ts('View'),
644 CRM_Core_Action
::UPDATE
=> [
645 'name' => ts('Edit'),
646 'url' => 'civicrm/case/activity',
647 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&id=%%aid%%&action=update%%cxt%%',
648 'title' => ts('Edit'),
649 'icon' => 'fa-pencil',
651 CRM_Core_Action
::DELETE
=> [
652 'name' => ts('Delete'),
653 'url' => 'civicrm/case/activity',
654 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&id=%%aid%%&action=delete%%cxt%%',
655 'title' => ts('Delete'),
656 'icon' => 'fa-trash',
658 CRM_Core_Action
::RENEW
=> [
659 'name' => ts('Restore'),
660 'url' => 'civicrm/case/activity',
661 'qs' => 'reset=1&cid=%%cid%%&caseid=%%caseid%%&id=%%aid%%&action=renew%%cxt%%',
662 'title' => ts('Restore'),
665 CRM_Core_Action
::DETACH
=> [
666 'name' => ts('Move To Case'),
667 'ref' => 'move_to_case_action',
668 'title' => ts('Move To Case'),
669 'extra' => 'onclick = "Javascript:fileOnCase( \'move\', %%aid%%, %%caseid%%, this ); return false;"',
670 'icon' => 'fa-clipboard',
672 CRM_Core_Action
::COPY
=> [
673 'name' => ts('Copy To Case'),
674 'ref' => 'copy_to_case_action',
675 'title' => ts('Copy To Case'),
676 'extra' => 'onclick = "Javascript:fileOnCase( \'copy\', %%aid%%, %%caseid%%, this ); return false;"',
677 'icon' => 'fa-files-o',
681 return self
::$_actionLinks;