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
19 * Base Search / View form for *all* listing of multiple contacts.
21 class CRM_Contact_Form_Search_Basic
extends CRM_Contact_Form_Search
{
24 * csv - common search values
28 public static $csv = ['contact_type', 'group', 'tag'];
31 * Build the form object.
33 public function buildQuickForm() {
34 $this->addSortNameField();
36 $searchOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
37 'advanced_search_options'
40 if (!empty($searchOptions['contactType'])) {
41 $contactTypes = ['' => ts('- any contact type -')] + CRM_Contact_BAO_ContactType
::getSelectElements();
42 $this->add('select', 'contact_type',
46 ['class' => 'crm-select2']
50 // add select for groups
51 // Get hierarchical listing of groups, respecting ACLs for CRM-16836.
52 $groupHierarchy = CRM_Contact_BAO_Group
::getGroupsHierarchy($this->_group
, NULL, ' ', TRUE);
53 if (!empty($searchOptions['groups'])) {
54 $this->addField('group', [
55 'entity' => 'group_contact',
57 'placeholder' => ts('- any group -'),
58 'options' => $groupHierarchy,
62 if (!empty($searchOptions['tags'])) {
64 if (!empty($this->_tag
)) {
65 $this->addField('tag', [
66 'entity' => 'entity_tag',
67 'label' => ts('with'),
68 'placeholder' => ts('- any tag -'),
73 parent
::buildQuickForm();
77 * Set the default form values.
81 * the default array reference
83 public function setDefaultValues() {
86 $defaults['sort_name'] = $this->_formValues
['sort_name'] ??
NULL;
87 foreach (self
::$csv as $v) {
88 if (!empty($this->_formValues
[$v]) && is_array($this->_formValues
[$v])) {
89 $tmpArray = array_keys($this->_formValues
[$v]);
90 $defaults[$v] = array_pop($tmpArray);
97 if ($this->_context
=== 'amtg') {
98 $defaults['task'] = CRM_Contact_Task
::GROUP_ADD
;
101 if ($this->_context
=== 'smog') {
102 $defaults['group_contact_status[Added]'] = TRUE;
109 * Add local and global form rules.
111 public function addRules() {
112 $this->addFormRule(['CRM_Contact_Form_Search_Basic', 'formRule']);
116 * Processing needed for buildForm and later.
118 public function preProcess() {
119 $this->set('searchFormName', 'Basic');
121 parent
::preProcess();
127 public function &getFormValues() {
128 return $this->_formValues
;
132 * This method is called for processing a submitted search form.
134 public function postProcess() {
135 $this->set('isAdvanced', '0');
136 $this->set('isSearchBuilder', '0');
138 // get user submitted values
139 // get it from controller only if form has been submitted, else preProcess has set this
140 if (!empty($_POST)) {
141 $this->_formValues
= $this->controller
->exportValues($this->_name
);
144 if (isset($this->_groupID
) && empty($this->_formValues
['group'])) {
145 $this->_formValues
['group'] = $this->_groupID
;
147 elseif (isset($this->_ssID
) && empty($_POST)) {
148 // if we are editing / running a saved search and the form has not been posted
149 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);
152 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID
, 'mapping_id')) {
153 $this->_params
= CRM_Contact_BAO_SavedSearch
::getSearchParams($this->_ssID
);
157 // we dont want to store the sortByCharacter in the formValue, it is more like
158 // a filter on the result set
159 // this filter is reset if we click on the search button
160 if ($this->_sortByCharacter
!== NULL && empty($_POST)) {
161 if (strtolower($this->_sortByCharacter
) == 'all') {
162 $this->_formValues
['sortByCharacter'] = NULL;
165 $this->_formValues
['sortByCharacter'] = $this->_sortByCharacter
;
169 $this->_sortByCharacter
= NULL;
172 $this->_params
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
173 $this->_returnProperties
= &$this->returnProperties();
175 parent
::postProcess();
179 * Add a form rule for this form.
181 * If Go is pressed then we must select some checkboxes and an action.
183 * @param array $fields
184 * @param array $files
185 * @param object $form
189 public static function formRule($fields, $files, $form) {
190 // check actionName and if next, then do not repeat a search, since we are going to the next page
191 if (array_key_exists('_qf_Search_next', $fields)) {
192 if (empty($fields['task'])) {
193 return ['task' => 'Please select a valid action.'];
196 if (CRM_Utils_Array
::value('task', $fields) == CRM_Contact_Task
::SAVE_SEARCH
) {
197 // dont need to check for selection of contacts for saving search
201 // if the all contact option is selected, ignore the contact checkbox validation
202 if ($fields['radio_ts'] == 'ts_all') {
206 foreach ($fields as $name => $dontCare) {
207 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
211 return ['task' => 'Please select one or more checkboxes to perform the action on.'];
217 * Return a descriptive name for the page, used in wizard header
225 public function getTitle() {
226 return ts('Find Contacts');