3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Class is a pane in advanced search and the membership search page.
23 class CRM_Member_Form_Search
extends CRM_Core_Form_Search
{
26 * The params that are sent to the query.
30 protected $_queryParams;
33 * Are we restricting ourselves to a single contact.
37 protected $_single = FALSE;
40 * Are we restricting ourselves to a single contact.
44 protected $_limit = NULL;
47 * Prefix for the controller.
50 protected $_prefix = "member_";
53 * Declare entity reference fields as they will need to be converted to using 'IN'.
57 protected $entityReferenceFields = ['membership_type_id'];
60 * Processing needed for buildForm and later.
62 * @throws \CRM_Core_Exception
63 * @throws \CiviCRM_API3_Exception
65 public function preProcess() {
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.
69 $this->set('searchFormName', 'Search');
71 $this->_actionButtonName
= $this->getButtonName('next', 'action');
77 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
78 $selector = new CRM_Member_Selector_Search($this->_queryParams
,
86 if ($this->_context
=== 'basic') {
87 $prefix = $this->_prefix
;
90 $this->assign("{$prefix}limit", $this->_limit
);
91 $this->assign("{$prefix}single", $this->_single
);
93 $controller = new CRM_Core_Selector_Controller($selector,
94 $this->get(CRM_Utils_Pager
::PAGE_ID
),
96 CRM_Core_Action
::VIEW
,
98 CRM_Core_Selector_Controller
::TRANSFER
,
102 $controller->setEmbedded(TRUE);
103 $controller->moveFromSessionToTemplate();
105 $this->assign('summary', $this->get('summary'));
109 * Build the form object.
111 * @throws \CRM_Core_Exception
112 * @throws \CiviCRM_API3_Exception
114 public function buildQuickForm() {
115 parent
::buildQuickForm();
116 $this->addContactSearchFields();
118 CRM_Member_BAO_Query
::buildSearchForm($this);
120 $rows = $this->get('rows');
121 if (is_array($rows)) {
122 if (!$this->_single
) {
123 $this->addRowSelectors($rows);
126 $this->addTaskMenu(CRM_Member_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
132 * Get the label for the sortName field if email searching is on.
134 * (email searching is a setting under search preferences).
138 protected function getSortNameLabelWithEmail() {
139 return ts('Member Name or Email');
143 * Get the label for the sortName field if email searching is off.
145 * (email searching is a setting under search preferences).
149 protected function getSortNameLabelWithOutEmail() {
150 return ts('Member Name');
154 * Get the label for the tag field.
156 * We do this in a function so the 'ts' wraps the whole string to allow
157 * better translation.
161 protected function getTagLabel() {
162 return ts('Member Tag(s)');
166 * Get the label for the group field.
170 protected function getGroupLabel() {
171 return ts('Member Group(s)');
175 * Get the label for the group field.
179 protected function getContactTypeLabel() {
180 return ts('Member Contact Type');
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;
196 return $this->_defaults
;
200 * The post processing of the form gets done here.
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
208 * The processing consists of using a Selector / Controller framework for getting the
211 * @throws \CRM_Core_Exception
213 public function postProcess() {
219 $this->setFormValues();
221 $this->fixFormValues();
223 // We don't show test records in summaries or dashboards
224 if (empty($this->_formValues
['member_test']) && $this->_force
) {
225 $this->_formValues
["member_test"] = 0;
228 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
230 $this->set('queryParams', $this->_queryParams
);
232 $buttonName = $this->controller
->getButtonName();
233 if ($buttonName == $this->_actionButtonName
) {
234 // check actionName and if next, then do not repeat a search, since we are going to the next page
236 // hack, make sure we reset the task values
237 $stateMachine = $this->controller
->getStateMachine();
238 $formName = $stateMachine->getTaskFormName();
239 $this->controller
->resetPage($formName);
243 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
245 $selector = new CRM_Member_Selector_Search($this->_queryParams
,
252 $selector->setKey($this->controller
->_key
);
255 if ($this->_context
=== 'basic') {
256 $prefix = $this->_prefix
;
259 $controller = new CRM_Core_Selector_Controller($selector,
260 $this->get(CRM_Utils_Pager
::PAGE_ID
),
262 CRM_Core_Action
::VIEW
,
264 CRM_Core_Selector_Controller
::SESSION
,
267 $controller->setEmbedded(TRUE);
272 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
274 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
276 * @throws \CRM_Core_Exception
278 public function fixFormValues() {
279 if (!$this->_force
) {
283 // @todo Most of the below is likely no longer required.
284 $status = CRM_Utils_Request
::retrieve('membership_status_id', 'String');
286 $status = explode(',', $status);
287 $this->_formValues
['membership_status_id'] = $this->_defaults
['membership_status_id'] = (array) $status;
290 $membershipType = CRM_Utils_Request
::retrieve('type', 'String');
292 if ($membershipType) {
293 $this->_formValues
['membership_type_id'] = [$membershipType];
294 $this->_defaults
['membership_type_id'] = [$membershipType];
297 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive');
300 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
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,
307 // also assign individual mode to the template
308 $this->_single
= TRUE;
312 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
314 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
315 $this->_formValues
['member_start_date_low'] = $this->_defaults
['member_start_date_low'] = $date;
318 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
320 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
321 $this->_formValues
['member_start_date_high'] = $this->_defaults
['member_start_date_high'] = $date;
323 $joinDate = CRM_Utils_Request
::retrieve('join', 'Date');
325 list($date) = CRM_Utils_Date
::setDateDefaults($joinDate);
326 $this->_formValues
['member_join_date_low'] = $this->_defaults
['member_join_date_low'] = $date;
329 $joinEndDate = CRM_Utils_Request
::retrieve('joinEnd', 'Date');
331 list($date) = CRM_Utils_Date
::setDateDefaults($joinEndDate);
332 $this->_formValues
['member_join_date_high'] = $this->_defaults
['member_join_date_high'] = $date;
335 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
341 * Return a descriptive name for the page, used in wizard header.
345 public function getTitle() {
346 return ts('Find Memberships');
350 * Set the metadata for the form.
352 * @throws \CiviCRM_API3_Exception
354 protected function setSearchMetadata() {
355 $this->addSearchFieldMetadata(['Membership' => CRM_Member_BAO_Query
::getSearchFieldMetadata()]);