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