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