Browse Source

#147 - category objects: categories list

Alex Trofimov 11 năm trước cách đây
mục cha
commit
2808a8cb72

+ 29 - 1
inc/classes/BxDolCategory.php

@@ -14,7 +14,25 @@
  */
 
 /**
- * Categories object, make to look form lists as categories by making it clickable and display search results with all contents with the same category
+ * Category object, it can make regular form lists to act as categories by making it clickable and display search results with all contents with the same category.
+ * Also it provides services to list all categories with number if items in each category.
+ *
+ * @section editor_create Creating the Category object:
+ *
+ *
+ * Add record to 'sys_objects_category' table:
+ *
+ * - object: name of the category object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; 
+ * - search_object: associated search object to display search results in 
+ * - form_object: form object which displays this category, so category will be transformed to the clickable url
+ * - list_name: form list name which values are used to populate categories from
+ * - table: table where category value is stores
+ * - field: table field name where category value is stored
+ * - join: custom SQL JOIN to use when getting number of items in the particular category (filter inactive items here)
+ * - where: custom SQL WHERE to use when getting number of items in the particular category (filter inactive items here)
+ * - override_class_name: user defined class name which is derived from 'Templ' class.
+ * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
+ *
  */
 class BxDolCategory extends BxDol implements iBxDolFactoryObject
 {
@@ -92,6 +110,16 @@ class BxDolCategory extends BxDol implements iBxDolFactoryObject
             'table' => $this->_aObject['table'],
         );
     }
+
+    /**
+     * Get number of items in the specified category
+     * @param $sCategoryValue category value
+     * @return number
+     */
+    public function getItemsNum($sCategoryValue)
+    {
+        return BxDolCategoryQuery::getItemsNumInCategory ($this->_aObject, $sCategoryValue);
+    }
 }
 
 /** @} */

+ 7 - 0
inc/classes/BxDolCategoryQuery.php

@@ -42,6 +42,13 @@ class BxDolCategoryQuery extends BxDolDb
 
         return $aObject;
     }
+
+    static public function getItemsNumInCategory ($aObject, $sCategoryValue)
+    {
+        $oDb = BxDolDb::getInstance();
+        $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `" . $aObject['table'] . "` " . $aObject['join'] . " WHERE `" . $aObject['field'] . "` = ? " . $aObject['where'], $sCategoryValue);
+        return $oDb->getOne($sQuery);
+    }
 }
 
 /** @} */

+ 2 - 0
install/sql/system.sql

@@ -3292,6 +3292,8 @@ CREATE TABLE `sys_objects_category` (
   `list_name` varchar(255) NOT NULL,
   `table` varchar(255) NOT NULL,
   `field` varchar(255) NOT NULL,
+  `join` varchar(255) NOT NULL,
+  `where` varchar(255) NOT NULL,
   `override_class_name` varchar(255) NOT NULL,
   `override_class_file` varchar(255) NOT NULL,
   PRIMARY KEY (`id`),

+ 17 - 0
modules/boonex/lagoon/data/template/system/scripts/BxTemplServiceCategory.php

@@ -0,0 +1,17 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    TridentCore Trident Core
+ * @{
+ */
+
+/**
+ * @see BxBaseServiceCategory
+ */
+class BxTemplServiceCategory extends BxBaseServiceCategory
+{
+}
+
+/** @} */

+ 17 - 0
modules/boonex/ocean/data/template/system/scripts/BxTemplServiceCategory.php

@@ -0,0 +1,17 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    TridentCore Trident Core
+ * @{
+ */
+
+/**
+ * @see BxBaseServiceCategory
+ */
+class BxTemplServiceCategory extends BxBaseServiceCategory
+{
+}
+
+/** @} */

+ 1 - 0
modules/boonex/organizations/install/langs/en.xml

@@ -78,6 +78,7 @@
 	<string name="_bx_orgs_page_block_title_system_manage"><![CDATA[Manage Own Organizations]]></string>
 	<string name="_bx_orgs_page_block_title_system_manage_administration"><![CDATA[Manage All Organizations]]></string>
 	<string name="_bx_orgs_page_block_title_manage"><![CDATA[Manage Organizations]]></string>
+    <string name="_bx_orgs_page_block_title_categories"><![CDATA[Organization categories]]></string>
 
     <string name="_bx_orgs_menu_title_submenu"><![CDATA[Organizations Submenu]]></string>
     <string name="_bx_orgs_menu_set_title_submenu"><![CDATA[Organizations Submenu]]></string>

+ 8 - 2
modules/boonex/organizations/install/sql/enable.sql

@@ -104,6 +104,12 @@ SET @iBlockOrder = (SELECT `order` FROM `sys_pages_blocks` WHERE `object` = 'sys
 INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `order`) VALUES
 ('sys_home', 1, 'bx_organizations', '_bx_orgs_page_block_title_latest_profiles', 11, 2147483647, 'service', 'a:2:{s:6:"module";s:16:"bx_organizations";s:6:"method";s:22:"browse_recent_profiles";}', 1, 0, IFNULL(@iBlockOrder, 0) + 1);
 
+-- PAGE: service blocks
+SET @iBlockOrder = (SELECT `order` FROM `sys_pages_blocks` WHERE `object` = '' AND `cell_id` = 0 ORDER BY `order` DESC LIMIT 1);
+INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title_system`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `active`, `order`) VALUES
+('', 0, 'bx_organizations', '', '_bx_orgs_page_block_title_categories', 11, 2147483647, 'service', 'a:4:{s:6:"module";s:6:"system";s:6:"method";s:15:"categories_list";s:6:"params";a:2:{i:0;s:21:"bx_organizations_cats";i:1;b:1;}s:5:"class";s:20:"TemplServiceCategory";}', 1, 1, 1, IFNULL(@iBlockOrder, 0) + 1);
+
+
 -- MENU
 
 -- MENU: add to site menu
@@ -275,8 +281,8 @@ INSERT INTO `sys_objects_metatags` (`object`, `table_keywords`, `table_locations
 ('bx_organizations', 'bx_organizations_meta_keywords', '', '', '', '');
 
 -- CATEGORY
-INSERT INTO `sys_objects_category` (`object`, `search_object`, `form_object`, `list_name`, `table`, `field`, `override_class_name`, `override_class_file`) VALUES
-('bx_organizations_cats', 'bx_organizations', 'bx_organization', 'bx_organizations_cats', 'bx_organizations_data', 'org_cat', '', '');
+INSERT INTO `sys_objects_category` (`object`, `search_object`, `form_object`, `list_name`, `table`, `field`, `join`, `where`, `override_class_name`, `override_class_file`) VALUES
+('bx_organizations_cats', 'bx_organizations', 'bx_organization', 'bx_organizations_cats', 'bx_organizations_data', 'org_cat', 'INNER JOIN `sys_profiles` ON (`sys_profiles`.`content_id` = `bx_organizations_data`.`id` AND `sys_profiles`.`type` = ''bx_organizations'')', 'AND `sys_profiles`.`status` = ''active''', '', '');
 
 -- SEARCH
 SET @iSearchOrder = (SELECT IFNULL(MAX(`Order`), 0) FROM `sys_objects_search`);

+ 2 - 1
modules/boonex/russian/data/langs/orgs.xml

@@ -63,8 +63,9 @@
 	<string name="_bx_orgs_page_title_sys_home"><![CDATA[Организации - Домой]]></string>
 	<string name="_bx_orgs_page_title_home"><![CDATA[Организации]]></string>
 	<string name="_bx_orgs_page_title_sys_manage"><![CDATA[Управление собственными организациями]]></string>
-		<string name="_bx_orgs_page_title_sys_manage_administration"><![CDATA[Управление всеми организациями]]></string>
+	<string name="_bx_orgs_page_title_sys_manage_administration"><![CDATA[Управление всеми организациями]]></string>
 	<string name="_bx_orgs_page_title_manage"><![CDATA[Управление организациями]]></string>
+    <string name="_bx_orgs_page_block_title_categories"><![CDATA[Категории организаций]]></string>
 
 	<string name="_bx_orgs_page_block_title_create_profile"><![CDATA[Создать профиль организации]]></string>
 	<string name="_bx_orgs_page_block_title_edit_profile"><![CDATA[Редактировать профиль]]></string>

+ 5 - 0
template/category_list.html

@@ -0,0 +1,5 @@
+<div class="bx-category-list">
+    <bx_repeat:cats>
+        <div class="bx-category-list-item"><a href="__url__">__name__</a> (__num__)</div>
+    </bx_repeat:cats>
+</div>

+ 31 - 7
template/scripts/BxBaseCategory.php

@@ -25,9 +25,9 @@ class BxBaseCategory extends BxDolCategory
             $this->_oTemplate = BxDolTemplate::getInstance();
     }
 
-    public function getCategoryUrl($sName, $sValue, $sCategoryObject)
+    public function getCategoryUrl($sName, $sValue)
     {
-        $sUrl = BX_DOL_URL_ROOT . 'searchKeyword.php?cat=' . rawurlencode($sCategoryObject) . '&keyword=' . rawurlencode($sValue);
+        $sUrl = BX_DOL_URL_ROOT . 'searchKeyword.php?cat=' . rawurlencode($this->getObjectName()) . '&keyword=' . rawurlencode($sValue);
         if ($this->_aObject['search_object'])
             $sUrl .= '&section[]=' . rawurlencode($this->_aObject['search_object']);
         return $sUrl;
@@ -35,19 +35,43 @@ class BxBaseCategory extends BxDolCategory
 
     /**
      * Get link to list all items with the same category
+     * @param $sName category title
+     * @param $sValue category value
+     * @return category name wrapped with A tag
      */
-    public function getCategoryLink($sName, $sValue, $sCategoryObject)
+    public function getCategoryLink($sName, $sValue)
     {
-        $sUrl = $this->getCategoryUrl($sName, $sValue, $sCategoryObject);
-        return '<a href="' . $sUrl . '">' . $sName . '</a>';
+        $sUrl = $this->getCategoryUrl($sName, $sValue);
+        return '<a class="bx-category-link" href="' . $sUrl . '">' . $sName . '</a>';
     }
 
     /**
      * Get all categories list
+     * @param $bDisplayEmptyCats display categories with no items, true by default
+     * @return categories list html
      */
-    public function getCategoriesList()
+    public function getCategoriesList($bDisplayEmptyCats = true)
     {
-        // TODO:
+        $a = BxDolForm::getDataItems($this->_aObject['list_name']);
+        if (!$a)
+            return '';
+
+        $aVars = array('bx_repeat:cats' => array());
+        foreach ($a as $sValue => $sName) {
+            if (!$sValue)
+                continue;
+            $iNum = $this->getItemsNum($sValue);
+            if (!$bDisplayEmptyCats && !$iNum)
+                continue;
+            $aVars['bx_repeat:cats'][] = array(
+                'url' => $this->getCategoryUrl($sName, $sValue),
+                'name' => $sName,
+                'value' => $sValue,
+                'num' => $iNum,
+            );
+        }
+
+        return $this->_oTemplate->parseHtmlByName('category_list.html', $aVars);
     }
 }
 

+ 1 - 1
template/scripts/BxBaseFormView.php

@@ -353,7 +353,7 @@ EOS;
     {
         $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
         if (isset($aInput['values_list_name'])  && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])))
-            return $oCategory->getCategoryLink($s, $aInput['value'], $oCategory->getObjectName());
+            return $oCategory->getCategoryLink($s, $aInput['value']);
         return $s;
     }
 

+ 24 - 0
template/scripts/BxBaseServiceCategory.php

@@ -0,0 +1,24 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    TridentCore Trident Core
+ * @{
+ */
+
+/**
+ * Services for category objects functionality - @see BxDolCategory
+ */
+class BxBaseServiceCategory extends BxDol
+{
+    /**
+     * Get categories list
+     */
+    public function serviceCategoriesList ($sObject, $bDisplayEmptyCats = true)
+    {
+        return BxTemplCategory::getObjectInstance($sObject)->getCategoriesList($bDisplayEmptyCats);
+    }
+}
+
+/** @} */