Merge pull request #2937 from seamuslee001/master
[civicrm-core.git] / CRM / Member / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Files required
38 */
39
40 /**
41 * This file is for civimember search
42 */
43 class CRM_Member_Form_Search extends CRM_Core_Form_Search {
44
45 /**
46 * Are we forced to run a search
47 *
48 * @var int
49 * @access protected
50 */
51 protected $_force;
52
53 /**
54 * name of search button
55 *
56 * @var string
57 * @access protected
58 */
59 protected $_searchButtonName;
60
61 /**
62 * name of action button
63 *
64 * @var string
65 * @access protected
66 */
67 protected $_actionButtonName;
68
69 /**
70 * form values that we will be using
71 *
72 * @var array
73 * @access protected
74 */
75 protected $_formValues;
76
77 /**
78 * the params that are sent to the query
79 *
80 * @var array
81 * @access protected
82 */
83 protected $_queryParams;
84
85 /**
86 * have we already done this search
87 *
88 * @access protected
89 * @var boolean
90 */
91 protected $_done;
92
93 /**
94 * are we restricting ourselves to a single contact
95 *
96 * @access protected
97 * @var boolean
98 */
99 protected $_single = FALSE;
100
101 /**
102 * are we restricting ourselves to a single contact
103 *
104 * @access protected
105 * @var boolean
106 */
107 protected $_limit = NULL;
108
109 /**
110 * what context are we being invoked from
111 *
112 * @access protected
113 * @var string
114 */
115 protected $_context = NULL;
116
117 protected $_defaults;
118
119 /**
120 * prefix for the controller
121 *
122 */
123 protected $_prefix = "member_";
124
125 /**
126 * processing needed for buildForm and later
127 *
128 * @return void
129 * @access public
130 */
131 function preProcess() {
132 $this->set('searchFormName', 'Search');
133
134 /**
135 * set the button names
136 */
137 $this->_searchButtonName = $this->getButtonName('refresh');
138 $this->_actionButtonName = $this->getButtonName('next', 'action');
139
140 $this->_done = FALSE;
141
142 $this->defaults = array();
143
144 /*
145 * we allow the controller to set force/reset externally, useful when we are being
146 * driven by the wizard framework
147 */
148
149 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
150 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
151 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
152 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
153
154 $this->assign("context", $this->_context);
155
156 // get user submitted values
157 // get it from controller only if form has been submitted, else preProcess has set this
158 if (!empty($_POST)) {
159 $this->_formValues = $this->controller->exportValues($this->_name);
160 }
161 else {
162 $this->_formValues = $this->get('formValues');
163 }
164
165 if ($this->_force) {
166 $this->postProcess();
167 $this->set('force', 0);
168 }
169
170 $sortID = NULL;
171 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
172 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
173 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
174 );
175 }
176
177 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
178 $selector = new CRM_Member_Selector_Search($this->_queryParams,
179 $this->_action,
180 NULL,
181 $this->_single,
182 $this->_limit,
183 $this->_context
184 );
185 $prefix = NULL;
186 if ($this->_context == 'basic') {
187 $prefix = $this->_prefix;
188 }
189
190 $this->assign("{$prefix}limit", $this->_limit);
191 $this->assign("{$prefix}single", $this->_single);
192
193 $controller = new CRM_Core_Selector_Controller($selector,
194 $this->get(CRM_Utils_Pager::PAGE_ID),
195 $sortID,
196 CRM_Core_Action::VIEW,
197 $this,
198 CRM_Core_Selector_Controller::TRANSFER,
199 $prefix
200 );
201
202 $controller->setEmbedded(TRUE);
203 $controller->moveFromSessionToTemplate();
204
205 $this->assign('summary', $this->get('summary'));
206 }
207
208 /**
209 * Build the form
210 *
211 * @access public
212 *
213 * @return void
214 */
215 function buildQuickForm() {
216 parent::buildQuickForm();
217 $this->addElement('text', 'sort_name', ts('Member Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
218
219 CRM_Member_BAO_Query::buildSearchForm($this);
220
221 /*
222 * add form checkboxes for each row. This is needed out here to conform to QF protocol
223 * of all elements being declared in builQuickForm
224 */
225
226 $rows = $this->get('rows');
227 if (is_array($rows)) {
228 if (!$this->_single) {
229 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows'));
230 foreach ($rows as $row) {
231 $this->addElement('checkbox', $row['checkbox'],
232 NULL, NULL,
233 array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row')
234 );
235 }
236 }
237
238 $total = $cancel = 0;
239
240 $permission = CRM_Core_Permission::getPermission();
241
242 $tasks = array('' => ts('- actions -')) + CRM_Member_Task::permissionedTaskTitles($permission);
243 $this->add('select', 'task', ts('Actions:') . ' ', $tasks);
244 $this->add('submit', $this->_actionButtonName, ts('Go'),
245 array(
246 'class' => 'form-submit',
247 'id' => 'Go',
248 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);",
249 )
250 );
251
252 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
253 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
254 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );"));
255 }
256
257 }
258
259 /**
260 * The post processing of the form gets done here.
261 *
262 * Key things done during post processing are
263 * - check for reset or next request. if present, skip post procesing.
264 * - now check if user requested running a saved search, if so, then
265 * the form values associated with the saved search are used for searching.
266 * - if user has done a submit with new values the regular post submissing is
267 * done.
268 * The processing consists of using a Selector / Controller framework for getting the
269 * search results.
270 *
271 * @param
272 *
273 * @return void
274 * @access public
275 */
276 function postProcess() {
277 if ($this->_done) {
278 return;
279 }
280
281 $this->_done = TRUE;
282
283 $this->_formValues = $this->controller->exportValues($this->_name);
284
285 $this->fixFormValues();
286
287 // We don't show test records in summaries or dashboards
288 if (empty($this->_formValues['member_test']) && $this->_force) {
289 $this->_formValues["member_test"] = 0;
290 }
291
292 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
293
294 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
295
296 $this->set('formValues', $this->_formValues);
297 $this->set('queryParams', $this->_queryParams);
298
299 $buttonName = $this->controller->getButtonName();
300 if ($buttonName == $this->_actionButtonName) {
301 // check actionName and if next, then do not repeat a search, since we are going to the next page
302
303 // hack, make sure we reset the task values
304 $stateMachine = $this->controller->getStateMachine();
305 $formName = $stateMachine->getTaskFormName();
306 $this->controller->resetPage($formName);
307 return;
308 }
309
310 $sortID = NULL;
311 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
312 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
313 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
314 );
315 }
316
317 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
318
319 $selector = new CRM_Member_Selector_Search($this->_queryParams,
320 $this->_action,
321 NULL,
322 $this->_single,
323 $this->_limit,
324 $this->_context
325 );
326 $selector->setKey($this->controller->_key);
327
328 $prefix = NULL;
329 if ($this->_context == 'basic') {
330 $prefix = $this->_prefix;
331 }
332
333 $controller = new CRM_Core_Selector_Controller($selector,
334 $this->get(CRM_Utils_Pager::PAGE_ID),
335 $sortID,
336 CRM_Core_Action::VIEW,
337 $this,
338 CRM_Core_Selector_Controller::SESSION,
339 $prefix
340 );
341 $controller->setEmbedded(TRUE);
342
343 $query = &$selector->getQuery();
344 $controller->run();
345 }
346
347 function setDefaultValues() {
348 return $this->_defaults;
349 }
350
351 function fixFormValues() {
352 // if this search has been forced
353 // then see if there are any get values, and if so over-ride the post values
354 // note that this means that GET over-rides POST :)
355
356 if (!$this->_force) {
357 return;
358 }
359
360 $status = CRM_Utils_Request::retrieve('status', 'String',
361 CRM_Core_DAO::$_nullObject
362 );
363 if ($status) {
364 $status = explode(',', $status);
365 $tempStatus = array();
366 foreach ($status as $value) {
367 $tempStatus[$value] = 1;
368 }
369 $this->_formValues['member_status_id'] = $tempStatus;
370 $this->_defaults['member_status_id'] = $tempStatus;
371 }
372
373 $membershipType = CRM_Utils_Request::retrieve('type', 'String',
374 CRM_Core_DAO::$_nullObject
375 );
376
377 if ($membershipType) {
378 $this->_formValues['member_membership_type_id'] = array($membershipType => 1);
379 $this->_defaults['member_membership_type_id'] = array($membershipType => 1);
380 }
381
382
383 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
384 CRM_Core_DAO::$_nullObject
385 );
386
387 if ($cid) {
388 $cid = CRM_Utils_Type::escape($cid, 'Integer');
389 if ($cid > 0) {
390 $this->_formValues['contact_id'] = $cid;
391 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
392 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
393 'sort_name'
394 );
395 // also assign individual mode to the template
396 $this->_single = TRUE;
397 }
398 }
399
400 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
401 CRM_Core_DAO::$_nullObject
402 );
403 if ($fromDate) {
404 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
405 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
406 }
407
408 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
409 CRM_Core_DAO::$_nullObject
410 );
411 if ($toDate) {
412 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
413 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
414 }
415 $joinDate = CRM_Utils_Request::retrieve('join', 'Date',
416 CRM_Core_DAO::$_nullObject
417 );
418 if ($joinDate) {
419 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
420 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
421 }
422
423 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date',
424 CRM_Core_DAO::$_nullObject
425 );
426 if ($joinEndDate) {
427 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
428 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
429 }
430
431 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
432 $this
433 );
434
435 //LCD also allow restrictions to membership owner via GET
436 $owner = CRM_Utils_Request::retrieve('owner', 'String', CRM_Core_DAO::$_nullObject);
437 if ($owner) {
438 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = 2;
439 }
440 }
441
442 /**
443 * Return a descriptive name for the page, used in wizard header
444 *
445 * @return string
446 * @access public
447 */
448 public function getTitle() {
449 return ts('Find Memberships');
450 }
451 }
452