CRM-12872 - Search js - move search-only js out of Common.js
[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 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 $this->addElement('text', 'sort_name', ts('Member Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
217
218 CRM_Member_BAO_Query::buildSearchForm($this);
219
220 /*
221 * add form checkboxes for each row. This is needed out here to conform to QF protocol
222 * of all elements being declared in builQuickForm
223 */
224
225 $rows = $this->get('rows');
226 if (is_array($rows)) {
227 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js');
228 if (!$this->_single) {
229 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);"));
230 foreach ($rows as $row) {
231 $this->addElement('checkbox', $row['checkbox'],
232 NULL, NULL,
233 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
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('onclick' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );"));
255 }
256
257
258 // add buttons
259 $this->addButtons(array(
260 array(
261 'type' => 'refresh',
262 'name' => ts('Search'),
263 'isDefault' => TRUE,
264 ),
265 ));
266 }
267
268 /**
269 * The post processing of the form gets done here.
270 *
271 * Key things done during post processing are
272 * - check for reset or next request. if present, skip post procesing.
273 * - now check if user requested running a saved search, if so, then
274 * the form values associated with the saved search are used for searching.
275 * - if user has done a submit with new values the regular post submissing is
276 * done.
277 * The processing consists of using a Selector / Controller framework for getting the
278 * search results.
279 *
280 * @param
281 *
282 * @return void
283 * @access public
284 */
285 function postProcess() {
286 if ($this->_done) {
287 return;
288 }
289
290 $this->_done = TRUE;
291
292 $this->_formValues = $this->controller->exportValues($this->_name);
293
294 $this->fixFormValues();
295
296 // We don't show test records in summaries or dashboards
297 if (empty($this->_formValues['member_test']) && $this->_force) {
298 $this->_formValues["member_test"] = 0;
299 }
300
301 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
302
303 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
304
305 $this->set('formValues', $this->_formValues);
306 $this->set('queryParams', $this->_queryParams);
307
308 $buttonName = $this->controller->getButtonName();
309 if ($buttonName == $this->_actionButtonName) {
310 // check actionName and if next, then do not repeat a search, since we are going to the next page
311
312 // hack, make sure we reset the task values
313 $stateMachine = $this->controller->getStateMachine();
314 $formName = $stateMachine->getTaskFormName();
315 $this->controller->resetPage($formName);
316 return;
317 }
318
319 $sortID = NULL;
320 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
321 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
322 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
323 );
324 }
325
326 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
327
328 $selector = new CRM_Member_Selector_Search($this->_queryParams,
329 $this->_action,
330 NULL,
331 $this->_single,
332 $this->_limit,
333 $this->_context
334 );
335 $selector->setKey($this->controller->_key);
336
337 $prefix = NULL;
338 if ($this->_context == 'basic') {
339 $prefix = $this->_prefix;
340 }
341
342 $controller = new CRM_Core_Selector_Controller($selector,
343 $this->get(CRM_Utils_Pager::PAGE_ID),
344 $sortID,
345 CRM_Core_Action::VIEW,
346 $this,
347 CRM_Core_Selector_Controller::SESSION,
348 $prefix
349 );
350 $controller->setEmbedded(TRUE);
351
352 $query = &$selector->getQuery();
353 $controller->run();
354 }
355
356 function setDefaultValues() {
357 return $this->_defaults;
358 }
359
360 function fixFormValues() {
361 // if this search has been forced
362 // then see if there are any get values, and if so over-ride the post values
363 // note that this means that GET over-rides POST :)
364
365 if (!$this->_force) {
366 return;
367 }
368
369 $status = CRM_Utils_Request::retrieve('status', 'String',
370 CRM_Core_DAO::$_nullObject
371 );
372 if ($status) {
373 $status = explode(',', $status);
374 $tempStatus = array();
375 foreach ($status as $value) {
376 $tempStatus[$value] = 1;
377 }
378 $this->_formValues['member_status_id'] = $tempStatus;
379 $this->_defaults['member_status_id'] = $tempStatus;
380 }
381
382 $membershipType = CRM_Utils_Request::retrieve('type', 'String',
383 CRM_Core_DAO::$_nullObject
384 );
385
386 if ($membershipType) {
387 $this->_formValues['member_membership_type_id'] = array($membershipType => 1);
388 $this->_defaults['member_membership_type_id'] = array($membershipType => 1);
389 }
390
391
392 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
393 CRM_Core_DAO::$_nullObject
394 );
395
396 if ($cid) {
397 $cid = CRM_Utils_Type::escape($cid, 'Integer');
398 if ($cid > 0) {
399 $this->_formValues['contact_id'] = $cid;
400 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
401 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
402 'sort_name'
403 );
404 // also assign individual mode to the template
405 $this->_single = TRUE;
406 }
407 }
408
409 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
410 CRM_Core_DAO::$_nullObject
411 );
412 if ($fromDate) {
413 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
414 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
415 }
416
417 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
418 CRM_Core_DAO::$_nullObject
419 );
420 if ($toDate) {
421 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
422 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
423 }
424 $joinDate = CRM_Utils_Request::retrieve('join', 'Date',
425 CRM_Core_DAO::$_nullObject
426 );
427 if ($joinDate) {
428 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
429 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
430 }
431
432 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date',
433 CRM_Core_DAO::$_nullObject
434 );
435 if ($joinEndDate) {
436 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
437 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
438 }
439
440 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
441 $this
442 );
443
444 //LCD also allow restrictions to membership owner via GET
445 $owner = CRM_Utils_Request::retrieve('owner', 'String', CRM_Core_DAO::$_nullObject);
446 if ($owner) {
447 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = 2;
448 }
449 }
450
451 /**
452 * Return a descriptive name for the page, used in wizard header
453 *
454 * @return string
455 * @access public
456 */
457 public function getTitle() {
458 return ts('Find Memberships');
459 }
460 }
461