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->_actionButtonName
= $this->getButtonName('next', 'action');
74 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
75 $selector = new CRM_Member_Selector_Search($this->_queryParams
,
83 if ($this->_context
=== 'basic') {
84 $prefix = $this->_prefix
;
87 $this->assign("{$prefix}limit", $this->_limit
);
88 $this->assign("{$prefix}single", $this->_single
);
90 $controller = new CRM_Core_Selector_Controller($selector,
91 $this->get(CRM_Utils_Pager
::PAGE_ID
),
93 CRM_Core_Action
::VIEW
,
95 CRM_Core_Selector_Controller
::TRANSFER
,
99 $controller->setEmbedded(TRUE);
100 $controller->moveFromSessionToTemplate();
102 $this->assign('summary', $this->get('summary'));
106 * Build the form object.
108 * @throws \CRM_Core_Exception
109 * @throws \CiviCRM_API3_Exception
111 public function buildQuickForm() {
112 parent
::buildQuickForm();
113 $this->addContactSearchFields();
115 CRM_Member_BAO_Query
::buildSearchForm($this);
117 $rows = $this->get('rows');
118 if (is_array($rows)) {
119 if (!$this->_single
) {
120 $this->addRowSelectors($rows);
123 $this->addTaskMenu(CRM_Member_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission()));
129 * Get the label for the sortName field if email searching is on.
131 * (email searching is a setting under search preferences).
135 protected function getSortNameLabelWithEmail() {
136 return ts('Member Name or Email');
140 * Get the label for the sortName field if email searching is off.
142 * (email searching is a setting under search preferences).
146 protected function getSortNameLabelWithOutEmail() {
147 return ts('Member Name');
151 * Get the label for the tag field.
153 * We do this in a function so the 'ts' wraps the whole string to allow
154 * better translation.
158 protected function getTagLabel() {
159 return ts('Member Tag(s)');
163 * Get the label for the group field.
167 protected function getGroupLabel() {
168 return ts('Member Group(s)');
172 * Get the label for the group field.
176 protected function getContactTypeLabel() {
177 return ts('Member Contact Type');
181 * The post processing of the form gets done here.
183 * Key things done during post processing are
184 * - check for reset or next request. if present, skip post procesing.
185 * - now check if user requested running a saved search, if so, then
186 * the form values associated with the saved search are used for searching.
187 * - if user has done a submit with new values the regular post submissing is
189 * The processing consists of using a Selector / Controller framework for getting the
192 * @throws \CRM_Core_Exception
194 public function postProcess() {
200 $this->setFormValues();
202 $this->fixFormValues();
204 // We don't show test records in summaries or dashboards
205 if (empty($this->_formValues
['member_test']) && $this->_force
) {
206 $this->_formValues
["member_test"] = 0;
209 CRM_Core_BAO_CustomValue
::fixCustomFieldValue($this->_formValues
);
211 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
213 $this->set('queryParams', $this->_queryParams
);
215 $buttonName = $this->controller
->getButtonName();
216 if ($buttonName == $this->_actionButtonName
) {
217 // check actionName and if next, then do not repeat a search, since we are going to the next page
219 // hack, make sure we reset the task values
220 $stateMachine = $this->controller
->getStateMachine();
221 $formName = $stateMachine->getTaskFormName();
222 $this->controller
->resetPage($formName);
226 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
, 0, FALSE, NULL, $this->entityReferenceFields
);
228 $selector = new CRM_Member_Selector_Search($this->_queryParams
,
235 $selector->setKey($this->controller
->_key
);
238 if ($this->_context
=== 'basic') {
239 $prefix = $this->_prefix
;
242 $controller = new CRM_Core_Selector_Controller($selector,
243 $this->get(CRM_Utils_Pager
::PAGE_ID
),
245 CRM_Core_Action
::VIEW
,
247 CRM_Core_Selector_Controller
::SESSION
,
250 $controller->setEmbedded(TRUE);
252 $query = &$selector->getQuery();
257 * If this search has been forced then see if there are any get values, and if so over-ride the post values.
259 * Note that this means that GET over-rides POST :) & that force with no parameters can be very destructive.
261 * @throws \CRM_Core_Exception
263 public function fixFormValues() {
264 if (!$this->_force
) {
268 $status = CRM_Utils_Request
::retrieve('status', 'String');
270 $status = explode(',', $status);
271 $this->_formValues
['membership_status_id'] = $this->_defaults
['membership_status_id'] = (array) $status;
274 $membershipType = CRM_Utils_Request
::retrieve('type', 'String');
276 if ($membershipType) {
277 $this->_formValues
['membership_type_id'] = [$membershipType];
278 $this->_defaults
['membership_type_id'] = [$membershipType];
281 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive');
284 $cid = CRM_Utils_Type
::escape($cid, 'Integer');
286 $this->_formValues
['contact_id'] = $cid;
287 list($display, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($cid);
288 $this->_defaults
['sort_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $cid,
291 // also assign individual mode to the template
292 $this->_single
= TRUE;
296 $fromDate = CRM_Utils_Request
::retrieve('start', 'Date');
298 list($date) = CRM_Utils_Date
::setDateDefaults($fromDate);
299 $this->_formValues
['member_start_date_low'] = $this->_defaults
['member_start_date_low'] = $date;
302 $toDate = CRM_Utils_Request
::retrieve('end', 'Date');
304 list($date) = CRM_Utils_Date
::setDateDefaults($toDate);
305 $this->_formValues
['member_start_date_high'] = $this->_defaults
['member_start_date_high'] = $date;
307 $joinDate = CRM_Utils_Request
::retrieve('join', 'Date');
309 list($date) = CRM_Utils_Date
::setDateDefaults($joinDate);
310 $this->_formValues
['member_join_date_low'] = $this->_defaults
['member_join_date_low'] = $date;
313 $joinEndDate = CRM_Utils_Request
::retrieve('joinEnd', 'Date');
315 list($date) = CRM_Utils_Date
::setDateDefaults($joinEndDate);
316 $this->_formValues
['member_join_date_high'] = $this->_defaults
['member_join_date_high'] = $date;
319 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive',
323 //LCD also allow restrictions to membership owner via GET
324 $owner = CRM_Utils_Request
::retrieve('owner', 'String');
325 if (in_array($owner, ['0', '1'])) {
326 $this->_formValues
['member_is_primary'] = $this->_defaults
['member_is_primary'] = $owner;
331 * Return a descriptive name for the page, used in wizard header.
335 public function getTitle() {
336 return ts('Find Memberships');
340 * Set the metadata for the form.
342 * @throws \CiviCRM_API3_Exception
344 protected function setSearchMetadata() {
345 $this->addSearchFieldMetadata(['Membership' => CRM_Member_BAO_Query
::getSearchFieldMetadata()]);