BxPollsDb.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 Polls Polls
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxPollsDb extends BxBaseModTextDb
  15. {
  16. function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getContentInfoById ($iContentId, $bForceGet = false)
  21. {
  22. $aContentInfo = parent::getContentInfoById($iContentId, $bForceGet);
  23. if(!empty($aContentInfo) && is_array($aContentInfo))
  24. $aContentInfo['salt'] = $this->_oConfig->getSalt();
  25. return $aContentInfo;
  26. }
  27. public function isPerformed($iEntryId, $iAuthorId, $iAuthorIp)
  28. {
  29. $CNF = &$this->_oConfig->CNF;
  30. $iAuthorId = (int)$iAuthorId;
  31. $aBindings = array('author_id' => $iAuthorId);
  32. $sWhereClause = "AND `author_id`=:author_id";
  33. if(empty($iAuthorId)) {
  34. $aBindings['author_nip'] = $iAuthorIp;
  35. $sWhereClause .= " AND `author_nip`=:author_nip";
  36. }
  37. $aSubentries = $this->getSubentries(array('type' => 'entry_id_pairs', 'entry_id' => $iEntryId));
  38. return (int)$this->getOne("SELECT `object_id` FROM `" . $CNF['TABLE_VOTES_SUBENTRIES_TRACK'] . "` WHERE `object_id` IN (" . $this->implode_escape(array_keys($aSubentries)) . ") " . $sWhereClause . " LIMIT 1", $aBindings) != 0;
  39. }
  40. public function getPerformedValue($iEntryId, $iAuthorId)
  41. {
  42. $CNF = &$this->_oConfig->CNF;
  43. $aBindings = ['author_id' => $iAuthorId];
  44. $sWhereClause = "AND `author_id`=:author_id";
  45. $aSubentries = $this->getSubentries([
  46. 'type' => 'entry_id_pairs',
  47. 'entry_id' => $iEntryId
  48. ]);
  49. return (int)$this->getOne("SELECT `object_id` FROM `" . $CNF['TABLE_VOTES_SUBENTRIES_TRACK'] . "` WHERE `object_id` IN (" . $this->implode_escape(array_keys($aSubentries)) . ") " . $sWhereClause . " LIMIT 1", $aBindings);
  50. }
  51. public function getContentInfoBySubentryId ($iSubentryId)
  52. {
  53. $CNF = &$this->_oConfig->CNF;
  54. $sQuery = $this->prepare ("SELECT `te`.* FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te` LEFT JOIN `" . $CNF['TABLE_SUBENTRIES'] . "` AS `tse` ON `te`.`" . $CNF['FIELD_ID'] . "`=`tse`.`entry_id` WHERE `tse`.`id` = ?", $iSubentryId);
  55. return $this->getRow($sQuery);
  56. }
  57. public function getSubentries($aParams = array())
  58. {
  59. $CNF = &$this->_oConfig->CNF;
  60. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  61. $sWhereClause = $sOrderByClause = "";
  62. $sSelectClause = "*";
  63. switch($aParams['type']) {
  64. case 'id':
  65. $aMethod['name'] = 'getRow';
  66. $aMethod['params'][1] = array(
  67. 'id' => $aParams['id']
  68. );
  69. $sWhereClause .= " AND `id`=:id";
  70. break;
  71. case 'entry_id_pairs':
  72. $aMethod['name'] = 'getPairs';
  73. $aMethod['params'][1] = 'id';
  74. $aMethod['params'][2] = 'title';
  75. $aMethod['params'][3] = array(
  76. 'entry_id' => $aParams['entry_id']
  77. );
  78. $sWhereClause .= " AND `entry_id`=:entry_id";
  79. break;
  80. case 'entry_id':
  81. $aMethod['params'][1] = array(
  82. 'entry_id' => $aParams['entry_id']
  83. );
  84. $sWhereClause .= " AND `entry_id`=:entry_id";
  85. break;
  86. case 'entry_id_max_order':
  87. $aMethod['name'] = 'getOne';
  88. $aMethod['params'][1] = array(
  89. 'entry_id' => $aParams['entry_id']
  90. );
  91. $sSelectClause = "MAX(`order`)";
  92. $sWhereClause .= " AND `entry_id`=:entry_id";
  93. break;
  94. case 'all':
  95. break;
  96. }
  97. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? '`' . $aParams['order_by'] . '`' : '`order`');
  98. $sOrderByClause .= " " . (isset($aParams['order_way']) ? strtoupper($aParams['order_way']) : 'ASC');
  99. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_SUBENTRIES'] . "` WHERE 1 " . $sWhereClause . $sOrderByClause;
  100. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  101. }
  102. public function insertSubentry($aParamsSet)
  103. {
  104. $CNF = &$this->_oConfig->CNF;
  105. if(empty($aParamsSet))
  106. return false;
  107. $sSql = "INSERT INTO `" . $CNF['TABLE_SUBENTRIES'] . "` SET " . $this->arrayToSQL($aParamsSet);
  108. return $this->query($sSql);
  109. }
  110. public function updateSubentry($aParamsSet, $aParamsWhere)
  111. {
  112. $CNF = &$this->_oConfig->CNF;
  113. if(empty($aParamsSet) || empty($aParamsWhere))
  114. return false;
  115. $sSql = "UPDATE `" . $CNF['TABLE_SUBENTRIES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  116. return $this->query($sSql);
  117. }
  118. public function deleteSubentry($aParams)
  119. {
  120. $CNF = &$this->_oConfig->CNF;
  121. $sSql = "DELETE FROM `" . $CNF['TABLE_SUBENTRIES'] . "` WHERE " . $this->arrayToSQL($aParams, " AND ");
  122. return $this->query($sSql);
  123. }
  124. public function deleteSubentryById($mixedId)
  125. {
  126. $CNF = &$this->_oConfig->CNF;
  127. if(!is_array($mixedId))
  128. $mixedId = array($mixedId);
  129. $sSql = "DELETE FROM `" . $CNF['TABLE_SUBENTRIES'] . "` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")";
  130. return $this->query($sSql);
  131. }
  132. }
  133. /** @} */