3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Base Search / View form for *all* listing of multiple contacts.
21 class CRM_Contact_Form_Search_Basic
extends CRM_Contact_Form_Search
{
24 * csv - common search values
28 public static $csv = ['contact_type', 'group', 'tag'];
31 * Build the form object.
33 public function buildQuickForm() {
34 $this->addSortNameField();
36 $searchOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
37 'advanced_search_options'
40 if (!empty($searchOptions['contactType'])) {
41 $contactTypes = ['' => ts('- any contact type -')] + CRM_Contact_BAO_ContactType
::getSelectElements();
42 $this->add('select', 'contact_type',
46 ['class' => 'crm-select2']
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, ' ');
53 if (!empty($searchOptions['groups'])) {
54 $this->addField('group', [
55 'entity' => 'group_contact',
57 'placeholder' => ts('- any group -'),
58 'options' => $groupHierarchy,
63 if (!empty($searchOptions['tags'])) {
65 if (!empty($this->_tag
)) {
66 $this->addField('tag', [
67 'entity' => 'entity_tag',
68 'label' => ts('with'),
69 'placeholder' => ts('- any tag -'),
74 parent
::buildQuickForm();
78 * Set the default form values.
82 * the default array reference
84 public function setDefaultValues() {
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);
98 if ($this->_context
=== 'amtg') {
99 $defaults['task'] = CRM_Contact_Task
::GROUP_ADD
;
102 if ($this->_context
=== 'smog') {
103 $defaults['group_contact_status[Added]'] = TRUE;
110 * Add local and global form rules.
112 public function addRules() {
113 $this->addFormRule(['CRM_Contact_Form_Search_Basic', 'formRule']);
117 * Processing needed for buildForm and later.
119 public function preProcess() {
120 $this->set('searchFormName', 'Basic');
122 parent
::preProcess();
128 public function &getFormValues() {
129 return $this->_formValues
;
133 * This method is called for processing a submitted search form.
135 public function postProcess() {
136 $this->set('isAdvanced', '0');
137 $this->set('isSearchBuilder', '0');
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
);
145 if (isset($this->_groupID
) && empty($this->_formValues
['group'])) {
146 $this->_formValues
['group'] = $this->_groupID
;
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
);
153 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID
, 'mapping_id')) {
154 $this->_params
= CRM_Contact_BAO_SavedSearch
::getSearchParams($this->_ssID
);
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;
166 $this->_formValues
['sortByCharacter'] = $this->_sortByCharacter
;
170 $this->_sortByCharacter
= NULL;
173 $this->_params
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
174 $this->_returnProperties
= &$this->returnProperties();
176 parent
::postProcess();
180 * Add a form rule for this form.
182 * If Go is pressed then we must select some checkboxes and an action.
184 * @param array $fields
185 * @param array $files
186 * @param object $form
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.'];
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
202 // if the all contact option is selected, ignore the contact checkbox validation
203 if ($fields['radio_ts'] == 'ts_all') {
207 foreach ($fields as $name => $dontCare) {
208 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
212 return ['task' => 'Please select one or more checkboxes to perform the action on.'];
218 * Return a descriptive name for the page, used in wizard header
226 public function getTitle() {
227 return ts('Find Contacts');