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