BxChartsModule.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. if(BxDolRequest::serviceExists($aModule['name'], 'act_as_profile') && BxDolService::call($aModule['name'], 'act_as_profile') == true){
  45. $aResult[$aModule['name']] = $aModule['title'];
  46. }
  47. }
  48. return $aResult;
  49. }
  50. /**
  51. * @page service Service Calls
  52. * @section bx_charts Charts
  53. * @subsection bx_charts-other Other
  54. * @subsubsection bx_charts-on-profile get_text_modules
  55. *
  56. * @code bx_srv('bx_charts', 'get_text_modules', [...]); @endcode
  57. *
  58. * Get list of avaliable text modules
  59. *
  60. * @return an array with avaliable modules.
  61. *
  62. * @see BxChartsModule::serviceGetTextModules
  63. */
  64. /**
  65. * @ref bx_charts-get_text_modules "get_text_modules"
  66. */
  67. public function serviceGetTextModules()
  68. {
  69. $aResult = array();
  70. $BxDolModuleQuery = BxDolModuleQuery::getInstance();
  71. $aModules = $BxDolModuleQuery->getModulesBy(array('type' => 'modules', 'active' => 1));
  72. foreach($aModules as $aModule){
  73. $oModule = BxDolModule::getInstance($aModule['name']);
  74. if($oModule instanceof iBxDolContentInfoService && isset($oModule->_oConfig->CNF['OBJECT_VOTES'])){
  75. $aResult[$aModule['name']] = $aModule['title'];
  76. }
  77. }
  78. return $aResult;
  79. }
  80. /**
  81. * @page service Service Calls
  82. * @section bx_charts Charts
  83. * @subsection bx_charts-other Other
  84. * @subsubsection bx_charts-on-profile get_chart_top_contents_by_likes
  85. *
  86. * @code bx_srv('bx_charts', 'get_chart_top_contents_by_likes', [...]); @endcode
  87. *
  88. * Get Chart Top Contents By Likes
  89. *
  90. * @return an html for chart1.
  91. *
  92. * @see BxChartsModule::serviceGetChartTopContentsByLikes
  93. */
  94. /**
  95. * @ref bx_charts-get_text_modules "get_chart_top_contents_by_likes"
  96. */
  97. public function serviceGetChartTopContentsByLikes()
  98. {
  99. return $this->_oTemplate->getChart('TopContentsByLikes', 400);
  100. }
  101. /**
  102. * @page service Service Calls
  103. * @section bx_charts Charts
  104. * @subsection bx_charts-other Other
  105. * @subsubsection bx_charts-on-profile get_chart_most_active_profiles
  106. *
  107. * @code bx_srv('bx_charts', 'get_chart_most_active_profiles', [...]); @endcode
  108. *
  109. * Get Chart Most Active Profiles
  110. *
  111. * @return an html for chart1.
  112. *
  113. * @see BxChartsModule::serviceGetChartMostActiveProfiles
  114. */
  115. /**
  116. * @ref bx_charts-get_text_modules "get_chart_most_active_profiles"
  117. */
  118. public function serviceGetChartMostActiveProfiles()
  119. {
  120. return $this->_oTemplate->getChart('MostActiveProfiles', 500);
  121. }
  122. public function actionGetChartData($Id = 0)
  123. {
  124. header('Content-Type: application/json');
  125. if ($Id == 'TopContentsByLikes'){
  126. $aValues = array('labels' => array(), 'values' => array(), 'colors' => array(), 'links' => array());
  127. $aData = $this->_oDb->getTopByLikes();
  128. foreach ($aData as $aValue) {
  129. $oModule = BxDolModule::getInstance($aValue['module']);
  130. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])) . ' (' . $oModule->_aModule['title'].')');
  131. array_push($aValues['values'], $aValue['value']);
  132. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  133. }
  134. $aValues['colors'] = array_slice($this->aColors, 0, count($aValues['values']));
  135. echo '{"type": "doughnut", "data":{"labels":' . json_encode($aValues['labels']) . ',"datasets":[{"data":' . json_encode($aValues['values']) . ',"backgroundColor":' . json_encode($aValues['colors']) . '}]}, "options": {"legend": {"position": "bottom"}}, "links": ' . json_encode($aValues['links']) . '}';
  136. }
  137. if ($Id == 'MostActiveProfiles'){
  138. $aValues = array('labels' => array(), 'values1' => array(), 'values2' => array(), 'links' => array());
  139. $aData = $this->_oDb->getMostActiveProfiles();
  140. foreach ($aData as $aValue) {
  141. $oModule = BxDolModule::getInstance($aValue['module']);
  142. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])));
  143. array_push($aValues['values1'], $aValue['create_count']);
  144. array_push($aValues['values2'], $aValue['views_count']);
  145. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  146. }
  147. echo '{"type":"horizontalBar", "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']) . '}]}, "options": {"legend": {"position": "bottom"}}, "links": ' . json_encode($aValues['links']) . '}';
  148. }
  149. }
  150. private function getItemName($sString)
  151. {
  152. $sString = strip_tags($sString);
  153. if ($sString == ''){
  154. $sString = _t('_bx_charts_txt_empty_title_item');
  155. }
  156. else{
  157. $sString = strmaxtextlen($sString, 50, '...');
  158. }
  159. return $sString;
  160. }
  161. }
  162. /** @} */