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;
42 protected $_limit = NULL;
45 * Prefix for the controller.
48 protected $_prefix = "grant_";
51 * Metadata of all fields to include on the form.
55 protected $searchFieldMetadata = [];
60 public function getDefaultEntity() {
65 * Processing needed for buildForm and later.
69 public function preProcess() {
71 * set the button names
73 $this->_searchButtonName
= $this->getButtonName('refresh');
74 $this->_actionButtonName
= $this->getButtonName('next', 'action');
80 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
81 $selector = new CRM_Grant_Selector_Search($this->_queryParams
,
89 if ($this->_context
== 'user') {
90 $prefix = $this->_prefix
;
93 $this->assign("{$prefix}limit", $this->_limit
);
94 $this->assign("{$prefix}single", $this->_single
);
96 $controller = new CRM_Core_Selector_Controller($selector,
97 $this->get(CRM_Utils_Pager
::PAGE_ID
),
99 CRM_Core_Action
::VIEW
,
101 CRM_Core_Selector_Controller
::TRANSFER
,
104 $controller->setEmbedded(TRUE);
105 $controller->moveFromSessionToTemplate();
107 $this->assign('summary', $this->get('summary'));
111 * Build the form object.
113 public function buildQuickForm() {
114 parent
::buildQuickForm();
115 $this->addSortNameField();
117 CRM_Grant_BAO_Query
::buildSearchForm($this);
119 $rows = $this->get('rows');
120 if (is_array($rows)) {
121 if (!$this->_single
) {
122 $this->addRowSelectors($rows);
125 $this->addTaskMenu(CRM_Grant_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
131 * The post processing of the form gets done here.
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
139 * The processing consists of using a Selector / Controller framework for getting the
146 public function postProcess() {
153 $this->setFormValues();
154 $this->fixFormValues();
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
);
161 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
163 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
165 $this->set('queryParams', $this->_queryParams
);
167 $buttonName = $this->controller
->getButtonName();
168 if ($buttonName == $this->_actionButtonName
) {
169 // check actionName and if next, then do not repeat a search, since we are going to the next page
171 // hack, make sure we reset the task values
172 $stateMachine = $this->controller
->getStateMachine();
173 $formName = $stateMachine->getTaskFormName();
174 $this->controller
->resetPage($formName);
178 $selector = new CRM_Grant_Selector_Search($this->_queryParams
,
185 $selector->setKey($this->controller
->_key
);
188 if ($this->_context
== 'basic' ||
$this->_context
== 'user') {
189 $prefix = $this->_prefix
;
192 $controller = new CRM_Core_Selector_Controller($selector,
193 $this->get(CRM_Utils_Pager
::PAGE_ID
),
195 CRM_Core_Action
::VIEW
,
197 CRM_Core_Selector_Controller
::SESSION
,
200 $controller->setEmbedded(TRUE);
202 $query = &$selector->getQuery();
203 if ($this->_context
== 'user') {
204 $query->setSkipPermission(TRUE);
209 public function fixFormValues() {
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 :)
214 if (!$this->_force
) {
218 $status = CRM_Utils_Request
::retrieve('status', 'String');
220 $this->_formValues
['grant_status_id'] = $status;
221 $this->_defaults
['grant_status_id'] = $status;
224 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
227 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
229 $this->_formValues
['contact_id'] = $cid;
231 // also assign individual mode to the template
232 $this->_single
= TRUE;
238 * Return a descriptive name for the page, used in wizard header
242 public function getTitle() {
243 return ts('Find Grants');
247 * Get metadata for fields being assigned by metadata.
251 protected function getEntityMetadata() {
252 return CRM_Grant_BAO_Query
::getSearchFieldMetadata();
256 * Set the metadata for the form.
258 * @throws \CiviCRM_API3_Exception
260 protected function setSearchMetadata() {
261 $this->addSearchFieldMetadata(['Grant' => $this->getEntityMetadata()]);