Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
67d19299 | 35 | * This class is used to retrieve and display a range of contacts that match the given criteria. |
6a488035 TO |
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 | |
6a488035 TO |
43 | */ |
44 | static $_links = NULL; | |
45 | ||
46 | /** | |
100fef9d | 47 | * We use desc to remind us what that column is, name is used in the tpl |
6a488035 TO |
48 | * |
49 | * @var array | |
6a488035 TO |
50 | */ |
51 | static $_columnHeaders; | |
52 | ||
53 | /** | |
54 | * Properties of contact we're interested in displaying | |
55 | * @var array | |
6a488035 TO |
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 | /** | |
100fef9d | 73 | * Are we restricting ourselves to a single contact |
6a488035 | 74 | * |
6a488035 TO |
75 | * @var boolean |
76 | */ | |
77 | protected $_single = FALSE; | |
78 | ||
79 | /** | |
100fef9d | 80 | * Are we restricting ourselves to a single contact |
6a488035 | 81 | * |
6a488035 TO |
82 | * @var boolean |
83 | */ | |
84 | protected $_limit = NULL; | |
85 | ||
86 | /** | |
100fef9d | 87 | * What context are we being invoked from |
6a488035 | 88 | * |
6a488035 TO |
89 | * @var string |
90 | */ | |
91 | protected $_context = NULL; | |
92 | ||
93 | /** | |
100fef9d | 94 | * QueryParams is the array returned by exportValues called on |
6a488035 TO |
95 | * the HTML_QuickForm_Controller for that page. |
96 | * | |
97 | * @var array | |
6a488035 TO |
98 | */ |
99 | public $_queryParams; | |
100 | ||
101 | /** | |
100fef9d | 102 | * Represent the type of selector |
6a488035 TO |
103 | * |
104 | * @var int | |
6a488035 TO |
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 | /** | |
fe482240 | 123 | * Class constructor. |
6a488035 | 124 | * |
64bd5a0e TO |
125 | * @param array $queryParams |
126 | * Array of parameters for query. | |
dd244018 | 127 | * @param \const|int $action - action of search basic or advanced. |
64bd5a0e TO |
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. | |
6a488035 | 134 | * |
dd244018 EM |
135 | * @param string $context |
136 | * | |
137 | * @return \CRM_Case_Selector_Search | |
dd244018 | 138 | */ |
8d7a9d07 | 139 | public function __construct( |
28d4d481 | 140 | &$queryParams, |
e547f744 | 141 | $action = CRM_Core_Action::NONE, |
6a488035 | 142 | $additionalClause = NULL, |
e547f744 TO |
143 | $single = FALSE, |
144 | $limit = NULL, | |
145 | $context = 'search' | |
6a488035 TO |
146 | ) { |
147 | // submitted form values | |
148 | $this->_queryParams = &$queryParams; | |
149 | ||
353ffa53 TO |
150 | $this->_single = $single; |
151 | $this->_limit = $limit; | |
6a488035 TO |
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 | } | |
6a488035 TO |
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 | * | |
da6b46f4 EM |
178 | * @param bool $isDeleted |
179 | * @param null $key | |
180 | * | |
6a488035 | 181 | * @return array |
6a488035 | 182 | */ |
8d7a9d07 | 183 | static public function &links($isDeleted = FALSE, $key = NULL) { |
6a488035 TO |
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', | |
47f03411 | 204 | 'class' => 'no-popup', |
6a488035 TO |
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', | |
a4400196 | 219 | 'class' => 'medium-popup', |
6a488035 TO |
220 | 'title' => ts('Assign to Another Client'), |
221 | ), | |
222 | ); | |
223 | } | |
224 | ||
225 | $actionLinks = array(); | |
226 | foreach (self::$_links as $key => $value) { | |
f6f26d03 | 227 | $actionLinks['primaryActions'][$key] = $value; |
6a488035 TO |
228 | } |
229 | ||
230 | return $actionLinks; | |
231 | } | |
6a488035 TO |
232 | |
233 | /** | |
100fef9d | 234 | * Getter for array of the parameters required for creating pager. |
6a488035 | 235 | * |
fd31fa4c | 236 | * @param $action |
c490a46a | 237 | * @param array $params |
6a488035 | 238 | */ |
00be9182 | 239 | public function getPagerParams($action, &$params) { |
6a488035 TO |
240 | $params['status'] = ts('Case') . ' %%StatusMessage%%'; |
241 | $params['csvString'] = NULL; | |
242 | if ($this->_limit) { | |
243 | $params['rowCount'] = $this->_limit; | |
244 | } | |
245 | else { | |
246 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
247 | } | |
248 | ||
249 | $params['buttonTop'] = 'PagerTopButton'; | |
250 | $params['buttonBottom'] = 'PagerBottomButton'; | |
251 | } | |
6a488035 TO |
252 | |
253 | /** | |
254 | * Returns total number of rows for the query. | |
255 | * | |
256 | * @param | |
257 | * | |
a6c01b45 CW |
258 | * @return int |
259 | * Total number of rows | |
6a488035 | 260 | */ |
00be9182 | 261 | public function getTotalCount($action) { |
6a488035 TO |
262 | return $this->_query->searchQuery(0, 0, NULL, |
263 | TRUE, FALSE, | |
264 | FALSE, FALSE, | |
265 | FALSE, | |
266 | $this->_additionalClause | |
267 | ); | |
268 | } | |
269 | ||
270 | /** | |
fe482240 | 271 | * Returns all the rows in the given offset and rowCount. |
6a488035 | 272 | * |
3f8d2862 | 273 | * @param string $action |
64bd5a0e TO |
274 | * The action being performed. |
275 | * @param int $offset | |
276 | * The row number to start from. | |
277 | * @param int $rowCount | |
278 | * The number of rows to return. | |
279 | * @param string $sort | |
280 | * The sql string that describes the sort order. | |
3f8d2862 | 281 | * @param string $output |
64bd5a0e | 282 | * What should the result set include (web/email/csv). |
6a488035 | 283 | * |
a6c01b45 CW |
284 | * @return int |
285 | * the total number of rows for this action | |
6a488035 | 286 | */ |
00be9182 | 287 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { |
6a488035 TO |
288 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, |
289 | FALSE, FALSE, | |
290 | FALSE, FALSE, | |
291 | FALSE, | |
292 | $this->_additionalClause | |
293 | ); | |
294 | // process the result of the query | |
295 | $rows = array(); | |
296 | ||
297 | //CRM-4418 check for view, edit, delete | |
298 | $permissions = array(CRM_Core_Permission::VIEW); | |
299 | if (CRM_Core_Permission::check('access all cases and activities') | |
300 | || CRM_Core_Permission::check('access my cases and activities') | |
301 | ) { | |
302 | $permissions[] = CRM_Core_Permission::EDIT; | |
303 | } | |
304 | if (CRM_Core_Permission::check('delete in CiviCase')) { | |
305 | $permissions[] = CRM_Core_Permission::DELETE; | |
306 | } | |
307 | $mask = CRM_Core_Action::mask($permissions); | |
308 | ||
309 | $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' "); | |
310 | ||
311 | $scheduledInfo = array(); | |
312 | ||
313 | while ($result->fetch()) { | |
314 | $row = array(); | |
315 | // the columns we are interested in | |
316 | foreach (self::$_properties as $property) { | |
317 | if (isset($result->$property)) { | |
318 | $row[$property] = $result->$property; | |
319 | } | |
320 | } | |
321 | ||
322 | $isDeleted = FALSE; | |
323 | if ($result->case_deleted) { | |
324 | $isDeleted = TRUE; | |
325 | $row['case_status_id'] = empty($row['case_status_id']) ? "" : $row['case_status_id'] . '<br />(deleted)'; | |
326 | } | |
327 | ||
328 | $scheduledInfo['case_id'][] = $result->case_id; | |
329 | $scheduledInfo['contact_id'][] = $result->contact_id; | |
330 | $scheduledInfo['case_deleted'] = $result->case_deleted; | |
331 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->case_id; | |
332 | ||
333 | $links = self::links($isDeleted, $this->_key); | |
334 | $row['action'] = CRM_Core_Action::formLink($links['primaryActions'], | |
335 | $mask, array( | |
336 | 'id' => $result->case_id, | |
337 | 'cid' => $result->contact_id, | |
338 | 'cxt' => $this->_context, | |
87dab4a4 AH |
339 | ), |
340 | ts('more'), | |
341 | FALSE, | |
342 | 'case.selector.actions', | |
343 | 'Case', | |
344 | $result->case_id | |
6a488035 | 345 | ); |
6a488035 | 346 | |
e547f744 | 347 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type |
6a488035 TO |
348 | ); |
349 | ||
350 | //adding case manager to case selector.CRM-4510. | |
351 | $caseType = CRM_Case_BAO_Case::getCaseType($result->case_id, 'name'); | |
352 | $caseManagerContact = CRM_Case_BAO_Case::getCaseManagerContact($caseType, $result->case_id); | |
353 | ||
354 | if (!empty($caseManagerContact)) { | |
355 | $row['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact); | |
356 | $row['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact); | |
357 | } | |
358 | ||
359 | if (isset($result->case_status_id) && | |
360 | array_key_exists($result->case_status_id, $caseStatus) | |
361 | ) { | |
362 | $row['class'] = "status-urgent"; | |
363 | } | |
364 | else { | |
365 | $row['class'] = "status-normal"; | |
366 | } | |
367 | ||
368 | $rows[$result->case_id] = $row; | |
369 | } | |
370 | ||
371 | //retrive the scheduled & recent Activity type and date for selector | |
372 | if (!empty($scheduledInfo)) { | |
373 | $schdeduledActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'upcoming'); | |
374 | foreach ($schdeduledActivity as $key => $value) { | |
375 | $rows[$key]['case_scheduled_activity_date'] = $value['date']; | |
376 | $rows[$key]['case_scheduled_activity_type'] = $value['type']; | |
377 | } | |
378 | $recentActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'recent'); | |
379 | foreach ($recentActivity as $key => $value) { | |
380 | $rows[$key]['case_recent_activity_date'] = $value['date']; | |
381 | $rows[$key]['case_recent_activity_type'] = $value['type']; | |
382 | } | |
383 | } | |
384 | return $rows; | |
385 | } | |
386 | ||
387 | /** | |
1054415f | 388 | * @inheritDoc |
6a488035 | 389 | */ |
6a488035 TO |
390 | public function getQILL() { |
391 | return $this->_query->qill(); | |
392 | } | |
393 | ||
394 | /** | |
100fef9d | 395 | * Returns the column headers as an array of tuples: |
6a488035 TO |
396 | * (name, sortName (key to the sort array)) |
397 | * | |
64bd5a0e TO |
398 | * @param string $action |
399 | * The action being performed. | |
3f8d2862 | 400 | * @param string $output |
64bd5a0e | 401 | * What should the result set include (web/email/csv). |
6a488035 | 402 | * |
a6c01b45 CW |
403 | * @return array |
404 | * the column headers that need to be displayed | |
6a488035 TO |
405 | */ |
406 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
407 | if (!isset(self::$_columnHeaders)) { | |
408 | self::$_columnHeaders = array( | |
409 | array( | |
410 | 'name' => ts('Subject'), | |
411 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
412 | ), | |
413 | array( | |
414 | 'name' => ts('Status'), | |
415 | 'sort' => 'case_status', | |
416 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
417 | ), | |
418 | array( | |
419 | 'name' => ts('Case Type'), | |
33a5a53d | 420 | 'sort' => 'case_type', |
6a488035 TO |
421 | 'direction' => CRM_Utils_Sort::DONTCARE, |
422 | ), | |
423 | array( | |
424 | 'name' => ts('My Role'), | |
425 | 'sort' => 'case_role', | |
426 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
427 | ), | |
428 | array( | |
429 | 'name' => ts('Case Manager'), | |
430 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
431 | ), | |
432 | array( | |
433 | 'name' => ts('Most Recent'), | |
434 | 'sort' => 'case_recent_activity_date', | |
435 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
436 | ), | |
437 | array( | |
438 | 'name' => ts('Next Sched.'), | |
439 | 'sort' => 'case_scheduled_activity_date', | |
440 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
441 | ), | |
442 | array('name' => ts('Actions')), | |
443 | ); | |
444 | ||
445 | if (!$this->_single) { | |
446 | $pre = array( | |
447 | array( | |
448 | 'name' => ts('Client'), | |
449 | 'sort' => 'sort_name', | |
450 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
451 | ), | |
452 | ); | |
453 | ||
454 | self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); | |
455 | } | |
456 | } | |
457 | return self::$_columnHeaders; | |
458 | } | |
459 | ||
4c6ce474 EM |
460 | /** |
461 | * @return mixed | |
462 | */ | |
00be9182 | 463 | public function alphabetQuery() { |
6a488035 TO |
464 | return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE); |
465 | } | |
466 | ||
4c6ce474 EM |
467 | /** |
468 | * @return string | |
469 | */ | |
00be9182 | 470 | public function &getQuery() { |
6a488035 TO |
471 | return $this->_query; |
472 | } | |
473 | ||
474 | /** | |
100fef9d | 475 | * Name of export file. |
6a488035 | 476 | * |
64bd5a0e TO |
477 | * @param string $output |
478 | * Type of output. | |
6a488035 | 479 | * |
a6c01b45 CW |
480 | * @return string |
481 | * name of the file | |
6a488035 | 482 | */ |
00be9182 | 483 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
484 | return ts('Case Search'); |
485 | } | |
96025800 | 486 | |
6a488035 | 487 | } |