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