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 | * | |
77b97be7 EM |
184 | * @param null $key |
185 | * | |
6a488035 | 186 | * @return array |
6a488035 | 187 | */ |
00be9182 | 188 | public static function &links($key = NULL) { |
6a488035 TO |
189 | $cid = CRM_Utils_Request::retrieve('cid', 'Integer', $this); |
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 | /** | |
a6c01b45 CW |
335 | * @return array |
336 | * which contains an array of strings | |
6a488035 TO |
337 | */ |
338 | ||
339 | // the current internationalisation is bad, but should more or less work | |
340 | // for most of "European" languages | |
341 | public function getQILL() { | |
342 | return $this->_query->qill(); | |
343 | } | |
344 | ||
345 | /** | |
100fef9d | 346 | * Returns the column headers as an array of tuples: |
6a488035 TO |
347 | * (name, sortName (key to the sort array)) |
348 | * | |
85511635 TO |
349 | * @param string $action |
350 | * The action being performed. | |
3f8d2862 | 351 | * @param string $output |
85511635 | 352 | * What should the result set include (web/email/csv). |
6a488035 | 353 | * |
a6c01b45 CW |
354 | * @return array |
355 | * the column headers that need to be displayed | |
6a488035 TO |
356 | */ |
357 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
358 | if (!isset(self::$_columnHeaders)) { | |
359 | self::$_columnHeaders = array( | |
1afc557a | 360 | array( |
353ffa53 | 361 | 'name' => ts('Status'), |
6a488035 TO |
362 | 'sort' => 'grant_status', |
363 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
364 | ), | |
365 | array( | |
366 | 'name' => ts('Type'), | |
367 | 'sort' => 'grant_type_id', | |
368 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
369 | ), | |
370 | array( | |
371 | 'name' => ts('Requested'), | |
372 | 'sort' => 'grant_amount_total', | |
373 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
374 | ), | |
375 | array( | |
376 | 'name' => ts('Granted'), | |
377 | 'sort' => 'grant_amount_granted', | |
378 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
379 | ), | |
380 | array( | |
381 | 'name' => ts('Application Received'), | |
382 | 'sort' => 'grant_application_received_date', | |
383 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
384 | ), | |
385 | array( | |
386 | 'name' => ts('Report Received'), | |
387 | 'sort' => 'grant_report_received', | |
388 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
389 | ), | |
390 | array( | |
391 | 'name' => ts('Money Transferred'), | |
392 | 'sort' => 'money_transfer_date', | |
393 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
394 | ), | |
395 | array('desc' => ts('Actions')), | |
396 | ); | |
397 | ||
398 | if (!$this->_single) { | |
399 | $pre = array( | |
400 | array('desc' => ts('Contact Type')), | |
401 | array( | |
402 | 'name' => ts('Name'), | |
403 | 'sort' => 'sort_name', | |
404 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
405 | ), | |
406 | ); | |
407 | self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); | |
408 | } | |
409 | } | |
410 | return self::$_columnHeaders; | |
411 | } | |
412 | ||
e0ef6999 EM |
413 | /** |
414 | * @return string | |
415 | */ | |
00be9182 | 416 | public function &getQuery() { |
6a488035 TO |
417 | return $this->_query; |
418 | } | |
419 | ||
420 | /** | |
100fef9d | 421 | * Name of export file. |
6a488035 | 422 | * |
85511635 TO |
423 | * @param string $output |
424 | * Type of output. | |
6a488035 | 425 | * |
a6c01b45 CW |
426 | * @return string |
427 | * name of the file | |
6a488035 | 428 | */ |
00be9182 | 429 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
430 | return ts('CiviCRM Grant Search'); |
431 | } | |
432 | } |