0c9c2b20e7a6df823315c0e8114ddbfce2c6dd02
[civicrm-core.git] / CRM / Member / Form / Search.php
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 (c) 2004-2020
32 */
33
34 /**
35 * Membership search.
36 *
37 * Class is a pane in advanced search and the membership search page.
38 */
39 class 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 public function preProcess() {
79 $this->set('searchFormName', 'Search');
80
81 $this->_searchButtonName = $this->getButtonName('refresh');
82 $this->_actionButtonName = $this->getButtonName('next', 'action');
83
84 $this->_done = FALSE;
85
86 $this->loadStandardSearchOptionsFromUrl();
87
88 // get user submitted values
89 // get it from controller only if form has been submitted, else preProcess has set this
90 if (!empty($_POST)) {
91 $this->_formValues = $this->controller->exportValues($this->_name);
92 }
93 else {
94 $this->_formValues = $this->get('formValues');
95 }
96
97 if ($this->_force) {
98 $this->addSearchFieldMetadata(['Membership' => CRM_Member_BAO_Query::getSearchFieldMetadata()]);
99 $this->addFormFieldsFromMetadata();
100 $this->postProcess();
101 $this->set('force', 0);
102 }
103
104 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
105 $selector = new CRM_Member_Selector_Search($this->_queryParams,
106 $this->_action,
107 NULL,
108 $this->_single,
109 $this->_limit,
110 $this->_context
111 );
112 $prefix = NULL;
113 if ($this->_context == 'basic') {
114 $prefix = $this->_prefix;
115 }
116
117 $this->assign("{$prefix}limit", $this->_limit);
118 $this->assign("{$prefix}single", $this->_single);
119
120 $controller = new CRM_Core_Selector_Controller($selector,
121 $this->get(CRM_Utils_Pager::PAGE_ID),
122 $this->getSortID(),
123 CRM_Core_Action::VIEW,
124 $this,
125 CRM_Core_Selector_Controller::TRANSFER,
126 $prefix
127 );
128
129 $controller->setEmbedded(TRUE);
130 $controller->moveFromSessionToTemplate();
131
132 $this->assign('summary', $this->get('summary'));
133 }
134
135 /**
136 * Build the form object.
137 */
138 public function buildQuickForm() {
139 parent::buildQuickForm();
140 $this->addContactSearchFields();
141
142 CRM_Member_BAO_Query::buildSearchForm($this);
143
144 $rows = $this->get('rows');
145 if (is_array($rows)) {
146 if (!$this->_single) {
147 $this->addRowSelectors($rows);
148 }
149
150 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
151 }
152
153 }
154
155 /**
156 * Get the label for the sortName field if email searching is on.
157 *
158 * (email searching is a setting under search preferences).
159 *
160 * @return string
161 */
162 protected function getSortNameLabelWithEmail() {
163 return ts('Member Name or Email');
164 }
165
166 /**
167 * Get the label for the sortName field if email searching is off.
168 *
169 * (email searching is a setting under search preferences).
170 *
171 * @return string
172 */
173 protected function getSortNameLabelWithOutEmail() {
174 return ts('Member Name');
175 }
176
177 /**
178 * Get the label for the tag field.
179 *
180 * We do this in a function so the 'ts' wraps the whole string to allow
181 * better translation.
182 *
183 * @return string
184 */
185 protected function getTagLabel() {
186 return ts('Member Tag(s)');
187 }
188
189 /**
190 * Get the label for the group field.
191 *
192 * @return string
193 */
194 protected function getGroupLabel() {
195 return ts('Member Group(s)');
196 }
197
198 /**
199 * Get the label for the group field.
200 *
201 * @return string
202 */
203 protected function getContactTypeLabel() {
204 return ts('Member Contact Type');
205 }
206
207 /**
208 * The post processing of the form gets done here.
209 *
210 * Key things done during post processing are
211 * - check for reset or next request. if present, skip post procesing.
212 * - now check if user requested running a saved search, if so, then
213 * the form values associated with the saved search are used for searching.
214 * - if user has done a submit with new values the regular post submissing is
215 * done.
216 * The processing consists of using a Selector / Controller framework for getting the
217 * search results.
218 */
219 public function postProcess() {
220 if ($this->_done) {
221 return;
222 }
223
224 $this->_done = TRUE;
225 $this->setFormValues();
226
227 $this->fixFormValues();
228
229 // We don't show test records in summaries or dashboards
230 if (empty($this->_formValues['member_test']) && $this->_force) {
231 $this->_formValues["member_test"] = 0;
232 }
233
234 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
235
236 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
237
238 $this->set('formValues', $this->_formValues);
239 $this->set('queryParams', $this->_queryParams);
240
241 $buttonName = $this->controller->getButtonName();
242 if ($buttonName == $this->_actionButtonName) {
243 // check actionName and if next, then do not repeat a search, since we are going to the next page
244
245 // hack, make sure we reset the task values
246 $stateMachine = $this->controller->getStateMachine();
247 $formName = $stateMachine->getTaskFormName();
248 $this->controller->resetPage($formName);
249 return;
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 $this->getSortID(),
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 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
284 *
285 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
286 */
287 public function fixFormValues() {
288 if (!$this->_force) {
289 return;
290 }
291
292 $status = CRM_Utils_Request::retrieve('status', 'String');
293 if ($status) {
294 $status = explode(',', $status);
295 $this->_formValues['membership_status_id'] = $this->_defaults['membership_status_id'] = (array) $status;
296 }
297
298 $membershipType = CRM_Utils_Request::retrieve('type', 'String');
299
300 if ($membershipType) {
301 $this->_formValues['membership_type_id'] = [$membershipType];
302 $this->_defaults['membership_type_id'] = [$membershipType];
303 }
304
305 $cid = CRM_Utils_Request::retrieve('cid', 'Positive');
306
307 if ($cid) {
308 $cid = CRM_Utils_Type::escape($cid, 'Integer');
309 if ($cid > 0) {
310 $this->_formValues['contact_id'] = $cid;
311 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
312 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
313 'sort_name'
314 );
315 // also assign individual mode to the template
316 $this->_single = TRUE;
317 }
318 }
319
320 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
321 if ($fromDate) {
322 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
323 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
324 }
325
326 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
327 if ($toDate) {
328 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
329 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
330 }
331 $joinDate = CRM_Utils_Request::retrieve('join', 'Date');
332 if ($joinDate) {
333 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
334 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
335 }
336
337 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date');
338 if ($joinEndDate) {
339 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
340 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
341 }
342
343 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
344 $this
345 );
346
347 //LCD also allow restrictions to membership owner via GET
348 $owner = CRM_Utils_Request::retrieve('owner', 'String');
349 if (in_array($owner, ['0', '1'])) {
350 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = $owner;
351 }
352 }
353
354 /**
355 * Return a descriptive name for the page, used in wizard header.
356 *
357 * @return string
358 */
359 public function getTitle() {
360 return ts('Find Memberships');
361 }
362
363 }