3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file is for CiviGrant search
21 class CRM_Grant_Form_Search
extends CRM_Core_Form_Search
{
24 * The params that are sent to the query.
28 protected $_queryParams;
31 * Are we restricting ourselves to a single contact.
35 protected $_single = FALSE;
45 * Prefix for the controller.
49 protected $_prefix = 'grant_';
52 * Metadata of all fields to include on the form.
56 protected $searchFieldMetadata = [];
61 public function getDefaultEntity() {
66 * Processing needed for buildForm and later.
68 * @throws \CRM_Core_Exception
69 * @throws \CiviCRM_API3_Exception
71 public function preProcess() {
73 * set the button names
75 $this->_searchButtonName
= $this->getButtonName('refresh');
76 $this->_actionButtonName
= $this->getButtonName('next', 'action');
79 $this->sortNameOnly
= TRUE;
83 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
84 $selector = new CRM_Grant_Selector_Search($this->_queryParams
,
92 if ($this->_context
=== 'user') {
93 $prefix = $this->_prefix
;
96 $this->assign("{$prefix}limit", $this->_limit
);
97 $this->assign("{$prefix}single", $this->_single
);
99 $controller = new CRM_Core_Selector_Controller($selector,
100 $this->get(CRM_Utils_Pager
::PAGE_ID
),
102 CRM_Core_Action
::VIEW
,
104 CRM_Core_Selector_Controller
::TRANSFER
,
107 $controller->setEmbedded(TRUE);
108 $controller->moveFromSessionToTemplate();
110 $this->assign('summary', $this->get('summary'));
114 * Build the form object.
116 * @throws \CRM_Core_Exception
117 * @throws \CiviCRM_API3_Exception
119 public function buildQuickForm() {
120 parent
::buildQuickForm();
121 $this->addSortNameField();
123 CRM_Grant_BAO_Query
::buildSearchForm($this);
125 $rows = $this->get('rows');
126 if (is_array($rows)) {
127 if (!$this->_single
) {
128 $this->addRowSelectors($rows);
131 $this->addTaskMenu(CRM_Grant_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
137 * The post processing of the form gets done here.
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
145 * The processing consists of using a Selector / Controller framework for getting the
149 * @throws \CRM_Core_Exception
151 public function postProcess() {
158 $this->setFormValues();
159 $this->fixFormValues();
161 if (isset($this->_ssID
) && empty($_POST)) {
162 // if we are editing / running a saved search and the form has not been posted
163 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
166 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
168 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
170 $this->set('queryParams', $this->_queryParams
);
172 $buttonName = $this->controller
->getButtonName();
173 if ($buttonName === $this->_actionButtonName
) {
174 // check actionName and if next, then do not repeat a search, since we are going to the next page
176 // hack, make sure we reset the task values
177 $stateMachine = $this->controller
->getStateMachine();
178 $formName = $stateMachine->getTaskFormName();
179 $this->controller
->resetPage($formName);
183 $selector = new CRM_Grant_Selector_Search($this->_queryParams
,
190 $selector->setKey($this->controller
->_key
);
193 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
194 $prefix = $this->_prefix
;
197 $controller = new CRM_Core_Selector_Controller($selector,
198 $this->get(CRM_Utils_Pager
::PAGE_ID
),
200 CRM_Core_Action
::VIEW
,
202 CRM_Core_Selector_Controller
::SESSION
,
205 $controller->setEmbedded(TRUE);
207 $query = &$selector->getQuery();
208 if ($this->_context
=== 'user') {
209 $query->setSkipPermission(TRUE);
215 * Hack form values :-(.
217 * @throws \CRM_Core_Exception
219 public function fixFormValues() {
220 // if this search has been forced
221 // then see if there are any get values, and if so over-ride the post values
222 // note that this means that GET over-rides POST :)
224 if (!$this->_force
) {
228 $status = CRM_Utils_Request
::retrieve('status', 'String');
230 $this->_formValues
['grant_status_id'] = $status;
231 $this->_defaults
['grant_status_id'] = $status;
234 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
237 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
239 $this->_formValues
['contact_id'] = $cid;
241 // also assign individual mode to the template
242 $this->_single
= TRUE;
248 * Return a descriptive name for the page, used in wizard header
252 public function getTitle() {
253 return ts('Find Grants');
257 * Get metadata for fields being assigned by metadata.
261 * @throws \CiviCRM_API3_Exception
263 protected function getEntityMetadata() {
264 return CRM_Grant_BAO_Query
::getSearchFieldMetadata();
268 * Set the metadata for the form.
270 * @throws \CiviCRM_API3_Exception
272 protected function setSearchMetadata() {
273 $this->addSearchFieldMetadata(['Grant' => $this->getEntityMetadata()]);