Merge pull request #15734 from seamuslee001/remove_activity_option_join_custom_search
[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
TO
41 */
42 protected $_limit = NULL;
43
6a488035 44 /**
fe482240 45 * Prefix for the controller.
7e8c8317 46 * @var string
6a488035
TO
47 */
48 protected $_prefix = "grant_";
49
0bcac7e7 50 /**
51 * Metadata of all fields to include on the form.
52 *
53 * @var array
54 */
55 protected $searchFieldMetadata = [];
56
57 /**
58 * @return string
59 */
60 public function getDefaultEntity() {
61 return 'Grant';
62 }
09e47399 63
6a488035 64 /**
fe482240 65 * Processing needed for buildForm and later.
6a488035
TO
66 *
67 * @return void
95ea96be 68 */
8d7a9d07 69 public function preProcess() {
6a488035
TO
70 /**
71 * set the button names
72 */
6a488035 73 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
74 $this->_actionButtonName = $this->getButtonName('next', 'action');
75
76 $this->_done = FALSE;
6a488035 77
64ffcefd
SL
78 $this->loadStandardSearchOptionsFromUrl();
79 $this->loadFormValues();
6a488035
TO
80
81 if ($this->_force) {
82 $this->postProcess();
83 $this->set('force', 0);
84 }
85
6a488035
TO
86 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
87 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
88 $this->_action,
89 NULL,
90 $this->_single,
91 $this->_limit,
92 $this->_context
93 );
94 $prefix = NULL;
95 if ($this->_context == 'user') {
96 $prefix = $this->_prefix;
97 }
98
99 $this->assign("{$prefix}limit", $this->_limit);
100 $this->assign("{$prefix}single", $this->_single);
101
102 $controller = new CRM_Core_Selector_Controller($selector,
103 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 104 $this->getSortID(),
6a488035
TO
105 CRM_Core_Action::VIEW,
106 $this,
107 CRM_Core_Selector_Controller::TRANSFER,
108 $prefix
109 );
110 $controller->setEmbedded(TRUE);
111 $controller->moveFromSessionToTemplate();
112
113 $this->assign('summary', $this->get('summary'));
114 }
115
116 /**
fe482240 117 * Build the form object.
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 *
148 * @param
149 *
150 * @return void
6a488035 151 */
00be9182 152 public function postProcess() {
6a488035
TO
153 if ($this->_done) {
154 return;
155 }
156
157 $this->_done = TRUE;
158
159 $this->_formValues = $this->controller->exportValues($this->_name);
160 $this->fixFormValues();
161
162 if (isset($this->_ssID) && empty($_POST)) {
163 // if we are editing / running a saved search and the form has not been posted
164 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
165 }
166
295bfbf8 167 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
168
6a488035
TO
169 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
170
171 $this->set('formValues', $this->_formValues);
172 $this->set('queryParams', $this->_queryParams);
173
174 $buttonName = $this->controller->getButtonName();
e341bbee 175 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
176 // check actionName and if next, then do not repeat a search, since we are going to the next page
177
178 // hack, make sure we reset the task values
95ea77ec 179 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
180 $formName = $stateMachine->getTaskFormName();
181 $this->controller->resetPage($formName);
182 return;
183 }
184
6a488035
TO
185 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
186 $this->_action,
187 NULL,
188 $this->_single,
189 $this->_limit,
190 $this->_context
191 );
192 $selector->setKey($this->controller->_key);
193
194 $prefix = NULL;
195 if ($this->_context == 'basic' || $this->_context == 'user') {
196 $prefix = $this->_prefix;
197 }
198
199 $controller = new CRM_Core_Selector_Controller($selector,
200 $this->get(CRM_Utils_Pager::PAGE_ID),
b5c63125 201 $this->getSortID(),
6a488035
TO
202 CRM_Core_Action::VIEW,
203 $this,
204 CRM_Core_Selector_Controller::SESSION,
205 $prefix
206 );
207 $controller->setEmbedded(TRUE);
208
209 $query = &$selector->getQuery();
210 if ($this->_context == 'user') {
211 $query->setSkipPermission(TRUE);
212 }
213 $controller->run();
214 }
215
216 /**
fe482240 217 * Set the default form values.
6a488035 218 *
6a488035 219 *
a6c01b45
CW
220 * @return array
221 * the default array reference
6a488035 222 */
00be9182 223 public function &setDefaultValues() {
6a488035
TO
224 return $this->_formValues;
225 }
226
00be9182 227 public function fixFormValues() {
6a488035
TO
228 // if this search has been forced
229 // then see if there are any get values, and if so over-ride the post values
230 // note that this means that GET over-rides POST :)
231
232 if (!$this->_force) {
233 return;
234 }
235
1273d77c 236 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035
TO
237 if ($status) {
238 $this->_formValues['grant_status_id'] = $status;
239 $this->_defaults['grant_status_id'] = $status;
240 }
241
242 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
243
244 if ($cid) {
245 $cid = CRM_Utils_Type::escape($cid, 'Integer');
246 if ($cid > 0) {
247 $this->_formValues['contact_id'] = $cid;
248
249 // also assign individual mode to the template
250 $this->_single = TRUE;
251 }
252 }
253 }
254
e0ef6999
EM
255 /**
256 * @return null
257 */
00be9182 258 public function getFormValues() {
6a488035
TO
259 return NULL;
260 }
261
262 /**
263 * Return a descriptive name for the page, used in wizard header
264 *
265 * @return string
6a488035
TO
266 */
267 public function getTitle() {
268 return ts('Find Grants');
269 }
96025800 270
0bcac7e7 271 /**
272 * Get metadata for fields being assigned by metadata.
273 *
274 * @return array
275 */
276 protected function getEntityMetadata() {
277 return CRM_Grant_BAO_Query::getSearchFieldMetadata();
278 }
279
18db97df 280 /**
281 * Set the metadata for the form.
282 *
283 * @throws \CiviCRM_API3_Exception
284 */
285 protected function setSearchMetadata() {
286 $this->addSearchFieldMetadata(['Grant' => $this->getEntityMetadata()]);
287 }
288
6a488035 289}