CRM-12872 - Remove print button from search results (except campaign)
[civicrm-core.git] / CRM / Grant / Form / 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, |
03e04002 24 | see the CiviCRM license FAQ at http://civicrm.org/licensing
6a488035
TO
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * Files required
38 */
39
40/**
41 * This file is for civigrant search
42 */
43class CRM_Grant_Form_Search extends CRM_Core_Form {
44
45 /**
46 * Are we forced to run a search
47 *
48 * @var int
49 * @access protected
50 */
51 protected $_force;
52
53 /**
54 * name of search button
55 *
56 * @var string
57 * @access protected
58 */
59 protected $_searchButtonName;
60
6a488035
TO
61 /**
62 * name of action button
63 *
64 * @var string
65 * @access protected
66 */
67 protected $_actionButtonName;
68
69 /**
70 * form values that we will be using
71 *
72 * @var array
73 * @access protected
74 */
75 protected $_formValues;
76
77 /**
78 * the params that are sent to the query
79 *
80 * @var array
81 * @access protected
82 */
83 protected $_queryParams;
84
85 /**
86 * have we already done this search
87 *
88 * @access protected
89 * @var boolean
90 */
91 protected $_done;
92
93 /**
94 * are we restricting ourselves to a single contact
95 *
96 * @access protected
97 * @var boolean
98 */
99 protected $_single = FALSE;
100
101 /**
102 * are we restricting ourselves to a single contact
103 *
104 * @access protected
105 * @var boolean
106 */
107 protected $_limit = NULL;
108
109 /**
110 * what context are we being invoked from
111 *
112 * @access protected
113 * @var string
114 */
115 protected $_context = NULL;
116
117 /**
118 * prefix for the controller
119 *
120 */
121 protected $_prefix = "grant_";
122
123 protected $_defaults;
124
125 /**
126 * processing needed for buildForm and later
127 *
128 * @return void
129 * @access public
130 */ function preProcess() {
131
132 /**
133 * set the button names
134 */
135
136 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
137 $this->_actionButtonName = $this->getButtonName('next', 'action');
138
139 $this->_done = FALSE;
140 $this->defaults = array();
141
03e04002 142 /*
143 * we allow the controller to set force/reset externally, useful when we are being
144 * driven by the wizard framework
6a488035
TO
145 */
146
147 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
148 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
149 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
150 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
151
152 $this->assign("context", $this->_context);
153
154 // get user submitted values
155 // get it from controller only if form has been submitted, else preProcess has set this
156 if (!empty($_POST)) {
157 $this->_formValues = $this->controller->exportValues($this->_name);
158 }
159 else {
160 $this->_formValues = $this->get('formValues');
161 }
162
163 if (empty($this->_formValues)) {
164 if (isset($this->_ssID)) {
165 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
166 }
167 }
168
169 if ($this->_force) {
170 $this->postProcess();
171 $this->set('force', 0);
172 }
173
174 $sortID = NULL;
175 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
176 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
177 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
178 );
179 }
180
181 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
182 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
183 $this->_action,
184 NULL,
185 $this->_single,
186 $this->_limit,
187 $this->_context
188 );
189 $prefix = NULL;
190 if ($this->_context == 'user') {
191 $prefix = $this->_prefix;
192 }
193
194 $this->assign("{$prefix}limit", $this->_limit);
195 $this->assign("{$prefix}single", $this->_single);
196
197 $controller = new CRM_Core_Selector_Controller($selector,
198 $this->get(CRM_Utils_Pager::PAGE_ID),
199 $sortID,
200 CRM_Core_Action::VIEW,
201 $this,
202 CRM_Core_Selector_Controller::TRANSFER,
203 $prefix
204 );
205 $controller->setEmbedded(TRUE);
206 $controller->moveFromSessionToTemplate();
207
208 $this->assign('summary', $this->get('summary'));
209 }
210
211 /**
212 * Build the form
213 *
214 * @access public
215 *
216 * @return void
217 */
218 function buildQuickForm() {
219 $this->addElement('text', 'sort_name', ts('Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
220
221 CRM_Grant_BAO_Query::buildSearchForm($this);
222
03e04002 223 /*
224 * add form checkboxes for each row. This is needed out here to conform to QF protocol
225 * of all elements being declared in builQuickForm
6a488035
TO
226 */
227
228
229 $rows = $this->get('rows');
230 if (is_array($rows)) {
b9599ed0 231 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
6a488035
TO
232 if (!$this->_single) {
233 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onchange' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);"));
234 foreach ($rows as $row) {
235 $this->addElement('checkbox', CRM_Utils_Array::value('checkbox', $row),
236 NULL, NULL,
237 array('onclick' => " toggleTaskAction( true ); return checkSelectedBox('" . CRM_Utils_Array::value('checkbox', $row) . "');")
238 );
239 $grant_id = $row['grant_id'];
240 }
241 }
242
243 $total = $cancel = 0;
244
245 $permission = CRM_Core_Permission::getPermission();
246
247 $tasks = array('' => ts('- actions -'));
248 $permissionedTask = CRM_Grant_Task::permissionedTaskTitles($permission);
249 if (is_array($permissionedTask) && !CRM_Utils_System::isNull($permissionedTask)) {
250 $tasks += $permissionedTask;
251 }
252
253 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
254 $this->add('submit', $this->_actionButtonName, ts('Go'),
255 array(
256 'class' => 'form-submit',
257 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
258 )
259 );
260
6a488035
TO
261 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
262 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
263 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('onchange' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );"));
264 }
265
266 // add buttons
267 $this->addButtons(array(
268 array(
269 'type' => 'refresh',
270 'name' => ts('Search'),
271 'isDefault' => TRUE,
272 ),
273 ));
274 }
275
276 /**
277 * The post processing of the form gets done here.
278 *
279 * Key things done during post processing are
280 * - check for reset or next request. if present, skip post procesing.
281 * - now check if user requested running a saved search, if so, then
282 * the form values associated with the saved search are used for searching.
283 * - if user has done a submit with new values the regular post submissing is
284 * done.
285 * The processing consists of using a Selector / Controller framework for getting the
286 * search results.
287 *
288 * @param
289 *
290 * @return void
291 * @access public
292 */
293 function postProcess() {
294 if ($this->_done) {
295 return;
296 }
297
298 $this->_done = TRUE;
299
300 $this->_formValues = $this->controller->exportValues($this->_name);
301 $this->fixFormValues();
302
303 if (isset($this->_ssID) && empty($_POST)) {
304 // if we are editing / running a saved search and the form has not been posted
305 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
306 }
307
308 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
309
310 $this->set('formValues', $this->_formValues);
311 $this->set('queryParams', $this->_queryParams);
312
313 $buttonName = $this->controller->getButtonName();
e341bbee 314 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
315 // check actionName and if next, then do not repeat a search, since we are going to the next page
316
317 // hack, make sure we reset the task values
95ea77ec 318 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
319 $formName = $stateMachine->getTaskFormName();
320 $this->controller->resetPage($formName);
321 return;
322 }
323
324 $sortID = NULL;
325 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
326 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
327 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
328 );
329 }
330
331
332 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
333 $this->_action,
334 NULL,
335 $this->_single,
336 $this->_limit,
337 $this->_context
338 );
339 $selector->setKey($this->controller->_key);
340
341 $prefix = NULL;
342 if ($this->_context == 'basic' || $this->_context == 'user') {
343 $prefix = $this->_prefix;
344 }
345
346 $controller = new CRM_Core_Selector_Controller($selector,
347 $this->get(CRM_Utils_Pager::PAGE_ID),
348 $sortID,
349 CRM_Core_Action::VIEW,
350 $this,
351 CRM_Core_Selector_Controller::SESSION,
352 $prefix
353 );
354 $controller->setEmbedded(TRUE);
355
356 $query = &$selector->getQuery();
357 if ($this->_context == 'user') {
358 $query->setSkipPermission(TRUE);
359 }
360 $controller->run();
361 }
362
363 /**
364 * Set the default form values
365 *
366 * @access protected
367 *
368 * @return array the default array reference
369 */
370 function &setDefaultValues() {
371 return $this->_formValues;
372 }
373
374 function fixFormValues() {
375 // if this search has been forced
376 // then see if there are any get values, and if so over-ride the post values
377 // note that this means that GET over-rides POST :)
378
379 if (!$this->_force) {
380 return;
381 }
382
383 $status = CRM_Utils_Request::retrieve('status', 'String',
384 CRM_Core_DAO::$_nullObject
385 );
386 if ($status) {
387 $this->_formValues['grant_status_id'] = $status;
388 $this->_defaults['grant_status_id'] = $status;
389 }
390
391 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
392
393 if ($cid) {
394 $cid = CRM_Utils_Type::escape($cid, 'Integer');
395 if ($cid > 0) {
396 $this->_formValues['contact_id'] = $cid;
397
398 // also assign individual mode to the template
399 $this->_single = TRUE;
400 }
401 }
402 }
403
404 function getFormValues() {
405 return NULL;
406 }
407
408 /**
409 * Return a descriptive name for the page, used in wizard header
410 *
411 * @return string
412 * @access public
413 */
414 public function getTitle() {
415 return ts('Find Grants');
416 }
417}
418