INFRA-132 - Comment format cleanup
[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 public function getPagerParams($action, &$params) {
255 $params['status'] = ts('Case') . ' %%StatusMessage%%';
256 $params['csvString'] = NULL;
257 if ($this->_limit) {
258 $params['rowCount'] = $this->_limit;
259 }
260 else {
261 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
262 }
263
264 $params['buttonTop'] = 'PagerTopButton';
265 $params['buttonBottom'] = 'PagerBottomButton';
266 }
267
268 /**
269 * Returns total number of rows for the query.
270 *
271 * @param
272 *
273 * @return int
274 * 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 string $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 string $output
297 * What should the result set include (web/email/csv).
298 *
299 * @return int
300 * the total number of rows for this action
301 */
302 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
303 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
304 FALSE, FALSE,
305 FALSE, FALSE,
306 FALSE,
307 $this->_additionalClause
308 );
309 // process the result of the query
310 $rows = array();
311
312 //CRM-4418 check for view, edit, delete
313 $permissions = array(CRM_Core_Permission::VIEW);
314 if (CRM_Core_Permission::check('access all cases and activities')
315 || CRM_Core_Permission::check('access my cases and activities')
316 ) {
317 $permissions[] = CRM_Core_Permission::EDIT;
318 }
319 if (CRM_Core_Permission::check('delete in CiviCase')) {
320 $permissions[] = CRM_Core_Permission::DELETE;
321 }
322 $mask = CRM_Core_Action::mask($permissions);
323
324 $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' ");
325
326 $scheduledInfo = array();
327
328 while ($result->fetch()) {
329 $row = array();
330 // the columns we are interested in
331 foreach (self::$_properties as $property) {
332 if (isset($result->$property)) {
333 $row[$property] = $result->$property;
334 }
335 }
336
337 $isDeleted = FALSE;
338 if ($result->case_deleted) {
339 $isDeleted = TRUE;
340 $row['case_status_id'] = empty($row['case_status_id']) ? "" : $row['case_status_id'] . '<br />(deleted)';
341 }
342
343 $scheduledInfo['case_id'][] = $result->case_id;
344 $scheduledInfo['contact_id'][] = $result->contact_id;
345 $scheduledInfo['case_deleted'] = $result->case_deleted;
346 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->case_id;
347
348 $links = self::links($isDeleted, $this->_key);
349 $row['action'] = CRM_Core_Action::formLink($links['primaryActions'],
350 $mask, array(
351 'id' => $result->case_id,
352 'cid' => $result->contact_id,
353 'cxt' => $this->_context,
354 ),
355 ts('more'),
356 FALSE,
357 'case.selector.actions',
358 'Case',
359 $result->case_id
360 );
361 $row['moreActions'] = CRM_Core_Action::formLink(CRM_Utils_Array::value('moreActions', $links),
362 $mask, array(
363 'id' => $result->case_id,
364 'cid' => $result->contact_id,
365 'cxt' => $this->_context,
366 ),
367 ts('more'),
368 TRUE,
369 'case.selector.moreActions',
370 'Case',
371 $result->case_id
372 );
373
374 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type
375 );
376
377 //adding case manager to case selector.CRM-4510.
378 $caseType = CRM_Case_BAO_Case::getCaseType($result->case_id, 'name');
379 $caseManagerContact = CRM_Case_BAO_Case::getCaseManagerContact($caseType, $result->case_id);
380
381 if (!empty($caseManagerContact)) {
382 $row['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact);
383 $row['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact);
384 }
385
386 if (isset($result->case_status_id) &&
387 array_key_exists($result->case_status_id, $caseStatus)
388 ) {
389 $row['class'] = "status-urgent";
390 }
391 else {
392 $row['class'] = "status-normal";
393 }
394
395 $rows[$result->case_id] = $row;
396 }
397
398 //retrive the scheduled & recent Activity type and date for selector
399 if (!empty($scheduledInfo)) {
400 $schdeduledActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'upcoming');
401 foreach ($schdeduledActivity as $key => $value) {
402 $rows[$key]['case_scheduled_activity_date'] = $value['date'];
403 $rows[$key]['case_scheduled_activity_type'] = $value['type'];
404 }
405 $recentActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'recent');
406 foreach ($recentActivity as $key => $value) {
407 $rows[$key]['case_recent_activity_date'] = $value['date'];
408 $rows[$key]['case_recent_activity_type'] = $value['type'];
409 }
410 }
411 return $rows;
412 }
413
414 /**
415 * @inheritDoc
416 */
417 public function getQILL() {
418 return $this->_query->qill();
419 }
420
421 /**
422 * Returns the column headers as an array of tuples:
423 * (name, sortName (key to the sort array))
424 *
425 * @param string $action
426 * The action being performed.
427 * @param string $output
428 * What should the result set include (web/email/csv).
429 *
430 * @return array
431 * the column headers that need to be displayed
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'),
447 'sort' => 'case_type',
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
487 /**
488 * @return mixed
489 */
490 public function alphabetQuery() {
491 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
492 }
493
494 /**
495 * @return string
496 */
497 public function &getQuery() {
498 return $this->_query;
499 }
500
501 /**
502 * Name of export file.
503 *
504 * @param string $output
505 * Type of output.
506 *
507 * @return string
508 * name of the file
509 */
510 public function getExportFileName($output = 'csv') {
511 return ts('Case Search');
512 }
513 }