Merge pull request #18591 from eileenmcnaughton/search
[civicrm-core.git] / CRM / Contact / Form / Search / Basic.php
... / ...
CommitLineData
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 */
21class 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 // 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.
123 $this->set('searchFormName', 'Basic');
124
125 parent::preProcess();
126 }
127
128 /**
129 * @return array
130 */
131 public function &getFormValues() {
132 return $this->_formValues;
133 }
134
135 /**
136 * This method is called for processing a submitted search form.
137 */
138 public function postProcess() {
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);
146 }
147
148 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
149 $this->_formValues['group'] = $this->_groupID;
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
164 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
165 if (strtolower($this->_sortByCharacter) == 'all') {
166 $this->_formValues['sortByCharacter'] = NULL;
167 }
168 else {
169 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
170 }
171 }
172 else {
173 $this->_sortByCharacter = NULL;
174 }
175
176 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
177 $this->_returnProperties = &$this->returnProperties();
178
179 parent::postProcess();
180 }
181
182 /**
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
188 * @param array $files
189 * @param object $form
190 *
191 * @return array|bool
192 */
193 public static function formRule($fields, $files, $form) {
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)) {
196 if (empty($fields['task'])) {
197 return ['task' => 'Please select a valid action.'];
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 }
215 return ['task' => 'Please select one or more checkboxes to perform the action on.'];
216 }
217 return TRUE;
218 }
219
220 /**
221 * Return a descriptive name for the page, used in wizard header
222 *
223 * @return string
224 */
225
226 /**
227 * @return string
228 */
229 public function getTitle() {
230 return ts('Find Contacts');
231 }
232
233}