BxDolRecommendationQuery.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. class BxDolRecommendationQuery extends BxDolDb
  10. {
  11. public static $sTableObjects = 'sys_objects_recommendation';
  12. public static $sTableCriteria = 'sys_recommendation_criteria';
  13. public static $sTableData = 'sys_recommendation_data';
  14. protected $_aObject;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->_aObject = [];
  19. }
  20. static public function getObjects($bActiveOnly = true)
  21. {
  22. $sWhereClause = "";
  23. if($bActiveOnly)
  24. $sWhereClause = " AND `active` = 1";
  25. return BxDolDb::getInstance()->getAllWithKey("SELECT * FROM `" . self::$sTableObjects . "` WHERE 1" . $sWhereClause, 'name');
  26. }
  27. static public function getObject($sName)
  28. {
  29. $oDb = BxDolDb::getInstance();
  30. $aObject = $oDb->getRow("SELECT * FROM `" . self::$sTableObjects . "` WHERE `name` = :name", ['name' => $sName]);
  31. if(!$aObject || !is_array($aObject))
  32. return false;
  33. $aCriteria = $oDb->getAllWithKey("SELECT * FROM `" . self::$sTableCriteria . "` WHERE `object_id` = :object_id AND `weight` > 0 AND `active` = 1 ORDER BY `weight` DESC", 'name', ['object_id' => $aObject['id']]);
  34. if(!$aCriteria || !is_array($aCriteria))
  35. return false;
  36. $aObject['weights'] = $oDb->getPairs("SELECT `name`, `weight` FROM `" . self::$sTableCriteria . "` WHERE `object_id` = :object_id AND `weight` > 0 AND `active` = 1 ORDER BY `weight` DESC", 'name', 'weight', ['object_id' => $aObject['id']]);
  37. return [
  38. 'object' => $aObject,
  39. 'criteria' => $aCriteria
  40. ];
  41. }
  42. public function init($aObject = [])
  43. {
  44. if(empty($aObject) || !is_array($aObject))
  45. return;
  46. $this->_aObject = $aObject;
  47. }
  48. public function clean($iProfileId, $iObjectId, $bAll = false)
  49. {
  50. $sWhereClause = "";
  51. if(!$bAll)
  52. $sWhereClause = " AND `item_reducer` = 0";
  53. return $this->query("DELETE FROM `" . self::$sTableData . "` WHERE `profile_id` = :profile_id AND `object_id` = :object_id" . $sWhereClause, [
  54. 'profile_id' => $iProfileId,
  55. 'object_id' => $iObjectId
  56. ]);
  57. }
  58. public function add($iProfileId, $iObjectId, $iItemId, $sItemType, $iItemValue)
  59. {
  60. return $this->query("INSERT INTO `" . self::$sTableData . "` (`profile_id`, `object_id`, `item_id`, `item_type`, `item_value`) VALUES (:profile_id, :object_id, :item_id, :item_type, :item_value) ON DUPLICATE KEY UPDATE `item_value` = :item_value", [
  61. 'profile_id' => $iProfileId,
  62. 'object_id' => $iObjectId,
  63. 'item_id' => $iItemId,
  64. 'item_type' => $sItemType,
  65. 'item_value' => $iItemValue
  66. ]) !== false;
  67. }
  68. public function update($iProfileId, $iObjectId, $iItemId, $aSet)
  69. {
  70. if(empty($aSet) || !is_array($aSet))
  71. return false;
  72. return $this->query("UPDATE `" . self::$sTableData . "` SET " . $this->arrayToSQL($aSet) . " WHERE `profile_id` = :profile_id AND `object_id` = :object_id AND `item_id` = :item_id ", [
  73. 'profile_id' => $iProfileId,
  74. 'object_id' => $iObjectId,
  75. 'item_id' => $iItemId
  76. ]) !== false;
  77. }
  78. public function delete($iProfileId, $iObjectId, $iItemId)
  79. {
  80. return $this->query("DELETE FROM `" . self::$sTableData . "` WHERE `profile_id` = :profile_id AND `object_id` = :object_id AND `item_id` = :item_id ", [
  81. 'profile_id' => $iProfileId,
  82. 'object_id' => $iObjectId,
  83. 'item_id' => $iItemId
  84. ]) !== false;
  85. }
  86. public function get($iProfileId, $iObjectId, $aParams = [])
  87. {
  88. $sWhereClause = "";
  89. if(!empty($aParams['type']))
  90. $sWhereClause = " AND `item_type` IN (" . $this->implode_escape(is_array($aParams['type']) ? $aParams['type'] : [$aParams['type']]) . ")";
  91. $sLimitClause = "";
  92. if(isset($aParams['start']) && !empty($aParams['per_page']))
  93. $sLimitClause = " LIMIT " . $aParams['start'] . ", " . $aParams['per_page'];
  94. return $this->getPairs("SELECT `item_id` AS `id`, (`item_value` - `item_reducer`) AS `value` FROM `" . self::$sTableData . "` WHERE `profile_id` = :profile_id AND `object_id` = :object_id AND (`item_value` - `item_reducer`) >= 0" . $sWhereClause . " ORDER BY `value` DESC" . $sLimitClause, 'id', 'value', [
  95. 'profile_id' => $iProfileId,
  96. 'object_id' => $iObjectId
  97. ]);
  98. }
  99. public function getBy($aParams)
  100. {
  101. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  102. $sFieldsClause = "*";
  103. $sJoinClause = $sWhereClause = $sGroupClause = $sLimitClause = $sOrderClause = "";
  104. switch($aParams['type']) {
  105. case 'profile_object_ids':
  106. $aMethod['params'][1] = [
  107. 'profile_id' => $aParams['profile_id'],
  108. 'object_id' => $aParams['object_id']
  109. ];
  110. $sWhereClause = " AND `profile_id`=:profile_id AND `object_id`=:object_id";
  111. break;
  112. case 'all':
  113. break;
  114. }
  115. $sOrderClause = $sOrderClause ? "ORDER BY " . $sOrderClause : "";
  116. $sLimitClause = $sLimitClause ? "LIMIT " . $sLimitClause : "";
  117. $aMethod['params'][0] = "SELECT
  118. " . $sFieldsClause . "
  119. FROM `" . self::$sTableData . "` " . $sJoinClause . "
  120. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  121. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  122. }
  123. public function getItem($iProfileId, $iObjectId, $iItemId)
  124. {
  125. return $this->getRow("SELECT * FROM `" . self::$sTableData . "` WHERE `profile_id` = :profile_id AND `object_id` = :object_id AND `item_id` = :item_id", [
  126. 'profile_id' => $iProfileId,
  127. 'object_id' => $iObjectId,
  128. 'item_id' => $iItemId
  129. ]);
  130. }
  131. }
  132. /** @} */