(NFC) (dev/core#878) Simplify copyright header (templates/*)
[civicrm-core.git] / CRM / Member / Form / Search.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 https://civicrm.org/licensing
32 */
33
34/**
35 * Membership search.
36 *
37 * Class is a pane in advanced search and the membership search page.
38 */
39class CRM_Member_Form_Search extends CRM_Core_Form_Search {
40
41 /**
42 * The params that are sent to the query.
43 *
44 * @var array
45 */
46 protected $_queryParams;
47
48 /**
49 * Are we restricting ourselves to a single contact.
50 *
51 * @var bool
52 */
53 protected $_single = FALSE;
54
55 /**
56 * Are we restricting ourselves to a single contact.
57 *
58 * @var bool
59 */
60 protected $_limit = NULL;
61
62 /**
63 * Prefix for the controller.
64 * @var string
65 */
66 protected $_prefix = "member_";
67
68 /**
69 * Declare entity reference fields as they will need to be converted to using 'IN'.
70 *
71 * @var array
72 */
73 protected $entityReferenceFields = ['membership_type_id'];
74
75 /**
76 * Processing needed for buildForm and later.
77 *
78 * @throws \CRM_Core_Exception
79 * @throws \CiviCRM_API3_Exception
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 parent::preProcess();
90
91 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
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),
109 $this->getSortID(),
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 /**
123 * Build the form object.
124 *
125 * @throws \CRM_Core_Exception
126 * @throws \CiviCRM_API3_Exception
127 */
128 public function buildQuickForm() {
129 parent::buildQuickForm();
130 $this->addContactSearchFields();
131
132 CRM_Member_BAO_Query::buildSearchForm($this);
133
134 $rows = $this->get('rows');
135 if (is_array($rows)) {
136 if (!$this->_single) {
137 $this->addRowSelectors($rows);
138 }
139
140 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
141 }
142
143 }
144
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
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
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.
208 */
209 public function postProcess() {
210 if ($this->_done) {
211 return;
212 }
213
214 $this->_done = TRUE;
215 $this->setFormValues();
216
217 $this->fixFormValues();
218
219 // We don't show test records in summaries or dashboards
220 if (empty($this->_formValues['member_test']) && $this->_force) {
221 $this->_formValues["member_test"] = 0;
222 }
223
224 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
225
226 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
227
228 $this->set('queryParams', $this->_queryParams);
229
230 $buttonName = $this->controller->getButtonName();
231 if ($buttonName == $this->_actionButtonName) {
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
235 $stateMachine = $this->controller->getStateMachine();
236 $formName = $stateMachine->getTaskFormName();
237 $this->controller->resetPage($formName);
238 return;
239 }
240
241 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
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),
259 $this->getSortID(),
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
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 */
276 public function fixFormValues() {
277 if (!$this->_force) {
278 return;
279 }
280
281 $status = CRM_Utils_Request::retrieve('status', 'String');
282 if ($status) {
283 $status = explode(',', $status);
284 $this->_formValues['membership_status_id'] = $this->_defaults['membership_status_id'] = (array) $status;
285 }
286
287 $membershipType = CRM_Utils_Request::retrieve('type', 'String');
288
289 if ($membershipType) {
290 $this->_formValues['membership_type_id'] = [$membershipType];
291 $this->_defaults['membership_type_id'] = [$membershipType];
292 }
293
294 $cid = CRM_Utils_Request::retrieve('cid', 'Positive');
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
309 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
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
315 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
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 }
320 $joinDate = CRM_Utils_Request::retrieve('join', 'Date');
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
326 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date');
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
337 $owner = CRM_Utils_Request::retrieve('owner', 'String');
338 if (in_array($owner, ['0', '1'])) {
339 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = $owner;
340 }
341 }
342
343 /**
344 * Return a descriptive name for the page, used in wizard header.
345 *
346 * @return string
347 */
348 public function getTitle() {
349 return ts('Find Memberships');
350 }
351
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
361}