BxDolCategory.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 $_bIsApi;
  33. protected $_sObject;
  34. protected $_aObject;
  35. /**
  36. * Constructor
  37. * @param $aObject array of editor options
  38. */
  39. protected function __construct($aObject)
  40. {
  41. parent::__construct();
  42. $this->_bIsApi = bx_is_api();
  43. $this->_sObject = $aObject['object'];
  44. $this->_aObject = $aObject;
  45. }
  46. /**
  47. * Get category object instance by object name
  48. * @param $sObject object name
  49. * @return object instance or false on error
  50. */
  51. static public function getObjectInstance($sObject = false)
  52. {
  53. if (isset($GLOBALS['bxDolClasses']['BxDolCategory!'.$sObject]))
  54. return $GLOBALS['bxDolClasses']['BxDolCategory!'.$sObject];
  55. $aObject = BxDolCategoryQuery::getCategoryObject($sObject);
  56. if (!$aObject || !is_array($aObject))
  57. return false;
  58. $sClass = 'BxTemplCategory';
  59. if (!empty($aObject['override_class_name']))
  60. $sClass = $aObject['override_class_name'];
  61. if (!empty($aObject['override_class_file']))
  62. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  63. $o = new $sClass($aObject);
  64. return ($GLOBALS['bxDolClasses']['BxDolCategory!'.$sObject] = $o);
  65. }
  66. /**
  67. * Get category object instance by form object and list name
  68. * @param $sObjectForm form object name
  69. * @patam $sListName list name
  70. * @return object instance or false on error
  71. */
  72. static public function getObjectInstanceByFormAndList($sObjectForm, $sListName)
  73. {
  74. $aObject = BxDolCategoryQuery::getCategoryObjectByFormAndList($sObjectForm, $sListName);
  75. if(empty($aObject) || !is_array($aObject))
  76. return false;
  77. return self::getObjectInstance($aObject['object']);
  78. }
  79. /**
  80. * Get object name
  81. */
  82. public function getObjectName()
  83. {
  84. return $this->_sObject;
  85. }
  86. /**
  87. * Get search object associated with category object
  88. */
  89. public function getSearchObject()
  90. {
  91. return $this->_aObject['search_object'];
  92. }
  93. /**
  94. * Set condition for search results object for category object
  95. * @param $oSearchResult search results object
  96. * @param $mixedCategory category
  97. */
  98. public function setSearchCondition($oSearchResult, $mixedCategory)
  99. {
  100. $oSearchResult->aCurrent['restriction']['category_' . $this->_sObject] = array(
  101. 'value' => $mixedCategory,
  102. 'field' => $this->_aObject['field'],
  103. 'operator' => '=',
  104. 'table' => $this->_aObject['table'],
  105. );
  106. }
  107. /**
  108. * Get number of items in the specified category
  109. * @param $sCategoryValue category value
  110. * @return number
  111. */
  112. public function getItemsNum($sCategoryValue, $aParams = [])
  113. {
  114. return BxDolCategoryQuery::getItemsNumInCategory ($this->_aObject, $sCategoryValue, true, $aParams);
  115. }
  116. }
  117. /** @} */