Merge pull request #8491 from seamuslee001/CRM-18752
[civicrm-core.git] / CRM / Contact / Form / Search / Basic.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
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
TO
43 */
44 static $csv = array('contact_type', 'group', 'tag');
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'])) {
46b3417a 57 $contactTypes = array('' => 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,
62 array('class' => 'crm-select2')
6a488035
TO
63 );
64 }
65
76773c5a 66 // add select for groups
a7488080 67 if (!empty($searchOptions['groups'])) {
2ee21eaa 68 $this->addField('group', array(
353ffa53
TO
69 'entity' => 'group_contact',
70 'label' => ts('in'),
389bcebf 71 'placeholder' => ts('- any group -'),
353ffa53 72 ));
6a488035
TO
73 }
74
a7488080 75 if (!empty($searchOptions['tags'])) {
6a488035
TO
76 // tag criteria
77 if (!empty($this->_tag)) {
2ee21eaa 78 $this->addField('tag', array(
353ffa53
TO
79 'entity' => 'entity_tag',
80 'label' => ts('with'),
389bcebf 81 'placeholder' => ts('- any tag -'),
353ffa53 82 ));
6a488035
TO
83 }
84 }
85
86 parent::buildQuickForm();
87 }
88
89 /**
fe482240 90 * Set the default form values.
6a488035 91 *
6a488035 92 *
a6c01b45
CW
93 * @return array
94 * the default array reference
6a488035 95 */
00be9182 96 public function setDefaultValues() {
6a488035
TO
97 $defaults = array();
98
99 $defaults['sort_name'] = CRM_Utils_Array::value('sort_name', $this->_formValues);
100 foreach (self::$csv as $v) {
a7488080 101 if (!empty($this->_formValues[$v]) && is_array($this->_formValues[$v])) {
6a488035
TO
102 $tmpArray = array_keys($this->_formValues[$v]);
103 $defaults[$v] = array_pop($tmpArray);
104 }
105 else {
106 $defaults[$v] = '';
107 }
108 }
109
110 if ($this->_context === 'amtg') {
111 $defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS;
112 }
6a488035
TO
113
114 if ($this->_context === 'smog') {
115 $defaults['group_contact_status[Added]'] = TRUE;
116 }
117
118 return $defaults;
119 }
120
121 /**
fe482240 122 * Add local and global form rules.
6a488035 123 */
00be9182 124 public function addRules() {
6a488035
TO
125 $this->addFormRule(array('CRM_Contact_Form_Search_Basic', 'formRule'));
126 }
127
128 /**
fe482240 129 * Processing needed for buildForm and later.
6a488035 130 */
00be9182 131 public function preProcess() {
6a488035
TO
132 $this->set('searchFormName', 'Basic');
133
134 parent::preProcess();
135 }
136
86538308
EM
137 /**
138 * @return array
139 */
00be9182 140 public function &getFormValues() {
6a488035
TO
141 return $this->_formValues;
142 }
143
144 /**
fe482240 145 * This method is called for processing a submitted search form.
6a488035 146 */
00be9182 147 public function postProcess() {
6a488035
TO
148 $this->set('isAdvanced', '0');
149 $this->set('isSearchBuilder', '0');
150
151 // get user submitted values
152 // get it from controller only if form has been submitted, else preProcess has set this
153 if (!empty($_POST)) {
154 $this->_formValues = $this->controller->exportValues($this->_name);
6a488035
TO
155 }
156
8cc574cf 157 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
db135a44 158 $this->_formValues['group'] = $this->_groupID;
6a488035
TO
159 }
160 elseif (isset($this->_ssID) && empty($_POST)) {
161 // if we are editing / running a saved search and the form has not been posted
162 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
163
164 //fix for CRM-1505
165 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) {
166 $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID);
167 }
168 }
169
170 // we dont want to store the sortByCharacter in the formValue, it is more like
171 // a filter on the result set
172 // this filter is reset if we click on the search button
e166ff79 173 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
6a488035
TO
174 if (strtolower($this->_sortByCharacter) == 'all') {
175 $this->_formValues['sortByCharacter'] = NULL;
176 }
177 else {
178 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
179 }
180 }
e166ff79
CW
181 else {
182 $this->_sortByCharacter = NULL;
183 }
6a488035
TO
184
185 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
186 $this->_returnProperties = &$this->returnProperties();
187
188 parent::postProcess();
189 }
190
6a488035 191 /**
ea3ddccf 192 * Add a form rule for this form.
193 *
194 * If Go is pressed then we must select some checkboxes and an action.
195 *
196 * @param array $fields
197 *
198 * @return array|bool
6a488035 199 */
00be9182 200 public static function formRule($fields) {
6a488035
TO
201 // check actionName and if next, then do not repeat a search, since we are going to the next page
202 if (array_key_exists('_qf_Search_next', $fields)) {
a7488080 203 if (empty($fields['task'])) {
6a488035
TO
204 return array('task' => 'Please select a valid action.');
205 }
206
207 if (CRM_Utils_Array::value('task', $fields) == CRM_Contact_Task::SAVE_SEARCH) {
208 // dont need to check for selection of contacts for saving search
209 return TRUE;
210 }
211
212 // if the all contact option is selected, ignore the contact checkbox validation
213 if ($fields['radio_ts'] == 'ts_all') {
214 return TRUE;
215 }
216
217 foreach ($fields as $name => $dontCare) {
218 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
219 return TRUE;
220 }
221 }
222 return array('task' => 'Please select one or more checkboxes to perform the action on.');
223 }
224 return TRUE;
225 }
226
86538308
EM
227 /**
228 * Return a descriptive name for the page, used in wizard header
229 *
230 * @return string
86538308
EM
231 */
232 /**
233 * @return string
234 */
00be9182 235 public function getTitle() {
6a488035
TO
236 return ts('Find Contacts');
237 }
96025800 238
6a488035 239}