Merge pull request #14475 from eileenmcnaughton/contact_def
authorMonish Deb <deb.monish@gmail.com>
Fri, 7 Jun 2019 06:41:00 +0000 (12:11 +0530)
committerGitHub <noreply@github.com>
Fri, 7 Jun 2019 06:41:00 +0000 (12:11 +0530)
Permit sort_name as a url parameter on advanced search

CRM/Activity/Form/Search.php
CRM/Contact/Form/Search/Advanced.php
CRM/Contact/Form/Search/Criteria.php
CRM/Core/Form/Search.php

index 063fa8001f319fb447a32e503d86dae9fc48e865..1c82959f2828326f167828c4bde58e402f6ec487 100644 (file)
@@ -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
    *
index 0656d737d63d5aebc5e5900f20f9bd8185bb752e..b0743b220311ace8ab34ff98ac80d8073ee0b794 100644 (file)
@@ -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;
   }
 
index 240ade9aeaa664f6b40265c8f22cf69fffa51cea..1a533342a05e6ac8ab4e603d122229b93dbda4a9 100644 (file)
 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.
    *
index 41688934afbc4b6c94f3dfcfaca5113c7c2b46ef..865cb9e694d265d6300b4b4f21e1c868e0c96946 100644 (file)
@@ -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);
         }
       }
     }