BxChartsModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 profile 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 get_modules
  85. *
  86. * @code bx_srv('bx_charts', 'get_modules', [...]); @endcode
  87. *
  88. * Get list of avaliable modules
  89. *
  90. * @return an array with avaliable modules.
  91. *
  92. * @see BxChartsModule::serviceGeModules
  93. */
  94. /**
  95. * @ref bx_charts-get_modules "get_modules"
  96. */
  97. public function serviceGetModules()
  98. {
  99. $aResult = array();
  100. $aItems = $this->_oDb->getStatistic();
  101. foreach($aItems as $aItem) {
  102. $aResult[$aItem['name']] = _t($aItem['title']);
  103. }
  104. return $aResult;
  105. }
  106. /**
  107. * @page service Service Calls
  108. * @section bx_charts Charts
  109. * @subsection bx_charts-other Other
  110. * @subsubsection bx_charts get_growth_group_by
  111. *
  112. * @code bx_srv('bx_charts', 'get_growth_group_by', [...]); @endcode
  113. *
  114. * Get list of avaliable grouping mode(day/week/month)
  115. *
  116. * @return an array with avaliable grouping mode(day/week/month).
  117. *
  118. * @see BxChartsModule::serviceGetGrowthGroupBy
  119. */
  120. /**
  121. * @ref bx_charts-get_growth_group_by "get_growth_group_by"
  122. */
  123. public function serviceGetGrowthGroupBy()
  124. {
  125. return array('date' => _t('_bx_charts_txt_growth_group_by_day'),'week' => _t('_bx_charts_txt_growth_group_by_week'),'month' => _t('_bx_charts_txt_growth_group_by_month'));
  126. }
  127. /**
  128. * @page service Service Calls
  129. * @section bx_charts Charts
  130. * @subsection bx_charts-other Other
  131. * @subsubsection bx_charts-on-profile get_chart_top_contents_by_likes
  132. *
  133. * @code bx_srv('bx_charts', 'get_chart_top_contents_by_likes', [...]); @endcode
  134. *
  135. * Get Chart Top Contents By Likes
  136. *
  137. * @return an html for chart.
  138. *
  139. * @see BxChartsModule::serviceGetChartTopContentsByLikes
  140. */
  141. /**
  142. * @ref bx_charts-get_chart_top_contents_by_likes "get_chart_top_contents_by_likes"
  143. */
  144. public function serviceGetChartTopContentsByLikes()
  145. {
  146. return $this->_oTemplate->getChart('TopContentsByLikes', 100);
  147. }
  148. /**
  149. * @page service Service Calls
  150. * @section bx_charts Charts
  151. * @subsection bx_charts-other Other
  152. * @subsubsection bx_charts-on-profile get_chart_most_active_profiles
  153. *
  154. * @code bx_srv('bx_charts', 'get_chart_most_active_profiles', [...]); @endcode
  155. *
  156. * Get Chart Most Active Profiles
  157. *
  158. * @return an html for chart.
  159. *
  160. * @see BxChartsModule::serviceGetChartMostActiveProfiles
  161. */
  162. /**
  163. * @ref bx_charts-get_chart_most_active_profiles "get_chart_most_active_profiles"
  164. */
  165. public function serviceGetChartMostActiveProfiles()
  166. {
  167. return $this->_oTemplate->getChart('MostActiveProfiles', 80);
  168. }
  169. /**
  170. * @page service Service Calls
  171. * @section bx_charts Charts
  172. * @subsection bx_charts-other Other
  173. * @subsubsection bx_charts-on-profile get_chart_most_followed_profiles
  174. *
  175. * @code bx_srv('bx_charts', 'get_chart_most_followed_profiles', [...]); @endcode
  176. *
  177. * Get Chart Most Followed Profiles
  178. *
  179. * @return an html for chart.
  180. *
  181. * @see BxChartsModule::serviceGetChartMostFollowedProfiles
  182. */
  183. /**
  184. * @ref bx_charts-get_chart_most_followed_profiles "get_chart_most_followed_profiles"
  185. */
  186. public function serviceGetChartMostFollowedProfiles()
  187. {
  188. return $this->_oTemplate->getChart('MostFollowedProfiles', 40);
  189. }
  190. /**
  191. * @page service Service Calls
  192. * @section bx_charts Charts
  193. * @subsection bx_charts-other Other
  194. * @subsubsection bx_charts-on-profile get_chart_content_by_modules
  195. *
  196. * @code bx_srv('bx_charts', 'get_chart_content_by_modules', [...]); @endcode
  197. *
  198. * Get Chart Content By Modules
  199. *
  200. * @return an html for chart.
  201. *
  202. * @see BxChartsModule::serviceGetChartContentByModules
  203. */
  204. /**
  205. * @ref bx_charts-get_chart_content_by_modules "get_chart_content_by_modules"
  206. */
  207. public function serviceGetChartContentByModules()
  208. {
  209. return $this->_oTemplate->getChart('ContentByModules', 100);
  210. }
  211. /**
  212. * @page service Service Calls
  213. * @section bx_charts Charts
  214. * @subsection bx_charts-other Other
  215. * @subsubsection bx_charts-on-profile get_chart_growth_by_modules
  216. *
  217. * @code bx_srv('bx_charts', 'get_chart_growth_by_modules', [...]); @endcode
  218. *
  219. * Get Chart Growth By Modules
  220. *
  221. * @return an html for chart.
  222. *
  223. * @see BxChartsModule::serviceGetChartGrowthByModules
  224. */
  225. /**
  226. * @ref bx_charts-get_chart_growth_by_modules "get_chart_growth_by_modules"
  227. */
  228. public function serviceGetChartGrowthByModules()
  229. {
  230. return $this->_oTemplate->getChartGrowth('GrowthByModules', 75);
  231. }
  232. public function actionGetChartData($Id = 0)
  233. {
  234. header('Content-Type: application/json');
  235. if ($Id == 'TopContentsByLikes'){
  236. $aValues = array('labels' => array(), 'values' => array(), 'colors' => array(), 'links' => array());
  237. $aData = $this->_oDb->getTopByLikes();
  238. foreach ($aData as $aValue) {
  239. $oModule = BxDolModule::getInstance($aValue['module']);
  240. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])) . ' (' . $oModule->_aModule['title'].')'. ' - ' . $aValue['value']);
  241. array_push($aValues['values'], $aValue['value']);
  242. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  243. }
  244. $aValues['colors'] = array_slice($this->aColors, 0, count($aValues['values']));
  245. 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']) . '}';
  246. }
  247. if ($Id == 'MostActiveProfiles'){
  248. $aValues = array('labels' => array(), 'values1' => array(), 'values2' => array(), 'links' => array());
  249. $aData = $this->_oDb->getMostActiveProfiles();
  250. foreach ($aData as $aValue) {
  251. $oModule = BxDolModule::getInstance($aValue['module']);
  252. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])));
  253. array_push($aValues['values1'], $aValue['create_count']);
  254. array_push($aValues['values2'], $aValue['views_count']);
  255. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  256. }
  257. 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']) . '}';
  258. }
  259. if ($Id == 'MostFollowedProfiles'){
  260. $aValues = array('labels' => array(), 'values' => array(), 'links' => array());
  261. $aData = $this->_oDb->getMostFollowedProfiles();
  262. foreach ($aData as $aValue) {
  263. $oModule = BxDolModule::getInstance($aValue['module']);
  264. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])));
  265. array_push($aValues['values'], $aValue['followers_count']);
  266. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  267. }
  268. echo '{"type":"horizontalBar", "data": {"labels":' . json_encode($aValues['labels']) . ',"datasets": [{"label": "' . _t('_bx_charts_most_followed_profiles_legend') . '","backgroundColor": "' . $this->aColors[0] . '","borderColor": "' . $this->aColors[0] . '","borderWidth": 1,"data": ' . json_encode($aValues['values']) . '}]}, "options": {"legend": {"position": "bottom"}}, "links": ' . json_encode($aValues['links']) . '}';
  269. }
  270. if ($Id == 'ContentByModules'){
  271. $aValues = array('labels' => array(), 'values' => array(), 'colors' => array(), 'links' => array());
  272. $aItems = $this->_oDb->getStatistic();
  273. foreach($aItems as $aItem) {
  274. $iValue = 0;
  275. if(!empty($aItem['query']))
  276. $iValue = (int)$this->_oDb->getOne($aItem['query']);
  277. else if(BxDolRequest::serviceExists($aItem['module'], 'get_query_statistics'))
  278. $iValue = (int)BxDolService::call($aItem['module'], 'get_query_statistics', array($aItem));
  279. $sLink = '';
  280. if(!empty($aItem['link']))
  281. $sLink = BxDolPermalinks::getInstance()->permalink($aItem['link']);
  282. array_push($aValues['labels'], _t($aItem['title']) . ' - ' . $iValue);
  283. array_push($aValues['values'], $iValue);
  284. array_push($aValues['links'], $sLink);
  285. }
  286. $aValues['colors'] = array_slice($this->aColors, 0, count($aValues['values']));
  287. 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']) . '}';
  288. }
  289. if ($Id == 'GrowthByModules'){
  290. $aValues = array('labels' => array(), 'values1' => array(), 'values2' => array(), 'links' => array());
  291. $sModuleName = $sTableName = bx_get('m');
  292. $sModuleTitle = "";
  293. $aTmp = $this->getSelectedModulesGrowth();
  294. if (empty($sModuleName)){
  295. $sModuleName = $sTableName = $aTmp[0][0];
  296. }
  297. $sModuleTitle = $aTmp[1][$sModuleName];
  298. $oModule = BxDolModule::getInstance($sModuleName);
  299. if (isset($oModule->_oConfig->CNF['TABLE_ENTRIES']))
  300. $sTableName = $oModule->_oConfig->CNF['TABLE_ENTRIES'];
  301. $aData = $this->_oDb->getGrowth($sTableName);
  302. $aTmp2 = $this->_oDb->getGrowthInitValue($sTableName);
  303. $iValuePrev = $aTmp2[0];
  304. $iMinTime = $aTmp2[1] * 1000;
  305. $iMaxTime = time() * 1000;
  306. $sGroupBy = getParam('bx_charts_chart_growth_group_by');
  307. foreach ($aData as $aValue) {
  308. $aValue['count1'] = $iValuePrev + $aValue['count'];
  309. $iX = $this->getXValueByParams($aValue, getParam('bx_charts_chart_growth_group_by'));
  310. array_push($aValues['values1'], array('x' => $iX , 'y' => $aValue['count1']));
  311. array_push($aValues['values2'], array('x' => $iX, 'y' => $aValue['count']));
  312. $iValuePrev = $aValue['count1'];
  313. }
  314. echo '{"type":"line", "data": {"datasets": [{"label": "' . $sModuleTitle . ':' . _t('_bx_charts_growth_speed_legend') . '","fill": "false","backgroundColor": "' . $this->aColors[2] . '","borderColor":"' . $this->aColors[2] . '","borderWidth": 1,"data": ' . json_encode($aValues['values2']) . '}, {"label": "' . $sModuleTitle . ':' . _t('_bx_charts_growth_legend') . '","fill": "false","backgroundColor": "' . $this->aColors[3] . '","borderColor": "' . $this->aColors[3] . '","borderWidth": 1,"data": ' . json_encode($aValues['values1']) . '}]}, "options": {"legend": {"position": "bottom"},"scales": {"xAxes": [{"type": "time", "time": {"tooltipFormat": "DD.MM.YYYY", "unit" : "' . ($sGroupBy == 'month' ? 'month' : 'day') . '"}, "ticks": {"min": "' . $iMinTime . '", "max": "' . $iMaxTime . '", "autoSkip": "true"}, "display": "true", "distribution": "linear"}]}}}';
  315. }
  316. }
  317. public function getSelectedModulesGrowth()
  318. {
  319. $aModulesList = $this->serviceGetModules();
  320. $aModules = array_keys($aModulesList);
  321. $sModulesDisabled = explode(',', getParam('bx_charts_chart_growth_modules'));
  322. return array(array_diff($aModules, $sModulesDisabled), $aModulesList);
  323. }
  324. private function getItemName($sString)
  325. {
  326. $sString = strip_tags($sString);
  327. if ($sString == ''){
  328. $sString = _t('_bx_charts_txt_empty_title_item');
  329. }
  330. else{
  331. $sString = strmaxtextlen($sString, 50, '...');
  332. }
  333. return $sString;
  334. }
  335. private function getXValueByParams($aValue, $sMode)
  336. {
  337. switch ($sMode) {
  338. case 'date':
  339. return strtotime($aValue['period']) * 1000;
  340. case 'week':
  341. $mixedTime = strtotime("01.01." . $aValue['year'], time());
  342. $iDay = date('w', $mixedTime);
  343. $mixedTime += ((7 * $aValue['period']) + 1 - $iDay) * 24 * 3600;
  344. return $mixedTime * 1000;
  345. case 'month':
  346. return strtotime('01.' . $aValue['period'] . '.' . $aValue['year']) * 1000;
  347. }
  348. }
  349. }
  350. /** @} */