Merge pull request #13289 from mfb/pear-mail
[civicrm-core.git] / CRM / Member / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
6a488035 34/**
0955d6b9 35 * Membership search.
36 *
37 * Class is a pane in advanced search and the membership search page.
6a488035 38 */
3efb5b86 39class CRM_Member_Form_Search extends CRM_Core_Form_Search {
6a488035 40
6a488035 41 /**
fe482240 42 * The params that are sent to the query.
6a488035
TO
43 *
44 * @var array
6a488035
TO
45 */
46 protected $_queryParams;
47
6a488035 48 /**
fe482240 49 * Are we restricting ourselves to a single contact.
6a488035 50 *
e5ad0335 51 * @var bool
6a488035
TO
52 */
53 protected $_single = FALSE;
54
55 /**
fe482240 56 * Are we restricting ourselves to a single contact.
6a488035 57 *
e5ad0335 58 * @var bool
6a488035
TO
59 */
60 protected $_limit = NULL;
61
6a488035 62 /**
fe482240 63 * Prefix for the controller.
6a488035
TO
64 */
65 protected $_prefix = "member_";
66
df60621b 67 /**
68 * Declare entity reference fields as they will need to be converted to using 'IN'.
69 *
70 * @var array
71 */
72 protected $entityReferenceFields = array('membership_type_id');
73
6a488035 74 /**
fe482240 75 * Processing needed for buildForm and later.
03e04002 76 */
00be9182 77 public function preProcess() {
6a488035
TO
78 $this->set('searchFormName', 'Search');
79
6a488035 80 $this->_searchButtonName = $this->getButtonName('refresh');
6a488035
TO
81 $this->_actionButtonName = $this->getButtonName('next', 'action');
82
83 $this->_done = FALSE;
84
64ffcefd 85 $this->loadStandardSearchOptionsFromUrl();
6a488035
TO
86
87 // get user submitted values
88 // get it from controller only if form has been submitted, else preProcess has set this
89 if (!empty($_POST)) {
90 $this->_formValues = $this->controller->exportValues($this->_name);
91 }
92 else {
93 $this->_formValues = $this->get('formValues');
94 }
95
96 if ($this->_force) {
97 $this->postProcess();
98 $this->set('force', 0);
99 }
100
101 $sortID = NULL;
102 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
103 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
104 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
105 );
106 }
107
df60621b 108 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
6a488035
TO
109 $selector = new CRM_Member_Selector_Search($this->_queryParams,
110 $this->_action,
111 NULL,
112 $this->_single,
113 $this->_limit,
114 $this->_context
115 );
116 $prefix = NULL;
117 if ($this->_context == 'basic') {
118 $prefix = $this->_prefix;
119 }
120
121 $this->assign("{$prefix}limit", $this->_limit);
122 $this->assign("{$prefix}single", $this->_single);
123
124 $controller = new CRM_Core_Selector_Controller($selector,
125 $this->get(CRM_Utils_Pager::PAGE_ID),
126 $sortID,
127 CRM_Core_Action::VIEW,
128 $this,
129 CRM_Core_Selector_Controller::TRANSFER,
130 $prefix
131 );
132
133 $controller->setEmbedded(TRUE);
134 $controller->moveFromSessionToTemplate();
135
136 $this->assign('summary', $this->get('summary'));
137 }
138
139 /**
fe482240 140 * Build the form object.
6a488035 141 */
00be9182 142 public function buildQuickForm() {
3efb5b86 143 parent::buildQuickForm();
0573fd28 144 $this->addContactSearchFields();
6a488035
TO
145
146 CRM_Member_BAO_Query::buildSearchForm($this);
147
6a488035
TO
148 $rows = $this->get('rows');
149 if (is_array($rows)) {
150 if (!$this->_single) {
8d36b801 151 $this->addRowSelectors($rows);
6a488035
TO
152 }
153
e335ab3b 154 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
6a488035
TO
155 }
156
6a488035
TO
157 }
158
e597fc33
DG
159 /**
160 * Get the label for the sortName field if email searching is on.
161 *
162 * (email searching is a setting under search preferences).
163 *
164 * @return string
165 */
166 protected function getSortNameLabelWithEmail() {
167 return ts('Member Name or Email');
168 }
169
170 /**
171 * Get the label for the sortName field if email searching is off.
172 *
173 * (email searching is a setting under search preferences).
174 *
175 * @return string
176 */
177 protected function getSortNameLabelWithOutEmail() {
178 return ts('Member Name');
179 }
180
0573fd28 181 /**
182 * Get the label for the tag field.
183 *
184 * We do this in a function so the 'ts' wraps the whole string to allow
185 * better translation.
186 *
187 * @return string
188 */
189 protected function getTagLabel() {
190 return ts('Member Tag(s)');
191 }
192
193 /**
194 * Get the label for the group field.
195 *
196 * @return string
197 */
198 protected function getGroupLabel() {
199 return ts('Member Group(s)');
200 }
201
202 /**
203 * Get the label for the group field.
204 *
205 * @return string
206 */
207 protected function getContactTypeLabel() {
208 return ts('Member Contact Type');
209 }
210
6a488035
TO
211 /**
212 * The post processing of the form gets done here.
213 *
214 * Key things done during post processing are
215 * - check for reset or next request. if present, skip post procesing.
216 * - now check if user requested running a saved search, if so, then
217 * the form values associated with the saved search are used for searching.
218 * - if user has done a submit with new values the regular post submissing is
219 * done.
220 * The processing consists of using a Selector / Controller framework for getting the
221 * search results.
6a488035 222 */
00be9182 223 public function postProcess() {
6a488035
TO
224 if ($this->_done) {
225 return;
226 }
227
228 $this->_done = TRUE;
229
230 $this->_formValues = $this->controller->exportValues($this->_name);
231
232 $this->fixFormValues();
233
d43b88cc
CW
234 // We don't show test records in summaries or dashboards
235 if (empty($this->_formValues['member_test']) && $this->_force) {
6a488035
TO
236 $this->_formValues["member_test"] = 0;
237 }
238
c94d39fd 239 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
6a488035 240
df60621b 241 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
6a488035
TO
242
243 $this->set('formValues', $this->_formValues);
244 $this->set('queryParams', $this->_queryParams);
245
246 $buttonName = $this->controller->getButtonName();
e341bbee 247 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
248 // check actionName and if next, then do not repeat a search, since we are going to the next page
249
250 // hack, make sure we reset the task values
95ea77ec 251 $stateMachine = $this->controller->getStateMachine();
6a488035
TO
252 $formName = $stateMachine->getTaskFormName();
253 $this->controller->resetPage($formName);
254 return;
255 }
256
257 $sortID = NULL;
258 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
259 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
260 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
261 );
262 }
263
ccc994a1 264 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
6a488035
TO
265
266 $selector = new CRM_Member_Selector_Search($this->_queryParams,
267 $this->_action,
268 NULL,
269 $this->_single,
270 $this->_limit,
271 $this->_context
272 );
273 $selector->setKey($this->controller->_key);
274
275 $prefix = NULL;
276 if ($this->_context == 'basic') {
277 $prefix = $this->_prefix;
278 }
279
280 $controller = new CRM_Core_Selector_Controller($selector,
281 $this->get(CRM_Utils_Pager::PAGE_ID),
282 $sortID,
283 CRM_Core_Action::VIEW,
284 $this,
285 CRM_Core_Selector_Controller::SESSION,
286 $prefix
287 );
288 $controller->setEmbedded(TRUE);
289
290 $query = &$selector->getQuery();
291 $controller->run();
292 }
293
0955d6b9 294 /**
295 * Set default values.
296 *
297 * @todo - can this function override be removed?
298 *
299 * @return array
300 */
00be9182 301 public function setDefaultValues() {
6a488035
TO
302 return $this->_defaults;
303 }
304
0955d6b9 305 /**
306 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
307 *
308 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
309 */
00be9182 310 public function fixFormValues() {
6a488035
TO
311 if (!$this->_force) {
312 return;
313 }
314
1273d77c 315 $status = CRM_Utils_Request::retrieve('status', 'String');
6a488035
TO
316 if ($status) {
317 $status = explode(',', $status);
fc5b907d 318 $this->_formValues['membership_status_id'] = $this->_defaults['membership_status_id'] = (array) $status;
6a488035
TO
319 }
320
1273d77c 321 $membershipType = CRM_Utils_Request::retrieve('type', 'String');
6a488035
TO
322
323 if ($membershipType) {
0a74debb 324 $this->_formValues['membership_type_id'] = array($membershipType);
325 $this->_defaults['membership_type_id'] = array($membershipType);
6a488035
TO
326 }
327
1273d77c 328 $cid = CRM_Utils_Request::retrieve('cid', 'Positive');
6a488035
TO
329
330 if ($cid) {
331 $cid = CRM_Utils_Type::escape($cid, 'Integer');
332 if ($cid > 0) {
333 $this->_formValues['contact_id'] = $cid;
334 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
335 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
336 'sort_name'
337 );
338 // also assign individual mode to the template
339 $this->_single = TRUE;
340 }
341 }
342
1273d77c 343 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
6a488035
TO
344 if ($fromDate) {
345 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
346 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
347 }
348
1273d77c 349 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
6a488035
TO
350 if ($toDate) {
351 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
352 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
353 }
1273d77c 354 $joinDate = CRM_Utils_Request::retrieve('join', 'Date');
6a488035
TO
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
1273d77c 360 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date');
6a488035
TO
361 if ($joinEndDate) {
362 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
363 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
364 }
365
366 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
367 $this
368 );
369
370 //LCD also allow restrictions to membership owner via GET
a3d827a7 371 $owner = CRM_Utils_Request::retrieve('owner', 'String');
330a8e32 372 if (in_array($owner, array('0', '1'))) {
0b585841 373 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = $owner;
6a488035
TO
374 }
375 }
376
377 /**
0955d6b9 378 * Return a descriptive name for the page, used in wizard header.
6a488035
TO
379 *
380 * @return string
6a488035
TO
381 */
382 public function getTitle() {
383 return ts('Find Memberships');
384 }
96025800 385
6a488035 386}