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 $this->set('searchFormName', 'Search');
68 $this->_searchButtonName
= $this->getButtonName('refresh');
69 $this->_actionButtonName
= $this->getButtonName('next', 'action');
75 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
76 $selector = new CRM_Member_Selector_Search($this->_queryParams
,
84 if ($this->_context
== 'basic') {
85 $prefix = $this->_prefix
;
88 $this->assign("{$prefix}limit", $this->_limit
);
89 $this->assign("{$prefix}single", $this->_single
);
91 $controller = new CRM_Core_Selector_Controller($selector,
92 $this->get(CRM_Utils_Pager
::PAGE_ID
),
94 CRM_Core_Action
::VIEW
,
96 CRM_Core_Selector_Controller
::TRANSFER
,
100 $controller->setEmbedded(TRUE);
101 $controller->moveFromSessionToTemplate();
103 $this->assign('summary', $this->get('summary'));
107 * Build the form object.
109 * @throws \CRM_Core_Exception
110 * @throws \CiviCRM_API3_Exception
112 public function buildQuickForm() {
113 parent
::buildQuickForm();
114 $this->addContactSearchFields();
116 CRM_Member_BAO_Query
::buildSearchForm($this);
118 $rows = $this->get('rows');
119 if (is_array($rows)) {
120 if (!$this->_single
) {
121 $this->addRowSelectors($rows);
124 $this->addTaskMenu(CRM_Member_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
130 * Get the label for the sortName field if email searching is on.
132 * (email searching is a setting under search preferences).
136 protected function getSortNameLabelWithEmail() {
137 return ts('Member Name or Email');
141 * Get the label for the sortName field if email searching is off.
143 * (email searching is a setting under search preferences).
147 protected function getSortNameLabelWithOutEmail() {
148 return ts('Member Name');
152 * Get the label for the tag field.
154 * We do this in a function so the 'ts' wraps the whole string to allow
155 * better translation.
159 protected function getTagLabel() {
160 return ts('Member Tag(s)');
164 * Get the label for the group field.
168 protected function getGroupLabel() {
169 return ts('Member Group(s)');
173 * Get the label for the group field.
177 protected function getContactTypeLabel() {
178 return ts('Member Contact Type');
182 * The post processing of the form gets done here.
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
190 * The processing consists of using a Selector / Controller framework for getting the
193 public function postProcess() {
199 $this->setFormValues();
201 $this->fixFormValues();
203 // We don't show test records in summaries or dashboards
204 if (empty($this->_formValues
['member_test']) && $this->_force
) {
205 $this->_formValues
["member_test"] = 0;
208 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
210 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
212 $this->set('queryParams', $this->_queryParams
);
214 $buttonName = $this->controller
->getButtonName();
215 if ($buttonName == $this->_actionButtonName
) {
216 // check actionName and if next, then do not repeat a search, since we are going to the next page
218 // hack, make sure we reset the task values
219 $stateMachine = $this->controller
->getStateMachine();
220 $formName = $stateMachine->getTaskFormName();
221 $this->controller
->resetPage($formName);
225 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
227 $selector = new CRM_Member_Selector_Search($this->_queryParams
,
234 $selector->setKey($this->controller
->_key
);
237 if ($this->_context
== 'basic') {
238 $prefix = $this->_prefix
;
241 $controller = new CRM_Core_Selector_Controller($selector,
242 $this->get(CRM_Utils_Pager
::PAGE_ID
),
244 CRM_Core_Action
::VIEW
,
246 CRM_Core_Selector_Controller
::SESSION
,
249 $controller->setEmbedded(TRUE);
251 $query = &$selector->getQuery();
256 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
258 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
260 public function fixFormValues() {
261 if (!$this->_force
) {
265 $status = CRM_Utils_Request
::retrieve('status', 'String');
267 $status = explode(',', $status);
268 $this->_formValues
['membership_status_id'] = $this->_defaults
['membership_status_id'] = (array) $status;
271 $membershipType = CRM_Utils_Request
::retrieve('type', 'String');
273 if ($membershipType) {
274 $this->_formValues
['membership_type_id'] = [$membershipType];
275 $this->_defaults
['membership_type_id'] = [$membershipType];
278 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive');
281 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
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,
288 // also assign individual mode to the template
289 $this->_single
= TRUE;
293 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
295 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
296 $this->_formValues
['member_start_date_low'] = $this->_defaults
['member_start_date_low'] = $date;
299 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
301 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
302 $this->_formValues
['member_start_date_high'] = $this->_defaults
['member_start_date_high'] = $date;
304 $joinDate = CRM_Utils_Request
::retrieve('join', 'Date');
306 list($date) = CRM_Utils_Date
::setDateDefaults($joinDate);
307 $this->_formValues
['member_join_date_low'] = $this->_defaults
['member_join_date_low'] = $date;
310 $joinEndDate = CRM_Utils_Request
::retrieve('joinEnd', 'Date');
312 list($date) = CRM_Utils_Date
::setDateDefaults($joinEndDate);
313 $this->_formValues
['member_join_date_high'] = $this->_defaults
['member_join_date_high'] = $date;
316 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
320 //LCD also allow restrictions to membership owner via GET
321 $owner = CRM_Utils_Request
::retrieve('owner', 'String');
322 if (in_array($owner, ['0', '1'])) {
323 $this->_formValues
['member_is_primary'] = $this->_defaults
['member_is_primary'] = $owner;
328 * Return a descriptive name for the page, used in wizard header.
332 public function getTitle() {
333 return ts('Find Memberships');
337 * Set the metadata for the form.
339 * @throws \CiviCRM_API3_Exception
341 protected function setSearchMetadata() {
342 $this->addSearchFieldMetadata(['Membership' => CRM_Member_BAO_Query
::getSearchFieldMetadata()]);