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