From 76773c5a5191673e990d6e9ee4e8d2c9a664e819 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 31 Dec 2014 22:54:43 -0500 Subject: [PATCH] Improve api.getoptions to return nested groups/tags --- CRM/Activity/BAO/Query.php | 2 +- CRM/Contact/BAO/Contact.php | 3 ++- CRM/Contact/BAO/GroupContact.php | 24 ++++++++++++++++++++++++ CRM/Contact/Form/Search/Basic.php | 11 +++-------- CRM/Contact/Form/Search/Criteria.php | 4 ++-- CRM/Contribute/BAO/Query.php | 7 +++---- CRM/Core/BAO/EntityTag.php | 24 ++++++++++++++++++++++++ CRM/Core/Form.php | 15 ++++++++++----- 8 files changed, 69 insertions(+), 21 deletions(-) diff --git a/CRM/Activity/BAO/Query.php b/CRM/Activity/BAO/Query.php index 912924c6cb..e5ff26e68f 100644 --- a/CRM/Activity/BAO/Query.php +++ b/CRM/Activity/BAO/Query.php @@ -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' ); diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 900ab32e55..6b4b2d42c1 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -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; diff --git a/CRM/Contact/BAO/GroupContact.php b/CRM/Contact/BAO/GroupContact.php index ea23c9a386..505f92cf23 100644 --- a/CRM/Contact/BAO/GroupContact.php +++ b/CRM/Contact/BAO/GroupContact.php @@ -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; + } } diff --git a/CRM/Contact/Form/Search/Basic.php b/CRM/Contact/Form/Search/Basic.php index 8bb31812ec..28b5c5a562 100644 --- a/CRM/Contact/Form/Search/Basic.php +++ b/CRM/Contact/Form/Search/Basic.php @@ -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, '  ', 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 -'))); } } diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index 7bd9fb9aad..18ac653726 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -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'); diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index 17c14a3c76..5df96403ac 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -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', ) ); diff --git a/CRM/Core/BAO/EntityTag.php b/CRM/Core/BAO/EntityTag.php index 9cb2ec4f77..e6b2d4bebb 100644 --- a/CRM/Core/BAO/EntityTag.php +++ b/CRM/Core/BAO/EntityTag.php @@ -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; + } } diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 4da2b6414b..b9689f6b11 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -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); } -- 2.25.1