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