CRM-17022 fix - Bug introduced in 4.6.6 breaks Grant Search
[civicrm-core.git] / CRM / Member / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Files required
38 */
39
40/**
41 * This file is for civimember search
42 */
3efb5b86 43class CRM_Member_Form_Search extends CRM_Core_Form_Search {
6a488035 44
6a488035 45 /**
fe482240 46 * The params that are sent to the query.
6a488035
TO
47 *
48 * @var array
6a488035
TO
49 */
50 protected $_queryParams;
51
6a488035 52 /**
fe482240 53 * Are we restricting ourselves to a single contact.
6a488035 54 *
6a488035
TO
55 * @var boolean
56 */
57 protected $_single = FALSE;
58
59 /**
fe482240 60 * Are we restricting ourselves to a single contact.
6a488035 61 *
6a488035
TO
62 * @var boolean
63 */
64 protected $_limit = NULL;
65
6a488035
TO
66 protected $_defaults;
67
68 /**
fe482240 69 * Prefix for the controller.
6a488035
TO
70 */
71 protected $_prefix = "member_";
72
73 /**
fe482240 74 * Processing needed for buildForm and later.
6a488035
TO
75 *
76 * @return void
03e04002 77 */
00be9182 78 public function preProcess() {
6a488035
TO
79 $this->set('searchFormName', 'Search');
80
81 /**
82 * set the button names
83 */
84 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
85 $this->_actionButtonName = $this->getButtonName('next', 'action');
86
87 $this->_done = FALSE;
88
89 $this->defaults = array();
90
03e04002 91 /*
d424ffde
CW
92 * we allow the controller to set force/reset externally, useful when we are being
93 * driven by the wizard framework
94 */
6a488035 95
353ffa53
TO
96 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
97 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
98 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
6a488035
TO
99 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
100
101 $this->assign("context", $this->_context);
102
103 // get user submitted values
104 // get it from controller only if form has been submitted, else preProcess has set this
105 if (!empty($_POST)) {
106 $this->_formValues = $this->controller->exportValues($this->_name);
107 }
108 else {
109 $this->_formValues = $this->get('formValues');
110 }
111
112 if ($this->_force) {
113 $this->postProcess();
114 $this->set('force', 0);
115 }
116
117 $sortID = NULL;
118 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
119 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
120 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
121 );
122 }
123
124 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
125 $selector = new CRM_Member_Selector_Search($this->_queryParams,
126 $this->_action,
127 NULL,
128 $this->_single,
129 $this->_limit,
130 $this->_context
131 );
132 $prefix = NULL;
133 if ($this->_context == 'basic') {
134 $prefix = $this->_prefix;
135 }
136
137 $this->assign("{$prefix}limit", $this->_limit);
138 $this->assign("{$prefix}single", $this->_single);
139
140 $controller = new CRM_Core_Selector_Controller($selector,
141 $this->get(CRM_Utils_Pager::PAGE_ID),
142 $sortID,
143 CRM_Core_Action::VIEW,
144 $this,
145 CRM_Core_Selector_Controller::TRANSFER,
146 $prefix
147 );
148
149 $controller->setEmbedded(TRUE);
150 $controller->moveFromSessionToTemplate();
151
152 $this->assign('summary', $this->get('summary'));
153 }
154
155 /**
fe482240 156 * Build the form object.
6a488035 157 *
6a488035
TO
158 *
159 * @return void
160 */
00be9182 161 public function buildQuickForm() {
3efb5b86 162 parent::buildQuickForm();
6a488035
TO
163 $this->addElement('text', 'sort_name', ts('Member Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
164
165 CRM_Member_BAO_Query::buildSearchForm($this);
166
6a488035
TO
167 $rows = $this->get('rows');
168 if (is_array($rows)) {
169 if (!$this->_single) {
8d36b801 170 $this->addRowSelectors($rows);
6a488035
TO
171 }
172
6a488035
TO
173 $permission = CRM_Core_Permission::getPermission();
174
34197a55 175 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles($permission));
6a488035
TO
176 }
177
6a488035
TO
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
6a488035 195 */
00be9182 196 public function postProcess() {
6a488035
TO
197 if ($this->_done) {
198 return;
199 }
200
201 $this->_done = TRUE;
202
203 $this->_formValues = $this->controller->exportValues($this->_name);
204
205 $this->fixFormValues();
206
d43b88cc
CW
207 // We don't show test records in summaries or dashboards
208 if (empty($this->_formValues['member_test']) && $this->_force) {
6a488035
TO
209 $this->_formValues["member_test"] = 0;
210 }
211
c94d39fd 212 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035
TO
213
214 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
215
216 $this->set('formValues', $this->_formValues);
217 $this->set('queryParams', $this->_queryParams);
218
219 $buttonName = $this->controller->getButtonName();
e341bbee 220 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
221 // check actionName and if next, then do not repeat a search, since we are going to the next page
222
223 // hack, make sure we reset the task values
95ea77ec 224 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
225 $formName = $stateMachine->getTaskFormName();
226 $this->controller->resetPage($formName);
227 return;
228 }
229
230 $sortID = NULL;
231 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
232 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
233 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
234 );
235 }
236
237 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
238
239 $selector = new CRM_Member_Selector_Search($this->_queryParams,
240 $this->_action,
241 NULL,
242 $this->_single,
243 $this->_limit,
244 $this->_context
245 );
246 $selector->setKey($this->controller->_key);
247
248 $prefix = NULL;
249 if ($this->_context == 'basic') {
250 $prefix = $this->_prefix;
251 }
252
253 $controller = new CRM_Core_Selector_Controller($selector,
254 $this->get(CRM_Utils_Pager::PAGE_ID),
255 $sortID,
256 CRM_Core_Action::VIEW,
257 $this,
258 CRM_Core_Selector_Controller::SESSION,
259 $prefix
260 );
261 $controller->setEmbedded(TRUE);
262
263 $query = &$selector->getQuery();
264 $controller->run();
265 }
266
00be9182 267 public function setDefaultValues() {
6a488035
TO
268 return $this->_defaults;
269 }
270
00be9182 271 public function fixFormValues() {
6a488035
TO
272 // if this search has been forced
273 // then see if there are any get values, and if so over-ride the post values
274 // note that this means that GET over-rides POST :)
275
276 if (!$this->_force) {
277 return;
278 }
279
280 $status = CRM_Utils_Request::retrieve('status', 'String',
281 CRM_Core_DAO::$_nullObject
282 );
283 if ($status) {
284 $status = explode(',', $status);
0a74debb 285 $this->_formValues['status_id'] = $this->_defaults['status_id'] = (array) $status;
6a488035
TO
286 }
287
288 $membershipType = CRM_Utils_Request::retrieve('type', 'String',
289 CRM_Core_DAO::$_nullObject
290 );
291
292 if ($membershipType) {
0a74debb 293 $this->_formValues['membership_type_id'] = array($membershipType);
294 $this->_defaults['membership_type_id'] = array($membershipType);
6a488035
TO
295 }
296
6a488035
TO
297 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
298 CRM_Core_DAO::$_nullObject
299 );
300
301 if ($cid) {
302 $cid = CRM_Utils_Type::escape($cid, 'Integer');
303 if ($cid > 0) {
304 $this->_formValues['contact_id'] = $cid;
305 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
306 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
307 'sort_name'
308 );
309 // also assign individual mode to the template
310 $this->_single = TRUE;
311 }
312 }
313
314 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
315 CRM_Core_DAO::$_nullObject
316 );
317 if ($fromDate) {
318 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
319 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
320 }
321
322 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
323 CRM_Core_DAO::$_nullObject
324 );
325 if ($toDate) {
326 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
327 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
328 }
329 $joinDate = CRM_Utils_Request::retrieve('join', 'Date',
330 CRM_Core_DAO::$_nullObject
331 );
332 if ($joinDate) {
333 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
334 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
335 }
336
337 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date',
338 CRM_Core_DAO::$_nullObject
339 );
340 if ($joinEndDate) {
341 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
342 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
343 }
344
345 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
346 $this
347 );
348
349 //LCD also allow restrictions to membership owner via GET
350 $owner = CRM_Utils_Request::retrieve('owner', 'String', CRM_Core_DAO::$_nullObject);
351 if ($owner) {
352 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = 2;
353 }
354 }
355
356 /**
357 * Return a descriptive name for the page, used in wizard header
358 *
359 * @return string
6a488035
TO
360 */
361 public function getTitle() {
362 return ts('Find Memberships');
363 }
96025800 364
6a488035 365}