Merge pull request #17932 from seamuslee001/ref_radio
[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;', TRUE);
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 ]);
60 }
61
62 if (!empty($searchOptions['tags'])) {
63 // tag criteria
64 if (!empty($this->_tag)) {
65 $this->addField('tag', [
66 'entity' => 'entity_tag',
67 'label' => ts('with'),
68 'placeholder' => ts('- any tag -'),
69 ]);
70 }
71 }
72
73 parent::buildQuickForm();
74 }
75
76 /**
77 * Set the default form values.
78 *
79 *
80 * @return array
81 * the default array reference
82 */
83 public function setDefaultValues() {
84 $defaults = [];
85
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);
91 }
92 else {
93 $defaults[$v] = '';
94 }
95 }
96
97 if ($this->_context === 'amtg') {
98 $defaults['task'] = CRM_Contact_Task::GROUP_ADD;
99 }
100
101 if ($this->_context === 'smog') {
102 $defaults['group_contact_status[Added]'] = TRUE;
103 }
104
105 return $defaults;
106 }
107
108 /**
109 * Add local and global form rules.
110 */
111 public function addRules() {
112 $this->addFormRule(['CRM_Contact_Form_Search_Basic', 'formRule']);
113 }
114
115 /**
116 * Processing needed for buildForm and later.
117 */
118 public function preProcess() {
119 $this->set('searchFormName', 'Basic');
120
121 parent::preProcess();
122 }
123
124 /**
125 * @return array
126 */
127 public function &getFormValues() {
128 return $this->_formValues;
129 }
130
131 /**
132 * This method is called for processing a submitted search form.
133 */
134 public function postProcess() {
135 $this->set('isAdvanced', '0');
136 $this->set('isSearchBuilder', '0');
137
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);
142 }
143
144 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
145 $this->_formValues['group'] = $this->_groupID;
146 }
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);
150
151 //fix for CRM-1505
152 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
153 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
154 }
155 }
156
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;
163 }
164 else {
165 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
166 }
167 }
168 else {
169 $this->_sortByCharacter = NULL;
170 }
171
172 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
173 $this->_returnProperties = &$this->returnProperties();
174
175 parent::postProcess();
176 }
177
178 /**
179 * Add a form rule for this form.
180 *
181 * If Go is pressed then we must select some checkboxes and an action.
182 *
183 * @param array $fields
184 * @param array $files
185 * @param object $form
186 *
187 * @return array|bool
188 */
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.'];
194 }
195
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
198 return TRUE;
199 }
200
201 // if the all contact option is selected, ignore the contact checkbox validation
202 if ($fields['radio_ts'] == 'ts_all') {
203 return TRUE;
204 }
205
206 foreach ($fields as $name => $dontCare) {
207 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
208 return TRUE;
209 }
210 }
211 return ['task' => 'Please select one or more checkboxes to perform the action on.'];
212 }
213 return TRUE;
214 }
215
216 /**
217 * Return a descriptive name for the page, used in wizard header
218 *
219 * @return string
220 */
221
222 /**
223 * @return string
224 */
225 public function getTitle() {
226 return ts('Find Contacts');
227 }
228
229 }