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