From 8c9caddcf1ee6c7be5aa22256f5b18e4163f3880 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 7 Jun 2019 15:01:16 +1200 Subject: [PATCH] Convert sort_name to use search form methodology to pass by url --- CRM/Activity/Form/Search.php | 10 ---------- CRM/Contact/Form/Search/Advanced.php | 8 ++++---- CRM/Contact/Form/Search/Criteria.php | 23 +++++++++++++++++++---- CRM/Core/Form/Search.php | 21 +++++++++++++++++++-- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/CRM/Activity/Form/Search.php b/CRM/Activity/Form/Search.php index 063fa8001f..1c82959f28 100644 --- a/CRM/Activity/Form/Search.php +++ b/CRM/Activity/Form/Search.php @@ -353,16 +353,6 @@ class CRM_Activity_Form_Search extends CRM_Core_Form_Search { return NULL; } - /** - * This virtual function is used to set the default values of various form elements. - * - * @return array|NULL - * reference to the array of default values - */ - public function setDefaultValues() { - return array_merge($this->getEntityDefaults($this->getDefaultEntity()), (array) $this->_formValues); - } - /** * Return a descriptive name for the page, used in wizard header * diff --git a/CRM/Contact/Form/Search/Advanced.php b/CRM/Contact/Form/Search/Advanced.php index 0656d737d6..b0743b2203 100644 --- a/CRM/Contact/Form/Search/Advanced.php +++ b/CRM/Contact/Form/Search/Advanced.php @@ -190,26 +190,26 @@ class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { /** * Set the default form values. * - * * @return array * the default array reference + * @throws \Exception */ public function setDefaultValues() { + $defaults = parent::setDefaultValues(); // Set ssID for unit tests. if (empty($this->_ssID)) { $this->_ssID = $this->get('ssID'); } - $defaults = array_merge($this->_formValues, array( + $defaults = array_merge($this->_formValues, [ 'privacy_toggle' => 1, 'operator' => 'AND', - )); + ], $defaults); $this->normalizeDefaultValues($defaults); if ($this->_context === 'amtg') { $defaults['task'] = CRM_Contact_Task::GROUP_ADD; } - return $defaults; } diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index 7ce75635f7..3ff122c8d4 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -33,9 +33,13 @@ class CRM_Contact_Form_Search_Criteria { /** - * @param CRM_Core_Form $form + * @param CRM_Contact_Form_Search_Advanced $form + * + * @throws \CRM_Core_Exception */ public static function basic(&$form) { + $form->addSearchFieldMetadata(['Contact' => self::getSearchFieldMetadata()]); + $form->addFormFieldsFromMetadata(); self::setBasicSearchFields($form); $form->addElement('hidden', 'hidden_basic', 1); @@ -100,9 +104,6 @@ class CRM_Contact_Form_Search_Criteria { } } - // add text box for last name, first name, street name, city - $form->addElement('text', 'sort_name', ts('Complete OR Partial Name'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); - // add text box for last name, first name, street name, city $form->add('text', 'email', ts('Complete OR Partial Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); @@ -254,6 +255,20 @@ class CRM_Contact_Form_Search_Criteria { $form->add('select', 'phone_phone_type_id', ts('Phone Type'), ['' => ts('- any -')] + $phoneType, FALSE, ['class' => 'crm-select2']); } + /** + * Get the metadata for fields to be included on the contact search form. + */ + public static function getSearchFieldMetadata() { + $fields = [ + 'sort_name' => ['title' => ts('Complete OR Partial Name'), 'template_grouping' => 'basic'], + ]; + $metadata = civicrm_api3('Contact', 'getfields', [])['values']; + foreach ($fields as $fieldName => $field) { + $fields[$fieldName] = array_merge(CRM_Utils_Array::value($fieldName, $metadata, []), $field); + } + return $fields; + } + /** * Defines the fields that can be displayed for the basic search section. * diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index 41688934af..865cb9e694 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -104,9 +104,11 @@ class CRM_Core_Form_Search extends CRM_Core_Form { /** * Metadata for fields on the search form. * + * Instantiate with empty array for contact to prevent e-notices. + * * @var array */ - protected $searchFieldMetadata = []; + protected $searchFieldMetadata = ['Contact' => []]; /** * @return array @@ -122,6 +124,17 @@ class CRM_Core_Form_Search extends CRM_Core_Form { $this->searchFieldMetadata = array_merge($this->searchFieldMetadata, $searchFieldMetadata); } + /** + * This virtual function is used to set the default values of various form elements. + * + * @return array|NULL + * reference to the array of default values + * @throws \Exception + */ + public function setDefaultValues() { + return array_merge($this->getEntityDefaults($this->getDefaultEntity()), (array) $this->_formValues); + } + /** * Common buildForm tasks required by all searches. */ @@ -159,7 +172,11 @@ class CRM_Core_Form_Search extends CRM_Core_Form { $this->addDatePickerRange($fieldName, $fieldSpec['title'], ($fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME))); } else { - $this->addField($fieldName, ['entity' => $entity]); + $props = ['entity' => $entity]; + if (isset($fields[$fieldName]['title'])) { + $props['label'] = $fields[$fieldName]['title']; + } + $this->addField($fieldName, $props); } } } -- 2.25.1