From 51f3527601d4ad14cdb676fc2b95a6debbaca691 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Frank=20J=2E=20G=C3=B3mez?= Date: Fri, 21 Dec 2018 22:36:47 -0500 Subject: [PATCH] Added support to generic settings form for sorting settings by weight. --- CRM/Admin/Form/Generic.php | 5 ++-- CRM/Admin/Form/SettingTrait.php | 47 ++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/CRM/Admin/Form/Generic.php b/CRM/Admin/Form/Generic.php index c61e223c2b..d0285bfe87 100644 --- a/CRM/Admin/Form/Generic.php +++ b/CRM/Admin/Form/Generic.php @@ -61,18 +61,19 @@ class CRM_Admin_Form_Generic extends CRM_Core_Form { $this->setDefaultsForMetadataDefinedFields(); return $this->_defaults; } + /** * Build the form object. */ public function buildQuickForm() { - $filter = array_pop($this->urlPath); + $filter = $this->getSettingPageFilter(); $settings = civicrm_api3('Setting', 'getfields', [])['values']; foreach ($settings as $key => $setting) { if (isset($setting['settings_pages'][$filter])) { $this->_settings[$key] = $setting; } } - // @todo sort settings by weight. + $this->addFieldsDefinedInSettingsMetadata(); // @todo look at sharing the code below in the settings trait. diff --git a/CRM/Admin/Form/SettingTrait.php b/CRM/Admin/Form/SettingTrait.php index a9f5e6ca47..580b2cf451 100644 --- a/CRM/Admin/Form/SettingTrait.php +++ b/CRM/Admin/Form/SettingTrait.php @@ -38,6 +38,13 @@ */ trait CRM_Admin_Form_SettingTrait { + /** + * The setting page filter. + * + * @var string + */ + private $_filter; + /** * @var array */ @@ -118,6 +125,44 @@ trait CRM_Admin_Form_SettingTrait { return CRM_Utils_Array::value($item, $this->getSettingsMetaData()[$setting]); } + /** + * @return string + */ + protected function getSettingPageFilter() { + if (!isset($this->_filter)) { + // Get the last URL component without modifying the urlPath property. + $urlPath = array_values($this->urlPath); + $this->_filter = end($urlPath); + } + return $this->_filter; + } + + /** + * Returns a re-keyed copy of the settings, ordered by weight. + * + * @return array + */ + protected function getSettingsOrderedByWeight() { + $settingMetaData = $this->getSettingsMetaData(); + $filter = $this->getSettingPageFilter(); + + usort($settingMetaData, function ($a, $b) use ($filter) { + // Handle cases in which a comparison is impossible. Such will be considered ties. + if ( + // A comparison can't be made unless both setting weights are declared. + !isset($a['settings_pages'][$filter]['weight'], $b['settings_pages'][$filter]['weight']) + // A pair of settings might actually have the same weight. + || $a['settings_pages'][$filter]['weight'] === $b['settings_pages'][$filter]['weight'] + ) { + return 0; + } + + return $a['settings_pages'][$filter]['weight'] > $b['settings_pages'][$filter]['weight'] ? 1 : -1; + }); + + return $settingMetaData; + } + /** * Add fields in the metadata to the template. */ @@ -207,7 +252,7 @@ trait CRM_Admin_Form_SettingTrait { // setting_description should be deprecated - see Mail.tpl for metadata based tpl. $this->assign('setting_descriptions', $descriptions); $this->assign('settings_fields', $settingMetaData); - $this->assign('fields', $settingMetaData); + $this->assign('fields', $this->getSettingsOrderedByWeight()); } /** -- 2.25.1