Merge pull request #7190 from galgeek/patch-1
[civicrm-core.git] / CRM / Member / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * Files required
36 */
37
38 /**
39 * Membership search.
40 *
41 * Class is a pane in advanced search and the membership search page.
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 */
50 protected $_queryParams;
51
52 /**
53 * Are we restricting ourselves to a single contact.
54 *
55 * @var boolean
56 */
57 protected $_single = FALSE;
58
59 /**
60 * Are we restricting ourselves to a single contact.
61 *
62 * @var boolean
63 */
64 protected $_limit = NULL;
65
66 /**
67 * Prefix for the controller.
68 */
69 protected $_prefix = "member_";
70
71 /**
72 * Declare entity reference fields as they will need to be converted to using 'IN'.
73 *
74 * @var array
75 */
76 protected $entityReferenceFields = array('membership_type_id');
77
78 /**
79 * Processing needed for buildForm and later.
80 */
81 public function preProcess() {
82 $this->set('searchFormName', 'Search');
83
84 $this->_searchButtonName = $this->getButtonName('refresh');
85 $this->_actionButtonName = $this->getButtonName('next', 'action');
86
87 $this->_done = FALSE;
88
89 $this->defaults = array();
90
91 /*
92 * we allow the controller to set force/reset externally, useful when we are being
93 * driven by the wizard framework
94 */
95
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);
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, 0, FALSE, NULL, $this->entityReferenceFields);
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 /**
156 * Build the form object.
157 */
158 public function buildQuickForm() {
159 parent::buildQuickForm();
160 $this->addSortNameField();
161
162 CRM_Member_BAO_Query::buildSearchForm($this);
163
164 $rows = $this->get('rows');
165 if (is_array($rows)) {
166 if (!$this->_single) {
167 $this->addRowSelectors($rows);
168 }
169
170 $permission = CRM_Core_Permission::getPermission();
171
172 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles($permission));
173 }
174
175 }
176
177 /**
178 * Get the label for the sortName field if email searching is on.
179 *
180 * (email searching is a setting under search preferences).
181 *
182 * @return string
183 */
184 protected function getSortNameLabelWithEmail() {
185 return ts('Member Name or Email');
186 }
187
188 /**
189 * Get the label for the sortName field if email searching is off.
190 *
191 * (email searching is a setting under search preferences).
192 *
193 * @return string
194 */
195 protected function getSortNameLabelWithOutEmail() {
196 return ts('Member Name');
197 }
198
199 /**
200 * The post processing of the form gets done here.
201 *
202 * Key things done during post processing are
203 * - check for reset or next request. if present, skip post procesing.
204 * - now check if user requested running a saved search, if so, then
205 * the form values associated with the saved search are used for searching.
206 * - if user has done a submit with new values the regular post submissing is
207 * done.
208 * The processing consists of using a Selector / Controller framework for getting the
209 * search results.
210 */
211 public function postProcess() {
212 if ($this->_done) {
213 return;
214 }
215
216 $this->_done = TRUE;
217
218 $this->_formValues = $this->controller->exportValues($this->_name);
219
220 $this->fixFormValues();
221
222 // We don't show test records in summaries or dashboards
223 if (empty($this->_formValues['member_test']) && $this->_force) {
224 $this->_formValues["member_test"] = 0;
225 }
226
227 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
228
229 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
230
231 $this->set('formValues', $this->_formValues);
232 $this->set('queryParams', $this->_queryParams);
233
234 $buttonName = $this->controller->getButtonName();
235 if ($buttonName == $this->_actionButtonName) {
236 // check actionName and if next, then do not repeat a search, since we are going to the next page
237
238 // hack, make sure we reset the task values
239 $stateMachine = $this->controller->getStateMachine();
240 $formName = $stateMachine->getTaskFormName();
241 $this->controller->resetPage($formName);
242 return;
243 }
244
245 $sortID = NULL;
246 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
247 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
248 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
249 );
250 }
251
252 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
253
254 $selector = new CRM_Member_Selector_Search($this->_queryParams,
255 $this->_action,
256 NULL,
257 $this->_single,
258 $this->_limit,
259 $this->_context
260 );
261 $selector->setKey($this->controller->_key);
262
263 $prefix = NULL;
264 if ($this->_context == 'basic') {
265 $prefix = $this->_prefix;
266 }
267
268 $controller = new CRM_Core_Selector_Controller($selector,
269 $this->get(CRM_Utils_Pager::PAGE_ID),
270 $sortID,
271 CRM_Core_Action::VIEW,
272 $this,
273 CRM_Core_Selector_Controller::SESSION,
274 $prefix
275 );
276 $controller->setEmbedded(TRUE);
277
278 $query = &$selector->getQuery();
279 $controller->run();
280 }
281
282 /**
283 * Set default values.
284 *
285 * @todo - can this function override be removed?
286 *
287 * @return array
288 */
289 public function setDefaultValues() {
290 return $this->_defaults;
291 }
292
293 /**
294 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
295 *
296 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
297 */
298 public function fixFormValues() {
299 if (!$this->_force) {
300 return;
301 }
302
303 $status = CRM_Utils_Request::retrieve('status', 'String',
304 CRM_Core_DAO::$_nullObject
305 );
306 if ($status) {
307 $status = explode(',', $status);
308 $this->_formValues['membership_status_id'] = $this->_defaults['membership_status_id'] = (array) $status;
309 }
310
311 $membershipType = CRM_Utils_Request::retrieve('type', 'String',
312 CRM_Core_DAO::$_nullObject
313 );
314
315 if ($membershipType) {
316 $this->_formValues['membership_type_id'] = array($membershipType);
317 $this->_defaults['membership_type_id'] = array($membershipType);
318 }
319
320 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
321 CRM_Core_DAO::$_nullObject
322 );
323
324 if ($cid) {
325 $cid = CRM_Utils_Type::escape($cid, 'Integer');
326 if ($cid > 0) {
327 $this->_formValues['contact_id'] = $cid;
328 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
329 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
330 'sort_name'
331 );
332 // also assign individual mode to the template
333 $this->_single = TRUE;
334 }
335 }
336
337 $fromDate = CRM_Utils_Request::retrieve('start', 'Date',
338 CRM_Core_DAO::$_nullObject
339 );
340 if ($fromDate) {
341 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
342 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
343 }
344
345 $toDate = CRM_Utils_Request::retrieve('end', 'Date',
346 CRM_Core_DAO::$_nullObject
347 );
348 if ($toDate) {
349 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
350 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
351 }
352 $joinDate = CRM_Utils_Request::retrieve('join', 'Date',
353 CRM_Core_DAO::$_nullObject
354 );
355 if ($joinDate) {
356 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
357 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
358 }
359
360 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date',
361 CRM_Core_DAO::$_nullObject
362 );
363 if ($joinEndDate) {
364 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
365 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
366 }
367
368 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
369 $this
370 );
371
372 //LCD also allow restrictions to membership owner via GET
373 $owner = CRM_Utils_Request::retrieve('owner', 'String', CRM_Core_DAO::$_nullObject);
374 if ($owner) {
375 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = 2;
376 }
377 }
378
379 /**
380 * Return a descriptive name for the page, used in wizard header.
381 *
382 * @return string
383 */
384 public function getTitle() {
385 return ts('Find Memberships');
386 }
387
388 }