BxDolRecommendationQuery.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, $iItemValue)
  59. {
  60. return $this->query("INSERT INTO `" . self::$sTableData . "` (`profile_id`, `object_id`, `item_id`, `item_value`) VALUES (:profile_id, :object_id, :item_id, :item_value) ON DUPLICATE KEY UPDATE `item_value` = :item_value", [
  61. 'profile_id' => $iProfileId,
  62. 'object_id' => $iObjectId,
  63. 'item_id' => $iItemId,
  64. 'item_value' => $iItemValue
  65. ]) !== false;
  66. }
  67. public function update($iProfileId, $iObjectId, $iItemId, $aSet)
  68. {
  69. if(empty($aSet) || !is_array($aSet))
  70. return false;
  71. 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 ", [
  72. 'profile_id' => $iProfileId,
  73. 'object_id' => $iObjectId,
  74. 'item_id' => $iItemId
  75. ]) !== false;
  76. }
  77. public function get($iProfileId, $iObjectId, $iStart = 0, $iPerPage = 0)
  78. {
  79. $sLimitClause = "";
  80. if($iPerPage)
  81. $sLimitClause = " LIMIT " . $iStart . ", " . $iPerPage;
  82. 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 ORDER BY `value` DESC" . $sLimitClause, 'id', 'value', [
  83. 'profile_id' => $iProfileId,
  84. 'object_id' => $iObjectId
  85. ]);
  86. }
  87. public function getItem($iProfileId, $iObjectId, $iItemId)
  88. {
  89. return $this->getRow("SELECT * FROM `" . self::$sTableData . "` WHERE `profile_id` = :profile_id AND `object_id` = :object_id AND `item_id` = :item_id", [
  90. 'profile_id' => $iProfileId,
  91. 'object_id' => $iObjectId,
  92. 'item_id' => $iItemId
  93. ]);
  94. }
  95. }
  96. /** @} */