Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
006389de | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
09e47399 | 19 | * This file is for CiviGrant search |
6a488035 | 20 | */ |
3efb5b86 | 21 | class CRM_Grant_Form_Search extends CRM_Core_Form_Search { |
6a488035 | 22 | |
6a488035 | 23 | /** |
fe482240 | 24 | * The params that are sent to the query. |
6a488035 TO |
25 | * |
26 | * @var array | |
6a488035 TO |
27 | */ |
28 | protected $_queryParams; | |
29 | ||
6a488035 | 30 | /** |
fe482240 | 31 | * Are we restricting ourselves to a single contact. |
6a488035 | 32 | * |
d51c6add | 33 | * @var bool |
6a488035 TO |
34 | */ |
35 | protected $_single = FALSE; | |
36 | ||
37 | /** | |
09e47399 | 38 | * Return limit. |
6a488035 | 39 | * |
09e47399 | 40 | * @var int |
6a488035 | 41 | */ |
1ec08f09 | 42 | protected $_limit; |
6a488035 | 43 | |
6a488035 | 44 | /** |
fe482240 | 45 | * Prefix for the controller. |
1ec08f09 | 46 | * |
7e8c8317 | 47 | * @var string |
6a488035 | 48 | */ |
1ec08f09 | 49 | protected $_prefix = 'grant_'; |
6a488035 | 50 | |
0bcac7e7 | 51 | /** |
52 | * Metadata of all fields to include on the form. | |
53 | * | |
54 | * @var array | |
55 | */ | |
56 | protected $searchFieldMetadata = []; | |
57 | ||
58 | /** | |
59 | * @return string | |
60 | */ | |
61 | public function getDefaultEntity() { | |
62 | return 'Grant'; | |
63 | } | |
09e47399 | 64 | |
6a488035 | 65 | /** |
fe482240 | 66 | * Processing needed for buildForm and later. |
6a488035 | 67 | * |
1ec08f09 | 68 | * @throws \CRM_Core_Exception |
69 | * @throws \CiviCRM_API3_Exception | |
95ea96be | 70 | */ |
8d7a9d07 | 71 | public function preProcess() { |
6a488035 TO |
72 | /** |
73 | * set the button names | |
74 | */ | |
6a488035 TO |
75 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
76 | ||
77 | $this->_done = FALSE; | |
c82067be | 78 | $this->sortNameOnly = TRUE; |
6a488035 | 79 | |
21443f0b | 80 | parent::preProcess(); |
6a488035 | 81 | |
6a488035 TO |
82 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
83 | $selector = new CRM_Grant_Selector_Search($this->_queryParams, | |
84 | $this->_action, | |
85 | NULL, | |
86 | $this->_single, | |
87 | $this->_limit, | |
88 | $this->_context | |
89 | ); | |
90 | $prefix = NULL; | |
1ec08f09 | 91 | if ($this->_context === 'user') { |
6a488035 TO |
92 | $prefix = $this->_prefix; |
93 | } | |
94 | ||
95 | $this->assign("{$prefix}limit", $this->_limit); | |
96 | $this->assign("{$prefix}single", $this->_single); | |
97 | ||
98 | $controller = new CRM_Core_Selector_Controller($selector, | |
99 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
b5c63125 | 100 | $this->getSortID(), |
6a488035 TO |
101 | CRM_Core_Action::VIEW, |
102 | $this, | |
103 | CRM_Core_Selector_Controller::TRANSFER, | |
104 | $prefix | |
105 | ); | |
106 | $controller->setEmbedded(TRUE); | |
107 | $controller->moveFromSessionToTemplate(); | |
108 | ||
109 | $this->assign('summary', $this->get('summary')); | |
110 | } | |
111 | ||
112 | /** | |
fe482240 | 113 | * Build the form object. |
1ec08f09 | 114 | * |
115 | * @throws \CRM_Core_Exception | |
116 | * @throws \CiviCRM_API3_Exception | |
6a488035 | 117 | */ |
00be9182 | 118 | public function buildQuickForm() { |
3efb5b86 | 119 | parent::buildQuickForm(); |
e597fc33 | 120 | $this->addSortNameField(); |
6a488035 TO |
121 | |
122 | CRM_Grant_BAO_Query::buildSearchForm($this); | |
123 | ||
6a488035 TO |
124 | $rows = $this->get('rows'); |
125 | if (is_array($rows)) { | |
126 | if (!$this->_single) { | |
8d36b801 | 127 | $this->addRowSelectors($rows); |
6a488035 TO |
128 | } |
129 | ||
7b9bce60 | 130 | $this->addTaskMenu(CRM_Grant_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission())); |
6a488035 TO |
131 | } |
132 | ||
6a488035 TO |
133 | } |
134 | ||
135 | /** | |
136 | * The post processing of the form gets done here. | |
137 | * | |
138 | * Key things done during post processing are | |
139 | * - check for reset or next request. if present, skip post procesing. | |
140 | * - now check if user requested running a saved search, if so, then | |
141 | * the form values associated with the saved search are used for searching. | |
142 | * - if user has done a submit with new values the regular post submissing is | |
143 | * done. | |
144 | * The processing consists of using a Selector / Controller framework for getting the | |
145 | * search results. | |
146 | * | |
6a488035 | 147 | * @return void |
1ec08f09 | 148 | * @throws \CRM_Core_Exception |
6a488035 | 149 | */ |
00be9182 | 150 | public function postProcess() { |
6a488035 TO |
151 | if ($this->_done) { |
152 | return; | |
153 | } | |
154 | ||
155 | $this->_done = TRUE; | |
156 | ||
21443f0b | 157 | $this->setFormValues(); |
6a488035 TO |
158 | $this->fixFormValues(); |
159 | ||
160 | if (isset($this->_ssID) && empty($_POST)) { | |
161 | // if we are editing / running a saved search and the form has not been posted | |
162 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
163 | } | |
164 | ||
165 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
166 | ||
6a488035 TO |
167 | $this->set('queryParams', $this->_queryParams); |
168 | ||
169 | $buttonName = $this->controller->getButtonName(); | |
1ec08f09 | 170 | if ($buttonName === $this->_actionButtonName) { |
6a488035 TO |
171 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
172 | ||
173 | // hack, make sure we reset the task values | |
95ea77ec | 174 | $stateMachine = $this->controller->getStateMachine(); |
6a488035 TO |
175 | $formName = $stateMachine->getTaskFormName(); |
176 | $this->controller->resetPage($formName); | |
177 | return; | |
178 | } | |
179 | ||
6a488035 TO |
180 | $selector = new CRM_Grant_Selector_Search($this->_queryParams, |
181 | $this->_action, | |
182 | NULL, | |
183 | $this->_single, | |
184 | $this->_limit, | |
185 | $this->_context | |
186 | ); | |
187 | $selector->setKey($this->controller->_key); | |
188 | ||
189 | $prefix = NULL; | |
1ec08f09 | 190 | if ($this->_context === 'basic' || $this->_context === 'user') { |
6a488035 TO |
191 | $prefix = $this->_prefix; |
192 | } | |
193 | ||
194 | $controller = new CRM_Core_Selector_Controller($selector, | |
195 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
b5c63125 | 196 | $this->getSortID(), |
6a488035 TO |
197 | CRM_Core_Action::VIEW, |
198 | $this, | |
199 | CRM_Core_Selector_Controller::SESSION, | |
200 | $prefix | |
201 | ); | |
202 | $controller->setEmbedded(TRUE); | |
203 | ||
204 | $query = &$selector->getQuery(); | |
1ec08f09 | 205 | if ($this->_context === 'user') { |
6a488035 TO |
206 | $query->setSkipPermission(TRUE); |
207 | } | |
208 | $controller->run(); | |
209 | } | |
210 | ||
1ec08f09 | 211 | /** |
212 | * Hack form values :-(. | |
213 | * | |
214 | * @throws \CRM_Core_Exception | |
215 | */ | |
00be9182 | 216 | public function fixFormValues() { |
6a488035 TO |
217 | // if this search has been forced |
218 | // then see if there are any get values, and if so over-ride the post values | |
219 | // note that this means that GET over-rides POST :) | |
220 | ||
221 | if (!$this->_force) { | |
222 | return; | |
223 | } | |
224 | ||
1273d77c | 225 | $status = CRM_Utils_Request::retrieve('status', 'String'); |
6a488035 TO |
226 | if ($status) { |
227 | $this->_formValues['grant_status_id'] = $status; | |
228 | $this->_defaults['grant_status_id'] = $status; | |
229 | } | |
230 | ||
231 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
232 | ||
233 | if ($cid) { | |
234 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); | |
235 | if ($cid > 0) { | |
236 | $this->_formValues['contact_id'] = $cid; | |
237 | ||
238 | // also assign individual mode to the template | |
239 | $this->_single = TRUE; | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
6a488035 TO |
244 | /** |
245 | * Return a descriptive name for the page, used in wizard header | |
246 | * | |
247 | * @return string | |
6a488035 TO |
248 | */ |
249 | public function getTitle() { | |
250 | return ts('Find Grants'); | |
251 | } | |
96025800 | 252 | |
0bcac7e7 | 253 | /** |
254 | * Get metadata for fields being assigned by metadata. | |
255 | * | |
256 | * @return array | |
1ec08f09 | 257 | * |
258 | * @throws \CiviCRM_API3_Exception | |
0bcac7e7 | 259 | */ |
260 | protected function getEntityMetadata() { | |
261 | return CRM_Grant_BAO_Query::getSearchFieldMetadata(); | |
262 | } | |
263 | ||
18db97df | 264 | /** |
265 | * Set the metadata for the form. | |
266 | * | |
267 | * @throws \CiviCRM_API3_Exception | |
268 | */ | |
269 | protected function setSearchMetadata() { | |
270 | $this->addSearchFieldMetadata(['Grant' => $this->getEntityMetadata()]); | |
271 | } | |
272 | ||
6a488035 | 273 | } |