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->_actionButtonName
= $this->getButtonName('next', 'action');
78 $this->sortNameOnly
= TRUE;
82 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
83 $selector = new CRM_Grant_Selector_Search($this->_queryParams
,
91 if ($this->_context
=== 'user') {
92 $prefix = $this->_prefix
;
95 $this->assign("{$prefix}limit", $this->_limit
);
96 $this->assign("{$prefix}single", $this->_single
);
98 $controller = new CRM_Core_Selector_Controller($selector,
99 $this->get(CRM_Utils_Pager
::PAGE_ID
),
101 CRM_Core_Action
::VIEW
,
103 CRM_Core_Selector_Controller
::TRANSFER
,
106 $controller->setEmbedded(TRUE);
107 $controller->moveFromSessionToTemplate();
109 $this->assign('summary', $this->get('summary'));
113 * Build the form object.
115 * @throws \CRM_Core_Exception
116 * @throws \CiviCRM_API3_Exception
118 public function buildQuickForm() {
119 parent
::buildQuickForm();
120 $this->addSortNameField();
122 CRM_Grant_BAO_Query
::buildSearchForm($this);
124 $rows = $this->get('rows');
125 if (is_array($rows)) {
126 if (!$this->_single
) {
127 $this->addRowSelectors($rows);
130 $this->addTaskMenu(CRM_Grant_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
136 * The post processing of the form gets done here.
138 * Key things done during post processing are
139 * - check for reset or next request. if present, skip post procesing.
140 * - now check if user requested running a saved search, if so, then
141 * the form values associated with the saved search are used for searching.
142 * - if user has done a submit with new values the regular post submissing is
144 * The processing consists of using a Selector / Controller framework for getting the
148 * @throws \CRM_Core_Exception
150 public function postProcess() {
157 $this->setFormValues();
158 $this->fixFormValues();
160 if (isset($this->_ssID
) && empty($_POST)) {
161 // if we are editing / running a saved search and the form has not been posted
162 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
165 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
167 $this->set('queryParams', $this->_queryParams
);
169 $buttonName = $this->controller
->getButtonName();
170 if ($buttonName === $this->_actionButtonName
) {
171 // check actionName and if next, then do not repeat a search, since we are going to the next page
173 // hack, make sure we reset the task values
174 $stateMachine = $this->controller
->getStateMachine();
175 $formName = $stateMachine->getTaskFormName();
176 $this->controller
->resetPage($formName);
180 $selector = new CRM_Grant_Selector_Search($this->_queryParams
,
187 $selector->setKey($this->controller
->_key
);
190 if ($this->_context
=== 'basic' ||
$this->_context
=== 'user') {
191 $prefix = $this->_prefix
;
194 $controller = new CRM_Core_Selector_Controller($selector,
195 $this->get(CRM_Utils_Pager
::PAGE_ID
),
197 CRM_Core_Action
::VIEW
,
199 CRM_Core_Selector_Controller
::SESSION
,
202 $controller->setEmbedded(TRUE);
204 $query = &$selector->getQuery();
205 if ($this->_context
=== 'user') {
206 $query->setSkipPermission(TRUE);
212 * Hack form values :-(.
214 * @throws \CRM_Core_Exception
216 public function fixFormValues() {
217 // if this search has been forced
218 // then see if there are any get values, and if so over-ride the post values
219 // note that this means that GET over-rides POST :)
221 if (!$this->_force
) {
225 $status = CRM_Utils_Request
::retrieve('status', 'String');
227 $this->_formValues
['grant_status_id'] = $status;
228 $this->_defaults
['grant_status_id'] = $status;
231 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
234 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
236 $this->_formValues
['contact_id'] = $cid;
238 // also assign individual mode to the template
239 $this->_single
= TRUE;
245 * Return a descriptive name for the page, used in wizard header
249 public function getTitle() {
250 return ts('Find Grants');
254 * Get metadata for fields being assigned by metadata.
258 * @throws \CiviCRM_API3_Exception
260 protected function getEntityMetadata() {
261 return CRM_Grant_BAO_Query
::getSearchFieldMetadata();
265 * Set the metadata for the form.
267 * @throws \CiviCRM_API3_Exception
269 protected function setSearchMetadata() {
270 $this->addSearchFieldMetadata(['Grant' => $this->getEntityMetadata()]);