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