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