Merge pull request #8328 from mlutfy/master-crm18524
[civicrm-core.git] / CRM / Core / Form / Search.php
CommitLineData
3efb5b86
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
3efb5b86 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
3efb5b86
CW
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 |
c73475ea 12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
3efb5b86
CW
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 |
c73475ea
WA
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
3efb5b86
CW
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 */
3efb5b86
CW
27
28/**
29 * Base class for most search forms
30 */
31class CRM_Core_Form_Search extends CRM_Core_Form {
32
48473171
CW
33 /**
34 * Are we forced to run a search
35 *
36 * @var int
48473171
CW
37 */
38 protected $_force;
39
40 /**
100fef9d 41 * Name of search button
48473171
CW
42 *
43 * @var string
48473171
CW
44 */
45 protected $_searchButtonName;
46
47 /**
100fef9d 48 * Name of action button
48473171
CW
49 *
50 * @var string
48473171
CW
51 */
52 protected $_actionButtonName;
53
54 /**
100fef9d 55 * Form values that we will be using
48473171
CW
56 *
57 * @var array
48473171
CW
58 */
59 public $_formValues;
60
61 /**
100fef9d 62 * Have we already done this search
48473171 63 *
48473171
CW
64 * @var boolean
65 */
66 protected $_done;
67
68 /**
100fef9d 69 * What context are we being invoked from
48473171 70 *
48473171
CW
71 * @var string
72 */
73 protected $_context = NULL;
74
7a3978aa
FG
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
df60621b 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
7a3978aa
FG
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
34197a55 104 /**
0955d6b9 105 * Common buildForm tasks required by all searches.
34197a55 106 */
00be9182 107 public function buildQuickform() {
13d9bc82 108 CRM_Core_Resources::singleton()
562fda4b
CW
109 ->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header')
110 ->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
3efb5b86
CW
111
112 $this->addButtons(array(
113 array(
114 'type' => 'refresh',
115 'name' => ts('Search'),
116 'isDefault' => TRUE,
117 ),
118 ));
8d36b801 119
023e90c3 120 $this->addClass('crm-search-form');
7a3978aa
FG
121
122 // for backwards compatibility we pass an argument to addTaskMenu even though
123 // it could just as well access $this->_taskList internally
124 $tasks = $this->buildTaskList();
125 $this->addTaskMenu($tasks);
8d36b801
CW
126 }
127
128 /**
0955d6b9 129 * Add checkboxes for each row plus a master checkbox.
ad37ac8e 130 *
131 * @param array $rows
8d36b801 132 */
00be9182 133 public function addRowSelectors($rows) {
8d36b801 134 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
4126499f 135 if (!empty($rows)) {
136 foreach ($rows as $row) {
6eb91e49 137 if (CRM_Utils_Array::value('checkbox', $row)) {
138 $this->addElement('checkbox', $row['checkbox'], NULL, NULL, array('class' => 'select-row'));
139 }
4126499f 140 }
8d36b801 141 }
3efb5b86 142 }
34197a55
CW
143
144 /**
ad37ac8e 145 * Add actions menu to search results form.
146 *
147 * @param array $tasks
34197a55 148 */
00be9182 149 public function addTaskMenu($tasks) {
34197a55
CW
150 if (is_array($tasks) && !empty($tasks)) {
151 $tasks = array('' => ts('Actions')) + $tasks;
9597c394 152 $this->add('select', 'task', NULL, $tasks, FALSE, array('class' => 'crm-select2 crm-action-menu fa-check-circle-o huge crm-search-result-actions'));
34197a55 153 $this->add('submit', $this->_actionButtonName, ts('Go'), array('class' => 'hiddenElement crm-search-go-button'));
8d36b801
CW
154
155 // Radio to choose "All items" or "Selected items only"
156 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
157 $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
158 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']);
159 $this->assign('ts_all_id', $allRowsRadio->_attributes['id']);
34197a55
CW
160 }
161 }
96025800 162
e597fc33
DG
163 /**
164 * Add the sort-name field to the form.
165 *
166 * There is a setting to determine whether email is included in the search & we look this up to determine
167 * which text to choose.
168 *
169 * Note that for translation purposes the full string works better than using 'prefix' hence we use override-able functions
170 * to define the string.
171 */
172 protected function addSortNameField() {
173 $this->addElement(
174 'text',
175 'sort_name',
176 civicrm_api3('setting', 'getvalue', array('name' => 'includeEmailInName', 'group' => 'Search Preferences')) ? $this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail(),
177 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
178 );
179 }
180
181 /**
182 * Get the label for the sortName field if email searching is on.
183 *
184 * (email searching is a setting under search preferences).
185 *
186 * @return string
187 */
188 protected function getSortNameLabelWithEmail() {
189 return ts('Name or Email');
190 }
191
192 /**
193 * Get the label for the sortName field if email searching is off.
194 *
195 * (email searching is a setting under search preferences).
196 *
197 * @return string
198 */
199 protected function getSortNameLabelWithOutEmail() {
200 return ts('Name');
201 }
202
2ee21eaa
CW
203 /**
204 * Explicitly declare the form context for addField().
205 */
206 public function getDefaultContext() {
207 return 'search';
208 }
209
3efb5b86 210}