Merge pull request #7011 from saurabhbatra96/comment-fixes-financial
[civicrm-core.git] / CRM / Grant / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * Files required
38 */
39
40 /**
41 * This file is for civigrant search
42 */
43 class CRM_Grant_Form_Search extends CRM_Core_Form_Search {
44
45 /**
46 * The params that are sent to the query.
47 *
48 * @var array
49 */
50 protected $_queryParams;
51
52 /**
53 * Are we restricting ourselves to a single contact.
54 *
55 * @var boolean
56 */
57 protected $_single = FALSE;
58
59 /**
60 * Are we restricting ourselves to a single contact.
61 *
62 * @var boolean
63 */
64 protected $_limit = NULL;
65
66 /**
67 * Prefix for the controller.
68 */
69 protected $_prefix = "grant_";
70
71 /**
72 * Processing needed for buildForm and later.
73 *
74 * @return void
75 */
76 public function preProcess() {
77 /**
78 * set the button names
79 */
80 $this->_searchButtonName = $this->getButtonName('refresh');
81 $this->_actionButtonName = $this->getButtonName('next', 'action');
82
83 $this->_done = FALSE;
84 $this->defaults = array();
85
86 /*
87 * we allow the controller to set force/reset externally, useful when we are being
88 * driven by the wizard framework
89 */
90
91 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
92 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
93 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
94 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
95
96 $this->assign("context", $this->_context);
97
98 // get user submitted values
99 // get it from controller only if form has been submitted, else preProcess has set this
100 if (!empty($_POST)) {
101 $this->_formValues = $this->controller->exportValues($this->_name);
102 }
103 else {
104 $this->_formValues = $this->get('formValues');
105 }
106
107 if (empty($this->_formValues)) {
108 if (isset($this->_ssID)) {
109 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
110 }
111 }
112
113 if ($this->_force) {
114 $this->postProcess();
115 $this->set('force', 0);
116 }
117
118 $sortID = NULL;
119 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
120 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
121 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
122 );
123 }
124
125 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
126 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
127 $this->_action,
128 NULL,
129 $this->_single,
130 $this->_limit,
131 $this->_context
132 );
133 $prefix = NULL;
134 if ($this->_context == 'user') {
135 $prefix = $this->_prefix;
136 }
137
138 $this->assign("{$prefix}limit", $this->_limit);
139 $this->assign("{$prefix}single", $this->_single);
140
141 $controller = new CRM_Core_Selector_Controller($selector,
142 $this->get(CRM_Utils_Pager::PAGE_ID),
143 $sortID,
144 CRM_Core_Action::VIEW,
145 $this,
146 CRM_Core_Selector_Controller::TRANSFER,
147 $prefix
148 );
149 $controller->setEmbedded(TRUE);
150 $controller->moveFromSessionToTemplate();
151
152 $this->assign('summary', $this->get('summary'));
153 }
154
155 /**
156 * Build the form object.
157 *
158 *
159 * @return void
160 */
161 public function buildQuickForm() {
162 parent::buildQuickForm();
163 $this->addElement('text', 'sort_name', ts('Name or Email'), array('class' => 'twenty') + CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
164
165 CRM_Grant_BAO_Query::buildSearchForm($this);
166
167 $rows = $this->get('rows');
168 if (is_array($rows)) {
169 if (!$this->_single) {
170 $this->addRowSelectors($rows);
171 }
172
173 $permission = CRM_Core_Permission::getPermission();
174
175 $this->addTaskMenu(CRM_Grant_Task::permissionedTaskTitles($permission));
176 }
177
178 }
179
180 /**
181 * The post processing of the form gets done here.
182 *
183 * Key things done during post processing are
184 * - check for reset or next request. if present, skip post procesing.
185 * - now check if user requested running a saved search, if so, then
186 * the form values associated with the saved search are used for searching.
187 * - if user has done a submit with new values the regular post submissing is
188 * done.
189 * The processing consists of using a Selector / Controller framework for getting the
190 * search results.
191 *
192 * @param
193 *
194 * @return void
195 */
196 public function postProcess() {
197 if ($this->_done) {
198 return;
199 }
200
201 $this->_done = TRUE;
202
203 $this->_formValues = $this->controller->exportValues($this->_name);
204 $this->fixFormValues();
205
206 if (isset($this->_ssID) && empty($_POST)) {
207 // if we are editing / running a saved search and the form has not been posted
208 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
209 }
210
211 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
212
213 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
214
215 $this->set('formValues', $this->_formValues);
216 $this->set('queryParams', $this->_queryParams);
217
218 $buttonName = $this->controller->getButtonName();
219 if ($buttonName == $this->_actionButtonName) {
220 // check actionName and if next, then do not repeat a search, since we are going to the next page
221
222 // hack, make sure we reset the task values
223 $stateMachine = $this->controller->getStateMachine();
224 $formName = $stateMachine->getTaskFormName();
225 $this->controller->resetPage($formName);
226 return;
227 }
228
229 $sortID = NULL;
230 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
231 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
232 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
233 );
234 }
235
236 $selector = new CRM_Grant_Selector_Search($this->_queryParams,
237 $this->_action,
238 NULL,
239 $this->_single,
240 $this->_limit,
241 $this->_context
242 );
243 $selector->setKey($this->controller->_key);
244
245 $prefix = NULL;
246 if ($this->_context == 'basic' || $this->_context == 'user') {
247 $prefix = $this->_prefix;
248 }
249
250 $controller = new CRM_Core_Selector_Controller($selector,
251 $this->get(CRM_Utils_Pager::PAGE_ID),
252 $sortID,
253 CRM_Core_Action::VIEW,
254 $this,
255 CRM_Core_Selector_Controller::SESSION,
256 $prefix
257 );
258 $controller->setEmbedded(TRUE);
259
260 $query = &$selector->getQuery();
261 if ($this->_context == 'user') {
262 $query->setSkipPermission(TRUE);
263 }
264 $controller->run();
265 }
266
267 /**
268 * Set the default form values.
269 *
270 *
271 * @return array
272 * the default array reference
273 */
274 public function &setDefaultValues() {
275 return $this->_formValues;
276 }
277
278 public function fixFormValues() {
279 // if this search has been forced
280 // then see if there are any get values, and if so over-ride the post values
281 // note that this means that GET over-rides POST :)
282
283 if (!$this->_force) {
284 return;
285 }
286
287 $status = CRM_Utils_Request::retrieve('status', 'String',
288 CRM_Core_DAO::$_nullObject
289 );
290 if ($status) {
291 $this->_formValues['grant_status_id'] = $status;
292 $this->_defaults['grant_status_id'] = $status;
293 }
294
295 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
296
297 if ($cid) {
298 $cid = CRM_Utils_Type::escape($cid, 'Integer');
299 if ($cid > 0) {
300 $this->_formValues['contact_id'] = $cid;
301
302 // also assign individual mode to the template
303 $this->_single = TRUE;
304 }
305 }
306 }
307
308 /**
309 * @return null
310 */
311 public function getFormValues() {
312 return NULL;
313 }
314
315 /**
316 * Return a descriptive name for the page, used in wizard header
317 *
318 * @return string
319 */
320 public function getTitle() {
321 return ts('Find Grants');
322 }
323
324 }