Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | */ | |
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 | * @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, | |
87dab4a4 AH |
345 | ), |
346 | ts('more'), | |
347 | FALSE, | |
348 | 'case.selector.actions', | |
349 | 'Case', | |
350 | $result->case_id | |
6a488035 TO |
351 | ); |
352 | $row['moreActions'] = CRM_Core_Action::formLink(CRM_Utils_Array::value('moreActions', $links), | |
353 | $mask, array( | |
354 | 'id' => $result->case_id, | |
355 | 'cid' => $result->contact_id, | |
356 | 'cxt' => $this->_context, | |
357 | ), | |
358 | ts('more'), | |
87dab4a4 AH |
359 | TRUE, |
360 | 'case.selector.moreActions', | |
361 | 'Case', | |
362 | $result->case_id | |
6a488035 TO |
363 | ); |
364 | ||
365 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? | |
366 | $result->contact_sub_type : $result->contact_type | |
367 | ); | |
368 | ||
369 | //adding case manager to case selector.CRM-4510. | |
370 | $caseType = CRM_Case_BAO_Case::getCaseType($result->case_id, 'name'); | |
371 | $caseManagerContact = CRM_Case_BAO_Case::getCaseManagerContact($caseType, $result->case_id); | |
372 | ||
373 | if (!empty($caseManagerContact)) { | |
374 | $row['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact); | |
375 | $row['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact); | |
376 | } | |
377 | ||
378 | if (isset($result->case_status_id) && | |
379 | array_key_exists($result->case_status_id, $caseStatus) | |
380 | ) { | |
381 | $row['class'] = "status-urgent"; | |
382 | } | |
383 | else { | |
384 | $row['class'] = "status-normal"; | |
385 | } | |
386 | ||
387 | $rows[$result->case_id] = $row; | |
388 | } | |
389 | ||
390 | //retrive the scheduled & recent Activity type and date for selector | |
391 | if (!empty($scheduledInfo)) { | |
392 | $schdeduledActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'upcoming'); | |
393 | foreach ($schdeduledActivity as $key => $value) { | |
394 | $rows[$key]['case_scheduled_activity_date'] = $value['date']; | |
395 | $rows[$key]['case_scheduled_activity_type'] = $value['type']; | |
396 | } | |
397 | $recentActivity = CRM_Case_BAO_Case::getNextScheduledActivity($scheduledInfo, 'recent'); | |
398 | foreach ($recentActivity as $key => $value) { | |
399 | $rows[$key]['case_recent_activity_date'] = $value['date']; | |
400 | $rows[$key]['case_recent_activity_type'] = $value['type']; | |
401 | } | |
402 | } | |
403 | return $rows; | |
404 | } | |
405 | ||
406 | /** | |
407 | * | |
408 | * @return array $qill which contains an array of strings | |
409 | * @access public | |
410 | */ | |
411 | ||
412 | // the current internationalisation is bad, but should more or less work | |
413 | // for most of "European" languages | |
414 | public function getQILL() { | |
415 | return $this->_query->qill(); | |
416 | } | |
417 | ||
418 | /** | |
419 | * returns the column headers as an array of tuples: | |
420 | * (name, sortName (key to the sort array)) | |
421 | * | |
422 | * @param string $action the action being performed | |
423 | * @param enum $output what should the result set include (web/email/csv) | |
424 | * | |
425 | * @return array the column headers that need to be displayed | |
426 | * @access public | |
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_id', | |
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 | function alphabetQuery() { | |
483 | return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE); | |
484 | } | |
485 | ||
486 | function &getQuery() { | |
487 | return $this->_query; | |
488 | } | |
489 | ||
490 | /** | |
491 | * name of export file. | |
492 | * | |
493 | * @param string $output type of output | |
494 | * | |
495 | * @return string name of the file | |
496 | */ | |
497 | function getExportFileName($output = 'csv') { | |
498 | return ts('Case Search'); | |
499 | } | |
500 | } | |
501 | //end of class | |
502 |