3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Base class for most search forms
31 class CRM_Core_Form_Search
extends CRM_Core_Form
{
34 * Are we forced to run a search
41 * Name of search button
45 protected $_searchButtonName;
48 * Name of action button
52 protected $_actionButtonName;
55 * Form values that we will be using
62 * Have we already done this search
69 * What context are we being invoked from
73 protected $_context = NULL;
76 * The list of tasks or actions that a searcher can perform on a result set.
80 protected $_taskList = [];
83 * Declare entity reference fields as they will need to be converted.
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.
90 protected $entityReferenceFields = [];
93 * Builds the list of tasks or actions that a searcher can perform on a result set.
95 * To modify the task list, child classes should alter $this->_taskList,
96 * preferably by extending this method.
100 protected function buildTaskList() {
101 return $this->_taskList
;
105 * Metadata for fields on the search form.
109 protected $searchFieldMetadata = [];
114 public function getSearchFieldMetadata() {
115 return $this->searchFieldMetadata
;
119 * @param array $searchFieldMetadata
121 public function addSearchFieldMetadata($searchFieldMetadata) {
122 $this->searchFieldMetadata
= array_merge($this->searchFieldMetadata
, $searchFieldMetadata);
126 * Common buildForm tasks required by all searches.
128 public function buildQuickform() {
129 CRM_Core_Resources
::singleton()
130 ->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header')
131 ->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
136 'name' => ts('Search'),
141 $this->addClass('crm-search-form');
143 $tasks = $this->buildTaskList();
144 $this->addTaskMenu($tasks);
148 * Add any fields described in metadata to the form.
150 * The goal is to describe all fields in metadata and handle from metadata rather
151 * than existing ad hoc handling.
153 public function addFormFieldsFromMetadata() {
154 $this->addFormRule(['CRM_Core_Form_Search', 'formRule'], $this);
155 $this->_action
= CRM_Core_Action
::ADVANCED
;
156 foreach ($this->getSearchFieldMetadata() as $entity => $fields) {
157 foreach ($fields as $fieldName => $fieldSpec) {
158 if ($fieldSpec['type'] === CRM_Utils_Type
::T_DATE ||
$fieldSpec['type'] === (CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
)) {
159 $this->addDatePickerRange($fieldName, $fieldSpec['title'], ($fieldSpec['type'] === (CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
)));
162 $this->addField($fieldName, ['entity' => $entity]);
169 * Global validation rules for the form.
171 * @param array $fields
172 * Posted values of the form.
173 * @param array $files
174 * @param object $form
177 * list of errors to be posted back to the form
179 public static function formRule($fields, $files, $form) {
181 foreach ($form->getSearchFieldMetadata() as $entity => $spec) {
182 foreach ($spec as $fieldName => $fieldSpec) {
183 if ($fieldSpec['type'] === CRM_Utils_Type
::T_DATE ||
$fieldSpec['type'] === (CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
)) {
184 if (isset($fields[$fieldName . '_high']) && isset($fields[$fieldName . '_low']) && empty($fields[$fieldName . '_relative'])) {
185 if (strtotime($fields[$fieldName . '_low']) > strtotime($fields[$fieldName . '_high'])) {
186 $errors[$fieldName . '_low'] = ts('%1: Please check that your date range is in correct chronological order.', [1 => $fieldSpec['title']]);
196 * Get the validation rule to apply to a function.
198 * Alphanumeric is designed to always be safe & for now we just return
199 * that but in future we can use tighter rules for types like int, bool etc.
201 * @param string $entity
202 * @param string $fieldName
206 protected function getValidationTypeForField($entity, $fieldName) {
207 switch ($this->getSearchFieldMetadata()[$entity][$fieldName]['type']) {
208 case CRM_Utils_Type
::T_BOOLEAN
:
211 case CRM_Utils_Type
::T_INT
:
212 return 'CommaSeparatedIntegers';
214 case CRM_Utils_Type
::T_DATE
:
215 case CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
:
219 return 'Alphanumeric';
224 * Get the defaults for the entity for any fields described in metadata.
226 * @param string $entity
230 protected function getEntityDefaults($entity) {
232 foreach ($this->getSearchFieldMetadata()[$entity] as $fieldSpec) {
233 if (empty($_POST[$fieldSpec['name']])) {
234 $value = CRM_Utils_Request
::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($entity, $fieldSpec['name']), FALSE, NULL, 'GET');
235 if ($value !== FALSE) {
236 $defaults[$fieldSpec['name']] = $value;
238 if ($fieldSpec['type'] === CRM_Utils_Type
::T_DATE ||
($fieldSpec['type'] === CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
)) {
239 $low = CRM_Utils_Request
::retrieveValue($fieldSpec['name'] . '_low', 'Timestamp', FALSE, NULL, 'GET');
240 $high = CRM_Utils_Request
::retrieveValue($fieldSpec['name'] . '_high', 'Timestamp', FALSE, NULL, 'GET');
241 if ($low !== FALSE ||
$high !== FALSE) {
242 $defaults[$fieldSpec['name'] . '_relative'] = 0;
243 $defaults[$fieldSpec['name'] . '_low'] = $low ?
date('Y-m-d H:i:s', strtotime($low)) : NULL;
244 $defaults[$fieldSpec['name'] . '_high'] = $high ?
date('Y-m-d H:i:s', strtotime($high)) : NULL;
253 * Add checkboxes for each row plus a master checkbox.
257 public function addRowSelectors($rows) {
258 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, ['class' => 'select-rows']);
260 foreach ($rows as $row) {
261 if (CRM_Utils_Array
::value('checkbox', $row)) {
262 $this->addElement('checkbox', $row['checkbox'], NULL, NULL, ['class' => 'select-row']);
269 * Add actions menu to search results form.
271 * @param array $tasks
273 public function addTaskMenu($tasks) {
275 foreach ($tasks as $key => $task) {
276 $taskMetaData[$key] = ['title' => $task];
278 parent
::addTaskMenu($taskMetaData);
282 * Add the sort-name field to the form.
284 * There is a setting to determine whether email is included in the search & we look this up to determine
285 * which text to choose.
287 * Note that for translation purposes the full string works better than using 'prefix' hence we use override-able functions
288 * to define the string.
290 protected function addSortNameField() {
294 civicrm_api3('setting', 'getvalue', ['name' => 'includeEmailInName', 'group' => 'Search Preferences']) ?
$this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail(),
295 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
300 * Get the label for the sortName field if email searching is on.
302 * (email searching is a setting under search preferences).
306 protected function getSortNameLabelWithEmail() {
307 return ts('Name or Email');
311 * Get the label for the sortName field if email searching is off.
313 * (email searching is a setting under search preferences).
317 protected function getSortNameLabelWithOutEmail() {
322 * Explicitly declare the form context for addField().
324 public function getDefaultContext() {
329 * Add generic fields that specify the contact.
331 protected function addContactSearchFields() {
332 if (!$this->isFormInViewOrEditMode()) {
335 $this->addSortNameField();
337 $this->_group
= CRM_Core_PseudoConstant
::nestedGroup();
339 $this->add('select', 'group', $this->getGroupLabel(), $this->_group
, FALSE,
342 'multiple' => 'multiple',
343 'class' => 'crm-select2',
348 $contactTags = CRM_Core_BAO_Tag
::getTags();
350 $this->add('select', 'contact_tags', $this->getTagLabel(), $contactTags, FALSE,
352 'id' => 'contact_tags',
353 'multiple' => 'multiple',
354 'class' => 'crm-select2',
358 $this->addField('contact_type', ['entity' => 'Contact']);
360 if (CRM_Core_Permission
::check('access deleted contacts') && Civi
::settings()->get('contact_undelete')) {
361 $this->addElement('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)'));
367 * we allow the controller to set force/reset externally, useful when we are being
368 * driven by the wizard framework
370 protected function loadStandardSearchOptionsFromUrl() {
371 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean');
372 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
373 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
374 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this, FALSE, 'search');
375 $this->_ssID
= CRM_Utils_Request
::retrieve('ssID', 'Positive', $this);
376 $this->assign("context", $this->_context
);
380 * Get user submitted values.
382 * Get it from controller only if form has been submitted, else preProcess has set this
384 protected function loadFormValues() {
385 if (!empty($_POST) && !$this->controller
->isModal()) {
386 $this->_formValues
= $this->controller
->exportValues($this->_name
);
389 $this->_formValues
= $this->get('formValues');
392 if (empty($this->_formValues
)) {
393 if (isset($this->_ssID
)) {
394 $this->_formValues
= CRM_Contact_BAO_SavedSearch
::getFormValues($this->_ssID
);