Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-07-15-48-59
[civicrm-core.git] / CRM / Grant / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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, |
03e04002 24 | see the CiviCRM license FAQ at http://civicrm.org/licensing
6a488035
TO
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 * Files required
38 */
39
40/**
41 * This file is for civigrant search
42 */
3efb5b86 43class CRM_Grant_Form_Search extends CRM_Core_Form_Search {
6a488035 44
6a488035
TO
45 /**
46 * the params that are sent to the query
47 *
48 * @var array
49 * @access protected
50 */
51 protected $_queryParams;
52
6a488035
TO
53 /**
54 * are we restricting ourselves to a single contact
55 *
56 * @access protected
57 * @var boolean
58 */
59 protected $_single = FALSE;
60
61 /**
62 * are we restricting ourselves to a single contact
63 *
64 * @access protected
65 * @var boolean
66 */
67 protected $_limit = NULL;
68
6a488035
TO
69 /**
70 * prefix for the controller
71 *
72 */
73 protected $_prefix = "grant_";
74
75 protected $_defaults;
76
77 /**
78 * processing needed for buildForm and later
79 *
80 * @return void
81 * @access public
82 */ function preProcess() {
83
84 /**
85 * set the button names
86 */
87
88 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
89 $this->_actionButtonName = $this->getButtonName('next', 'action');
90
91 $this->_done = FALSE;
92 $this->defaults = array();
93
03e04002 94 /*
95 * we allow the controller to set force/reset externally, useful when we are being
96 * driven by the wizard framework
6a488035
TO
97 */
98
99 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
100 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
101 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
102 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
103
104 $this->assign("context", $this->_context);
105
106 // get user submitted values
107 // get it from controller only if form has been submitted, else preProcess has set this
108 if (!empty($_POST)) {
109 $this->_formValues = $this->controller->exportValues($this->_name);
110 }
111 else {
112 $this->_formValues = $this->get('formValues');
113 }
114
115 if (empty($this->_formValues)) {
116 if (isset($this->_ssID)) {
117 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
118 }
119 }
120
121 if ($this->_force) {
122 $this->postProcess();
123 $this->set('force', 0);
124 }
125
126 $sortID = NULL;
127 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
128 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
129 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
130 );
131 }
132
133 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
134 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
135 $this->_action,
136 NULL,
137 $this->_single,
138 $this->_limit,
139 $this->_context
140 );
141 $prefix = NULL;
142 if ($this->_context == 'user') {
143 $prefix = $this->_prefix;
144 }
145
146 $this->assign("{$prefix}limit", $this->_limit);
147 $this->assign("{$prefix}single", $this->_single);
148
149 $controller = new CRM_Core_Selector_Controller($selector,
150 $this->get(CRM_Utils_Pager::PAGE_ID),
151 $sortID,
152 CRM_Core_Action::VIEW,
153 $this,
154 CRM_Core_Selector_Controller::TRANSFER,
155 $prefix
156 );
157 $controller->setEmbedded(TRUE);
158 $controller->moveFromSessionToTemplate();
159
160 $this->assign('summary', $this->get('summary'));
161 }
162
163 /**
164 * Build the form
165 *
166 * @access public
167 *
168 * @return void
169 */
170 function buildQuickForm() {
3efb5b86 171 parent::buildQuickForm();
6a488035
TO
172 $this->addElement('text', 'sort_name', ts('Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
173
174 CRM_Grant_BAO_Query::buildSearchForm($this);
175
6a488035
TO
176 $rows = $this->get('rows');
177 if (is_array($rows)) {
178 if (!$this->_single) {
8d36b801 179 $this->addRowSelectors($rows);
6a488035
TO
180 }
181
6a488035
TO
182 $permission = CRM_Core_Permission::getPermission();
183
34197a55 184 $this->addTaskMenu(CRM_Grant_Task::permissionedTaskTitles($permission));
6a488035
TO
185 }
186
6a488035
TO
187 }
188
189 /**
190 * The post processing of the form gets done here.
191 *
192 * Key things done during post processing are
193 * - check for reset or next request. if present, skip post procesing.
194 * - now check if user requested running a saved search, if so, then
195 * the form values associated with the saved search are used for searching.
196 * - if user has done a submit with new values the regular post submissing is
197 * done.
198 * The processing consists of using a Selector / Controller framework for getting the
199 * search results.
200 *
201 * @param
202 *
203 * @return void
204 * @access public
205 */
206 function postProcess() {
207 if ($this->_done) {
208 return;
209 }
210
211 $this->_done = TRUE;
212
213 $this->_formValues = $this->controller->exportValues($this->_name);
214 $this->fixFormValues();
215
216 if (isset($this->_ssID) && empty($_POST)) {
217 // if we are editing / running a saved search and the form has not been posted
218 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
219 }
220
221 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
222
223 $this->set('formValues', $this->_formValues);
224 $this->set('queryParams', $this->_queryParams);
225
226 $buttonName = $this->controller->getButtonName();
e341bbee 227 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
228 // check actionName and if next, then do not repeat a search, since we are going to the next page
229
230 // hack, make sure we reset the task values
95ea77ec 231 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
232 $formName = $stateMachine->getTaskFormName();
233 $this->controller->resetPage($formName);
234 return;
235 }
236
237 $sortID = NULL;
238 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
239 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
240 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
241 );
242 }
243
244
245 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
246 $this->_action,
247 NULL,
248 $this->_single,
249 $this->_limit,
250 $this->_context
251 );
252 $selector->setKey($this->controller->_key);
253
254 $prefix = NULL;
255 if ($this->_context == 'basic' || $this->_context == 'user') {
256 $prefix = $this->_prefix;
257 }
258
259 $controller = new CRM_Core_Selector_Controller($selector,
260 $this->get(CRM_Utils_Pager::PAGE_ID),
261 $sortID,
262 CRM_Core_Action::VIEW,
263 $this,
264 CRM_Core_Selector_Controller::SESSION,
265 $prefix
266 );
267 $controller->setEmbedded(TRUE);
268
269 $query = &$selector->getQuery();
270 if ($this->_context == 'user') {
271 $query->setSkipPermission(TRUE);
272 }
273 $controller->run();
274 }
275
276 /**
277 * Set the default form values
278 *
279 * @access protected
280 *
281 * @return array the default array reference
282 */
283 function &setDefaultValues() {
284 return $this->_formValues;
285 }
286
287 function fixFormValues() {
288 // if this search has been forced
289 // then see if there are any get values, and if so over-ride the post values
290 // note that this means that GET over-rides POST :)
291
292 if (!$this->_force) {
293 return;
294 }
295
296 $status = CRM_Utils_Request::retrieve('status', 'String',
297 CRM_Core_DAO::$_nullObject
298 );
299 if ($status) {
300 $this->_formValues['grant_status_id'] = $status;
301 $this->_defaults['grant_status_id'] = $status;
302 }
303
304 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
305
306 if ($cid) {
307 $cid = CRM_Utils_Type::escape($cid, 'Integer');
308 if ($cid > 0) {
309 $this->_formValues['contact_id'] = $cid;
310
311 // also assign individual mode to the template
312 $this->_single = TRUE;
313 }
314 }
315 }
316
e0ef6999
EM
317 /**
318 * @return null
319 */
6a488035
TO
320 function getFormValues() {
321 return NULL;
322 }
323
324 /**
325 * Return a descriptive name for the page, used in wizard header
326 *
327 * @return string
328 * @access public
329 */
330 public function getTitle() {
331 return ts('Find Grants');
332 }
333}
334