Improve api.getoptions to return nested groups/tags
authorColeman Watts <coleman@civicrm.org>
Thu, 1 Jan 2015 03:54:43 +0000 (22:54 -0500)
committerColeman Watts <coleman@civicrm.org>
Thu, 1 Jan 2015 03:56:51 +0000 (22:56 -0500)
CRM/Activity/BAO/Query.php
CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/GroupContact.php
CRM/Contact/Form/Search/Basic.php
CRM/Contact/Form/Search/Criteria.php
CRM/Contribute/BAO/Query.php
CRM/Core/BAO/EntityTag.php
CRM/Core/Form.php

index 912924c6cbd8705c48b7cb9022acf299d0732a6b..e5ff26e68f0125ce9c0f9f41c458e5d391d992fe 100644 (file)
@@ -541,7 +541,7 @@ class CRM_Activity_BAO_Query {
       CRM_Campaign_BAO_Campaign::accessCampaign()
     ) {
       $buildEngagementLevel = TRUE;
-      $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'option_url' => NULL));
+      $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'context' => 'search'));
 
       // Add survey result field.
       $optionGroups  = CRM_Campaign_BAO_Survey::getResultSets( 'name' );
index 900ab32e55101ea87d772ffb71354fb0f39b19e0..6b4b2d42c193a6621db5afb5fe89287cca343156 100644 (file)
@@ -3217,7 +3217,8 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id )
         break;
       case 'contact_type':
         if ($context == 'search') {
-          // CRM-15495 - EntityRef filters expect this format
+          // CRM-15495 - EntityRef filters and basic search forms expect this format
+          // FIXME: Search builder does not
           return CRM_Contact_BAO_ContactType::getSelectElements();
         }
         break;
index ea23c9a3867b08a67a48d181bfb42eb637eed761..505f92cf2379415b345cb124e6f60a781c337306 100644 (file)
@@ -766,4 +766,28 @@ AND    contact_id IN ( $contactStr )
 
     return array($numContactsAdded, $numContactsNotAdded);
   }
+
+  /**
+   * Get options for a given field.
+   * @see CRM_Core_DAO::buildOptions
+   *
+   * @param String $fieldName
+   * @param String $context : @see CRM_Core_DAO::buildOptionsContext
+   * @param Array $props : whatever is known about this dao object
+   *
+   * @return Array|bool
+   */
+  public static function buildOptions($fieldName, $context = NULL, $props = array()) {
+    $params = array();
+
+    $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
+
+    // Sort group list by hierarchy
+    // TODO: This will only work when api.entity is "group_contact". What about others?
+    if (($fieldName == 'group' || $fieldName == 'group_id') && ($context == 'search' || $context == 'create')) {
+      $options = CRM_Contact_BAO_Group::getGroupsHierarchy($options, NULL, '- ', TRUE);
+    }
+
+    return $options;
+  }
 }
index 8bb31812ece72c9d057b8212a964ace33f2e4c3f..28b5c5a562d9d74c8bbf13f74066cb3011e230c4 100644 (file)
@@ -81,20 +81,15 @@ class CRM_Contact_Form_Search_Basic extends CRM_Contact_Form_Search {
       );
     }
 
+    // add select for groups
     if (!empty($searchOptions['groups'])) {
-      // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
-      $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($this->_group, NULL, '&nbsp;&nbsp;', TRUE);
-
-      // add select for groups
-      $group = array('' => ts('- any group -')) + $groupHierarchy;
-      $this->add('select', 'group', ts('in'), $group, FALSE, array('class' => 'crm-select2'));
+      $this->addSelect('group', array('entity' => 'group_contact', 'label' => ts('in'), 'context' => 'search', 'placeholder' => ts('- any group -')));
     }
 
     if (!empty($searchOptions['tags'])) {
       // tag criteria
       if (!empty($this->_tag)) {
-        $tag = array('' => ts('- any tag -')) + $this->_tag;
-        $this->add('select', 'tag', ts('with'), $tag, FALSE, array('class' => 'crm-select2'));
+        $this->addSelect('tag', array('entity' => 'entity_tag', 'label' => ts('with'), 'context' => 'search', 'placeholder' => ts('- any tag -')));
       }
     }
 
index 7bd9fb9aadaf061cdeeaf70afa39ee27d5cb8a3e..18ac6537264b22990a4a48d26fbfc471e2f6b5ce 100644 (file)
@@ -259,7 +259,7 @@ class CRM_Contact_Form_Search_Criteria {
     $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Communication Method'));
 
     //CRM-6138 Preferred Language
-    $form->addSelect('preferred_language', array('class' => 'twenty', 'option_url' => NULL));
+    $form->addSelect('preferred_language', array('class' => 'twenty', 'context' => 'search'));
 
     // Phone search
     $form->addElement('text', 'phone_numeric', ts('Phone Number'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone'));
@@ -357,7 +357,7 @@ class CRM_Contact_Form_Search_Criteria {
       $form->addRule('prox_distance', ts('Please enter positive number as a distance'), 'numeric');
     }
 
-    $form->addSelect('world_region', array('entity' => 'address', 'placeholder' => ts('- any -'), 'option_url' => NULL));
+    $form->addSelect('world_region', array('entity' => 'address', 'context' => 'search'));
 
     // select for location type
     $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
index 17c14a3c761e43b67a3bd40beebc760138dc9098..5df96403accccca54725142e727c1633450c3fbb 100644 (file)
@@ -985,7 +985,7 @@ class CRM_Contribute_BAO_Query {
 
     // CRM-13848
     $form->addSelect('financial_type_id',
-      array('entity' => 'contribution', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
+      array('entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search')
     );
 
     $form->add('select', 'contribution_page_id',
@@ -1064,9 +1064,8 @@ class CRM_Contribute_BAO_Query {
       array(
         'entity'      => 'contribution_soft',
         'field'       => 'soft_credit_type_id',
-        'multiple'    => 'multiple',
-        'option_url'  => NULL,
-        'placeholder' => ts('- any -')
+        'multiple'    => TRUE,
+        'context'     => 'search',
       )
     );
 
index 9cb2ec4f77ba09e64e111506dc6bd57032630d0c..e6b2d4bebbb7e96e0c93055ef6be79f8d0f392ab 100644 (file)
@@ -378,4 +378,28 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
     $tags['status'] = TRUE;
     return $tags;
   }
+
+  /**
+   * Get options for a given field.
+   * @see CRM_Core_DAO::buildOptions
+   *
+   * @param String $fieldName
+   * @param String $context : @see CRM_Core_DAO::buildOptionsContext
+   * @param Array $props : whatever is known about this dao object
+   *
+   * @return Array|bool
+   */
+  public static function buildOptions($fieldName, $context = NULL, $props = array()) {
+    $params = array();
+
+    $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
+
+    // Output tag list as nested hierarchy
+    // TODO: This will only work when api.entity is "entity_tag". What about others?
+    if (($fieldName == 'tag' || $fieldName == 'tag_id') && ($context == 'search' || $context == 'create')) {
+      $options = CRM_Core_BAO_Tag::getTags('civicrm_contact', CRM_Core_DAO::$_nullArray, CRM_Utils_Array::value('parent_id', $params), '- ');
+    }
+
+    return $options;
+  }
 }
index 4da2b6414b19a140f27b69897d85be2e5f5515c2..b9689f6b11ea7ec205ebea1eed6bf8631d7f8f99 100644 (file)
@@ -989,6 +989,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    *   - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
    *   - placeholder - set to NULL to disable
    *   - multiple - bool
+   *   - context - @see CRM_Core_DAO::buildOptionsContext
    * @param bool $required
    * @throws CRM_Core_Exception
    * @return HTML_QuickForm_Element
@@ -1005,18 +1006,20 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       $options = $props['options'];
     }
     else {
-      $info = civicrm_api3($props['entity'], 'getoptions', array('field' => $props['field']));
+      $info = civicrm_api3($props['entity'], 'getoptions', $props);
       $options = $info['values'];
     }
     if (!array_key_exists('placeholder', $props)) {
-      $props['placeholder'] = $required ? ts('- select -') : ts('- none -');
+      $props['placeholder'] = $required ? ts('- select -') : CRM_Utils_Array::value('context', $props) == 'search' ? ts('- any -') : ts('- none -');
     }
     // Handle custom field
     if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
       list(, $id) = explode('_', $name);
       $label = isset($props['label']) ? $props['label'] : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id);
       $gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id);
-      $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $gid);
+      if (CRM_Utils_Array::value('context', $props) != 'search') {
+        $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $gid);
+      }
     }
     // Core field
     else {
@@ -1031,12 +1034,14 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
         }
       }
       $label = isset($props['label']) ? $props['label'] : $fieldSpec['title'];
-      $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : $props['data-option-edit-path'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec);
+      if (CRM_Utils_Array::value('context', $props) != 'search') {
+        $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : $props['data-option-edit-path'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec);
+      }
     }
     $props['class'] = (isset($props['class']) ? $props['class'] . ' ' : '') . "crm-select2";
     $props['data-api-entity'] = $props['entity'];
     $props['data-api-field'] = $props['field'];
-    CRM_Utils_Array::remove($props, 'label', 'entity', 'field', 'option_url', 'options');
+    CRM_Utils_Array::remove($props, 'label', 'entity', 'field', 'option_url', 'options', 'context');
     return $this->add('select', $name, $label, $options, $required, $props);
   }