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