dev/core#2147 Switch the groups search field on Simple search back from select2 to...
[civicrm-core.git] / CRM / Contact / Form / Search / Basic.php
CommitLineData
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
18/**
5a409b50 19 * Base Search / View form for *all* listing of multiple contacts.
6a488035
TO
20 */
21class CRM_Contact_Form_Search_Basic extends CRM_Contact_Form_Search {
22
d424ffde 23 /**
6a488035
TO
24 * csv - common search values
25 *
26 * @var array
6a488035 27 */
69078420 28 public static $csv = ['contact_type', 'group', 'tag'];
6a488035
TO
29
30 /**
fe482240 31 * Build the form object.
6a488035 32 */
00be9182 33 public function buildQuickForm() {
e597fc33 34 $this->addSortNameField();
6a488035
TO
35
36 $searchOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
37 'advanced_search_options'
38 );
39
a7488080 40 if (!empty($searchOptions['contactType'])) {
be2fb01f 41 $contactTypes = ['' => ts('- any contact type -')] + CRM_Contact_BAO_ContactType::getSelectElements();
6a488035
TO
42 $this->add('select', 'contact_type',
43 ts('is...'),
ba021dc0
CW
44 $contactTypes,
45 FALSE,
be2fb01f 46 ['class' => 'crm-select2']
6a488035
TO
47 );
48 }
49
76773c5a 50 // add select for groups
4b8a7579 51 // Get hierarchical listing of groups, respecting ACLs for CRM-16836.
5a56465c 52 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($this->_group, NULL, '- ', TRUE);
a7488080 53 if (!empty($searchOptions['groups'])) {
3ea1b537
MD
54 $this->addField('group', [
55 'entity' => 'group_contact',
56 'label' => ts('in'),
57 'placeholder' => ts('- any group -'),
58 'options' => $groupHierarchy,
3ea1b537 59 ]);
6a488035
TO
60 }
61
a7488080 62 if (!empty($searchOptions['tags'])) {
6a488035
TO
63 // tag criteria
64 if (!empty($this->_tag)) {
be2fb01f 65 $this->addField('tag', [
69078420
SL
66 'entity' => 'entity_tag',
67 'label' => ts('with'),
68 'placeholder' => ts('- any tag -'),
69 ]);
6a488035
TO
70 }
71 }
72
73 parent::buildQuickForm();
74 }
75
76 /**
fe482240 77 * Set the default form values.
6a488035 78 *
6a488035 79 *
a6c01b45
CW
80 * @return array
81 * the default array reference
6a488035 82 */
00be9182 83 public function setDefaultValues() {
be2fb01f 84 $defaults = [];
6a488035 85
9c1bc317 86 $defaults['sort_name'] = $this->_formValues['sort_name'] ?? NULL;
6a488035 87 foreach (self::$csv as $v) {
a7488080 88 if (!empty($this->_formValues[$v]) && is_array($this->_formValues[$v])) {
6a488035
TO
89 $tmpArray = array_keys($this->_formValues[$v]);
90 $defaults[$v] = array_pop($tmpArray);
91 }
92 else {
93 $defaults[$v] = '';
94 }
95 }
96
97 if ($this->_context === 'amtg') {
eda34f9b 98 $defaults['task'] = CRM_Contact_Task::GROUP_ADD;
6a488035 99 }
6a488035
TO
100
101 if ($this->_context === 'smog') {
102 $defaults['group_contact_status[Added]'] = TRUE;
103 }
104
105 return $defaults;
106 }
107
108 /**
fe482240 109 * Add local and global form rules.
6a488035 110 */
00be9182 111 public function addRules() {
be2fb01f 112 $this->addFormRule(['CRM_Contact_Form_Search_Basic', 'formRule']);
6a488035
TO
113 }
114
115 /**
fe482240 116 * Processing needed for buildForm and later.
6a488035 117 */
00be9182 118 public function preProcess() {
2d09a0c3 119 // SearchFormName is deprecated & to be removed - the replacement is for the task to
120 // call $this->form->getSearchFormValues()
121 // A couple of extensions use it.
6a488035
TO
122 $this->set('searchFormName', 'Basic');
123
124 parent::preProcess();
125 }
126
86538308
EM
127 /**
128 * @return array
129 */
00be9182 130 public function &getFormValues() {
6a488035
TO
131 return $this->_formValues;
132 }
133
134 /**
fe482240 135 * This method is called for processing a submitted search form.
6a488035 136 */
00be9182 137 public function postProcess() {
6a488035
TO
138 $this->set('isAdvanced', '0');
139 $this->set('isSearchBuilder', '0');
140
141 // get user submitted values
142 // get it from controller only if form has been submitted, else preProcess has set this
143 if (!empty($_POST)) {
144 $this->_formValues = $this->controller->exportValues($this->_name);
6a488035
TO
145 }
146
8cc574cf 147 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
db135a44 148 $this->_formValues['group'] = $this->_groupID;
6a488035
TO
149 }
150 elseif (isset($this->_ssID) && empty($_POST)) {
151 // if we are editing / running a saved search and the form has not been posted
152 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
153
154 //fix for CRM-1505
155 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
156 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
157 }
158 }
159
160 // we dont want to store the sortByCharacter in the formValue, it is more like
161 // a filter on the result set
162 // this filter is reset if we click on the search button
e166ff79 163 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
6a488035
TO
164 if (strtolower($this->_sortByCharacter) == 'all') {
165 $this->_formValues['sortByCharacter'] = NULL;
166 }
167 else {
168 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
169 }
170 }
e166ff79
CW
171 else {
172 $this->_sortByCharacter = NULL;
173 }
6a488035
TO
174
175 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
176 $this->_returnProperties = &$this->returnProperties();
177
178 parent::postProcess();
179 }
180
6a488035 181 /**
ea3ddccf 182 * Add a form rule for this form.
183 *
184 * If Go is pressed then we must select some checkboxes and an action.
185 *
186 * @param array $fields
69078420
SL
187 * @param array $files
188 * @param object $form
ea3ddccf 189 *
190 * @return array|bool
6a488035 191 */
d7ea25cd 192 public static function formRule($fields, $files, $form) {
6a488035
TO
193 // check actionName and if next, then do not repeat a search, since we are going to the next page
194 if (array_key_exists('_qf_Search_next', $fields)) {
a7488080 195 if (empty($fields['task'])) {
be2fb01f 196 return ['task' => 'Please select a valid action.'];
6a488035
TO
197 }
198
199 if (CRM_Utils_Array::value('task', $fields) == CRM_Contact_Task::SAVE_SEARCH) {
200 // dont need to check for selection of contacts for saving search
201 return TRUE;
202 }
203
204 // if the all contact option is selected, ignore the contact checkbox validation
205 if ($fields['radio_ts'] == 'ts_all') {
206 return TRUE;
207 }
208
209 foreach ($fields as $name => $dontCare) {
210 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
211 return TRUE;
212 }
213 }
be2fb01f 214 return ['task' => 'Please select one or more checkboxes to perform the action on.'];
6a488035
TO
215 }
216 return TRUE;
217 }
218
86538308
EM
219 /**
220 * Return a descriptive name for the page, used in wizard header
221 *
222 * @return string
86538308 223 */
69078420 224
86538308
EM
225 /**
226 * @return string
227 */
00be9182 228 public function getTitle() {
6a488035
TO
229 return ts('Find Contacts');
230 }
96025800 231
6a488035 232}