Merge pull request #12834 from pradpnayak/preRelHook
[civicrm-core.git] / CRM / Contact / Form / Search / Basic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Base Search / View form for *all* listing of multiple contacts.
36 */
37 class CRM_Contact_Form_Search_Basic extends CRM_Contact_Form_Search {
38
39 /**
40 * csv - common search values
41 *
42 * @var array
43 */
44 static $csv = array('contact_type', 'group', 'tag');
45
46 /**
47 * Build the form object.
48 */
49 public function buildQuickForm() {
50 $this->addSortNameField();
51
52 $searchOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
53 'advanced_search_options'
54 );
55
56 if (!empty($searchOptions['contactType'])) {
57 $contactTypes = array('' => ts('- any contact type -')) + CRM_Contact_BAO_ContactType::getSelectElements();
58 $this->add('select', 'contact_type',
59 ts('is...'),
60 $contactTypes,
61 FALSE,
62 array('class' => 'crm-select2')
63 );
64 }
65
66 // add select for groups
67 // Get hierarchical listing of groups, respecting ACLs for CRM-16836.
68 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($this->_group, NULL, '&nbsp;&nbsp;', TRUE);
69 if (!empty($searchOptions['groups'])) {
70 $this->addField('group', array(
71 'entity' => 'group_contact',
72 'label' => ts('in'),
73 'placeholder' => ts('- any group -'),
74 'options' => $groupHierarchy,
75 ));
76 }
77
78 if (!empty($searchOptions['tags'])) {
79 // tag criteria
80 if (!empty($this->_tag)) {
81 $this->addField('tag', array(
82 'entity' => 'entity_tag',
83 'label' => ts('with'),
84 'placeholder' => ts('- any tag -'),
85 ));
86 }
87 }
88
89 parent::buildQuickForm();
90 }
91
92 /**
93 * Set the default form values.
94 *
95 *
96 * @return array
97 * the default array reference
98 */
99 public function setDefaultValues() {
100 $defaults = array();
101
102 $defaults['sort_name'] = CRM_Utils_Array::value('sort_name', $this->_formValues);
103 foreach (self::$csv as $v) {
104 if (!empty($this->_formValues[$v]) && is_array($this->_formValues[$v])) {
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') {
114 $defaults['task'] = CRM_Contact_Task::GROUP_ADD;
115 }
116
117 if ($this->_context === 'smog') {
118 $defaults['group_contact_status[Added]'] = TRUE;
119 }
120
121 return $defaults;
122 }
123
124 /**
125 * Add local and global form rules.
126 */
127 public function addRules() {
128 $this->addFormRule(array('CRM_Contact_Form_Search_Basic', 'formRule'));
129 }
130
131 /**
132 * Processing needed for buildForm and later.
133 */
134 public function preProcess() {
135 $this->set('searchFormName', 'Basic');
136
137 parent::preProcess();
138 }
139
140 /**
141 * @return array
142 */
143 public function &getFormValues() {
144 return $this->_formValues;
145 }
146
147 /**
148 * This method is called for processing a submitted search form.
149 */
150 public function postProcess() {
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);
158 }
159
160 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
161 $this->_formValues['group'] = $this->_groupID;
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
176 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
177 if (strtolower($this->_sortByCharacter) == 'all') {
178 $this->_formValues['sortByCharacter'] = NULL;
179 }
180 else {
181 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
182 }
183 }
184 else {
185 $this->_sortByCharacter = NULL;
186 }
187
188 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
189 $this->_returnProperties = &$this->returnProperties();
190
191 parent::postProcess();
192 }
193
194 /**
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
200 *
201 * @return array|bool
202 */
203 public static function formRule($fields) {
204 // check actionName and if next, then do not repeat a search, since we are going to the next page
205 if (array_key_exists('_qf_Search_next', $fields)) {
206 if (empty($fields['task'])) {
207 return array('task' => 'Please select a valid action.');
208 }
209
210 if (CRM_Utils_Array::value('task', $fields) == CRM_Contact_Task::SAVE_SEARCH) {
211 // dont need to check for selection of contacts for saving search
212 return TRUE;
213 }
214
215 // if the all contact option is selected, ignore the contact checkbox validation
216 if ($fields['radio_ts'] == 'ts_all') {
217 return TRUE;
218 }
219
220 foreach ($fields as $name => $dontCare) {
221 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
222 return TRUE;
223 }
224 }
225 return array('task' => 'Please select one or more checkboxes to perform the action on.');
226 }
227 return TRUE;
228 }
229
230 /**
231 * Return a descriptive name for the page, used in wizard header
232 *
233 * @return string
234 */
235 /**
236 * @return string
237 */
238 public function getTitle() {
239 return ts('Find Contacts');
240 }
241
242 }