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 | +--------------------------------------------------------------------+ | |
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 | */ | |
42 | class CRM_Grant_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API { | |
43 | ||
44 | /** | |
45 | * This defines two actions- View and Edit. | |
46 | * | |
47 | * @var array | |
48 | * @static | |
49 | */ | |
50 | static $_links = NULL; | |
51 | ||
52 | /** | |
100fef9d | 53 | * We use desc to remind us what that column is, name is used in the tpl |
6a488035 TO |
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 | 'grant_id', | |
70 | 'grant_status_id', | |
71 | 'grant_status', | |
72 | 'grant_type_id', | |
73 | 'grant_type', | |
74 | 'grant_amount_total', | |
75 | 'grant_amount_requested', | |
76 | 'grant_amount_granted', | |
77 | 'grant_application_received_date', | |
78 | 'grant_report_received', | |
79 | 'grant_money_transfer_date', | |
80 | ); | |
81 | ||
82 | /** | |
100fef9d | 83 | * Are we restricting ourselves to a single contact |
6a488035 | 84 | * |
6a488035 TO |
85 | * @var boolean |
86 | */ | |
87 | protected $_single = FALSE; | |
88 | ||
89 | /** | |
100fef9d | 90 | * Are we restricting ourselves to a single contact |
6a488035 | 91 | * |
6a488035 TO |
92 | * @var boolean |
93 | */ | |
94 | protected $_limit = NULL; | |
95 | ||
96 | /** | |
100fef9d | 97 | * What context are we being invoked from |
6a488035 | 98 | * |
6a488035 TO |
99 | * @var string |
100 | */ | |
101 | protected $_context = NULL; | |
102 | ||
103 | /** | |
100fef9d | 104 | * QueryParams is the array returned by exportValues called on |
6a488035 TO |
105 | * the HTML_QuickForm_Controller for that page. |
106 | * | |
107 | * @var array | |
6a488035 TO |
108 | */ |
109 | public $_queryParams; | |
110 | ||
111 | /** | |
100fef9d | 112 | * Represent the type of selector |
6a488035 TO |
113 | * |
114 | * @var int | |
6a488035 TO |
115 | */ |
116 | protected $_action; | |
117 | ||
118 | /** | |
119 | * The additional clause that we restrict the search with | |
120 | * | |
121 | * @var string | |
122 | */ | |
123 | protected $_grantClause = NULL; | |
124 | ||
125 | /** | |
126 | * The query object | |
127 | * | |
128 | * @var string | |
129 | */ | |
130 | protected $_query; | |
131 | ||
132 | /** | |
133 | * Class constructor | |
134 | * | |
85511635 TO |
135 | * @param array $queryParams |
136 | * Array of parameters for query. | |
77b97be7 | 137 | * @param \const|int $action - action of search basic or advanced. |
85511635 TO |
138 | * @param string $grantClause |
139 | * If the caller wants to further restrict the search. | |
140 | * @param bool $single | |
141 | * Are we dealing only with one contact?. | |
142 | * @param int $limit | |
143 | * How many participations do we want returned. | |
6a488035 | 144 | * |
77b97be7 EM |
145 | * @param string $context |
146 | * | |
147 | * @return \CRM_Grant_Selector_Search | |
d60f50a8 | 148 | * @access public |
6a488035 | 149 | */ |
73714296 TO |
150 | function __construct( |
151 | &$queryParams, | |
1afc557a | 152 | $action = CRM_Core_Action::NONE, |
6a488035 | 153 | $grantClause = NULL, |
1afc557a TO |
154 | $single = FALSE, |
155 | $limit = NULL, | |
156 | $context = 'search' | |
6a488035 TO |
157 | ) { |
158 | // submitted form values | |
159 | $this->_queryParams = &$queryParams; | |
160 | ||
353ffa53 TO |
161 | $this->_single = $single; |
162 | $this->_limit = $limit; | |
6a488035 TO |
163 | $this->_context = $context; |
164 | ||
165 | $this->_grantClause = $grantClause; | |
166 | ||
167 | // type of selector | |
168 | $this->_action = $action; | |
169 | ||
170 | $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE, | |
171 | CRM_Contact_BAO_Query::MODE_GRANT | |
172 | ); | |
173 | $this->_query->_distinctComponentClause = " civicrm_grant.id"; | |
174 | $this->_query->_groupByComponentClause = " GROUP BY civicrm_grant.id "; | |
175 | } | |
6a488035 TO |
176 | |
177 | /** | |
178 | * This method returns the links that are given for each search row. | |
179 | * currently the links added for each row are | |
180 | * | |
181 | * - View | |
182 | * - Edit | |
183 | * | |
546b78fa | 184 | * @param string|null $key |
77b97be7 | 185 | * |
6a488035 | 186 | * @return array |
6a488035 | 187 | */ |
00be9182 | 188 | public static function &links($key = NULL) { |
b9c81d6a | 189 | $cid = CRM_Utils_Request::retrieve('cid', 'Integer'); |
6a488035 TO |
190 | $extraParams = ($key) ? "&key={$key}" : NULL; |
191 | ||
192 | if (!(self::$_links)) { | |
193 | self::$_links = array( | |
194 | CRM_Core_Action::VIEW => array( | |
195 | 'name' => ts('View'), | |
196 | 'url' => 'civicrm/contact/view/grant', | |
197 | 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=grant' . $extraParams, | |
198 | 'title' => ts('View Grant'), | |
199 | ), | |
200 | CRM_Core_Action::UPDATE => array( | |
201 | 'name' => ts('Edit'), | |
202 | 'url' => 'civicrm/contact/view/grant', | |
203 | 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams, | |
204 | 'title' => ts('Edit Grant'), | |
205 | ), | |
206 | ); | |
207 | ||
208 | if ($cid) { | |
6a488035 | 209 | $delLink = array( |
1afc557a | 210 | CRM_Core_Action::DELETE => array( |
353ffa53 | 211 | 'name' => ts('Delete'), |
6a488035 TO |
212 | 'url' => 'civicrm/contact/view/grant', |
213 | 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=grant' . $extraParams, | |
6a488035 TO |
214 | 'title' => ts('Delete Grant'), |
215 | ), | |
216 | ); | |
217 | self::$_links = self::$_links + $delLink; | |
218 | } | |
219 | } | |
220 | return self::$_links; | |
221 | } | |
6a488035 TO |
222 | |
223 | /** | |
100fef9d | 224 | * Getter for array of the parameters required for creating pager. |
6a488035 | 225 | * |
77b97be7 | 226 | * @param $action |
c490a46a | 227 | * @param array $params |
6a488035 | 228 | */ |
00be9182 | 229 | public function getPagerParams($action, &$params) { |
6a488035 TO |
230 | $params['status'] = ts('Grant') . ' %%StatusMessage%%'; |
231 | $params['csvString'] = NULL; | |
232 | if ($this->_limit) { | |
233 | $params['rowCount'] = $this->_limit; | |
234 | } | |
235 | else { | |
236 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
237 | } | |
238 | ||
239 | $params['buttonTop'] = 'PagerTopButton'; | |
240 | $params['buttonBottom'] = 'PagerBottomButton'; | |
241 | } | |
6a488035 TO |
242 | |
243 | /** | |
244 | * Returns total number of rows for the query. | |
245 | * | |
246 | * @param | |
247 | * | |
a6c01b45 CW |
248 | * @return int |
249 | * Total number of rows | |
6a488035 | 250 | */ |
00be9182 | 251 | public function getTotalCount($action) { |
6a488035 TO |
252 | return $this->_query->searchQuery(0, 0, NULL, |
253 | TRUE, FALSE, | |
254 | FALSE, FALSE, | |
255 | FALSE, | |
256 | $this->_grantClause | |
257 | ); | |
258 | } | |
259 | ||
260 | /** | |
100fef9d | 261 | * Returns all the rows in the given offset and rowCount * |
6a488035 | 262 | * |
3f8d2862 | 263 | * @param string $action |
85511635 TO |
264 | * The action being performed. |
265 | * @param int $offset | |
266 | * The row number to start from. | |
267 | * @param int $rowCount | |
268 | * The number of rows to return. | |
269 | * @param string $sort | |
270 | * The sql string that describes the sort order. | |
3f8d2862 | 271 | * @param string $output |
85511635 | 272 | * What should the result set include (web/email/csv). |
6a488035 | 273 | * |
a6c01b45 CW |
274 | * @return int |
275 | * the total number of rows for this action | |
6a488035 | 276 | */ |
00be9182 | 277 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { |
6a488035 TO |
278 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, |
279 | FALSE, FALSE, | |
280 | FALSE, FALSE, | |
281 | FALSE, | |
282 | $this->_grantClause | |
283 | ); | |
284 | ||
285 | // process the result of the query | |
286 | $rows = array(); | |
287 | ||
288 | //CRM-4418 check for view, edit, delete | |
289 | $permissions = array(CRM_Core_Permission::VIEW); | |
290 | if (CRM_Core_Permission::check('edit grants')) { | |
291 | $permissions[] = CRM_Core_Permission::EDIT; | |
292 | } | |
293 | if (CRM_Core_Permission::check('delete in CiviGrant')) { | |
294 | $permissions[] = CRM_Core_Permission::DELETE; | |
295 | } | |
296 | $mask = CRM_Core_Action::mask($permissions); | |
297 | ||
298 | while ($result->fetch()) { | |
299 | $row = array(); | |
300 | // the columns we are interested in | |
301 | foreach (self::$_properties as $property) { | |
302 | if (isset($result->$property)) { | |
303 | $row[$property] = $result->$property; | |
304 | } | |
305 | } | |
306 | ||
307 | if ($this->_context == 'search') { | |
308 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->grant_id; | |
309 | } | |
310 | ||
311 | $row['action'] = CRM_Core_Action::formLink(self::links($this->_key), | |
312 | $mask, | |
313 | array( | |
314 | 'id' => $result->grant_id, | |
315 | 'cid' => $result->contact_id, | |
316 | 'cxt' => $this->_context, | |
87dab4a4 AH |
317 | ), |
318 | ts('more'), | |
319 | FALSE, | |
320 | 'grant.selector.row', | |
321 | 'Grant', | |
322 | $result->grant_id | |
6a488035 TO |
323 | ); |
324 | ||
1afc557a | 325 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id |
6a488035 TO |
326 | ); |
327 | ||
328 | $rows[] = $row; | |
329 | } | |
330 | ||
331 | return $rows; | |
332 | } | |
333 | ||
334 | /** | |
1054415f | 335 | * @inheritDoc |
6a488035 | 336 | */ |
6a488035 TO |
337 | public function getQILL() { |
338 | return $this->_query->qill(); | |
339 | } | |
340 | ||
341 | /** | |
100fef9d | 342 | * Returns the column headers as an array of tuples: |
6a488035 TO |
343 | * (name, sortName (key to the sort array)) |
344 | * | |
85511635 TO |
345 | * @param string $action |
346 | * The action being performed. | |
3f8d2862 | 347 | * @param string $output |
85511635 | 348 | * What should the result set include (web/email/csv). |
6a488035 | 349 | * |
a6c01b45 CW |
350 | * @return array |
351 | * the column headers that need to be displayed | |
6a488035 TO |
352 | */ |
353 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
354 | if (!isset(self::$_columnHeaders)) { | |
355 | self::$_columnHeaders = array( | |
1afc557a | 356 | array( |
353ffa53 | 357 | 'name' => ts('Status'), |
6a488035 TO |
358 | 'sort' => 'grant_status', |
359 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
360 | ), | |
361 | array( | |
362 | 'name' => ts('Type'), | |
363 | 'sort' => 'grant_type_id', | |
364 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
365 | ), | |
366 | array( | |
367 | 'name' => ts('Requested'), | |
368 | 'sort' => 'grant_amount_total', | |
369 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
370 | ), | |
371 | array( | |
372 | 'name' => ts('Granted'), | |
373 | 'sort' => 'grant_amount_granted', | |
374 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
375 | ), | |
376 | array( | |
377 | 'name' => ts('Application Received'), | |
378 | 'sort' => 'grant_application_received_date', | |
379 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
380 | ), | |
381 | array( | |
382 | 'name' => ts('Report Received'), | |
383 | 'sort' => 'grant_report_received', | |
384 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
385 | ), | |
386 | array( | |
387 | 'name' => ts('Money Transferred'), | |
388 | 'sort' => 'money_transfer_date', | |
389 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
390 | ), | |
391 | array('desc' => ts('Actions')), | |
392 | ); | |
393 | ||
394 | if (!$this->_single) { | |
395 | $pre = array( | |
396 | array('desc' => ts('Contact Type')), | |
397 | array( | |
398 | 'name' => ts('Name'), | |
399 | 'sort' => 'sort_name', | |
400 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
401 | ), | |
402 | ); | |
403 | self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); | |
404 | } | |
405 | } | |
406 | return self::$_columnHeaders; | |
407 | } | |
408 | ||
e0ef6999 EM |
409 | /** |
410 | * @return string | |
411 | */ | |
00be9182 | 412 | public function &getQuery() { |
6a488035 TO |
413 | return $this->_query; |
414 | } | |
415 | ||
416 | /** | |
100fef9d | 417 | * Name of export file. |
6a488035 | 418 | * |
85511635 TO |
419 | * @param string $output |
420 | * Type of output. | |
6a488035 | 421 | * |
a6c01b45 CW |
422 | * @return string |
423 | * name of the file | |
6a488035 | 424 | */ |
00be9182 | 425 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
426 | return ts('CiviCRM Grant Search'); |
427 | } | |
428 | } |