BxCnlDb.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @defdroup Channels Channels
  7. * @indroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Channels module database queries
  13. */
  14. class BxCnlDb extends BxBaseModGroupsDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getChannelIdByName($sName)
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. return $this->getOne("SELECT `" . $CNF['FIELD_ID'] . "` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_NAME'] . "` = :name", array(
  24. 'name' => $sName,
  25. ));
  26. }
  27. public function getChannelInfoByName($sName)
  28. {
  29. $CNF = &$this->_oConfig->CNF;
  30. return $this->getRow("SELECT `c`.*, `p`.`account_id`, `p`.`id` AS `profile_id`, `a`.`email` AS `profile_email`, `a`.`ip` AS `profile_ip`, `p`.`status` AS `profile_status` FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `c` INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `c`.`id` AND `p`.`type` = :type) INNER JOIN `sys_accounts` AS `a` ON (`p`.`account_id` = `a`.`id`) WHERE `c`.`channel_name` = :name", array(
  31. 'type' => $this->_oConfig->getName(),
  32. 'name' => $sName,
  33. ));
  34. }
  35. public function addContentToChannel($iContentId, $iCnlId, $sModuleName, $iAuthorId)
  36. {
  37. $CNF = &$this->_oConfig->CNF;
  38. $iNow = time();
  39. if(!$this->query("INSERT INTO `" . $CNF['TABLE_CONTENT'] . "` (`content_id`, `cnl_id`, `author_id`, `module_name`, `date`) VALUES (:content_id, :cnl_id, :author_id, :module_name, :date)", [
  40. 'content_id' => $iContentId,
  41. 'cnl_id' => $iCnlId,
  42. 'module_name' => $sModuleName,
  43. 'author_id' => $iAuthorId,
  44. 'date' => $iNow
  45. ]))
  46. return 0;
  47. $iCnlContentId = $this->lastId();
  48. $this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `lc_id`=:lc_id, `lc_date`=:lc_date, `contents`=:contents WHERE `" . $CNF['FIELD_ID'] . "`=:id", [
  49. 'lc_id' => $iCnlContentId,
  50. 'lc_date' => $iNow,
  51. 'contents' => (int)$this->getOne("SELECT COUNT(*) FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `cnl_id`=:cnl_id LIMIT 1", ['cnl_id' => $iCnlId]),
  52. 'id' => $iCnlId
  53. ]);
  54. return $iCnlContentId;
  55. }
  56. public function checkContentInChannel($iContentId, $iCnlId, $sModuleName, $iAuthorId)
  57. {
  58. $CNF = &$this->_oConfig->CNF;
  59. $aBindings = array(
  60. 'content_id' => $iContentId,
  61. 'cnl_id' => $iCnlId,
  62. 'module_name' => $sModuleName,
  63. 'author_id' => $iAuthorId,
  64. );
  65. return $this->getOne("SELECT COUNT(*) FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `content_id` = :content_id AND `module_name` = :module_name AND `cnl_id` = :cnl_id AND `author_id` =:author_id", $aBindings);
  66. }
  67. public function getContentById($iId)
  68. {
  69. $CNF = &$this->_oConfig->CNF;
  70. return $this->getRow("SELECT * FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `id` = :id LIMIT 1", array(
  71. 'id' => $iId
  72. ));
  73. }
  74. public function getDataByContent($iContentId, $sModuleName)
  75. {
  76. $CNF = &$this->_oConfig->CNF;
  77. $aBindings = array(
  78. 'content_id' => $iContentId,
  79. 'module_name' => $sModuleName
  80. );
  81. return $this->getAll("SELECT `id`, `author_id`, `cnl_id` FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `content_id` = :content_id AND `module_name` = :module_name", $aBindings);
  82. }
  83. public function removeContentFromChannel($iContentId, $sModuleName)
  84. {
  85. $CNF = &$this->_oConfig->CNF;
  86. $aBindings = [
  87. 'content_id' => $iContentId,
  88. 'module_name' => $sModuleName
  89. ];
  90. $aContents = $this->getAll("SELECT * FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `content_id` = :content_id AND `module_name` = :module_name", $aBindings);
  91. if(empty($aContents) || !is_array($aContents))
  92. return;
  93. $this->query("DELETE FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `content_id` = :content_id AND `module_name` = :module_name", $aBindings);
  94. foreach($aContents as $aContent) {
  95. $iCnlId = (int)$aContent['cnl_id'];
  96. $aLatest = $this->getRow("SELECT * FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `cnl_id`=:cnl_id ORDER BY `date` DESC LIMIT 1", [
  97. 'cnl_id' => $iCnlId
  98. ]);
  99. $iLcId = $iLcDate = 0;
  100. if(!empty($aLatest) && is_array($aLatest)) {
  101. $iLcId = (int)$aLatest['id'];
  102. $iLcDate = (int)$aLatest['date'];
  103. }
  104. $this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `lc_id`=:lc_id, `lc_date`=:lc_date, `contents`=:contents WHERE `" . $CNF['FIELD_ID'] . "`=:id", [
  105. 'lc_id' => $iLcId,
  106. 'lc_date' => $iLcDate,
  107. 'contents' => (int)$this->getOne("SELECT COUNT(*) FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `cnl_id`=:cnl_id LIMIT 1", ['cnl_id' => $iCnlId]),
  108. 'id' => $iCnlId
  109. ]);
  110. }
  111. }
  112. public function removeChannelContent($iChannelId)
  113. {
  114. $CNF = &$this->_oConfig->CNF;
  115. $this->query("DELETE FROM `" . $CNF['TABLE_CONTENT'] . "` WHERE `cnl_id` = :channel_id", [
  116. 'channel_id' => $iChannelId
  117. ]);
  118. }
  119. }
  120. /** @} */