Merge pull request #14014 from MegaphoneJon/reporting-14
[civicrm-core.git] / CRM / Grant / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This file is for CiviGrant search
36 */
37 class CRM_Grant_Form_Search extends CRM_Core_Form_Search {
38
39 /**
40 * The params that are sent to the query.
41 *
42 * @var array
43 */
44 protected $_queryParams;
45
46 /**
47 * Are we restricting ourselves to a single contact.
48 *
49 * @var boolean
50 */
51 protected $_single = FALSE;
52
53 /**
54 * Return limit.
55 *
56 * @var int
57 */
58 protected $_limit = NULL;
59
60 /**
61 * Prefix for the controller.
62 * @var string
63 */
64 protected $_prefix = "grant_";
65
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 }
79
80 /**
81 * Processing needed for buildForm and later.
82 *
83 * @return void
84 */
85 public function preProcess() {
86 /**
87 * set the button names
88 */
89 $this->_searchButtonName = $this->getButtonName('refresh');
90 $this->_actionButtonName = $this->getButtonName('next', 'action');
91
92 $this->_done = FALSE;
93
94 $this->loadStandardSearchOptionsFromUrl();
95 $this->loadFormValues();
96
97 if ($this->_force) {
98 $this->postProcess();
99 $this->set('force', 0);
100 }
101
102 $sortID = NULL;
103 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
104 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
105 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
106 );
107 }
108
109 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
110 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
111 $this->_action,
112 NULL,
113 $this->_single,
114 $this->_limit,
115 $this->_context
116 );
117 $prefix = NULL;
118 if ($this->_context == 'user') {
119 $prefix = $this->_prefix;
120 }
121
122 $this->assign("{$prefix}limit", $this->_limit);
123 $this->assign("{$prefix}single", $this->_single);
124
125 $controller = new CRM_Core_Selector_Controller($selector,
126 $this->get(CRM_Utils_Pager::PAGE_ID),
127 $sortID,
128 CRM_Core_Action::VIEW,
129 $this,
130 CRM_Core_Selector_Controller::TRANSFER,
131 $prefix
132 );
133 $controller->setEmbedded(TRUE);
134 $controller->moveFromSessionToTemplate();
135
136 $this->assign('summary', $this->get('summary'));
137 }
138
139 /**
140 * Build the form object.
141 */
142 public function buildQuickForm() {
143 parent::buildQuickForm();
144 $this->addSortNameField();
145
146 CRM_Grant_BAO_Query::buildSearchForm($this);
147
148 $rows = $this->get('rows');
149 if (is_array($rows)) {
150 if (!$this->_single) {
151 $this->addRowSelectors($rows);
152 }
153
154 $this->addTaskMenu(CRM_Grant_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
155 }
156
157 }
158
159 /**
160 * The post processing of the form gets done here.
161 *
162 * Key things done during post processing are
163 * - check for reset or next request. if present, skip post procesing.
164 * - now check if user requested running a saved search, if so, then
165 * the form values associated with the saved search are used for searching.
166 * - if user has done a submit with new values the regular post submissing is
167 * done.
168 * The processing consists of using a Selector / Controller framework for getting the
169 * search results.
170 *
171 * @param
172 *
173 * @return void
174 */
175 public function postProcess() {
176 if ($this->_done) {
177 return;
178 }
179
180 $this->_done = TRUE;
181
182 $this->_formValues = $this->controller->exportValues($this->_name);
183 $this->fixFormValues();
184
185 if (isset($this->_ssID) && empty($_POST)) {
186 // if we are editing / running a saved search and the form has not been posted
187 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
188 }
189
190 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
191
192 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
193
194 $this->set('formValues', $this->_formValues);
195 $this->set('queryParams', $this->_queryParams);
196
197 $buttonName = $this->controller->getButtonName();
198 if ($buttonName == $this->_actionButtonName) {
199 // check actionName and if next, then do not repeat a search, since we are going to the next page
200
201 // hack, make sure we reset the task values
202 $stateMachine = $this->controller->getStateMachine();
203 $formName = $stateMachine->getTaskFormName();
204 $this->controller->resetPage($formName);
205 return;
206 }
207
208 $sortID = NULL;
209 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
210 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
211 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
212 );
213 }
214
215 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
216 $this->_action,
217 NULL,
218 $this->_single,
219 $this->_limit,
220 $this->_context
221 );
222 $selector->setKey($this->controller->_key);
223
224 $prefix = NULL;
225 if ($this->_context == 'basic' || $this->_context == 'user') {
226 $prefix = $this->_prefix;
227 }
228
229 $controller = new CRM_Core_Selector_Controller($selector,
230 $this->get(CRM_Utils_Pager::PAGE_ID),
231 $sortID,
232 CRM_Core_Action::VIEW,
233 $this,
234 CRM_Core_Selector_Controller::SESSION,
235 $prefix
236 );
237 $controller->setEmbedded(TRUE);
238
239 $query = &$selector->getQuery();
240 if ($this->_context == 'user') {
241 $query->setSkipPermission(TRUE);
242 }
243 $controller->run();
244 }
245
246 /**
247 * Set the default form values.
248 *
249 *
250 * @return array
251 * the default array reference
252 */
253 public function &setDefaultValues() {
254 return $this->_formValues;
255 }
256
257 public function fixFormValues() {
258 // if this search has been forced
259 // then see if there are any get values, and if so over-ride the post values
260 // note that this means that GET over-rides POST :)
261
262 if (!$this->_force) {
263 return;
264 }
265
266 $status = CRM_Utils_Request::retrieve('status', 'String');
267 if ($status) {
268 $this->_formValues['grant_status_id'] = $status;
269 $this->_defaults['grant_status_id'] = $status;
270 }
271
272 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
273
274 if ($cid) {
275 $cid = CRM_Utils_Type::escape($cid, 'Integer');
276 if ($cid > 0) {
277 $this->_formValues['contact_id'] = $cid;
278
279 // also assign individual mode to the template
280 $this->_single = TRUE;
281 }
282 }
283 }
284
285 /**
286 * @return null
287 */
288 public function getFormValues() {
289 return NULL;
290 }
291
292 /**
293 * Return a descriptive name for the page, used in wizard header
294 *
295 * @return string
296 */
297 public function getTitle() {
298 return ts('Find Grants');
299 }
300
301 /**
302 * Get metadata for fields being assigned by metadata.
303 *
304 * @return array
305 */
306 protected function getEntityMetadata() {
307 return CRM_Grant_BAO_Query::getSearchFieldMetadata();
308 }
309
310 }