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