bulk comment fixes
[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 *
188 * @return array
189 * @access public
190 *
191 */
192 static
193 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',
47f03411 214 'class' => 'no-popup',
6a488035
TO
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',
a4400196 229 'class' => 'medium-popup',
6a488035
TO
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 //end of function
248
249 /**
250 * getter for array of the parameters required for creating pager.
251 *
252 * @param
253 * @access public
254 */
255 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 //end of function
269
270 /**
271 * Returns total number of rows for the query.
272 *
273 * @param
274 *
275 * @return int Total number of rows
276 * @access public
277 */
278 function getTotalCount($action) {
279 return $this->_query->searchQuery(0, 0, NULL,
280 TRUE, FALSE,
281 FALSE, FALSE,
282 FALSE,
283 $this->_additionalClause
284 );
285 }
286
287 /**
288 * returns all the rows in the given offset and rowCount
289 *
290 * @param enum $action the action being performed
291 * @param int $offset the row number to start from
292 * @param int $rowCount the number of rows to return
293 * @param string $sort the sql string that describes the sort order
294 * @param enum $output what should the result set include (web/email/csv)
295 *
296 * @return int the total number of rows for this action
297 */
298 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
299 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
300 FALSE, FALSE,
301 FALSE, FALSE,
302 FALSE,
303 $this->_additionalClause
304 );
305 // process the result of the query
306 $rows = array();
307
308 //CRM-4418 check for view, edit, delete
309 $permissions = array(CRM_Core_Permission::VIEW);
310 if (CRM_Core_Permission::check('access all cases and activities')
311 || CRM_Core_Permission::check('access my cases and activities')
312 ) {
313 $permissions[] = CRM_Core_Permission::EDIT;
314 }
315 if (CRM_Core_Permission::check('delete in CiviCase')) {
316 $permissions[] = CRM_Core_Permission::DELETE;
317 }
318 $mask = CRM_Core_Action::mask($permissions);
319
320 $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' ");
321
322 $scheduledInfo = array();
323
324 while ($result->fetch()) {
325 $row = array();
326 // the columns we are interested in
327 foreach (self::$_properties as $property) {
328 if (isset($result->$property)) {
329 $row[$property] = $result->$property;
330 }
331 }
332
333 $isDeleted = FALSE;
334 if ($result->case_deleted) {
335 $isDeleted = TRUE;
336 $row['case_status_id'] = empty($row['case_status_id']) ? "" : $row['case_status_id'] . '<br />(deleted)';
337 }
338
339 $scheduledInfo['case_id'][] = $result->case_id;
340 $scheduledInfo['contact_id'][] = $result->contact_id;
341 $scheduledInfo['case_deleted'] = $result->case_deleted;
342 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->case_id;
343
344 $links = self::links($isDeleted, $this->_key);
345 $row['action'] = CRM_Core_Action::formLink($links['primaryActions'],
346 $mask, array(
347 'id' => $result->case_id,
348 'cid' => $result->contact_id,
349 'cxt' => $this->_context,
87dab4a4
AH
350 ),
351 ts('more'),
352 FALSE,
353 'case.selector.actions',
354 'Case',
355 $result->case_id
6a488035
TO
356 );
357 $row['moreActions'] = CRM_Core_Action::formLink(CRM_Utils_Array::value('moreActions', $links),
358 $mask, array(
359 'id' => $result->case_id,
360 'cid' => $result->contact_id,
361 'cxt' => $this->_context,
362 ),
363 ts('more'),
87dab4a4
AH
364 TRUE,
365 'case.selector.moreActions',
366 'Case',
367 $result->case_id
6a488035
TO
368 );
369
370 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
371 $result->contact_sub_type : $result->contact_type
372 );
373
374 //adding case manager to case selector.CRM-4510.
375 $caseType = CRM_Case_BAO_Case::getCaseType($result->case_id, 'name');
376 $caseManagerContact = CRM_Case_BAO_Case::getCaseManagerContact($caseType, $result->case_id);
377
378 if (!empty($caseManagerContact)) {
379 $row['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact);
380 $row['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact);
381 }
382
383 if (isset($result->case_status_id) &&
384 array_key_exists($result->case_status_id, $caseStatus)
385 ) {
386 $row['class'] = "status-urgent";
387 }
388 else {
389 $row['class'] = "status-normal";
390 }
391
392 $rows[$result->case_id] = $row;
393 }
394
395 //retrive the scheduled & recent Activity type and date for selector
396 if (!empty($scheduledInfo)) {
397 $schdeduledActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'upcoming');
398 foreach ($schdeduledActivity as $key => $value) {
399 $rows[$key]['case_scheduled_activity_date'] = $value['date'];
400 $rows[$key]['case_scheduled_activity_type'] = $value['type'];
401 }
402 $recentActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'recent');
403 foreach ($recentActivity as $key => $value) {
404 $rows[$key]['case_recent_activity_date'] = $value['date'];
405 $rows[$key]['case_recent_activity_type'] = $value['type'];
406 }
407 }
408 return $rows;
409 }
410
411 /**
412 *
413 * @return array $qill which contains an array of strings
414 * @access public
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 the action being performed
428 * @param enum $output what should the result set include (web/email/csv)
429 *
430 * @return array the column headers that need to be displayed
431 * @access public
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_id',
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 function alphabetQuery() {
488 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
489 }
490
491 function &getQuery() {
492 return $this->_query;
493 }
494
495 /**
496 * name of export file.
497 *
498 * @param string $output type of output
499 *
500 * @return string name of the file
501 */
502 function getExportFileName($output = 'csv') {
503 return ts('Case Search');
504 }
505}
506//end of class
507