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