Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
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 | */ | |
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 | |
6a488035 TO |
48 | */ |
49 | static $_links = NULL; | |
50 | ||
51 | /** | |
100fef9d | 52 | * We use desc to remind us what that column is, name is used in the tpl |
6a488035 TO |
53 | * |
54 | * @var array | |
6a488035 TO |
55 | */ |
56 | static $_columnHeaders; | |
57 | ||
58 | /** | |
59 | * Properties of contact we're interested in displaying | |
60 | * @var array | |
6a488035 TO |
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 | /** | |
100fef9d | 78 | * Are we restricting ourselves to a single contact |
6a488035 | 79 | * |
6a488035 TO |
80 | * @var boolean |
81 | */ | |
82 | protected $_single = FALSE; | |
83 | ||
84 | /** | |
100fef9d | 85 | * Are we restricting ourselves to a single contact |
6a488035 | 86 | * |
6a488035 TO |
87 | * @var boolean |
88 | */ | |
89 | protected $_limit = NULL; | |
90 | ||
91 | /** | |
100fef9d | 92 | * What context are we being invoked from |
6a488035 | 93 | * |
6a488035 TO |
94 | * @var string |
95 | */ | |
96 | protected $_context = NULL; | |
97 | ||
98 | /** | |
100fef9d | 99 | * QueryParams is the array returned by exportValues called on |
6a488035 TO |
100 | * the HTML_QuickForm_Controller for that page. |
101 | * | |
102 | * @var array | |
6a488035 TO |
103 | */ |
104 | public $_queryParams; | |
105 | ||
106 | /** | |
100fef9d | 107 | * Represent the type of selector |
6a488035 TO |
108 | * |
109 | * @var int | |
6a488035 TO |
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 | /** | |
fe482240 | 128 | * Class constructor. |
6a488035 | 129 | * |
64bd5a0e TO |
130 | * @param array $queryParams |
131 | * Array of parameters for query. | |
dd244018 | 132 | * @param \const|int $action - action of search basic or advanced. |
64bd5a0e TO |
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. | |
6a488035 | 139 | * |
dd244018 EM |
140 | * @param string $context |
141 | * | |
142 | * @return \CRM_Case_Selector_Search | |
dd244018 | 143 | */ |
8d7a9d07 | 144 | public function __construct( |
28d4d481 | 145 | &$queryParams, |
e547f744 | 146 | $action = CRM_Core_Action::NONE, |
6a488035 | 147 | $additionalClause = NULL, |
e547f744 TO |
148 | $single = FALSE, |
149 | $limit = NULL, | |
150 | $context = 'search' | |
6a488035 TO |
151 | ) { |
152 | // submitted form values | |
153 | $this->_queryParams = &$queryParams; | |
154 | ||
353ffa53 TO |
155 | $this->_single = $single; |
156 | $this->_limit = $limit; | |
6a488035 TO |
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 | } | |
6a488035 TO |
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 | * | |
da6b46f4 EM |
183 | * @param bool $isDeleted |
184 | * @param null $key | |
185 | * | |
6a488035 | 186 | * @return array |
6a488035 | 187 | */ |
8d7a9d07 | 188 | static public function &links($isDeleted = FALSE, $key = NULL) { |
6a488035 TO |
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', | |
47f03411 | 209 | 'class' => 'no-popup', |
6a488035 TO |
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', | |
a4400196 | 224 | 'class' => 'medium-popup', |
6a488035 TO |
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 | } | |
6a488035 TO |
242 | |
243 | /** | |
100fef9d | 244 | * Getter for array of the parameters required for creating pager. |
6a488035 | 245 | * |
fd31fa4c | 246 | * @param $action |
c490a46a | 247 | * @param array $params |
6a488035 | 248 | */ |
00be9182 | 249 | public function getPagerParams($action, &$params) { |
6a488035 TO |
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 | } | |
6a488035 TO |
262 | |
263 | /** | |
264 | * Returns total number of rows for the query. | |
265 | * | |
266 | * @param | |
267 | * | |
a6c01b45 CW |
268 | * @return int |
269 | * Total number of rows | |
6a488035 | 270 | */ |
00be9182 | 271 | public function getTotalCount($action) { |
6a488035 TO |
272 | return $this->_query->searchQuery(0, 0, NULL, |
273 | TRUE, FALSE, | |
274 | FALSE, FALSE, | |
275 | FALSE, | |
276 | $this->_additionalClause | |
277 | ); | |
278 | } | |
279 | ||
280 | /** | |
fe482240 | 281 | * Returns all the rows in the given offset and rowCount. |
6a488035 | 282 | * |
3f8d2862 | 283 | * @param string $action |
64bd5a0e TO |
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. | |
3f8d2862 | 291 | * @param string $output |
64bd5a0e | 292 | * What should the result set include (web/email/csv). |
6a488035 | 293 | * |
a6c01b45 CW |
294 | * @return int |
295 | * the total number of rows for this action | |
6a488035 | 296 | */ |
00be9182 | 297 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { |
6a488035 TO |
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, | |
87dab4a4 AH |
349 | ), |
350 | ts('more'), | |
351 | FALSE, | |
352 | 'case.selector.actions', | |
353 | 'Case', | |
354 | $result->case_id | |
6a488035 TO |
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'), | |
87dab4a4 AH |
363 | TRUE, |
364 | 'case.selector.moreActions', | |
365 | 'Case', | |
366 | $result->case_id | |
6a488035 TO |
367 | ); |
368 | ||
e547f744 | 369 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type |
6a488035 TO |
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 | /** | |
1054415f | 410 | * @inheritDoc |
6a488035 | 411 | */ |
6a488035 TO |
412 | public function getQILL() { |
413 | return $this->_query->qill(); | |
414 | } | |
415 | ||
416 | /** | |
100fef9d | 417 | * Returns the column headers as an array of tuples: |
6a488035 TO |
418 | * (name, sortName (key to the sort array)) |
419 | * | |
64bd5a0e TO |
420 | * @param string $action |
421 | * The action being performed. | |
3f8d2862 | 422 | * @param string $output |
64bd5a0e | 423 | * What should the result set include (web/email/csv). |
6a488035 | 424 | * |
a6c01b45 CW |
425 | * @return array |
426 | * the column headers that need to be displayed | |
6a488035 TO |
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'), | |
33a5a53d | 442 | 'sort' => 'case_type', |
6a488035 TO |
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 | ||
4c6ce474 EM |
482 | /** |
483 | * @return mixed | |
484 | */ | |
00be9182 | 485 | public function alphabetQuery() { |
6a488035 TO |
486 | return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE); |
487 | } | |
488 | ||
4c6ce474 EM |
489 | /** |
490 | * @return string | |
491 | */ | |
00be9182 | 492 | public function &getQuery() { |
6a488035 TO |
493 | return $this->_query; |
494 | } | |
495 | ||
496 | /** | |
100fef9d | 497 | * Name of export file. |
6a488035 | 498 | * |
64bd5a0e TO |
499 | * @param string $output |
500 | * Type of output. | |
6a488035 | 501 | * |
a6c01b45 CW |
502 | * @return string |
503 | * name of the file | |
6a488035 | 504 | */ |
00be9182 | 505 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
506 | return ts('Case Search'); |
507 | } | |
96025800 | 508 | |
6a488035 | 509 | } |