Merge pull request #16091 from civicrm/5.21
[civicrm-core.git] / CRM / Grant / Form / Search.php
CommitLineData
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 21class 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 75 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
76 $this->_actionButtonName = $this->getButtonName('next', 'action');
77
78 $this->_done = FALSE;
c82067be 79 $this->sortNameOnly = TRUE;
6a488035 80
21443f0b 81 parent::preProcess();
6a488035 82
6a488035
TO
83 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
84 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
85 $this->_action,
86 NULL,
87 $this->_single,
88 $this->_limit,
89 $this->_context
90 );
91 $prefix = NULL;
1ec08f09 92 if ($this->_context === 'user') {
6a488035
TO
93 $prefix = $this->_prefix;
94 }
95
96 $this->assign("{$prefix}limit", $this->_limit);
97 $this->assign("{$prefix}single", $this->_single);
98
99 $controller = new CRM_Core_Selector_Controller($selector,
100 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 101 $this->getSortID(),
6a488035
TO
102 CRM_Core_Action::VIEW,
103 $this,
104 CRM_Core_Selector_Controller::TRANSFER,
105 $prefix
106 );
107 $controller->setEmbedded(TRUE);
108 $controller->moveFromSessionToTemplate();
109
110 $this->assign('summary', $this->get('summary'));
111 }
112
113 /**
fe482240 114 * Build the form object.
1ec08f09 115 *
116 * @throws \CRM_Core_Exception
117 * @throws \CiviCRM_API3_Exception
6a488035 118 */
00be9182 119 public function buildQuickForm() {
3efb5b86 120 parent::buildQuickForm();
e597fc33 121 $this->addSortNameField();
6a488035
TO
122
123 CRM_Grant_BAO_Query::buildSearchForm($this);
124
6a488035
TO
125 $rows = $this->get('rows');
126 if (is_array($rows)) {
127 if (!$this->_single) {
8d36b801 128 $this->addRowSelectors($rows);
6a488035
TO
129 }
130
7b9bce60 131 $this->addTaskMenu(CRM_Grant_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
6a488035
TO
132 }
133
6a488035
TO
134 }
135
136 /**
137 * The post processing of the form gets done here.
138 *
139 * Key things done during post processing are
140 * - check for reset or next request. if present, skip post procesing.
141 * - now check if user requested running a saved search, if so, then
142 * the form values associated with the saved search are used for searching.
143 * - if user has done a submit with new values the regular post submissing is
144 * done.
145 * The processing consists of using a Selector / Controller framework for getting the
146 * search results.
147 *
6a488035 148 * @return void
1ec08f09 149 * @throws \CRM_Core_Exception
6a488035 150 */
00be9182 151 public function postProcess() {
6a488035
TO
152 if ($this->_done) {
153 return;
154 }
155
156 $this->_done = TRUE;
157
21443f0b 158 $this->setFormValues();
6a488035
TO
159 $this->fixFormValues();
160
161 if (isset($this->_ssID) && empty($_POST)) {
162 // if we are editing / running a saved search and the form has not been posted
163 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
164 }
165
295bfbf8 166 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
167
6a488035
TO
168 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
169
6a488035
TO
170 $this->set('queryParams', $this->_queryParams);
171
172 $buttonName = $this->controller->getButtonName();
1ec08f09 173 if ($buttonName === $this->_actionButtonName) {
6a488035
TO
174 // check actionName and if next, then do not repeat a search, since we are going to the next page
175
176 // hack, make sure we reset the task values
95ea77ec 177 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
178 $formName = $stateMachine->getTaskFormName();
179 $this->controller->resetPage($formName);
180 return;
181 }
182
6a488035
TO
183 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
184 $this->_action,
185 NULL,
186 $this->_single,
187 $this->_limit,
188 $this->_context
189 );
190 $selector->setKey($this->controller->_key);
191
192 $prefix = NULL;
1ec08f09 193 if ($this->_context === 'basic' || $this->_context === 'user') {
6a488035
TO
194 $prefix = $this->_prefix;
195 }
196
197 $controller = new CRM_Core_Selector_Controller($selector,
198 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 199 $this->getSortID(),
6a488035
TO
200 CRM_Core_Action::VIEW,
201 $this,
202 CRM_Core_Selector_Controller::SESSION,
203 $prefix
204 );
205 $controller->setEmbedded(TRUE);
206
207 $query = &$selector->getQuery();
1ec08f09 208 if ($this->_context === 'user') {
6a488035
TO
209 $query->setSkipPermission(TRUE);
210 }
211 $controller->run();
212 }
213
1ec08f09 214 /**
215 * Hack form values :-(.
216 *
217 * @throws \CRM_Core_Exception
218 */
00be9182 219 public function fixFormValues() {
6a488035
TO
220 // if this search has been forced
221 // then see if there are any get values, and if so over-ride the post values
222 // note that this means that GET over-rides POST :)
223
224 if (!$this->_force) {
225 return;
226 }
227
1273d77c 228 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035
TO
229 if ($status) {
230 $this->_formValues['grant_status_id'] = $status;
231 $this->_defaults['grant_status_id'] = $status;
232 }
233
234 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
235
236 if ($cid) {
237 $cid = CRM_Utils_Type::escape($cid, 'Integer');
238 if ($cid > 0) {
239 $this->_formValues['contact_id'] = $cid;
240
241 // also assign individual mode to the template
242 $this->_single = TRUE;
243 }
244 }
245 }
246
6a488035
TO
247 /**
248 * Return a descriptive name for the page, used in wizard header
249 *
250 * @return string
6a488035
TO
251 */
252 public function getTitle() {
253 return ts('Find Grants');
254 }
96025800 255
0bcac7e7 256 /**
257 * Get metadata for fields being assigned by metadata.
258 *
259 * @return array
1ec08f09 260 *
261 * @throws \CiviCRM_API3_Exception
0bcac7e7 262 */
263 protected function getEntityMetadata() {
264 return CRM_Grant_BAO_Query::getSearchFieldMetadata();
265 }
266
18db97df 267 /**
268 * Set the metadata for the form.
269 *
270 * @throws \CiviCRM_API3_Exception
271 */
272 protected function setSearchMetadata() {
273 $this->addSearchFieldMetadata(['Grant' => $this->getEntityMetadata()]);
274 }
275
6a488035 276}