Merge pull request #3428 from jitendrapurohit/CRM-14222
[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 /**
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 * @access protected
84 * @var boolean
85 */
86 protected $_single = FALSE;
87
88 /**
89 * are we restricting ourselves to a single contact
90 *
91 * @access protected
92 * @var boolean
93 */
94 protected $_limit = NULL;
95
96 /**
97 * what context are we being invoked from
98 *
99 * @access protected
100 * @var string
101 */
102 protected $_context = NULL;
103
104 /**
105 * queryParams is the array returned by exportValues called on
106 * the HTML_QuickForm_Controller for that page.
107 *
108 * @var array
109 * @access protected
110 */
111 public $_queryParams;
112
113 /**
114 * represent the type of selector
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 }
179 //end of constructor
180
181 /**
182 * This method returns the links that are given for each search row.
183 * currently the links added for each row are
184 *
185 * - View
186 * - Edit
187 *
da6b46f4
EM
188 * @param bool $isDeleted
189 * @param null $key
190 *
6a488035
TO
191 * @return array
192 * @access public
6a488035
TO
193 */
194 static
195 function &links($isDeleted = FALSE, $key = NULL) {
196 $extraParams = ($key) ? "&key={$key}" : NULL;
197
198 if ($isDeleted) {
199 self::$_links = array(
200 CRM_Core_Action::RENEW => array(
201 'name' => ts('Restore'),
202 'url' => 'civicrm/contact/view/case',
203 'qs' => 'reset=1&action=renew&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
204 'ref' => 'restore-case',
205 'title' => ts('Restore Case'),
206 ),
207 );
208 }
209 else {
210 self::$_links = array(
211 CRM_Core_Action::VIEW => array(
212 'name' => ts('Manage'),
213 'url' => 'civicrm/contact/view/case',
214 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=case' . $extraParams,
215 'ref' => 'manage-case',
47f03411 216 'class' => 'no-popup',
6a488035
TO
217 'title' => ts('Manage Case'),
218 ),
219 CRM_Core_Action::DELETE => array(
220 'name' => ts('Delete'),
221 'url' => 'civicrm/contact/view/case',
222 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
223 'ref' => 'delete-case',
224 'title' => ts('Delete Case'),
225 ),
226 CRM_Core_Action::UPDATE => array(
227 'name' => ts('Assign to Another Client'),
228 'url' => 'civicrm/contact/view/case/editClient',
229 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
230 'ref' => 'reassign',
a4400196 231 'class' => 'medium-popup',
6a488035
TO
232 'title' => ts('Assign to Another Client'),
233 ),
234 );
235 }
236
237 $actionLinks = array();
238 foreach (self::$_links as $key => $value) {
239 if ($value['ref'] == 'reassign') {
240 $actionLinks['moreActions'][$key] = $value;
241 }
242 else {
243 $actionLinks['primaryActions'][$key] = $value;
244 }
245 }
246
247 return $actionLinks;
248 }
249 //end of function
250
251 /**
252 * getter for array of the parameters required for creating pager.
253 *
fd31fa4c
EM
254 * @param $action
255 * @param $params
256 *
257 * @internal param $
6a488035
TO
258 * @access public
259 */
260 function getPagerParams($action, &$params) {
261 $params['status'] = ts('Case') . ' %%StatusMessage%%';
262 $params['csvString'] = NULL;
263 if ($this->_limit) {
264 $params['rowCount'] = $this->_limit;
265 }
266 else {
267 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
268 }
269
270 $params['buttonTop'] = 'PagerTopButton';
271 $params['buttonBottom'] = 'PagerBottomButton';
272 }
273 //end of function
274
275 /**
276 * Returns total number of rows for the query.
277 *
278 * @param
279 *
280 * @return int Total number of rows
281 * @access public
282 */
283 function getTotalCount($action) {
284 return $this->_query->searchQuery(0, 0, NULL,
285 TRUE, FALSE,
286 FALSE, FALSE,
287 FALSE,
288 $this->_additionalClause
289 );
290 }
291
292 /**
293 * returns all the rows in the given offset and rowCount
294 *
295 * @param enum $action the action being performed
296 * @param int $offset the row number to start from
297 * @param int $rowCount the number of rows to return
298 * @param string $sort the sql string that describes the sort order
299 * @param enum $output what should the result set include (web/email/csv)
300 *
301 * @return int the total number of rows for this action
302 */
303 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
304 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
305 FALSE, FALSE,
306 FALSE, FALSE,
307 FALSE,
308 $this->_additionalClause
309 );
310 // process the result of the query
311 $rows = array();
312
313 //CRM-4418 check for view, edit, delete
314 $permissions = array(CRM_Core_Permission::VIEW);
315 if (CRM_Core_Permission::check('access all cases and activities')
316 || CRM_Core_Permission::check('access my cases and activities')
317 ) {
318 $permissions[] = CRM_Core_Permission::EDIT;
319 }
320 if (CRM_Core_Permission::check('delete in CiviCase')) {
321 $permissions[] = CRM_Core_Permission::DELETE;
322 }
323 $mask = CRM_Core_Action::mask($permissions);
324
325 $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' ");
326
327 $scheduledInfo = array();
328
329 while ($result->fetch()) {
330 $row = array();
331 // the columns we are interested in
332 foreach (self::$_properties as $property) {
333 if (isset($result->$property)) {
334 $row[$property] = $result->$property;
335 }
336 }
337
338 $isDeleted = FALSE;
339 if ($result->case_deleted) {
340 $isDeleted = TRUE;
341 $row['case_status_id'] = empty($row['case_status_id']) ? "" : $row['case_status_id'] . '<br />(deleted)';
342 }
343
344 $scheduledInfo['case_id'][] = $result->case_id;
345 $scheduledInfo['contact_id'][] = $result->contact_id;
346 $scheduledInfo['case_deleted'] = $result->case_deleted;
347 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->case_id;
348
349 $links = self::links($isDeleted, $this->_key);
350 $row['action'] = CRM_Core_Action::formLink($links['primaryActions'],
351 $mask, array(
352 'id' => $result->case_id,
353 'cid' => $result->contact_id,
354 'cxt' => $this->_context,
87dab4a4
AH
355 ),
356 ts('more'),
357 FALSE,
358 'case.selector.actions',
359 'Case',
360 $result->case_id
6a488035
TO
361 );
362 $row['moreActions'] = CRM_Core_Action::formLink(CRM_Utils_Array::value('moreActions', $links),
363 $mask, array(
364 'id' => $result->case_id,
365 'cid' => $result->contact_id,
366 'cxt' => $this->_context,
367 ),
368 ts('more'),
87dab4a4
AH
369 TRUE,
370 'case.selector.moreActions',
371 'Case',
372 $result->case_id
6a488035
TO
373 );
374
375 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
376 $result->contact_sub_type : $result->contact_type
377 );
378
379 //adding case manager to case selector.CRM-4510.
380 $caseType = CRM_Case_BAO_Case::getCaseType($result->case_id, 'name');
381 $caseManagerContact = CRM_Case_BAO_Case::getCaseManagerContact($caseType, $result->case_id);
382
383 if (!empty($caseManagerContact)) {
384 $row['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact);
385 $row['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact);
386 }
387
388 if (isset($result->case_status_id) &&
389 array_key_exists($result->case_status_id, $caseStatus)
390 ) {
391 $row['class'] = "status-urgent";
392 }
393 else {
394 $row['class'] = "status-normal";
395 }
396
397 $rows[$result->case_id] = $row;
398 }
399
400 //retrive the scheduled & recent Activity type and date for selector
401 if (!empty($scheduledInfo)) {
402 $schdeduledActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'upcoming');
403 foreach ($schdeduledActivity as $key => $value) {
404 $rows[$key]['case_scheduled_activity_date'] = $value['date'];
405 $rows[$key]['case_scheduled_activity_type'] = $value['type'];
406 }
407 $recentActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'recent');
408 foreach ($recentActivity as $key => $value) {
409 $rows[$key]['case_recent_activity_date'] = $value['date'];
410 $rows[$key]['case_recent_activity_type'] = $value['type'];
411 }
412 }
413 return $rows;
414 }
415
416 /**
417 *
418 * @return array $qill which contains an array of strings
419 * @access public
420 */
421
422 // the current internationalisation is bad, but should more or less work
423 // for most of "European" languages
424 public function getQILL() {
425 return $this->_query->qill();
426 }
427
428 /**
429 * returns the column headers as an array of tuples:
430 * (name, sortName (key to the sort array))
431 *
432 * @param string $action the action being performed
433 * @param enum $output what should the result set include (web/email/csv)
434 *
435 * @return array the column headers that need to be displayed
436 * @access public
437 */
438 public function &getColumnHeaders($action = NULL, $output = NULL) {
439 if (!isset(self::$_columnHeaders)) {
440 self::$_columnHeaders = array(
441 array(
442 'name' => ts('Subject'),
443 'direction' => CRM_Utils_Sort::DONTCARE,
444 ),
445 array(
446 'name' => ts('Status'),
447 'sort' => 'case_status',
448 'direction' => CRM_Utils_Sort::DONTCARE,
449 ),
450 array(
451 'name' => ts('Case Type'),
452 'sort' => 'case_type_id',
453 'direction' => CRM_Utils_Sort::DONTCARE,
454 ),
455 array(
456 'name' => ts('My Role'),
457 'sort' => 'case_role',
458 'direction' => CRM_Utils_Sort::DONTCARE,
459 ),
460 array(
461 'name' => ts('Case Manager'),
462 'direction' => CRM_Utils_Sort::DONTCARE,
463 ),
464 array(
465 'name' => ts('Most Recent'),
466 'sort' => 'case_recent_activity_date',
467 'direction' => CRM_Utils_Sort::DONTCARE,
468 ),
469 array(
470 'name' => ts('Next Sched.'),
471 'sort' => 'case_scheduled_activity_date',
472 'direction' => CRM_Utils_Sort::DONTCARE,
473 ),
474 array('name' => ts('Actions')),
475 );
476
477 if (!$this->_single) {
478 $pre = array(
479 array(
480 'name' => ts('Client'),
481 'sort' => 'sort_name',
482 'direction' => CRM_Utils_Sort::ASCENDING,
483 ),
484 );
485
486 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
487 }
488 }
489 return self::$_columnHeaders;
490 }
491
4c6ce474
EM
492 /**
493 * @return mixed
494 */
6a488035
TO
495 function alphabetQuery() {
496 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
497 }
498
4c6ce474
EM
499 /**
500 * @return string
501 */
6a488035
TO
502 function &getQuery() {
503 return $this->_query;
504 }
505
506 /**
507 * name of export file.
508 *
509 * @param string $output type of output
510 *
511 * @return string name of the file
512 */
513 function getExportFileName($output = 'csv') {
514 return ts('Case Search');
515 }
516}
517//end of class
518