BxChartsDb.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Charts Charts
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolModuleDb');
  12. class BxChartsDb extends BxBaseModGeneralDb
  13. {
  14. protected $_sTableTopByLikes;
  15. protected $_sTableMostActiveProfiles;
  16. protected $_sTableMostFollowedProfiles;
  17. public function __construct(&$oConfig)
  18. {
  19. parent::__construct($oConfig);
  20. $this->_sTableTopByLikes = $this->_sPrefix . 'top_by_likes';
  21. $this->_sTableMostActiveProfiles = $this->_sPrefix . 'most_active_profiles';
  22. $this->_sTableMostFollowedProfiles = $this->_sPrefix . 'most_followed_profiles';
  23. }
  24. /* TopByLikes part */
  25. public function saveTopByLikes($sModuleName, $sTableName)
  26. {
  27. $iInterval = intval(getParam('bx_charts_chart_top_contents_by_likes_interval_day'));
  28. $sQuery = "INSERT INTO `" . $this->_sTableTopByLikes . "` (`object_id`, `module`, `value`) SELECT `object_id`,'" . $sModuleName . "', COUNT(`id`) FROM `" . $sTableName . "` WHERE `date` > " . ($iInterval > 0 ? $this->getTimeFromDaysBefore($iInterval) : 0) . " GROUP BY `object_id` ORDER BY COUNT(`id`) DESC LIMIT 0, " . intval(getParam('bx_charts_chart_top_contents_by_likes_count'));
  29. $this->query($sQuery);
  30. }
  31. public function clearTopByLikes()
  32. {
  33. $sQuery = "TRUNCATE TABLE `" . $this->_sTableTopByLikes . "`";
  34. $this->query($sQuery);
  35. }
  36. public function getTopByLikes()
  37. {
  38. return $this->getAll("SELECT * FROM `" . $this->_sTableTopByLikes . "` ORDER BY `value` DESC LIMIT 0," . intval(getParam('bx_charts_chart_top_contents_by_likes_count')));
  39. }
  40. /* MostActiveProfiles part */
  41. public function saveMostActiveProfiles_View($sProfileModuleName, $sTableName)
  42. {
  43. $iInterval = intval(getParam('bx_charts_chart_most_active_profiles_interval_day'));
  44. $sQuery = "INSERT INTO `" . $this->_sTableMostActiveProfiles . "` (`object_id`, `profile_module`, `views_count`) SELECT `object_id`,'" . $sProfileModuleName . "', COUNT(`date`) AS views FROM `" . $sTableName . "`
  45. WHERE `object_id` IN (SELECT `object_id` FROM `" . $this->_sTableMostActiveProfiles . "` WHERE `profile_module` = '" . $sProfileModuleName . "' ) AND `date` > " . ($iInterval > 0 ? $this->getTimeFromDaysBefore($iInterval) : 0) . " GROUP BY `object_id`";
  46. $this->query($sQuery);
  47. }
  48. public function saveMostActiveProfiles_Create($sProfileModuleName, $sContentModuleName, $sTableName, $sColumnAuthor, $sColumnAdded)
  49. {
  50. $iInterval = intval(getParam('bx_charts_chart_most_active_profiles_interval_day'));
  51. $sQuery = "INSERT INTO `" . $this->_sTableMostActiveProfiles . "` (`object_id`, `profile_module`, `content_module`, `create_count`) SELECT `sys_profiles`.`content_id`, `sys_profiles`.`type`, '" . $sContentModuleName . "' , COUNT(`" . $sTableName . "`.`id`) FROM `" . $sTableName . "`
  52. INNER JOIN `sys_profiles` ON `sys_profiles`.`id` = `" . $sTableName . "`.`" . $sColumnAuthor . "` WHERE sys_profiles.type='" . $sProfileModuleName . "' AND `" . $sTableName . "`.`" . $sColumnAdded . "` > " . ($iInterval > 0 ? $this->getTimeFromDaysBefore($iInterval) : 0) . " GROUP BY `sys_profiles`.`content_id`, `sys_profiles`.`type` ORDER BY COUNT(`" . $sTableName . "`.`id`) DESC LIMIT 0," . intval(getParam('bx_charts_chart_most_active_profiles_count'));
  53. $this->query($sQuery);
  54. }
  55. public function clearMostActiveProfiles()
  56. {
  57. $sQuery = "TRUNCATE TABLE `" . $this->_sTableMostActiveProfiles . "`";
  58. $this->query($sQuery);
  59. }
  60. public function getMostActiveProfiles()
  61. {
  62. return $this->getAll("SELECT SUM(`views_count`) AS views_count, SUM(`create_count`) AS create_count, `object_id`, `profile_module` as `module` FROM `" . $this->_sTableMostActiveProfiles . "` GROUP BY `object_id`, `module` ORDER BY SUM(`create_count`) DESC, SUM(`views_count`) DESC LIMIT 0," . intval(getParam('bx_charts_chart_most_active_profiles_count')));
  63. }
  64. /* MostFollowedProfiles part */
  65. public function saveMostFollowedProfiles($sProfileModuleName)
  66. {
  67. $iInterval = intval(getParam('bx_charts_chart_most_followed_profiles_interval_day'));
  68. $sQuery = "INSERT INTO `" . $this->_sTableMostFollowedProfiles . "` (`object_id`, `profile_module`, `followers_count`) SELECT `sys_profiles`.`content_id`, `sys_profiles`.`type` , COUNT(`sys_profiles_conn_subscriptions`.`id`) FROM `sys_profiles_conn_subscriptions`
  69. INNER JOIN `sys_profiles` ON `sys_profiles`.`id` = `sys_profiles_conn_subscriptions`.`content` WHERE sys_profiles.type='" . $sProfileModuleName . "' AND `sys_profiles_conn_subscriptions`.`added` > " . ($iInterval > 0 ? $this->getTimeFromDaysBefore($iInterval) : 0) . " GROUP BY `sys_profiles`.`content_id`, `sys_profiles`.`type` ORDER BY COUNT(`sys_profiles_conn_subscriptions`.`id`) DESC LIMIT 0," . intval(getParam('bx_charts_chart_most_followed_profiles_count'));
  70. $this->query($sQuery);
  71. }
  72. public function clearMostFollowedProfiles()
  73. {
  74. $sQuery = "TRUNCATE TABLE `" . $this->_sTableMostFollowedProfiles . "`";
  75. $this->query($sQuery);
  76. }
  77. public function getMostFollowedProfiles()
  78. {
  79. return $this->getAll("SELECT SUM(`followers_count`) AS followers_count, `object_id`, `profile_module` as `module` FROM `" . $this->_sTableMostFollowedProfiles . "` GROUP BY `object_id`, `module` ORDER BY SUM(`followers_count`) DESC LIMIT 0," . intval(getParam('bx_charts_chart_most_followed_profiles_count')));
  80. }
  81. /* Growth part */
  82. public function getStatistic()
  83. {
  84. return $this->getAll("SELECT * FROM `sys_statistics` WHERE `name` <> 'bx_albums_media' ORDER BY `order`");
  85. }
  86. /* Growth part */
  87. public function getGrowth($sTableName)
  88. {
  89. $iInterval = intval(getParam('bx_charts_chart_growth_interval_day'));
  90. $sQuery = "SELECT " . getParam('bx_charts_chart_growth_group_by') . "(FROM_UNIXTIME(`added`)) AS `period`, YEAR(FROM_UNIXTIME(`added`)) AS `year`, COUNT(*) AS `count` FROM " . $sTableName . " WHERE `added` > " . ($iInterval > 0 ? $this->getTimeFromDaysBefore($iInterval) : 0) . " GROUP BY `period`, `year` ORDER BY `year`, `period` ASC";
  91. return $this->getAll($sQuery);
  92. }
  93. public function getGrowthInitValue($sTableName)
  94. {
  95. $iInterval = intval(getParam('bx_charts_chart_growth_interval_day'));
  96. $mixedStartDate = $this->getTimeFromDaysBefore($iInterval);
  97. $sQuery = "SELECT COUNT(*) AS `count` FROM " . $sTableName . " WHERE `added` < " . ($iInterval > 0 ? $mixedStartDate : 0);
  98. $mixedStartValue = $this->getOne($sQuery);
  99. if ($iInterval == 0){
  100. $sQuery = "SELECT MIN(`added`) AS `mindata` FROM " . $sTableName;
  101. $mixedStartDate = $this->getOne($sQuery);
  102. }
  103. return array($mixedStartValue,$mixedStartDate);
  104. }
  105. private function getTimeFromDaysBefore($iDay){
  106. return time() - $iDay * 86400;
  107. }
  108. }
  109. /** @} */