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