3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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_Core_DAO $dao
499 * HTML formatted Link
501 public static function addCaseActivityLinks($caseID, $contactID, $userID, $context, $dao) {
502 // FIXME: Why are we not using CRM_Core_Action for these links? This is too much manual work and likely to get out-of-sync with core markup.
503 $caseActivityId = $dao->id
;
504 $allowView = CRM_Case_BAO_Case
::checkPermission($caseActivityId, 'view', $dao->activity_type_id
, $userID);
505 $allowEdit = CRM_Case_BAO_Case
::checkPermission($caseActivityId, 'edit', $dao->activity_type_id
, $userID);
506 $allowDelete = CRM_Case_BAO_Case
::checkPermission($caseActivityId, 'delete', $dao->activity_type_id
, $userID);
507 $emailActivityTypeIDs = [
508 'Email' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email'),
509 'Inbound Email' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email'),
511 $url = CRM_Utils_System
::url("civicrm/case/activity",
512 "reset=1&cid={$contactID}&caseid={$caseID}", FALSE, NULL, FALSE
515 if ($context == 'fulltext') {
516 $contextUrl = "&context={$context}";
518 $editUrl = "{$url}&action=update{$contextUrl}";
519 $deleteUrl = "{$url}&action=delete{$contextUrl}";
520 $restoreUrl = "{$url}&action=renew{$contextUrl}";
521 $viewTitle = ts('View activity');
522 $caseDeleted = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted');
525 $css = 'class="action-item crm-hover-button"';
527 $viewUrl = CRM_Utils_System
::url('civicrm/case/activity/view', array('cid' => $contactID, 'aid' => $caseActivityId));
528 $url = '<a ' . str_replace('action-item', 'action-item medium-pop-up', $css) . 'href="' . $viewUrl . '" title="' . $viewTitle . '">' . ts('View') . '</a>';
530 $additionalUrl = "&id={$caseActivityId}";
531 if (!$dao->deleted
) {
532 //hide edit link of activity type email.CRM-4530.
533 if (!in_array($dao->type
, $emailActivityTypeIDs)) {
534 //hide Edit link if activity type is NOT editable (special case activities).CRM-5871
536 $url .= '<a ' . $css . ' href="' . $editUrl . $additionalUrl . '">' . ts('Edit') . '</a> ';
540 $url .= ' <a ' . str_replace('action-item', 'action-item small-popup', $css) . ' href="' . $deleteUrl . $additionalUrl . '">' . ts('Delete') . '</a>';
543 elseif (!$caseDeleted) {
544 $url = ' <a ' . $css . ' href="' . $restoreUrl . $additionalUrl . '">' . ts('Restore') . '</a>';
547 //check for operations.
548 if (CRM_Case_BAO_Case
::checkPermission($caseActivityId, 'Move To Case', $dao->activity_type_id
)) {
549 $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'move\',' . $caseActivityId . ', ' . $caseID . ', this ); return false;">' . ts('Move To Case') . '</a> ';
551 if (CRM_Case_BAO_Case
::checkPermission($caseActivityId, 'Copy To Case', $dao->activity_type_id
)) {
552 $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'copy\',' . $caseActivityId . ',' . $caseID . ', this ); return false;">' . ts('Copy To Case') . '</a> ';
554 // if there are file attachments we will return how many and, if only one, add a link to it
555 if (!empty($dao->attachment_ids
)) {
556 $url .= implode(' ', CRM_Core_BAO_File
::paperIconAttachment('civicrm_activity', $caseActivityId));