BxDolCategory.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * 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.
  11. * Also it provides services to list all categories with number if items in each category.
  12. *
  13. * @section editor_create Creating the Category object:
  14. *
  15. *
  16. * Add record to 'sys_objects_category' table:
  17. *
  18. * - object: name of the category object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing;
  19. * - search_object: associated search object to display search results in
  20. * - form_object: form object which displays this category, so category will be transformed to the clickable url
  21. * - list_name: form list name which values are used to populate categories from
  22. * - table: table where category value is stores
  23. * - field: table field name where category value is stored
  24. * - join: custom SQL JOIN to use when getting number of items in the particular category (filter inactive items here)
  25. * - where: custom SQL WHERE to use when getting number of items in the particular category (filter inactive items here)
  26. * - override_class_name: user defined class name which is derived from 'Templ' class.
  27. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  28. *
  29. */
  30. class BxDolCategory extends BxDolFactory implements iBxDolFactoryObject
  31. {
  32. protected $_sObject;
  33. protected $_aObject;
  34. /**
  35. * Constructor
  36. * @param $aObject array of editor options
  37. */
  38. protected function __construct($aObject)
  39. {
  40. parent::__construct();
  41. $this->_sObject = $aObject['object'];
  42. $this->_aObject = $aObject;
  43. }
  44. /**
  45. * Get category object instance by object name
  46. * @param $sObject object name
  47. * @return object instance or false on error
  48. */
  49. static public function getObjectInstance($sObject = false)
  50. {
  51. if (isset($GLOBALS['bxDolClasses']['BxDolCategory!'.$sObject]))
  52. return $GLOBALS['bxDolClasses']['BxDolCategory!'.$sObject];
  53. $aObject = BxDolCategoryQuery::getCategoryObject($sObject);
  54. if (!$aObject || !is_array($aObject))
  55. return false;
  56. $sClass = 'BxTemplCategory';
  57. if (!empty($aObject['override_class_name']))
  58. $sClass = $aObject['override_class_name'];
  59. if (!empty($aObject['override_class_file']))
  60. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  61. $o = new $sClass($aObject);
  62. return ($GLOBALS['bxDolClasses']['BxDolCategory!'.$sObject] = $o);
  63. }
  64. /**
  65. * Get category object instance by form object and list name
  66. * @param $sObjectForm form object name
  67. * @patam $sListName list name
  68. * @return object instance or false on error
  69. */
  70. static public function getObjectInstanceByFormAndList($sObjectForm, $sListName)
  71. {
  72. $aObject = BxDolCategoryQuery::getCategoryObjectByFormAndList($sObjectForm, $sListName);
  73. if(empty($aObject) || !is_array($aObject))
  74. return false;
  75. return self::getObjectInstance($aObject['object']);
  76. }
  77. /**
  78. * Get object name
  79. */
  80. public function getObjectName()
  81. {
  82. return $this->_sObject;
  83. }
  84. /**
  85. * Set condition for search results object for category object
  86. * @param $oSearchResult search results object
  87. * @param $mixedCategory category
  88. */
  89. public function setSearchCondition($oSearchResult, $mixedCategory)
  90. {
  91. $oSearchResult->aCurrent['restriction']['category_' . $this->_sObject] = array(
  92. 'value' => $mixedCategory,
  93. 'field' => $this->_aObject['field'],
  94. 'operator' => '=',
  95. 'table' => $this->_aObject['table'],
  96. );
  97. }
  98. /**
  99. * Get number of items in the specified category
  100. * @param $sCategoryValue category value
  101. * @return number
  102. */
  103. public function getItemsNum($sCategoryValue)
  104. {
  105. return BxDolCategoryQuery::getItemsNumInCategory ($this->_aObject, $sCategoryValue);
  106. }
  107. }
  108. /** @} */