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