send most action links thru hook_civicrm_links
[civicrm-core.git] / CRM / Grant / Selector / Search.php
CommitLineData
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 */
42class 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 /**
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 '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 /**
83 * are we restricting ourselves to a single contact
84 *
85 * @access protected
86 * @var boolean
87 */
88 protected $_single = FALSE;
89
90 /**
91 * are we restricting ourselves to a single contact
92 *
93 * @access protected
94 * @var boolean
95 */
96 protected $_limit = NULL;
97
98 /**
99 * what context are we being invoked from
100 *
101 * @access protected
102 * @var string
103 */
104 protected $_context = NULL;
105
106 /**
107 * queryParams is the array returned by exportValues called on
108 * the HTML_QuickForm_Controller for that page.
109 *
110 * @var array
111 * @access protected
112 */
113 public $_queryParams;
114
115 /**
116 * represent the type of selector
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 *
140 * @param array $queryParams array of parameters for query
141 * @param int $action - action of search basic or advanced.
142 * @param string $grantClause if the caller wants to further restrict the search
143 * @param boolean $single are we dealing only with one contact?
144 * @param int $limit how many participations do we want returned
145 *
146 * @return CRM_Contact_Selector
147 * @access public
148 */
149 function __construct(&$queryParams,
150 $action = CRM_Core_Action::NONE,
151 $grantClause = NULL,
152 $single = FALSE,
153 $limit = NULL,
154 $context = 'search'
155 ) {
156 // submitted form values
157 $this->_queryParams = &$queryParams;
158
159
160 $this->_single = $single;
161 $this->_limit = $limit;
162 $this->_context = $context;
163
164 $this->_grantClause = $grantClause;
165
166 // type of selector
167 $this->_action = $action;
168
169 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE,
170 CRM_Contact_BAO_Query::MODE_GRANT
171 );
172 $this->_query->_distinctComponentClause = " civicrm_grant.id";
173 $this->_query->_groupByComponentClause = " GROUP BY civicrm_grant.id ";
174 }
175 //end of constructor
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 *
184 * @return array
185 * @access public
186 *
187 */
188 static function &links($key = NULL) {
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) {
209 $deleteExtra = ts('Are you sure you want to delete this grant?');
210
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,
215 'extra' => 'onclick = "if (confirm(\'' . $deleteExtra . '\') ) this.href+=\'&amp;confirmed=1\'; else return false;"',
216 'title' => ts('Delete Grant'),
217 ),
218 );
219 self::$_links = self::$_links + $delLink;
220 }
221 }
222 return self::$_links;
223 }
224 //end of function
225
226 /**
227 * getter for array of the parameters required for creating pager.
228 *
229 * @param
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 }
245 //end of function
246
247 /**
248 * Returns total number of rows for the query.
249 *
250 * @param
251 *
252 * @return int Total number of rows
253 * @access public
254 */
255 function getTotalCount($action) {
256 return $this->_query->searchQuery(0, 0, NULL,
257 TRUE, FALSE,
258 FALSE, FALSE,
259 FALSE,
260 $this->_grantClause
261 );
262 }
263
264 /**
265 * returns all the rows in the given offset and rowCount *
266 *
267 * @param enum $action the action being performed
268 * @param int $offset the row number to start from
269 * @param int $rowCount the number of rows to return
270 * @param string $sort the sql string that describes the sort order
271 * @param enum $output what should the result set include (web/email/csv)
272 *
273 * @return int the total number of rows for this action
274 */
275 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
276 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
277 FALSE, FALSE,
278 FALSE, FALSE,
279 FALSE,
280 $this->_grantClause
281 );
282
283 // process the result of the query
284 $rows = array();
285
286 //CRM-4418 check for view, edit, delete
287 $permissions = array(CRM_Core_Permission::VIEW);
288 if (CRM_Core_Permission::check('edit grants')) {
289 $permissions[] = CRM_Core_Permission::EDIT;
290 }
291 if (CRM_Core_Permission::check('delete in CiviGrant')) {
292 $permissions[] = CRM_Core_Permission::DELETE;
293 }
294 $mask = CRM_Core_Action::mask($permissions);
295
296 while ($result->fetch()) {
297 $row = array();
298 // the columns we are interested in
299 foreach (self::$_properties as $property) {
300 if (isset($result->$property)) {
301 $row[$property] = $result->$property;
302 }
303 }
304
305 if ($this->_context == 'search') {
306 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->grant_id;
307 }
308
309 $row['action'] = CRM_Core_Action::formLink(self::links($this->_key),
310 $mask,
311 array(
312 'id' => $result->grant_id,
313 'cid' => $result->contact_id,
314 'cxt' => $this->_context,
87dab4a4
AH
315 ),
316 ts('more'),
317 FALSE,
318 'grant.selector.row',
319 'Grant',
320 $result->grant_id
6a488035
TO
321 );
322
323 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
324 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
325 );
326
327 $rows[] = $row;
328 }
329
330 return $rows;
331 }
332
333 /**
334 *
335 * @return array $qill which contains an array of strings
336 * @access public
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 /**
346 * returns the column headers as an array of tuples:
347 * (name, sortName (key to the sort array))
348 *
349 * @param string $action the action being performed
350 * @param enum $output what should the result set include (web/email/csv)
351 *
352 * @return array the column headers that need to be displayed
353 * @access public
354 */
355 public function &getColumnHeaders($action = NULL, $output = NULL) {
356 if (!isset(self::$_columnHeaders)) {
357 self::$_columnHeaders = array(
358 array('name' => ts('Status'),
359 'sort' => 'grant_status',
360 'direction' => CRM_Utils_Sort::DONTCARE,
361 ),
362 array(
363 'name' => ts('Type'),
364 'sort' => 'grant_type_id',
365 'direction' => CRM_Utils_Sort::DONTCARE,
366 ),
367 array(
368 'name' => ts('Requested'),
369 'sort' => 'grant_amount_total',
370 'direction' => CRM_Utils_Sort::DONTCARE,
371 ),
372 array(
373 'name' => ts('Granted'),
374 'sort' => 'grant_amount_granted',
375 'direction' => CRM_Utils_Sort::DONTCARE,
376 ),
377 array(
378 'name' => ts('Application Received'),
379 'sort' => 'grant_application_received_date',
380 'direction' => CRM_Utils_Sort::DONTCARE,
381 ),
382 array(
383 'name' => ts('Report Received'),
384 'sort' => 'grant_report_received',
385 'direction' => CRM_Utils_Sort::DONTCARE,
386 ),
387 array(
388 'name' => ts('Money Transferred'),
389 'sort' => 'money_transfer_date',
390 'direction' => CRM_Utils_Sort::DONTCARE,
391 ),
392 array('desc' => ts('Actions')),
393 );
394
395 if (!$this->_single) {
396 $pre = array(
397 array('desc' => ts('Contact Type')),
398 array(
399 'name' => ts('Name'),
400 'sort' => 'sort_name',
401 'direction' => CRM_Utils_Sort::ASCENDING,
402 ),
403 );
404 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
405 }
406 }
407 return self::$_columnHeaders;
408 }
409
410 function &getQuery() {
411 return $this->_query;
412 }
413
414 /**
415 * name of export file.
416 *
417 * @param string $output type of output
418 *
419 * @return string name of the file
420 */
421 function getExportFileName($output = 'csv') {
422 return ts('CiviCRM Grant Search');
423 }
424}
425//end of class
426