Просмотр исходного кода

Ticket #5115 - Spaces: Implement multi categories.Core changes (icons).

AntonLV 1 год назад
Родитель
Сommit
e160387801

+ 52 - 6
inc/classes/BxDolCategories.php

@@ -9,13 +9,28 @@
 
 class BxDolCategories extends BxDolFactory implements iBxDolSingleton
 {
+    protected $_bIsApi;
+
     protected $_oDb;
 
+    protected $_sModule;
+    protected $_oModule;
+
+    protected $_sBrowseUrl;
+
     protected function __construct()
     {
         parent::__construct();
 
+        $this->_bIsApi = bx_is_api();
+
         $this->_oDb = new BxDolCategoriesQuery();
+
+        $this->_sBrowseUrl = bx_append_url_params('searchKeyword.php', [
+            'keyword' => '{keyword}',
+            'cat' => 'multi',
+            'section' => '{section}'
+        ], true, ['{keyword}', '{section}']) . '{add}';
     }
 
     public function __clone()
@@ -26,10 +41,32 @@ class BxDolCategories extends BxDolFactory implements iBxDolSingleton
 
     public static function getInstance()
     {
-        if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
-            $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolCategories();
+        return self::getInstanceWithTemplate(null);
+    }
+
+    public static function getInstanceWithTemplate($oTemplate)
+    {
+        $sClassName = __CLASS__;
+        if($oTemplate)
+            $sClassName .= get_class($oTemplate);
+
+        if(!isset($GLOBALS['bxDolClasses'][$sClassName]))
+            $GLOBALS['bxDolClasses'][$sClassName] = new BxTemplCategories($oTemplate);
+
+        return $GLOBALS['bxDolClasses'][$sClassName];
+    }
+
+    public function setModule($sModule)
+    {
+        if(!($this->_sModule = $sModule) || !($this->_oModule = BxDolModule::getInstance($sModule))) 
+            return;
+
+        $CNF = &$this->_oModule->_oConfig->CNF;
 
-        return $GLOBALS['bxDolClasses'][__CLASS__];
+        if(($sKey = 'URL_MULTI_CATEGORY') && !empty($CNF[$sKey]))
+            $this->_sBrowseUrl = bx_append_url_params(BxDolPermalinks::getInstance()->permalink($CNF[$sKey]), [
+                'category' => '{keyword}'
+            ], true, ['{keyword}']);
     }
 
     public function getData($aParams = array())
@@ -46,10 +83,19 @@ class BxDolCategories extends BxDolFactory implements iBxDolSingleton
     {
         return $this->_oDb->add($sModule, $iProfileId, $sValue, $iObject, $bAutoActivation);
     }
-    
-    public function getUrl ($sModule, $sValue, $sAddParams = '')
+
+    public function getUrl($sModule, $sValue, $sAddParams = '')
     {
-        return  BX_DOL_URL_ROOT . 'searchKeyword.php?keyword=' . rawurlencode($sValue) . '&cat=multi&section=' . $sModule . $sAddParams;
+        $s = BX_DOL_URL_ROOT . bx_replace_markers($this->_sBrowseUrl, [
+            'keyword' => rawurlencode($sValue),
+            'section' => $sModule,
+            'add' => $sAddParams
+        ]);
+
+        if($this->_bIsApi)
+            return bx_api_get_relative_url($s);
+
+        return $s;
     }
 }
 

+ 2 - 2
inc/classes/BxDolCategoriesQuery.php

@@ -69,7 +69,7 @@ class BxDolCategoriesQuery extends BxDolDb
                 $oModule = BxDolModule::getInstance($aParams['module']);
                 $CNF = $oModule->_oConfig->CNF;
                 
-                $sSelectClause = "`sc`.`value`, COUNT(`sc`.`id`) as `num`";
+                $sSelectClause = "`sc`.`value`, `sc`.`icon_type`, `sc`.`icon`, COUNT(`sc`.`id`) as `num`";
                 $sJoinClause = "INNER JOIN `sys_categories2objects` `soc` ON `sc`.`id` =  `soc`.`category_id`";
                 if (isset($CNF['FIELD_STATUS']))
                     $sJoinClause .= "INNER JOIN `" . $CNF['TABLE_ENTRIES'] . "` `data` ON `soc`.`object_id` =  `data`.`id` AND `data`.`" . $CNF['FIELD_STATUS'] . "` = 'active'";
@@ -88,7 +88,7 @@ class BxDolCategoriesQuery extends BxDolDb
                 $oModule = BxDolModule::getInstance($aParams['module']);
                 $CNF = $oModule->_oConfig->CNF;
                 
-                $sSelectClause = "`sc`.`value`, COUNT(`sc`.`id`) as `num`";
+                $sSelectClause = "`sc`.`value`, `sc`.`icon_type`, `sc`.`icon`, COUNT(`sc`.`id`) as `num`";
                 $sJoinClause = " INNER JOIN `sys_categories2objects` `soc` ON `sc`.`id` =  `soc`.`category_id`";
                 $sJoinClause .= " INNER JOIN `" . $CNF['TABLE_ENTRIES'] . "` `data` ON `soc`.`object_id` =  `data`.`id` AND `data`.`" . $CNF['FIELD_ALLOW_VIEW_TO'] . "` = :context_id";
                 if (isset($CNF['FIELD_STATUS']))

+ 2 - 0
install/sql/system.sql

@@ -6355,6 +6355,8 @@ CREATE TABLE IF NOT EXISTS `sys_categories` (
   `added` int(11) NOT NULL,
   `module` varchar(32) NOT NULL,
   `value` varchar(100) NOT NULL,
+  `icon_type` varchar(8) NOT NULL DEFAULT '',
+  `icon` text NOT NULL,
   `status` enum ('active', 'hidden') NOT NULL DEFAULT 'active',
   PRIMARY KEY (`id`)
 );

+ 35 - 36
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -1526,6 +1526,36 @@ class BxBaseModGeneralModule extends BxDolModule
         return $this->_serviceBrowse($sType, $aParams, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
     }
 
+    /**
+     * @page service Service Calls
+     * @section bx_base_general Base General
+     * @subsection bx_base_general-browsing Browsing
+     * @subsubsection bx_base_general-browse_multi_category category
+     * 
+     * @code bx_srv('bx_posts', 'browse_multi_category', [...]); @endcode
+     * 
+     * Display entries in multi category
+     * @param $sUnitView browsing unity view, 
+     *                   such as: full, extended, gallery, showcase
+     * @param $bEmptyMessage display or not "empty" message when there is no content
+     * @param $bAjaxPaginate use AJAX paginate or not
+     * 
+     * @see BxBaseModGeneralModule::serviceBrowseMultiCategory BxBaseModGeneralModule::serviceBrowse
+     */
+    /** 
+     * @ref bx_base_general-browse_multi_category "browse_multi_category"
+     */
+    public function serviceBrowseMultiCategory($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true, $aParams = [])
+    {
+        $sType = 'multi_category';
+
+        $aParams = array_merge(['category' => bx_process_input(bx_get('category'))], $aParams);
+        if($sUnitView)
+            $aParams['unit_view'] = $sUnitView;
+
+        return $this->_serviceBrowse($sType, $aParams, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
+    }
+
     /**
      * @page service Service Calls
      * @section bx_base_general Base General
@@ -3311,51 +3341,20 @@ class BxBaseModGeneralModule extends BxDolModule
      * @code bx_srv('bx_posts', 'categories_multi_list', [...]); @endcode
      * 
      * Display multi-categorories block with number of posts in each category
-     * @param $bDisplayEmptyCats display empty categories
+     * @param $bAsArray boolean reature as array or not.
      * 
      * @see BxBaseModGeneralModule::serviceCategoriesMultiList
      */
     /** 
      * @ref bx_base_general-categories_multi_list "categories_multi_list"
      */
-    public function serviceCategoriesMultiList($bDisplayEmptyCats = true)
+    public function serviceCategoriesMultiList($bAsArray = false)
     {
-        $aContextInfo = bx_get_page_info();
-
-        $mProfileContextId = false;
-        if ($aContextInfo !== false)
-            $mProfileContextId = $aContextInfo['context_profile_id'];
-        
         $oCategories = BxDolCategories::getInstance();
-        if ($mProfileContextId)
-            $aCats = $oCategories->getData([
-                'type' => 'by_module&context_with_num', 
-                'module' => $this->getName(), 
-                'context_id' => $mProfileContextId
-            ]);
-        else{
-            $aCats = $oCategories->getData([
-                'type' => 'by_module_with_num', 
-                'module' => $this->getName()
-            ]);
-        }
-        $aVars = array('bx_repeat:cats' => array());
-        foreach ($aCats as $oCat) {
-            $sValue = $oCat['value'];
-            $iNum = $oCat['num'];
-            
-            $aVars['bx_repeat:cats'][] = array(
-                'url' => $oCategories->getUrl($this->getName(), $sValue, $mProfileContextId ? '&context_id=' . $mProfileContextId : ''),
-                'name' => _t($sValue),
-                'value' => $sValue,
-                'num' => $iNum,
-            );
-        }
-        
-        if (!$aVars['bx_repeat:cats'])
-            return '';
+        $oCategories->setModule($this->getName());
+        $mixedResult = $oCategories->getCategoriesList($bAsArray || $this->_bIsApi);
 
-        return $this->_oTemplate->parseHtmlByName('category_list_multi.html', $aVars);
+        return !$this->_bIsApi ? $mixedResult : [bx_api_get_block('categories_list',  $mixedResult)];
     }
 
     /**

+ 14 - 6
modules/base/general/classes/BxBaseModGeneralPageBrowse.php

@@ -33,13 +33,21 @@ class BxBaseModGeneralPageBrowse extends BxTemplPage
                 $oMenuSubmenu->setObjectSubmenu($this->_oModule->_oConfig->CNF['OBJECT_MENU_SUBMENU']);
         }
 
-        if(!empty($CNF['OBJECT_CATEGORY']) && ($iCategory = bx_get('category')) !== false) {
-            $iCategory = bx_process_input($iCategory, BX_DATA_INT);
+        if(($mixedCategory = bx_get('category')) !== false) {
+            $aMarkers = [];
 
-            $this->addMarkers([
-                'category_id' => $iCategory,
-                'category_name' => BxDolCategory::getObjectInstance($CNF['OBJECT_CATEGORY'])->getCategoryTitle($iCategory),
-            ]);
+            if(!empty($CNF['OBJECT_CATEGORY']) && is_numeric($mixedCategory)) {
+                $mixedCategory = bx_process_input($mixedCategory, BX_DATA_INT);
+
+                $aMarkers = [
+                    'category_id' => $mixedCategory,
+                    'category_name' => BxDolCategory::getObjectInstance($CNF['OBJECT_CATEGORY'])->getCategoryTitle($mixedCategory),
+                ];
+            }
+            else
+                $aMarkers['category_name'] = _t($mixedCategory);
+
+            $this->addMarkers($aMarkers);
         }
     }
 

+ 32 - 1
studio/template/scripts/BxBaseStudioFormsCategories.php

@@ -194,8 +194,10 @@ class BxBaseStudioFormsCategories extends BxDolStudioFormsCategories
         return parent::_getCellDefault(_t($mixedValue), $sKey, $aField, $aRow);
     }
     
-    protected function _getForm($sAction, $aCategory = array())
+    protected function _getForm($sAction, $aCategory = [])
     {
+        $sIconType = !empty($aCategory['icon_type']) ? $aCategory['icon_type'] : '';
+
         $aForm = array(
             'form_attrs' => array(
                 'id' => 'adm-form-cats-form-' . $sAction,
@@ -237,6 +239,35 @@ class BxBaseStudioFormsCategories extends BxDolStudioFormsCategories
                         'error' => _t('_adm_form_err_categories_value'),
                     ),
                 ),
+                'icon_type' => [
+                    'type' => 'select',
+                    'name' => 'icon_type',
+                    'caption' => _t('_adm_form_txt_pre_values_icon_type'),
+                    'info' => '',
+                    'values' => [
+                        ['key' => '', 'value' => _t('_sys_please_select')],
+                        ['key' => 'icon', 'value' => _t('_adm_form_txt_pre_values_icon_type_icon')],
+                        ['key' => 'emoji', 'value' => _t('_adm_form_txt_pre_values_icon_type_emoji')],
+                        ['key' => 'image', 'value' => _t('_adm_form_txt_pre_values_icon_type_image')]
+                    ],
+                    'value' => isset($aCategory['icon_type']) ? $aCategory['icon_type'] : '',
+                    'required' => '0',
+                    'db' => [
+                        'pass' => 'Xss',
+                    ]
+                ],
+                'icon' => [
+                    'type' => 'textarea',
+                    'name' => 'icon',
+                    'caption' => _t('_adm_form_txt_pre_values_icon'),
+                    'info' => '',
+                    'value' => isset($aCategory['icon']) ? $aCategory['icon'] : '',
+                    'required' => '0',
+                    'code' => 1,
+                    'db' => [
+                        'pass' => 'Xss',
+                    ]
+                ],
                 'controls' => array(
                     'name' => 'controls',
                     'type' => 'input_set',

+ 6 - 6
template/category_list.html

@@ -1,17 +1,17 @@
-<div class="bx-category-list">
+<div class="bx-category-list space-y-1">
     <bx_if:show_all>
         <div class="bx-category-list-item"><a href="__url__">__name__</a></div>
     </bx_if:show_all>
     <bx_repeat:cats>
-        <div class="bx-category-list-item __selected_class__">
-            <a class="flex items-center" href="__url__">
+        <div class="bx-category-list-item __selected_class__ bx-stl-mil-wo-gaps">
+            <a class="bx-stl-mil-gaps flex items-center" href="__url__">
                 <bx_if:show_icon>
-                    <span class="bx-cli-icon">
+                    <span class="bx-cli-icon bx-stl-mii">
                         <bx_if:show_icon_font><i class="sys-icon __icon_name__"></i></bx_if:show_icon_font>
-                        <bx_if:show_icon_html>__icon_code__</bx_if:show_icon_html>
+                        <bx_if:show_icon_html><i class="sys-icon sys-icon-emoji">__icon_code__</i></bx_if:show_icon_html>
                     </span>
                 </bx_if:show_icon>
-                <span class="bx-cli-text">__name__ (__num__)</span>
+                <span class="bx-cli-text bx-stl-mit">__name__ (__num__)</span>
             </a>
         </div>
     </bx_repeat:cats>

+ 12 - 2
template/category_list_multi.html

@@ -1,5 +1,15 @@
-<div class="bx-category-list bx-cl-multi">
+<div class="bx-category-list bx-cl-multi space-y-1">
     <bx_repeat:cats>
-        <div class="bx-category-list-item"><a href="__url__">__name__</a> (__num__)</div>
+        <div class="bx-category-list-item __selected_class__ bx-stl-mil-wo-gaps">
+            <a class="bx-stl-mil-gaps flex items-center" href="__url__">
+                <bx_if:show_icon>
+                    <span class="bx-cli-icon bx-stl-mii">
+                        <bx_if:show_icon_font><i class="sys-icon __icon_name__"></i></bx_if:show_icon_font>
+                        <bx_if:show_icon_html><i class="sys-icon sys-icon-emoji">__icon_code__</i></bx_if:show_icon_html>
+                    </span>
+                </bx_if:show_icon>
+                <span class="bx-cli-text bx-stl-mit">__name__ (__num__)</span>
+            </a>
+        </div>
     </bx_repeat:cats>
 </div>

+ 0 - 9
template/css/tailwind.css

@@ -1253,15 +1253,6 @@
         @apply hover:bg-gray-100 dark:hover:bg-gray-600;
     }
 
-    /* Common elements: Categories */
-    .bx-cli-icon,
-    .bx-cli-icon > * {
-        @apply flex items-center justify-center h-6 w-6 text-gray-500 text-lg;
-    }
-    .bx-cli-icon {
-        @apply ltr:mr-3 rtl:ml-3;
-    }
-
     /* Common elements: Comments */
     .bx-artificer .cmt .bx-base-general-unit-meta-username {
         @apply text-gray-700 dark:text-gray-200 hover:text-gray-900 dark:hover:text-white;

+ 93 - 0
template/scripts/BxBaseCategories.php

@@ -0,0 +1,93 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaBaseView UNA Base Representation Classes
+ * @{
+ */
+
+/**
+ * Multi Categories object representation.
+ * @see BxDolCategories
+ */
+class BxBaseCategories extends BxDolCategories
+{
+    protected $_oTemplate;
+
+    public function __construct($oTemplate = null)
+    {
+        parent::__construct();
+
+        if($oTemplate)
+            $this->_oTemplate = $oTemplate;
+        else
+            $this->_oTemplate = BxDolTemplate::getInstance();
+    }
+
+    public function getCategoriesList($bAsArray = false)
+    {
+        $aContextInfo = bx_get_page_info();
+
+        $mProfileContextId = false;
+        if($aContextInfo !== false)
+            $mProfileContextId = $aContextInfo['context_profile_id'];
+
+        if($mProfileContextId)
+            $aCats = $this->getData([
+                'type' => 'by_module&context_with_num', 
+                'module' => $this->_sModule, 
+                'context_id' => $mProfileContextId
+            ]);
+        else
+            $aCats = $this->getData([
+                'type' => 'by_module_with_num', 
+                'module' => $this->_sModule
+            ]);
+
+        $aTmplVarsCats = [];
+        foreach($aCats as $aCat) {
+            $bIcon = !empty($aCat['icon']);
+
+            $sValue = $aCat['value'];
+
+            $aTmplVarsCats[] = array_merge([
+                'url' => $this->getUrl($this->_sModule, $sValue, $mProfileContextId ? '&context_id=' . $mProfileContextId : ''),
+                'name' => _t($sValue),
+                'value' => $sValue,
+                'num' => (int)$aCat['num'],
+            ], !$this->_bIsApi ? [
+                'bx_if:show_icon' => [
+                    'condition' => $bIcon,
+                    'content' => [
+                        'bx_if:show_icon_font' => [
+                            'condition' => $bIcon && $aCat['icon_type'] == 'icon',
+                            'content' => [
+                                'icon_name' => $aCat['icon'],
+                            ]
+                        ],
+                        'bx_if:show_icon_html' => [
+                            'condition' => $bIcon && in_array($aCat['icon_type'], ['emoji', 'image']),
+                            'content' => [
+                                'icon_code' => $aCat['icon'],
+                            ]
+                        ],
+                    ]
+                ],
+                'selected_class' => $sValue == bx_get('category') ? 'bx-menu-tab-active' : '',
+            ] : [
+                'icon_type' => $aCat['icon_type'],
+                'icon' => $aCat['icon']
+            ]);
+        }
+
+        if($bAsArray)
+            return $aTmplVarsCats;
+
+        return !empty($aTmplVarsCats) ? $this->_oTemplate->parseHtmlByName('category_list_multi.html', [
+            'bx_repeat:cats' => $aTmplVarsCats
+        ]) : '';
+    }
+}
+
+/** @} */

+ 1 - 1
template/scripts/BxBaseCategory.php

@@ -179,7 +179,7 @@ class BxBaseCategory extends BxDolCategory
                         ],
                     ]
                 ],
-                'selected_class' => $sValue == bx_get('category') ? 'bx-category-list-item-selected' : '',
+                'selected_class' => $sValue == bx_get('category') ? 'bx-menu-tab-active' : '',
             ] : [
                 'icon_type' => $sIconType,
                 'icon' => $sIcon

+ 18 - 0
template/scripts_templ/BxTemplCategories.php

@@ -0,0 +1,18 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolCategories
+ */
+class BxTemplCategories extends BxBaseCategories
+{
+    
+}
+
+/** @} */