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