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