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