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 // 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');
125 parent
::preProcess();
131 public function &getFormValues() {
132 return $this->_formValues
;
136 * This method is called for processing a submitted search form.
138 public function postProcess() {
139 $this->set('isAdvanced', '0');
140 $this->set('isSearchBuilder', '0');
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
);
148 if (isset($this->_groupID
) && empty($this->_formValues
['group'])) {
149 $this->_formValues
['group'] = $this->_groupID
;
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
);
156 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID
, 'mapping_id')) {
157 $this->_params
= CRM_Contact_BAO_SavedSearch
::getSearchParams($this->_ssID
);
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;
169 $this->_formValues
['sortByCharacter'] = $this->_sortByCharacter
;
173 $this->_sortByCharacter
= NULL;
176 $this->_params
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
177 $this->_returnProperties
= &$this->returnProperties();
179 parent
::postProcess();
183 * Add a form rule for this form.
185 * If Go is pressed then we must select some checkboxes and an action.
187 * @param array $fields
188 * @param array $files
189 * @param object $form
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.'];
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
205 // if the all contact option is selected, ignore the contact checkbox validation
206 if ($fields['radio_ts'] == 'ts_all') {
210 foreach ($fields as $name => $dontCare) {
211 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
215 return ['task' => 'Please select one or more checkboxes to perform the action on.'];
221 * Return a descriptive name for the page, used in wizard header
229 public function getTitle() {
230 return ts('Find Contacts');