Merge pull request #15912 from mydropwizard/d8-language-empty-prefix
[civicrm-core.git] / CRM / Member / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Membership search.
20 *
21 * Class is a pane in advanced search and the membership search page.
22 */
23 class CRM_Member_Form_Search extends CRM_Core_Form_Search {
24
25 /**
26 * The params that are sent to the query.
27 *
28 * @var array
29 */
30 protected $_queryParams;
31
32 /**
33 * Are we restricting ourselves to a single contact.
34 *
35 * @var bool
36 */
37 protected $_single = FALSE;
38
39 /**
40 * Are we restricting ourselves to a single contact.
41 *
42 * @var bool
43 */
44 protected $_limit = NULL;
45
46 /**
47 * Prefix for the controller.
48 * @var string
49 */
50 protected $_prefix = "member_";
51
52 /**
53 * Declare entity reference fields as they will need to be converted to using 'IN'.
54 *
55 * @var array
56 */
57 protected $entityReferenceFields = ['membership_type_id'];
58
59 /**
60 * Processing needed for buildForm and later.
61 *
62 * @throws \CRM_Core_Exception
63 * @throws \CiviCRM_API3_Exception
64 */
65 public function preProcess() {
66 $this->set('searchFormName', 'Search');
67
68 $this->_actionButtonName = $this->getButtonName('next', 'action');
69
70 $this->_done = FALSE;
71
72 parent::preProcess();
73
74 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
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;
83 if ($this->_context === 'basic') {
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),
92 $this->getSortID(),
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 /**
106 * Build the form object.
107 *
108 * @throws \CRM_Core_Exception
109 * @throws \CiviCRM_API3_Exception
110 */
111 public function buildQuickForm() {
112 parent::buildQuickForm();
113 $this->addContactSearchFields();
114
115 CRM_Member_BAO_Query::buildSearchForm($this);
116
117 $rows = $this->get('rows');
118 if (is_array($rows)) {
119 if (!$this->_single) {
120 $this->addRowSelectors($rows);
121 }
122
123 $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission()));
124 }
125
126 }
127
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
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
180 /**
181 * The post processing of the form gets done here.
182 *
183 * Key things done during post processing are
184 * - check for reset or next request. if present, skip post procesing.
185 * - now check if user requested running a saved search, if so, then
186 * the form values associated with the saved search are used for searching.
187 * - if user has done a submit with new values the regular post submissing is
188 * done.
189 * The processing consists of using a Selector / Controller framework for getting the
190 * search results.
191 *
192 * @throws \CRM_Core_Exception
193 */
194 public function postProcess() {
195 if ($this->_done) {
196 return;
197 }
198
199 $this->_done = TRUE;
200 $this->setFormValues();
201
202 $this->fixFormValues();
203
204 // We don't show test records in summaries or dashboards
205 if (empty($this->_formValues['member_test']) && $this->_force) {
206 $this->_formValues["member_test"] = 0;
207 }
208
209 CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
210
211 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
212
213 $this->set('queryParams', $this->_queryParams);
214
215 $buttonName = $this->controller->getButtonName();
216 if ($buttonName == $this->_actionButtonName) {
217 // check actionName and if next, then do not repeat a search, since we are going to the next page
218
219 // hack, make sure we reset the task values
220 $stateMachine = $this->controller->getStateMachine();
221 $formName = $stateMachine->getTaskFormName();
222 $this->controller->resetPage($formName);
223 return;
224 }
225
226 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, $this->entityReferenceFields);
227
228 $selector = new CRM_Member_Selector_Search($this->_queryParams,
229 $this->_action,
230 NULL,
231 $this->_single,
232 $this->_limit,
233 $this->_context
234 );
235 $selector->setKey($this->controller->_key);
236
237 $prefix = NULL;
238 if ($this->_context === 'basic') {
239 $prefix = $this->_prefix;
240 }
241
242 $controller = new CRM_Core_Selector_Controller($selector,
243 $this->get(CRM_Utils_Pager::PAGE_ID),
244 $this->getSortID(),
245 CRM_Core_Action::VIEW,
246 $this,
247 CRM_Core_Selector_Controller::SESSION,
248 $prefix
249 );
250 $controller->setEmbedded(TRUE);
251 $controller->run();
252 }
253
254 /**
255 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
256 *
257 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
258 *
259 * @throws \CRM_Core_Exception
260 */
261 public function fixFormValues() {
262 if (!$this->_force) {
263 return;
264 }
265
266 $status = CRM_Utils_Request::retrieve('status', 'String');
267 if ($status) {
268 $status = explode(',', $status);
269 $this->_formValues['membership_status_id'] = $this->_defaults['membership_status_id'] = (array) $status;
270 }
271
272 $membershipType = CRM_Utils_Request::retrieve('type', 'String');
273
274 if ($membershipType) {
275 $this->_formValues['membership_type_id'] = [$membershipType];
276 $this->_defaults['membership_type_id'] = [$membershipType];
277 }
278
279 $cid = CRM_Utils_Request::retrieve('cid', 'Positive');
280
281 if ($cid) {
282 $cid = CRM_Utils_Type::escape($cid, 'Integer');
283 if ($cid > 0) {
284 $this->_formValues['contact_id'] = $cid;
285 list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid);
286 $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid,
287 'sort_name'
288 );
289 // also assign individual mode to the template
290 $this->_single = TRUE;
291 }
292 }
293
294 $fromDate = CRM_Utils_Request::retrieve('start', 'Date');
295 if ($fromDate) {
296 list($date) = CRM_Utils_Date::setDateDefaults($fromDate);
297 $this->_formValues['member_start_date_low'] = $this->_defaults['member_start_date_low'] = $date;
298 }
299
300 $toDate = CRM_Utils_Request::retrieve('end', 'Date');
301 if ($toDate) {
302 list($date) = CRM_Utils_Date::setDateDefaults($toDate);
303 $this->_formValues['member_start_date_high'] = $this->_defaults['member_start_date_high'] = $date;
304 }
305 $joinDate = CRM_Utils_Request::retrieve('join', 'Date');
306 if ($joinDate) {
307 list($date) = CRM_Utils_Date::setDateDefaults($joinDate);
308 $this->_formValues['member_join_date_low'] = $this->_defaults['member_join_date_low'] = $date;
309 }
310
311 $joinEndDate = CRM_Utils_Request::retrieve('joinEnd', 'Date');
312 if ($joinEndDate) {
313 list($date) = CRM_Utils_Date::setDateDefaults($joinEndDate);
314 $this->_formValues['member_join_date_high'] = $this->_defaults['member_join_date_high'] = $date;
315 }
316
317 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive',
318 $this
319 );
320
321 //LCD also allow restrictions to membership owner via GET
322 $owner = CRM_Utils_Request::retrieve('owner', 'String');
323 if (in_array($owner, ['0', '1'])) {
324 $this->_formValues['member_is_primary'] = $this->_defaults['member_is_primary'] = $owner;
325 }
326 }
327
328 /**
329 * Return a descriptive name for the page, used in wizard header.
330 *
331 * @return string
332 */
333 public function getTitle() {
334 return ts('Find Memberships');
335 }
336
337 /**
338 * Set the metadata for the form.
339 *
340 * @throws \CiviCRM_API3_Exception
341 */
342 protected function setSearchMetadata() {
343 $this->addSearchFieldMetadata(['Membership' => CRM_Member_BAO_Query::getSearchFieldMetadata()]);
344 }
345
346 }