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