BxForumDb.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Forum Forum
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxForumDb extends BxBaseModTextDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function updateEntries($aSet, $aWhere)
  21. {
  22. $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1");
  23. return (int)$this->query($sQuery) > 0;
  24. }
  25. public function updateLastCommentTimeProfile($iConversationId, $iProfileId, $iCommentId, $iTimestamp)
  26. {
  27. $sQuery = $this->prepare("UPDATE `" . $this->getPrefix() . "discussions` SET `lr_profile_id` = ?, `lr_timestamp` = ?, `lr_comment_id` = ? WHERE `id` = ?", $iProfileId, $iTimestamp, $iCommentId, $iConversationId);
  28. return $this->query($sQuery);
  29. }
  30. public function getComments($aParams)
  31. {
  32. $CNF = &$this->_oConfig->CNF;
  33. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  34. $sFieldsClause = "`te`.*";
  35. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  36. switch($aParams['type']) {
  37. case 'entry_last':
  38. $aMethod['name'] = 'getRow';
  39. $aMethod['params'][1]['cmt_object_id'] = $aParams['entry_id'];
  40. $sWhereClause = " AND `te`.`cmt_object_id` = :cmt_object_id";
  41. $sOrderClause = "`te`.`cmt_time` DESC";
  42. $sLimitClause = "1";
  43. break;
  44. case 'entries_author_search':
  45. $aMethod['name'] = 'getColumn';
  46. $sFieldsClause = "`te`.`cmt_object_id`";
  47. $sWhereClause = " AND `te`.`cmt_author_id` IN (" . $this->implode_escape($aParams['author']) . ")";
  48. $sGroupClause = "`te`.`cmt_object_id`";
  49. break;
  50. case 'entries_keyword_search':
  51. $aMethod['name'] = 'getColumn';
  52. $sFieldsClause = "`te`.`cmt_object_id`";
  53. $sWhereClause = " AND `te`.`cmt_text` LIKE " . $this->escape("%" . $aParams['keyword'] . "%");
  54. $sGroupClause = "`te`.`cmt_object_id`";
  55. break;
  56. case 'author_comments':
  57. $aMethod['name'] = 'getPairs';
  58. $sFieldsClause = "`te`.`cmt_author_id`, COUNT(`te`.`cmt_id`) AS `cmt_count`";
  59. $sWhereClause = $this->prepareAsString(" AND `te`.`cmt_object_id`=? ", $aParams['object_id']);
  60. $sGroupClause = "`te`.`cmt_author_id`";
  61. $aMethod['params'][1] = "cmt_author_id";
  62. $aMethod['params'][2] = "cmt_count";
  63. break;
  64. }
  65. $sGroupClause = $sGroupClause ? "GROUP BY " . $sGroupClause : "";
  66. $sOrderClause = $sOrderClause ? "ORDER BY " . $sOrderClause : "";
  67. $sLimitClause = $sLimitClause ? "LIMIT " . $sLimitClause : "";
  68. $aMethod['params'][0] = "SELECT
  69. " . $sFieldsClause . "
  70. FROM `" . $this->getPrefix() . "cmts` AS `te`" . $sJoinClause . "
  71. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  72. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  73. }
  74. public function getUnrepliedDiscussionsNum ($iProfileId)
  75. {
  76. $CNF = &$this->_oConfig->CNF;
  77. $sQuery = $this->prepare("SELECT COUNT(`te`.`id`)
  78. FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te`
  79. WHERE `te`.`author`=? AND `te`.`lr_profile_id`<>?", $iProfileId, $iProfileId);
  80. return $this->getOne($sQuery);
  81. }
  82. public function updateStatus($sAction, $aContentInfo)
  83. {
  84. $CNF = &$this->_oConfig->CNF;
  85. $aActions = array(
  86. 'stick' => array($CNF['FIELD_STICK'] => 1),
  87. 'unstick' => array($CNF['FIELD_STICK'] => 0),
  88. 'lock' => array($CNF['FIELD_LOCK'] => 1),
  89. 'unlock' => array($CNF['FIELD_LOCK'] => 0),
  90. 'resolve' => array($CNF['FIELD_RESOLVE'] => 1),
  91. 'unresolve' => array($CNF['FIELD_RESOLVE'] => 0),
  92. 'hide' => array($CNF['FIELD_STATUS_ADMIN'] => 'hidden'),
  93. 'unhide' => array($CNF['FIELD_STATUS_ADMIN'] => 'active')
  94. );
  95. return $this->updateEntries($aActions[$sAction], array($CNF['FIELD_ID'] => $aContentInfo[$CNF['FIELD_ID']]));
  96. }
  97. public function insertCategory($aSet)
  98. {
  99. $sQuery = "INSERT INTO `" . $this->getPrefix() . "categories` SET " . $this->arrayToSQL($aSet);
  100. return (int)$this->query($sQuery) > 0;
  101. }
  102. public function getCategories($aParams)
  103. {
  104. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  105. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  106. if(!isset($aParams['order']) || empty($aParams['order']))
  107. $sOrderClause = "ORDER BY `tc`.`category` ASC";
  108. switch($aParams['type']) {
  109. case 'by_category':
  110. $aMethod['name'] = 'getRow';
  111. $aMethod['params'][1] = array(
  112. 'category' => $aParams['category']
  113. );
  114. $sWhereClause = " AND `tc`.`category`=:category ";
  115. break;
  116. case 'all_pairs':
  117. $aMethod['name'] = 'getPairs';
  118. $aMethod['params'][1] = 'category';
  119. $aMethod['params'][2] = 'visible_for_levels';
  120. break;
  121. case 'all_with_key':
  122. $aMethod['name'] = 'getAllWithKey';
  123. $aMethod['params'][1] = 'category';
  124. break;
  125. }
  126. $aMethod['params'][0] = "SELECT
  127. `tc`.*" . $sSelectClause . "
  128. FROM `" . $this->getPrefix() . "categories` AS `tc` " . $sJoinClause . "
  129. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  130. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  131. }
  132. }
  133. /** @} */