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