BxChartsModule.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. class BxChartsModule extends BxDolModule
  12. {
  13. protected $aColors = array('#3366CC','#DC3912','#FF9900','#109618','#990099','#3B3EAC','#0099C6','#DD4477','#66AA00','#B82E2E','#316395','#994499','#22AA99','#AAAA11','#6633CC','#E67300','#8B0707','#329262','#5574A6','#3B3EAC');
  14. function __construct(&$aModule)
  15. {
  16. parent::__construct($aModule);
  17. }
  18. /**
  19. * Service methods
  20. */
  21. /**
  22. * @page service Service Calls
  23. * @section bx_charts Charts
  24. * @subsection bx_charts-other Other
  25. * @subsubsection bx_charts get_profile_modules
  26. *
  27. * @code bx_srv('bx_charts', 'get_profile_modules', [...]); @endcode
  28. *
  29. * Get list of avaliable prifile modules
  30. *
  31. * @return an array with avaliable modules.
  32. *
  33. * @see BxChartsModule::serviceGetProfileModules
  34. */
  35. /**
  36. * @ref bx_charts-get_profile_modules "get_profile_modules"
  37. */
  38. public function serviceGetProfileModules()
  39. {
  40. $aResult = array();
  41. $BxDolModuleQuery = BxDolModuleQuery::getInstance();
  42. $aModules = $BxDolModuleQuery->getModulesBy(array('type' => 'modules', 'active' => 1));
  43. foreach($aModules as $aModule){
  44. $oModule = BxDolModule::getInstance($aModule['name']);
  45. if($oModule instanceof iBxDolProfileService){
  46. $aResult[$aModule['name']] = $aModule['title'];
  47. }
  48. }
  49. return $aResult;
  50. }
  51. /**
  52. * @page service Service Calls
  53. * @section bx_charts Charts
  54. * @subsection bx_charts-other Other
  55. * @subsubsection bx_charts-on-profile get_text_modules
  56. *
  57. * @code bx_srv('bx_charts', 'get_text_modules', [...]); @endcode
  58. *
  59. * Get list of avaliable text modules
  60. *
  61. * @return an array with avaliable modules.
  62. *
  63. * @see BxChartsModule::serviceGetTextModules
  64. */
  65. /**
  66. * @ref bx_charts-get_text_modules "get_text_modules"
  67. */
  68. public function serviceGetTextModules()
  69. {
  70. $aResult = array();
  71. $BxDolModuleQuery = BxDolModuleQuery::getInstance();
  72. $aModules = $BxDolModuleQuery->getModulesBy(array('type' => 'modules', 'active' => 1));
  73. foreach($aModules as $aModule){
  74. $oModule = BxDolModule::getInstance($aModule['name']);
  75. if($oModule instanceof iBxDolContentInfoService && isset($oModule->_oConfig->CNF['OBJECT_VOTES'])){
  76. $aResult[$aModule['name']] = $aModule['title'];
  77. }
  78. }
  79. return $aResult;
  80. }
  81. /**
  82. * @page service Service Calls
  83. * @section bx_charts Charts
  84. * @subsection bx_charts-other Other
  85. * @subsubsection bx_charts-on-profile get_chart_top_contents_by_likes
  86. *
  87. * @code bx_srv('bx_charts', 'get_chart_top_contents_by_likes', [...]); @endcode
  88. *
  89. * Get Chart Top Contents By Likes
  90. *
  91. * @return an html for chart1.
  92. *
  93. * @see BxChartsModule::serviceGetChartTopContentsByLikes
  94. */
  95. /**
  96. * @ref bx_charts-get_text_modules "get_chart_top_contents_by_likes"
  97. */
  98. public function serviceGetChartTopContentsByLikes()
  99. {
  100. return $this->_oTemplate->getChart('TopContentsByLikes');
  101. }
  102. /**
  103. * @page service Service Calls
  104. * @section bx_charts Charts
  105. * @subsection bx_charts-other Other
  106. * @subsubsection bx_charts-on-profile get_chart_most_active_profiles
  107. *
  108. * @code bx_srv('bx_charts', 'get_chart_most_active_profiles', [...]); @endcode
  109. *
  110. * Get Chart Most Active Profiles
  111. *
  112. * @return an html for chart1.
  113. *
  114. * @see BxChartsModule::serviceGetChartMostActiveProfiles
  115. */
  116. /**
  117. * @ref bx_charts-get_text_modules "get_chart_most_active_profiles"
  118. */
  119. public function serviceGetChartMostActiveProfiles()
  120. {
  121. return $this->_oTemplate->getChart('MostActiveProfiles');
  122. }
  123. public function actionGetChartData($Id = 0)
  124. {
  125. header('Content-Type: application/json');
  126. if ($Id == 'TopContentsByLikes'){
  127. $aValues = array('labels' => array(), 'values' => array(), 'colors' => array(), 'links' => array());
  128. $aData = $this->_oDb->getTopByLikes();
  129. foreach ($aData as $aValue) {
  130. $oModule = BxDolModule::getInstance($aValue['module']);
  131. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])) . ' (' . $oModule->_aModule['title'].')');
  132. array_push($aValues['values'], $aValue['value']);
  133. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  134. }
  135. $aValues['colors'] = array_slice($this->aColors, 0, count($aValues['values']));
  136. echo '{"type": "doughnut", "data":{"labels":' . json_encode($aValues['labels']) . ',"datasets":[{"data":' . json_encode($aValues['values']) . ',"backgroundColor":' . json_encode($aValues['colors']) . '}]}, "links": ' . json_encode($aValues['links']) . '}';
  137. }
  138. if ($Id == 'MostActiveProfiles'){
  139. $aValues = array('labels' => array(), 'values1' => array(), 'values2' => array(), 'links' => array());
  140. $aData = $this->_oDb->getMostActiveProfiles();
  141. foreach ($aData as $aValue) {
  142. $oModule = BxDolModule::getInstance($aValue['module']);
  143. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])) . ' (' . $oModule->_aModule['title'].')');
  144. array_push($aValues['values1'], $aValue['create_count']);
  145. array_push($aValues['values2'], $aValue['views_count']);
  146. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  147. }
  148. echo '{"type":"bar", "data": {"labels":' . json_encode($aValues['labels']) . ',"datasets": [{"label": "' . _t('_bx_charts_most_active_profiles_legend_posts') . '","backgroundColor": "' . $this->aColors[0] . '","borderColor": "' . $this->aColors[0] . '","borderWidth": 1,"data": ' . json_encode($aValues['values1']) . '}, {"label": "' . _t('_bx_charts_most_active_profiles_legend_views') . '","backgroundColor": "' . $this->aColors[1] . '","borderColor":"' . $this->aColors[1] . '","borderWidth": 1,"data": ' . json_encode($aValues['values2']) . '}]},"links": ' . json_encode($aValues['links']) . '}';
  149. }
  150. }
  151. private function getItemName($sString)
  152. {
  153. $sString = strip_tags($sString);
  154. if ($sString == ''){
  155. $sString = _t('_bx_charts_txt_empty_title_item');
  156. }
  157. else{
  158. $sString = strmaxtextlen($sString, 10, '...');
  159. }
  160. return $sString;
  161. }
  162. }
  163. /** @} */