remove some @access from phpdoc
[civicrm-core.git] / CRM / Case / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42class CRM_Case_Selector_Search extends CRM_Core_Selector_Base {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
6a488035
TO
48 */
49 static $_links = NULL;
50
51 /**
100fef9d 52 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
53 *
54 * @var array
6a488035
TO
55 */
56 static $_columnHeaders;
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
6a488035
TO
61 */
62 static $_properties = array(
63 'contact_id',
64 'contact_type',
65 'sort_name',
66 'display_name',
67 'case_id',
68 'case_subject',
69 'case_status_id',
70 'case_status',
71 'case_type_id',
72 'case_type',
73 'case_role',
74 'phone',
75 );
76
77 /**
100fef9d 78 * Are we restricting ourselves to a single contact
6a488035 79 *
6a488035
TO
80 * @var boolean
81 */
82 protected $_single = FALSE;
83
84 /**
100fef9d 85 * Are we restricting ourselves to a single contact
6a488035 86 *
6a488035
TO
87 * @var boolean
88 */
89 protected $_limit = NULL;
90
91 /**
100fef9d 92 * What context are we being invoked from
6a488035 93 *
6a488035
TO
94 * @var string
95 */
96 protected $_context = NULL;
97
98 /**
100fef9d 99 * QueryParams is the array returned by exportValues called on
6a488035
TO
100 * the HTML_QuickForm_Controller for that page.
101 *
102 * @var array
6a488035
TO
103 */
104 public $_queryParams;
105
106 /**
100fef9d 107 * Represent the type of selector
6a488035
TO
108 *
109 * @var int
6a488035
TO
110 */
111 protected $_action;
112
113 /**
114 * The additional clause that we restrict the search with
115 *
116 * @var string
117 */
118 protected $_additionalClause = NULL;
119
120 /**
121 * The query object
122 *
123 * @var string
124 */
125 protected $_query;
126
127 /**
128 * Class constructor
129 *
64bd5a0e
TO
130 * @param array $queryParams
131 * Array of parameters for query.
dd244018 132 * @param \const|int $action - action of search basic or advanced.
64bd5a0e
TO
133 * @param string $additionalClause
134 * If the caller wants to further restrict the search (used in participations).
135 * @param bool $single
136 * Are we dealing only with one contact?.
137 * @param int $limit
138 * How many signers do we want returned.
6a488035 139 *
dd244018
EM
140 * @param string $context
141 *
142 * @return \CRM_Case_Selector_Search
dd244018 143 */
28d4d481
TO
144 function __construct(
145 &$queryParams,
e547f744 146 $action = CRM_Core_Action::NONE,
6a488035 147 $additionalClause = NULL,
e547f744
TO
148 $single = FALSE,
149 $limit = NULL,
150 $context = 'search'
6a488035
TO
151 ) {
152 // submitted form values
153 $this->_queryParams = &$queryParams;
154
353ffa53
TO
155 $this->_single = $single;
156 $this->_limit = $limit;
6a488035
TO
157 $this->_context = $context;
158
159 $this->_additionalClause = $additionalClause;
160
161 // type of selector
162 $this->_action = $action;
163
164 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
165 CRM_Case_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CASE,
166 FALSE
167 ),
168 NULL, FALSE, FALSE,
169 CRM_Contact_BAO_Query::MODE_CASE
170 );
171
172 $this->_query->_distinctComponentClause = " civicrm_case.id ";
173 $this->_query->_groupByComponentClause = " GROUP BY civicrm_case.id ";
174 }
6a488035
TO
175
176 /**
177 * This method returns the links that are given for each search row.
178 * currently the links added for each row are
179 *
180 * - View
181 * - Edit
182 *
da6b46f4
EM
183 * @param bool $isDeleted
184 * @param null $key
185 *
6a488035 186 * @return array
6a488035
TO
187 */
188 static
00be9182 189 public function &links($isDeleted = FALSE, $key = NULL) {
6a488035
TO
190 $extraParams = ($key) ? "&key={$key}" : NULL;
191
192 if ($isDeleted) {
193 self::$_links = array(
194 CRM_Core_Action::RENEW => array(
195 'name' => ts('Restore'),
196 'url' => 'civicrm/contact/view/case',
197 'qs' => 'reset=1&action=renew&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
198 'ref' => 'restore-case',
199 'title' => ts('Restore Case'),
200 ),
201 );
202 }
203 else {
204 self::$_links = array(
205 CRM_Core_Action::VIEW => array(
206 'name' => ts('Manage'),
207 'url' => 'civicrm/contact/view/case',
208 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=case' . $extraParams,
209 'ref' => 'manage-case',
47f03411 210 'class' => 'no-popup',
6a488035
TO
211 'title' => ts('Manage Case'),
212 ),
213 CRM_Core_Action::DELETE => array(
214 'name' => ts('Delete'),
215 'url' => 'civicrm/contact/view/case',
216 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
217 'ref' => 'delete-case',
218 'title' => ts('Delete Case'),
219 ),
220 CRM_Core_Action::UPDATE => array(
221 'name' => ts('Assign to Another Client'),
222 'url' => 'civicrm/contact/view/case/editClient',
223 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
224 'ref' => 'reassign',
a4400196 225 'class' => 'medium-popup',
6a488035
TO
226 'title' => ts('Assign to Another Client'),
227 ),
228 );
229 }
230
231 $actionLinks = array();
232 foreach (self::$_links as $key => $value) {
233 if ($value['ref'] == 'reassign') {
234 $actionLinks['moreActions'][$key] = $value;
235 }
236 else {
237 $actionLinks['primaryActions'][$key] = $value;
238 }
239 }
240
241 return $actionLinks;
242 }
6a488035
TO
243
244 /**
100fef9d 245 * Getter for array of the parameters required for creating pager.
6a488035 246 *
fd31fa4c 247 * @param $action
c490a46a 248 * @param array $params
6a488035 249 */
00be9182 250 public function getPagerParams($action, &$params) {
6a488035
TO
251 $params['status'] = ts('Case') . ' %%StatusMessage%%';
252 $params['csvString'] = NULL;
253 if ($this->_limit) {
254 $params['rowCount'] = $this->_limit;
255 }
256 else {
257 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
258 }
259
260 $params['buttonTop'] = 'PagerTopButton';
261 $params['buttonBottom'] = 'PagerBottomButton';
262 }
6a488035
TO
263
264 /**
265 * Returns total number of rows for the query.
266 *
267 * @param
268 *
a6c01b45
CW
269 * @return int
270 * Total number of rows
6a488035 271 */
00be9182 272 public function getTotalCount($action) {
6a488035
TO
273 return $this->_query->searchQuery(0, 0, NULL,
274 TRUE, FALSE,
275 FALSE, FALSE,
276 FALSE,
277 $this->_additionalClause
278 );
279 }
280
281 /**
100fef9d 282 * Returns all the rows in the given offset and rowCount
6a488035 283 *
3f8d2862 284 * @param string $action
64bd5a0e
TO
285 * The action being performed.
286 * @param int $offset
287 * The row number to start from.
288 * @param int $rowCount
289 * The number of rows to return.
290 * @param string $sort
291 * The sql string that describes the sort order.
3f8d2862 292 * @param string $output
64bd5a0e 293 * What should the result set include (web/email/csv).
6a488035 294 *
a6c01b45
CW
295 * @return int
296 * the total number of rows for this action
6a488035 297 */
00be9182 298 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
299 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
300 FALSE, FALSE,
301 FALSE, FALSE,
302 FALSE,
303 $this->_additionalClause
304 );
305 // process the result of the query
306 $rows = array();
307
308 //CRM-4418 check for view, edit, delete
309 $permissions = array(CRM_Core_Permission::VIEW);
310 if (CRM_Core_Permission::check('access all cases and activities')
311 || CRM_Core_Permission::check('access my cases and activities')
312 ) {
313 $permissions[] = CRM_Core_Permission::EDIT;
314 }
315 if (CRM_Core_Permission::check('delete in CiviCase')) {
316 $permissions[] = CRM_Core_Permission::DELETE;
317 }
318 $mask = CRM_Core_Action::mask($permissions);
319
320 $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' ");
321
322 $scheduledInfo = array();
323
324 while ($result->fetch()) {
325 $row = array();
326 // the columns we are interested in
327 foreach (self::$_properties as $property) {
328 if (isset($result->$property)) {
329 $row[$property] = $result->$property;
330 }
331 }
332
333 $isDeleted = FALSE;
334 if ($result->case_deleted) {
335 $isDeleted = TRUE;
336 $row['case_status_id'] = empty($row['case_status_id']) ? "" : $row['case_status_id'] . '<br />(deleted)';
337 }
338
339 $scheduledInfo['case_id'][] = $result->case_id;
340 $scheduledInfo['contact_id'][] = $result->contact_id;
341 $scheduledInfo['case_deleted'] = $result->case_deleted;
342 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->case_id;
343
344 $links = self::links($isDeleted, $this->_key);
345 $row['action'] = CRM_Core_Action::formLink($links['primaryActions'],
346 $mask, array(
347 'id' => $result->case_id,
348 'cid' => $result->contact_id,
349 'cxt' => $this->_context,
87dab4a4
AH
350 ),
351 ts('more'),
352 FALSE,
353 'case.selector.actions',
354 'Case',
355 $result->case_id
6a488035
TO
356 );
357 $row['moreActions'] = CRM_Core_Action::formLink(CRM_Utils_Array::value('moreActions', $links),
358 $mask, array(
359 'id' => $result->case_id,
360 'cid' => $result->contact_id,
361 'cxt' => $this->_context,
362 ),
363 ts('more'),
87dab4a4
AH
364 TRUE,
365 'case.selector.moreActions',
366 'Case',
367 $result->case_id
6a488035
TO
368 );
369
e547f744 370 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type
6a488035
TO
371 );
372
373 //adding case manager to case selector.CRM-4510.
374 $caseType = CRM_Case_BAO_Case::getCaseType($result->case_id, 'name');
375 $caseManagerContact = CRM_Case_BAO_Case::getCaseManagerContact($caseType, $result->case_id);
376
377 if (!empty($caseManagerContact)) {
378 $row['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact);
379 $row['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact);
380 }
381
382 if (isset($result->case_status_id) &&
383 array_key_exists($result->case_status_id, $caseStatus)
384 ) {
385 $row['class'] = "status-urgent";
386 }
387 else {
388 $row['class'] = "status-normal";
389 }
390
391 $rows[$result->case_id] = $row;
392 }
393
394 //retrive the scheduled & recent Activity type and date for selector
395 if (!empty($scheduledInfo)) {
396 $schdeduledActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'upcoming');
397 foreach ($schdeduledActivity as $key => $value) {
398 $rows[$key]['case_scheduled_activity_date'] = $value['date'];
399 $rows[$key]['case_scheduled_activity_type'] = $value['type'];
400 }
401 $recentActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'recent');
402 foreach ($recentActivity as $key => $value) {
403 $rows[$key]['case_recent_activity_date'] = $value['date'];
404 $rows[$key]['case_recent_activity_type'] = $value['type'];
405 }
406 }
407 return $rows;
408 }
409
410 /**
1054415f 411 * @inheritDoc
6a488035 412 */
6a488035
TO
413 public function getQILL() {
414 return $this->_query->qill();
415 }
416
417 /**
100fef9d 418 * Returns the column headers as an array of tuples:
6a488035
TO
419 * (name, sortName (key to the sort array))
420 *
64bd5a0e
TO
421 * @param string $action
422 * The action being performed.
3f8d2862 423 * @param string $output
64bd5a0e 424 * What should the result set include (web/email/csv).
6a488035 425 *
a6c01b45
CW
426 * @return array
427 * the column headers that need to be displayed
6a488035
TO
428 */
429 public function &getColumnHeaders($action = NULL, $output = NULL) {
430 if (!isset(self::$_columnHeaders)) {
431 self::$_columnHeaders = array(
432 array(
433 'name' => ts('Subject'),
434 'direction' => CRM_Utils_Sort::DONTCARE,
435 ),
436 array(
437 'name' => ts('Status'),
438 'sort' => 'case_status',
439 'direction' => CRM_Utils_Sort::DONTCARE,
440 ),
441 array(
442 'name' => ts('Case Type'),
33a5a53d 443 'sort' => 'case_type',
6a488035
TO
444 'direction' => CRM_Utils_Sort::DONTCARE,
445 ),
446 array(
447 'name' => ts('My Role'),
448 'sort' => 'case_role',
449 'direction' => CRM_Utils_Sort::DONTCARE,
450 ),
451 array(
452 'name' => ts('Case Manager'),
453 'direction' => CRM_Utils_Sort::DONTCARE,
454 ),
455 array(
456 'name' => ts('Most Recent'),
457 'sort' => 'case_recent_activity_date',
458 'direction' => CRM_Utils_Sort::DONTCARE,
459 ),
460 array(
461 'name' => ts('Next Sched.'),
462 'sort' => 'case_scheduled_activity_date',
463 'direction' => CRM_Utils_Sort::DONTCARE,
464 ),
465 array('name' => ts('Actions')),
466 );
467
468 if (!$this->_single) {
469 $pre = array(
470 array(
471 'name' => ts('Client'),
472 'sort' => 'sort_name',
473 'direction' => CRM_Utils_Sort::ASCENDING,
474 ),
475 );
476
477 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
478 }
479 }
480 return self::$_columnHeaders;
481 }
482
4c6ce474
EM
483 /**
484 * @return mixed
485 */
00be9182 486 public function alphabetQuery() {
6a488035
TO
487 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
488 }
489
4c6ce474
EM
490 /**
491 * @return string
492 */
00be9182 493 public function &getQuery() {
6a488035
TO
494 return $this->_query;
495 }
496
497 /**
100fef9d 498 * Name of export file.
6a488035 499 *
64bd5a0e
TO
500 * @param string $output
501 * Type of output.
6a488035 502 *
a6c01b45
CW
503 * @return string
504 * name of the file
6a488035 505 */
00be9182 506 public function getExportFileName($output = 'csv') {
6a488035
TO
507 return ts('Case Search');
508 }
509}