| 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 | * 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 | * Metadata for fields on the search form. |
| 106 | * |
| 107 | * @var array |
| 108 | */ |
| 109 | protected $searchFieldMetadata = []; |
| 110 | |
| 111 | /** |
| 112 | * @return array |
| 113 | */ |
| 114 | public function getSearchFieldMetadata() { |
| 115 | return $this->searchFieldMetadata; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param array $searchFieldMetadata |
| 120 | */ |
| 121 | public function addSearchFieldMetadata($searchFieldMetadata) { |
| 122 | $this->searchFieldMetadata = array_merge($this->searchFieldMetadata, $searchFieldMetadata); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Common buildForm tasks required by all searches. |
| 127 | */ |
| 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'); |
| 132 | |
| 133 | $this->addButtons(array( |
| 134 | array( |
| 135 | 'type' => 'refresh', |
| 136 | 'name' => ts('Search'), |
| 137 | 'isDefault' => TRUE, |
| 138 | ), |
| 139 | )); |
| 140 | |
| 141 | $this->addClass('crm-search-form'); |
| 142 | |
| 143 | $tasks = $this->buildTaskList(); |
| 144 | $this->addTaskMenu($tasks); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Add any fields described in metadata to the form. |
| 149 | * |
| 150 | * The goal is to describe all fields in metadata and handle from metadata rather |
| 151 | * than existing ad hoc handling. |
| 152 | */ |
| 153 | public function addFormFieldsFromMetadata() { |
| 154 | $this->_action = CRM_Core_Action::ADVANCED; |
| 155 | foreach ($this->getSearchFieldMetadata() as $entity => $fields) { |
| 156 | foreach ($fields as $fieldName => $fieldSpec) { |
| 157 | if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || $fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { |
| 158 | $this->addDatePickerRange($fieldName, $fieldSpec['title'], ($fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME))); |
| 159 | } |
| 160 | else { |
| 161 | $this->addField($fieldName, ['entity' => $entity]); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get the validation rule to apply to a function. |
| 169 | * |
| 170 | * Alphanumeric is designed to always be safe & for now we just return |
| 171 | * that but in future we can use tighter rules for types like int, bool etc. |
| 172 | * |
| 173 | * @param string $entity |
| 174 | * @param string $fieldName |
| 175 | * |
| 176 | * @return string |
| 177 | */ |
| 178 | protected function getValidationTypeForField($entity, $fieldName) { |
| 179 | switch ($this->getSearchFieldMetadata()[$entity][$fieldName]['type']) { |
| 180 | case CRM_Utils_Type::T_BOOLEAN: |
| 181 | return 'Boolean'; |
| 182 | |
| 183 | case CRM_Utils_Type::T_INT: |
| 184 | return 'CommaSeparatedIntegers'; |
| 185 | |
| 186 | case CRM_Utils_Type::T_DATE: |
| 187 | case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME: |
| 188 | return 'Timestamp'; |
| 189 | |
| 190 | default: |
| 191 | return 'Alphanumeric'; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Get the defaults for the entity for any fields described in metadata. |
| 197 | * |
| 198 | * @param string $entity |
| 199 | * |
| 200 | * @return array |
| 201 | */ |
| 202 | protected function getEntityDefaults($entity) { |
| 203 | $defaults = []; |
| 204 | foreach ($this->getSearchFieldMetadata()[$entity] as $fieldSpec) { |
| 205 | if (empty($_POST[$fieldSpec['name']])) { |
| 206 | $value = CRM_Utils_Request::retrieveValue($fieldSpec['name'], $this->getValidationTypeForField($entity, $fieldSpec['name']), FALSE, NULL, 'GET'); |
| 207 | if ($value !== FALSE) { |
| 208 | $defaults[$fieldSpec['name']] = $value; |
| 209 | } |
| 210 | if ($fieldSpec['type'] === CRM_Utils_Type::T_DATE || ($fieldSpec['type'] === CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)) { |
| 211 | $low = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_low', 'Timestamp', FALSE, NULL, 'GET'); |
| 212 | $high = CRM_Utils_Request::retrieveValue($fieldSpec['name'] . '_high', 'Timestamp', FALSE, NULL, 'GET'); |
| 213 | if ($low !== FALSE || $high !== FALSE) { |
| 214 | $defaults[$fieldSpec['name'] . '_relative'] = 0; |
| 215 | $defaults[$fieldSpec['name'] . '_low'] = $low ? date('Y-m-d H:i:s', strtotime($low)) : NULL; |
| 216 | $defaults[$fieldSpec['name'] . '_high'] = $high ? date('Y-m-d H:i:s', strtotime($high)) : NULL; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | return $defaults; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Add checkboxes for each row plus a master checkbox. |
| 226 | * |
| 227 | * @param array $rows |
| 228 | */ |
| 229 | public function addRowSelectors($rows) { |
| 230 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows')); |
| 231 | if (!empty($rows)) { |
| 232 | foreach ($rows as $row) { |
| 233 | if (CRM_Utils_Array::value('checkbox', $row)) { |
| 234 | $this->addElement('checkbox', $row['checkbox'], NULL, NULL, array('class' => 'select-row')); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Add actions menu to search results form. |
| 242 | * |
| 243 | * @param array $tasks |
| 244 | */ |
| 245 | public function addTaskMenu($tasks) { |
| 246 | $taskMetaData = array(); |
| 247 | foreach ($tasks as $key => $task) { |
| 248 | $taskMetaData[$key] = array('title' => $task); |
| 249 | } |
| 250 | parent::addTaskMenu($taskMetaData); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Add the sort-name field to the form. |
| 255 | * |
| 256 | * There is a setting to determine whether email is included in the search & we look this up to determine |
| 257 | * which text to choose. |
| 258 | * |
| 259 | * Note that for translation purposes the full string works better than using 'prefix' hence we use override-able functions |
| 260 | * to define the string. |
| 261 | */ |
| 262 | protected function addSortNameField() { |
| 263 | $this->addElement( |
| 264 | 'text', |
| 265 | 'sort_name', |
| 266 | civicrm_api3('setting', 'getvalue', array('name' => 'includeEmailInName', 'group' => 'Search Preferences')) ? $this->getSortNameLabelWithEmail() : $this->getSortNameLabelWithOutEmail(), |
| 267 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name') |
| 268 | ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get the label for the sortName field if email searching is on. |
| 273 | * |
| 274 | * (email searching is a setting under search preferences). |
| 275 | * |
| 276 | * @return string |
| 277 | */ |
| 278 | protected function getSortNameLabelWithEmail() { |
| 279 | return ts('Name or Email'); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Get the label for the sortName field if email searching is off. |
| 284 | * |
| 285 | * (email searching is a setting under search preferences). |
| 286 | * |
| 287 | * @return string |
| 288 | */ |
| 289 | protected function getSortNameLabelWithOutEmail() { |
| 290 | return ts('Name'); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Explicitly declare the form context for addField(). |
| 295 | */ |
| 296 | public function getDefaultContext() { |
| 297 | return 'search'; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Add generic fields that specify the contact. |
| 302 | */ |
| 303 | protected function addContactSearchFields() { |
| 304 | if (!$this->isFormInViewOrEditMode()) { |
| 305 | return; |
| 306 | } |
| 307 | $this->addSortNameField(); |
| 308 | |
| 309 | $this->_group = CRM_Core_PseudoConstant::nestedGroup(); |
| 310 | if ($this->_group) { |
| 311 | $this->add('select', 'group', $this->getGroupLabel(), $this->_group, FALSE, |
| 312 | array( |
| 313 | 'id' => 'group', |
| 314 | 'multiple' => 'multiple', |
| 315 | 'class' => 'crm-select2', |
| 316 | ) |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | $contactTags = CRM_Core_BAO_Tag::getTags(); |
| 321 | if ($contactTags) { |
| 322 | $this->add('select', 'contact_tags', $this->getTagLabel(), $contactTags, FALSE, |
| 323 | array( |
| 324 | 'id' => 'contact_tags', |
| 325 | 'multiple' => 'multiple', |
| 326 | 'class' => 'crm-select2', |
| 327 | ) |
| 328 | ); |
| 329 | } |
| 330 | $this->addField('contact_type', array('entity' => 'Contact')); |
| 331 | |
| 332 | if (CRM_Core_Permission::check('access deleted contacts') && Civi::settings()->get('contact_undelete')) { |
| 333 | $this->addElement('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)')); |
| 334 | } |
| 335 | |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * we allow the controller to set force/reset externally, useful when we are being |
| 340 | * driven by the wizard framework |
| 341 | */ |
| 342 | protected function loadStandardSearchOptionsFromUrl() { |
| 343 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean'); |
| 344 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
| 345 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); |
| 346 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'search'); |
| 347 | $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); |
| 348 | $this->assign("context", $this->_context); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Get user submitted values. |
| 353 | * |
| 354 | * Get it from controller only if form has been submitted, else preProcess has set this |
| 355 | */ |
| 356 | protected function loadFormValues() { |
| 357 | if (!empty($_POST) && !$this->controller->isModal()) { |
| 358 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 359 | } |
| 360 | else { |
| 361 | $this->_formValues = $this->get('formValues'); |
| 362 | } |
| 363 | |
| 364 | if (empty($this->_formValues)) { |
| 365 | if (isset($this->_ssID)) { |
| 366 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | } |