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