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