Merge pull request #16095 from seamuslee001/dev_core_1450
[civicrm-core.git] / CRM / Grant / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This file is for CiviGrant search
20 */
21 class CRM_Grant_Form_Search extends CRM_Core_Form_Search {
22
23 /**
24 * The params that are sent to the query.
25 *
26 * @var array
27 */
28 protected $_queryParams;
29
30 /**
31 * Are we restricting ourselves to a single contact.
32 *
33 * @var bool
34 */
35 protected $_single = FALSE;
36
37 /**
38 * Return limit.
39 *
40 * @var int
41 */
42 protected $_limit;
43
44 /**
45 * Prefix for the controller.
46 *
47 * @var string
48 */
49 protected $_prefix = 'grant_';
50
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 }
64
65 /**
66 * Processing needed for buildForm and later.
67 *
68 * @throws \CRM_Core_Exception
69 * @throws \CiviCRM_API3_Exception
70 */
71 public function preProcess() {
72 /**
73 * set the button names
74 */
75 $this->_actionButtonName = $this->getButtonName('next', 'action');
76
77 $this->_done = FALSE;
78 $this->sortNameOnly = TRUE;
79
80 parent::preProcess();
81
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;
91 if ($this->_context === 'user') {
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),
100 $this->getSortID(),
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 /**
113 * Build the form object.
114 *
115 * @throws \CRM_Core_Exception
116 * @throws \CiviCRM_API3_Exception
117 */
118 public function buildQuickForm() {
119 parent::buildQuickForm();
120 $this->addSortNameField();
121
122 CRM_Grant_BAO_Query::buildSearchForm($this);
123
124 $rows = $this->get('rows');
125 if (is_array($rows)) {
126 if (!$this->_single) {
127 $this->addRowSelectors($rows);
128 }
129
130 $this->addTaskMenu(CRM_Grant_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
131 }
132
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 *
147 * @return void
148 * @throws \CRM_Core_Exception
149 */
150 public function postProcess() {
151 if ($this->_done) {
152 return;
153 }
154
155 $this->_done = TRUE;
156
157 $this->setFormValues();
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 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
166
167 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
168
169 $this->set('queryParams', $this->_queryParams);
170
171 $buttonName = $this->controller->getButtonName();
172 if ($buttonName === $this->_actionButtonName) {
173 // check actionName and if next, then do not repeat a search, since we are going to the next page
174
175 // hack, make sure we reset the task values
176 $stateMachine = $this->controller->getStateMachine();
177 $formName = $stateMachine->getTaskFormName();
178 $this->controller->resetPage($formName);
179 return;
180 }
181
182 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
183 $this->_action,
184 NULL,
185 $this->_single,
186 $this->_limit,
187 $this->_context
188 );
189 $selector->setKey($this->controller->_key);
190
191 $prefix = NULL;
192 if ($this->_context === 'basic' || $this->_context === 'user') {
193 $prefix = $this->_prefix;
194 }
195
196 $controller = new CRM_Core_Selector_Controller($selector,
197 $this->get(CRM_Utils_Pager::PAGE_ID),
198 $this->getSortID(),
199 CRM_Core_Action::VIEW,
200 $this,
201 CRM_Core_Selector_Controller::SESSION,
202 $prefix
203 );
204 $controller->setEmbedded(TRUE);
205
206 $query = &$selector->getQuery();
207 if ($this->_context === 'user') {
208 $query->setSkipPermission(TRUE);
209 }
210 $controller->run();
211 }
212
213 /**
214 * Hack form values :-(.
215 *
216 * @throws \CRM_Core_Exception
217 */
218 public function fixFormValues() {
219 // if this search has been forced
220 // then see if there are any get values, and if so over-ride the post values
221 // note that this means that GET over-rides POST :)
222
223 if (!$this->_force) {
224 return;
225 }
226
227 $status = CRM_Utils_Request::retrieve('status', 'String');
228 if ($status) {
229 $this->_formValues['grant_status_id'] = $status;
230 $this->_defaults['grant_status_id'] = $status;
231 }
232
233 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
234
235 if ($cid) {
236 $cid = CRM_Utils_Type::escape($cid, 'Integer');
237 if ($cid > 0) {
238 $this->_formValues['contact_id'] = $cid;
239
240 // also assign individual mode to the template
241 $this->_single = TRUE;
242 }
243 }
244 }
245
246 /**
247 * Return a descriptive name for the page, used in wizard header
248 *
249 * @return string
250 */
251 public function getTitle() {
252 return ts('Find Grants');
253 }
254
255 /**
256 * Get metadata for fields being assigned by metadata.
257 *
258 * @return array
259 *
260 * @throws \CiviCRM_API3_Exception
261 */
262 protected function getEntityMetadata() {
263 return CRM_Grant_BAO_Query::getSearchFieldMetadata();
264 }
265
266 /**
267 * Set the metadata for the form.
268 *
269 * @throws \CiviCRM_API3_Exception
270 */
271 protected function setSearchMetadata() {
272 $this->addSearchFieldMetadata(['Grant' => $this->getEntityMetadata()]);
273 }
274
275 }