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