Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Case / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 *
138 * @param array $queryParams array of parameters for query
139 * @param int $action - action of search basic or advanced.
140 * @param string $additionalClause if the caller wants to further restrict the search (used in participations)
141 * @param boolean $single are we dealing only with one contact?
142 * @param int $limit how many signers do we want returned
143 *
144 * @return CRM_Contact_Selector
145 * @access public
146 */ function __construct(&$queryParams,
147 $action = CRM_Core_Action::NONE,
148 $additionalClause = NULL,
149 $single = FALSE,
150 $limit = NULL,
151 $context = 'search'
152 ) {
153 // submitted form values
154 $this->_queryParams = &$queryParams;
155
156 $this->_single = $single;
157 $this->_limit = $limit;
158 $this->_context = $context;
159
160 $this->_additionalClause = $additionalClause;
161
162 // type of selector
163 $this->_action = $action;
164
165 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
166 CRM_Case_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CASE,
167 FALSE
168 ),
169 NULL, FALSE, FALSE,
170 CRM_Contact_BAO_Query::MODE_CASE
171 );
172
173 $this->_query->_distinctComponentClause = " civicrm_case.id ";
174 $this->_query->_groupByComponentClause = " GROUP BY civicrm_case.id ";
175 }
176 //end of constructor
177
178 /**
179 * This method returns the links that are given for each search row.
180 * currently the links added for each row are
181 *
182 * - View
183 * - Edit
184 *
185 * @return array
186 * @access public
187 *
188 */
189 static
190 function &links($isDeleted = FALSE, $key = NULL) {
191 $extraParams = ($key) ? "&key={$key}" : NULL;
192
193 if ($isDeleted) {
194 self::$_links = array(
195 CRM_Core_Action::RENEW => array(
196 'name' => ts('Restore'),
197 'url' => 'civicrm/contact/view/case',
198 'qs' => 'reset=1&action=renew&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
199 'ref' => 'restore-case',
200 'title' => ts('Restore Case'),
201 ),
202 );
203 }
204 else {
205 self::$_links = array(
206 CRM_Core_Action::VIEW => array(
207 'name' => ts('Manage'),
208 'url' => 'civicrm/contact/view/case',
209 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=case' . $extraParams,
210 'ref' => 'manage-case',
211 'title' => ts('Manage Case'),
212 ),
213 CRM_Core_Action::DELETE => array(
214 'name' => ts('Delete'),
215 'url' => 'civicrm/contact/view/case',
216 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
217 'ref' => 'delete-case',
218 'title' => ts('Delete Case'),
219 ),
220 CRM_Core_Action::UPDATE => array(
221 'name' => ts('Assign to Another Client'),
222 'url' => 'civicrm/contact/view/case/editClient',
223 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
224 'ref' => 'reassign',
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 //end of function
243
244 /**
245 * getter for array of the parameters required for creating pager.
246 *
247 * @param
248 * @access public
249 */
250 function getPagerParams($action, &$params) {
251 $params['status'] = ts('Case') . ' %%StatusMessage%%';
252 $params['csvString'] = NULL;
253 if ($this->_limit) {
254 $params['rowCount'] = $this->_limit;
255 }
256 else {
257 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
258 }
259
260 $params['buttonTop'] = 'PagerTopButton';
261 $params['buttonBottom'] = 'PagerBottomButton';
262 }
263 //end of function
264
265 /**
266 * Returns total number of rows for the query.
267 *
268 * @param
269 *
270 * @return int Total number of rows
271 * @access public
272 */
273 function getTotalCount($action) {
274 return $this->_query->searchQuery(0, 0, NULL,
275 TRUE, FALSE,
276 FALSE, FALSE,
277 FALSE,
278 $this->_additionalClause
279 );
280 }
281
282 /**
283 * returns all the rows in the given offset and rowCount
284 *
285 * @param enum $action the action being performed
286 * @param int $offset the row number to start from
287 * @param int $rowCount the number of rows to return
288 * @param string $sort the sql string that describes the sort order
289 * @param enum $output what should the result set include (web/email/csv)
290 *
291 * @return int the total number of rows for this action
292 */
293 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
294 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
295 FALSE, FALSE,
296 FALSE, FALSE,
297 FALSE,
298 $this->_additionalClause
299 );
300 // process the result of the query
301 $rows = array();
302
303 //CRM-4418 check for view, edit, delete
304 $permissions = array(CRM_Core_Permission::VIEW);
305 if (CRM_Core_Permission::check('access all cases and activities')
306 || CRM_Core_Permission::check('access my cases and activities')
307 ) {
308 $permissions[] = CRM_Core_Permission::EDIT;
309 }
310 if (CRM_Core_Permission::check('delete in CiviCase')) {
311 $permissions[] = CRM_Core_Permission::DELETE;
312 }
313 $mask = CRM_Core_Action::mask($permissions);
314
315 $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' ");
316
317 $scheduledInfo = array();
318
319 while ($result->fetch()) {
320 $row = array();
321 // the columns we are interested in
322 foreach (self::$_properties as $property) {
323 if (isset($result->$property)) {
324 $row[$property] = $result->$property;
325 }
326 }
327
328 $isDeleted = FALSE;
329 if ($result->case_deleted) {
330 $isDeleted = TRUE;
331 $row['case_status_id'] = empty($row['case_status_id']) ? "" : $row['case_status_id'] . '<br />(deleted)';
332 }
333
334 $scheduledInfo['case_id'][] = $result->case_id;
335 $scheduledInfo['contact_id'][] = $result->contact_id;
336 $scheduledInfo['case_deleted'] = $result->case_deleted;
337 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->case_id;
338
339 $links = self::links($isDeleted, $this->_key);
340 $row['action'] = CRM_Core_Action::formLink($links['primaryActions'],
341 $mask, array(
342 'id' => $result->case_id,
343 'cid' => $result->contact_id,
344 'cxt' => $this->_context,
345 )
346 );
347 $row['moreActions'] = CRM_Core_Action::formLink(CRM_Utils_Array::value('moreActions', $links),
348 $mask, array(
349 'id' => $result->case_id,
350 'cid' => $result->contact_id,
351 'cxt' => $this->_context,
352 ),
353 ts('more'),
354 TRUE
355 );
356
357 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
358 $result->contact_sub_type : $result->contact_type
359 );
360
361 //adding case manager to case selector.CRM-4510.
362 $caseType = CRM_Case_BAO_Case::getCaseType($result->case_id, 'name');
363 $caseManagerContact = CRM_Case_BAO_Case::getCaseManagerContact($caseType, $result->case_id);
364
365 if (!empty($caseManagerContact)) {
366 $row['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact);
367 $row['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact);
368 }
369
370 if (isset($result->case_status_id) &&
371 array_key_exists($result->case_status_id, $caseStatus)
372 ) {
373 $row['class'] = "status-urgent";
374 }
375 else {
376 $row['class'] = "status-normal";
377 }
378
379 $rows[$result->case_id] = $row;
380 }
381
382 //retrive the scheduled & recent Activity type and date for selector
383 if (!empty($scheduledInfo)) {
384 $schdeduledActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'upcoming');
385 foreach ($schdeduledActivity as $key => $value) {
386 $rows[$key]['case_scheduled_activity_date'] = $value['date'];
387 $rows[$key]['case_scheduled_activity_type'] = $value['type'];
388 }
389 $recentActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'recent');
390 foreach ($recentActivity as $key => $value) {
391 $rows[$key]['case_recent_activity_date'] = $value['date'];
392 $rows[$key]['case_recent_activity_type'] = $value['type'];
393 }
394 }
395 return $rows;
396 }
397
398 /**
399 *
400 * @return array $qill which contains an array of strings
401 * @access public
402 */
403
404 // the current internationalisation is bad, but should more or less work
405 // for most of "European" languages
406 public function getQILL() {
407 return $this->_query->qill();
408 }
409
410 /**
411 * returns the column headers as an array of tuples:
412 * (name, sortName (key to the sort array))
413 *
414 * @param string $action the action being performed
415 * @param enum $output what should the result set include (web/email/csv)
416 *
417 * @return array the column headers that need to be displayed
418 * @access public
419 */
420 public function &getColumnHeaders($action = NULL, $output = NULL) {
421 if (!isset(self::$_columnHeaders)) {
422 self::$_columnHeaders = array(
423 array(
424 'name' => ts('Subject'),
425 'direction' => CRM_Utils_Sort::DONTCARE,
426 ),
427 array(
428 'name' => ts('Status'),
429 'sort' => 'case_status',
430 'direction' => CRM_Utils_Sort::DONTCARE,
431 ),
432 array(
433 'name' => ts('Case Type'),
434 'sort' => 'case_type_id',
435 'direction' => CRM_Utils_Sort::DONTCARE,
436 ),
437 array(
438 'name' => ts('My Role'),
439 'sort' => 'case_role',
440 'direction' => CRM_Utils_Sort::DONTCARE,
441 ),
442 array(
443 'name' => ts('Case Manager'),
444 'direction' => CRM_Utils_Sort::DONTCARE,
445 ),
446 array(
447 'name' => ts('Most Recent'),
448 'sort' => 'case_recent_activity_date',
449 'direction' => CRM_Utils_Sort::DONTCARE,
450 ),
451 array(
452 'name' => ts('Next Sched.'),
453 'sort' => 'case_scheduled_activity_date',
454 'direction' => CRM_Utils_Sort::DONTCARE,
455 ),
456 array('name' => ts('Actions')),
457 );
458
459 if (!$this->_single) {
460 $pre = array(
461 array(
462 'name' => ts('Client'),
463 'sort' => 'sort_name',
464 'direction' => CRM_Utils_Sort::ASCENDING,
465 ),
466 );
467
468 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
469 }
470 }
471 return self::$_columnHeaders;
472 }
473
474 function alphabetQuery() {
475 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
476 }
477
478 function &getQuery() {
479 return $this->_query;
480 }
481
482 /**
483 * name of export file.
484 *
485 * @param string $output type of output
486 *
487 * @return string name of the file
488 */
489 function getExportFileName($output = 'csv') {
490 return ts('Case Search');
491 }
492}
493//end of class
494