Merge pull request #11703 from eileenmcnaughton/export
[civicrm-core.git] / CRM / Core / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 * Base class for most search forms
30 */
31 class CRM_Core_Form_Search extends CRM_Core_Form {
32
33 /**
34 * Are we forced to run a search
35 *
36 * @var int
37 */
38 protected $_force;
39
40 /**
41 * Name of search button
42 *
43 * @var string
44 */
45 protected $_searchButtonName;
46
47 /**
48 * Name of action button
49 *
50 * @var string
51 */
52 protected $_actionButtonName;
53
54 /**
55 * Form values that we will be using
56 *
57 * @var array
58 */
59 public $_formValues;
60
61 /**
62 * Have we already done this search
63 *
64 * @var boolean
65 */
66 protected $_done;
67
68 /**
69 * What context are we being invoked from
70 *
71 * @var string
72 */
73 protected $_context = NULL;
74
75 /**
76 * The list of tasks or actions that a searcher can perform on a result set.
77 *
78 * @var array
79 */
80 protected $_taskList = array();
81
82 /**
83 * Declare entity reference fields as they will need to be converted.
84 *
85 * The entity reference format looks like '2,3' whereas the Query object expects array(2, 3)
86 * or array('IN' => array(2, 3). The latter is the one we are moving towards standardising on.
87 *
88 * @var array
89 */
90 protected $entityReferenceFields = array();
91
92 /**
93 * Builds the list of tasks or actions that a searcher can perform on a result set.
94 *
95 * To modify the task list, child classes should alter $this->_taskList,
96 * preferably by extending this method.
97 *
98 * @return array
99 */
100 protected function buildTaskList() {
101 return $this->_taskList;
102 }
103
104 /**
105 * Common buildForm tasks required by all searches.
106 */
107 public function buildQuickform() {
108 CRM_Core_Resources::singleton()
109 ->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header')
110 ->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
111
112 $this->addButtons(array(
113 array(
114 'type' => 'refresh',
115 'name' => ts('Search'),
116 'isDefault' => TRUE,
117 ),
118 ));
119
120 $this->addClass('crm-search-form');
121
122 $tasks = $this->buildTaskList();
123 $this->addTaskMenu($tasks);
124 }
125
126 /**
127 * Add checkboxes for each row plus a master checkbox.
128 *
129 * @param array $rows
130 */
131 public function addRowSelectors($rows) {
132 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
133 if (!empty($rows)) {
134 foreach ($rows as $row) {
135 if (CRM_Utils_Array::value('checkbox', $row)) {
136 $this->addElement('checkbox', $row['checkbox'], NULL, NULL, array('class' => 'select-row'));
137 }
138 }
139 }
140 }
141
142 /**
143 * Add actions menu to search results form.
144 *
145 * @param array $tasks
146 */
147 public function addTaskMenu($tasks) {
148 $taskMetaData = array();
149 foreach ($tasks as $key => $task) {
150 $taskMetaData[$key] = array('title' => $task);
151 }
152 parent::addTaskMenu($taskMetaData);
153 }
154
155 /**
156 * Add the sort-name field to the form.
157 *
158 * There is a setting to determine whether email is included in the search & we look this up to determine
159 * which text to choose.
160 *
161 * Note that for translation purposes the full string works better than using 'prefix' hence we use override-able functions
162 * to define the string.
163 */
164 protected function addSortNameField() {
165 $this->addElement(
166 'text',
167 'sort_name',
168 civicrm_api3('setting', 'getvalue', array('name' => 'includeEmailInName', 'group' => 'Search Preferences')) ? $this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail(),
169 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
170 );
171 }
172
173 /**
174 * Get the label for the sortName field if email searching is on.
175 *
176 * (email searching is a setting under search preferences).
177 *
178 * @return string
179 */
180 protected function getSortNameLabelWithEmail() {
181 return ts('Name or Email');
182 }
183
184 /**
185 * Get the label for the sortName field if email searching is off.
186 *
187 * (email searching is a setting under search preferences).
188 *
189 * @return string
190 */
191 protected function getSortNameLabelWithOutEmail() {
192 return ts('Name');
193 }
194
195 /**
196 * Explicitly declare the form context for addField().
197 */
198 public function getDefaultContext() {
199 return 'search';
200 }
201
202 /**
203 * Add generic fields that specify the contact.
204 */
205 protected function addContactSearchFields() {
206 if (!$this->isFormInViewOrEditMode()) {
207 return;
208 }
209 $this->addSortNameField();
210
211 $this->_group = CRM_Core_PseudoConstant::nestedGroup();
212 if ($this->_group) {
213 $this->add('select', 'group', $this->getGroupLabel(), $this->_group, FALSE,
214 array(
215 'id' => 'group',
216 'multiple' => 'multiple',
217 'class' => 'crm-select2',
218 )
219 );
220 }
221
222 $contactTags = CRM_Core_BAO_Tag::getTags();
223 if ($contactTags) {
224 $this->add('select', 'contact_tags', $this->getTagLabel(), $contactTags, FALSE,
225 array(
226 'id' => 'contact_tags',
227 'multiple' => 'multiple',
228 'class' => 'crm-select2',
229 )
230 );
231 }
232 $this->addField('contact_type', array('entity' => 'Contact'));
233
234 if (CRM_Core_Permission::check('access deleted contacts') && Civi::settings()->get('contact_undelete')) {
235 $this->addElement('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)'));
236 }
237
238 }
239
240 }