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