Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-06-25-23-42-42
[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 * the params that are sent to the query
47 *
48 * @var array
49 * @access protected
50 */
51 protected $_queryParams;
52
53 /**
54 * are we restricting ourselves to a single contact
55 *
56 * @access protected
57 * @var boolean
58 */
59 protected $_single = FALSE;
60
61 /**
62 * are we restricting ourselves to a single contact
63 *
64 * @access protected
65 * @var boolean
66 */
67 protected $_limit = NULL;
68
69 protected $_defaults;
70
71 /**
72 * prefix for the controller
73 *
74 */
75 protected $_prefix = "member_";
76
77 /**
78 * processing needed for buildForm and later
79 *
80 * @return void
81 * @access public
82 */
83 function preProcess() {
84 $this->set('searchFormName', 'Search');
85
86 /**
87 * set the button names
88 */
89 $this->_searchButtonName = $this->getButtonName('refresh');
90 $this->_actionButtonName = $this->getButtonName('next', 'action');
91
92 $this->_done = FALSE;
93
94 $this->defaults = array();
95
96 /*
97 * we allow the controller to set force/reset externally, useful when we are being
98 * driven by the wizard framework
99 */
100
101 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
102 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
103 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
104 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
105
106 $this->assign("context", $this->_context);
107
108 // get user submitted values
109 // get it from controller only if form has been submitted, else preProcess has set this
110 if (!empty($_POST)) {
111 $this->_formValues = $this->controller->exportValues($this->_name);
112 }
113 else {
114 $this->_formValues = $this->get('formValues');
115 }
116
117 if ($this->_force) {
118 $this->postProcess();
119 $this->set('force', 0);
120 }
121
122 $sortID = NULL;
123 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
124 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
125 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
126 );
127 }
128
129 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
130 $selector = new CRM_Member_Selector_Search($this->_queryParams,
131 $this->_action,
132 NULL,
133 $this->_single,
134 $this->_limit,
135 $this->_context
136 );
137 $prefix = NULL;
138 if ($this->_context == 'basic') {
139 $prefix = $this->_prefix;
140 }
141
142 $this->assign("{$prefix}limit", $this->_limit);
143 $this->assign("{$prefix}single", $this->_single);
144
145 $controller = new CRM_Core_Selector_Controller($selector,
146 $this->get(CRM_Utils_Pager::PAGE_ID),
147 $sortID,
148 CRM_Core_Action::VIEW,
149 $this,
150 CRM_Core_Selector_Controller::TRANSFER,
151 $prefix
152 );
153
154 $controller->setEmbedded(TRUE);
155 $controller->moveFromSessionToTemplate();
156
157 $this->assign('summary', $this->get('summary'));
158 }
159
160 /**
161 * Build the form
162 *
163 * @access public
164 *
165 * @return void
166 */
167 function buildQuickForm() {
168 parent::buildQuickForm();
169 $this->addElement('text', 'sort_name', ts('Member Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
170
171 CRM_Member_BAO_Query::buildSearchForm($this);
172
173 $rows = $this->get('rows');
174 if (is_array($rows)) {
175 if (!$this->_single) {
176 $this->addRowSelectors($rows);
177 }
178
179 $permission = CRM_Core_Permission::getPermission();
180
181 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles($permission));
182 }
183
184 }
185
186 /**
187 * The post processing of the form gets done here.
188 *
189 * Key things done during post processing are
190 * - check for reset or next request. if present, skip post procesing.
191 * - now check if user requested running a saved search, if so, then
192 * the form values associated with the saved search are used for searching.
193 * - if user has done a submit with new values the regular post submissing is
194 * done.
195 * The processing consists of using a Selector / Controller framework for getting the
196 * search results.
197 *
198 * @param
199 *
200 * @return void
201 * @access public
202 */
203 function postProcess() {
204 if ($this->_done) {
205 return;
206 }
207
208 $this->_done = TRUE;
209
210 $this->_formValues = $this->controller->exportValues($this->_name);
211
212 $this->fixFormValues();
213
214 // We don't show test records in summaries or dashboards
215 if (empty($this->_formValues['member_test']) && $this->_force) {
216 $this->_formValues["member_test"] = 0;
217 }
218
219 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
220
221 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
222
223 $this->set('formValues', $this->_formValues);
224 $this->set('queryParams', $this->_queryParams);
225
226 $buttonName = $this->controller->getButtonName();
227 if ($buttonName == $this->_actionButtonName) {
228 // check actionName and if next, then do not repeat a search, since we are going to the next page
229
230 // hack, make sure we reset the task values
231 $stateMachine = $this->controller->getStateMachine();
232 $formName = $stateMachine->getTaskFormName();
233 $this->controller->resetPage($formName);
234 return;
235 }
236
237 $sortID = NULL;
238 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
239 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
240 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
241 );
242 }
243
244 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
245
246 $selector = new CRM_Member_Selector_Search($this->_queryParams,
247 $this->_action,
248 NULL,
249 $this->_single,
250 $this->_limit,
251 $this->_context
252 );
253 $selector->setKey($this->controller->_key);
254
255 $prefix = NULL;
256 if ($this->_context == 'basic') {
257 $prefix = $this->_prefix;
258 }
259
260 $controller = new CRM_Core_Selector_Controller($selector,
261 $this->get(CRM_Utils_Pager::PAGE_ID),
262 $sortID,
263 CRM_Core_Action::VIEW,
264 $this,
265 CRM_Core_Selector_Controller::SESSION,
266 $prefix
267 );
268 $controller->setEmbedded(TRUE);
269
270 $query = &$selector->getQuery();
271 $controller->run();
272 }
273
274 function setDefaultValues() {
275 return $this->_defaults;
276 }
277
278 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 $status = explode(',', $status);
292 $tempStatus = array();
293 foreach ($status as $value) {
294 $tempStatus[$value] = 1;
295 }
296 $this->_formValues['member_status_id'] = $tempStatus;
297 $this->_defaults['member_status_id'] = $tempStatus;
298 }
299
300 $membershipType = CRM_Utils_Request::retrieve('type', 'String',
301 CRM_Core_DAO::$_nullObject
302 );
303
304 if ($membershipType) {
305 $this->_formValues['member_membership_type_id'] = array($membershipType => 1);
306 $this->_defaults['member_membership_type_id'] = array($membershipType => 1);
307 }
308
309
310 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
311 CRM_Core_DAO::$_nullObject
312 );
313
314 if ($cid) {
315 $cid = CRM_Utils_Type::escape($cid, 'Integer');
316 if ($cid > 0) {
317 $this->_formValues['contact_id'] = $cid;
318 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
319 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
320 'sort_name'
321 );
322 // also assign individual mode to the template
323 $this->_single = TRUE;
324 }
325 }
326
327 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
328 CRM_Core_DAO::$_nullObject
329 );
330 if ($fromDate) {
331 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
332 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
333 }
334
335 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
336 CRM_Core_DAO::$_nullObject
337 );
338 if ($toDate) {
339 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
340 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
341 }
342 $joinDate = CRM_Utils_Request::retrieve('join', 'Date',
343 CRM_Core_DAO::$_nullObject
344 );
345 if ($joinDate) {
346 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
347 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
348 }
349
350 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date',
351 CRM_Core_DAO::$_nullObject
352 );
353 if ($joinEndDate) {
354 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
355 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
356 }
357
358 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
359 $this
360 );
361
362 //LCD also allow restrictions to membership owner via GET
363 $owner = CRM_Utils_Request::retrieve('owner', 'String', CRM_Core_DAO::$_nullObject);
364 if ($owner) {
365 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = 2;
366 }
367 }
368
369 /**
370 * Return a descriptive name for the page, used in wizard header
371 *
372 * @return string
373 * @access public
374 */
375 public function getTitle() {
376 return ts('Find Memberships');
377 }
378 }
379