BxChartsModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_growth_by_modules
  195. *
  196. * @code bx_srv('bx_charts', 'get_chart_growth_by_modules', [...]); @endcode
  197. *
  198. * Get Chart Growth By Modules
  199. *
  200. * @return an html for chart.
  201. *
  202. * @see BxChartsModule::serviceGetChartGrowthByModules
  203. */
  204. /**
  205. * @ref bx_charts-get_chart_growth_by_modules "get_chart_growth_by_modules"
  206. */
  207. public function serviceGetChartGrowthByModules()
  208. {
  209. return $this->_oTemplate->getChartGrowth('GrowthByModules', 75);
  210. }
  211. public function actionGetChartData($Id = 0)
  212. {
  213. header('Content-Type: application/json');
  214. if ($Id == 'TopContentsByLikes'){
  215. $aValues = array('labels' => array(), 'values' => array(), 'colors' => array(), 'links' => array());
  216. $aData = $this->_oDb->getTopByLikes();
  217. foreach ($aData as $aValue) {
  218. $oModule = BxDolModule::getInstance($aValue['module']);
  219. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])) . ' (' . $oModule->_aModule['title'].')'. ' - ' . $aValue['value']);
  220. array_push($aValues['values'], $aValue['value']);
  221. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  222. }
  223. $aValues['colors'] = array_slice($this->aColors, 0, count($aValues['values']));
  224. echo json_encode([
  225. "type" => "doughnut",
  226. "data" => [
  227. "labels" => $aValues['labels'],
  228. "datasets" => [[
  229. "data" => $aValues['values'],
  230. "backgroundColor" => $aValues['colors'],
  231. ]],
  232. ],
  233. "options" => [
  234. "plugins" => [
  235. "legend" => [
  236. "position" => "bottom",
  237. ],
  238. ],
  239. ],
  240. "links" => $aValues['links'],
  241. ]);
  242. }
  243. if ($Id == 'MostActiveProfiles'){
  244. $aValues = array('labels' => array(), 'values1' => array(), 'values2' => array(), 'links' => array());
  245. $aData = $this->_oDb->getMostActiveProfiles();
  246. foreach ($aData as $aValue) {
  247. $oModule = BxDolModule::getInstance($aValue['module']);
  248. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])));
  249. array_push($aValues['values1'], $aValue['create_count']);
  250. array_push($aValues['values2'], $aValue['views_count']);
  251. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  252. }
  253. echo json_encode([
  254. "type" => "bar",
  255. "data" => [
  256. "labels" => $aValues['labels'],
  257. "datasets" => [
  258. [
  259. "label" => _t('_bx_charts_most_active_profiles_legend_posts'),
  260. "backgroundColor" => $this->aColors[0],
  261. "borderColor" => $this->aColors[0],
  262. "borderWidth" => 1,
  263. "data" => $aValues['values1'],
  264. ], [
  265. "label" => _t('_bx_charts_most_active_profiles_legend_views'),
  266. "backgroundColor" => $this->aColors[1],
  267. "borderColor" => $this->aColors[1],
  268. "borderWidth" => 1,
  269. "data" => $aValues['values2'],
  270. ]
  271. ]
  272. ],
  273. "options" => [
  274. "plugins" => [
  275. "legend" => [
  276. "position" => "bottom",
  277. ],
  278. ],
  279. ],
  280. "links" => $aValues['links'],
  281. ]);
  282. }
  283. if ($Id == 'MostFollowedProfiles'){
  284. $aValues = array('labels' => array(), 'values' => array(), 'links' => array());
  285. $aData = $this->_oDb->getMostFollowedProfiles();
  286. foreach ($aData as $aValue) {
  287. $oModule = BxDolModule::getInstance($aValue['module']);
  288. array_push($aValues['labels'], $this->getItemName($oModule->serviceGetTitle($aValue['object_id'])));
  289. array_push($aValues['values'], $aValue['followers_count']);
  290. array_push($aValues['links'], $oModule->serviceGetLink($aValue['object_id']));
  291. }
  292. echo json_encode([
  293. "type" => "bar",
  294. "data" => [
  295. "labels" => $aValues['labels'],
  296. "datasets" => [[
  297. "label" => _t('_bx_charts_most_followed_profiles_legend'),
  298. "backgroundColor" => $this->aColors[0],
  299. "borderColor" => $this->aColors[0],
  300. "borderWidth" => 1,
  301. "data" => $aValues['values'],
  302. ]],
  303. ],
  304. "options" => [
  305. "plugins" => [
  306. "legend" => [
  307. "position" => "bottom",
  308. ],
  309. ],
  310. ],
  311. "links" => $aValues['links'],
  312. ]);
  313. }
  314. if ($Id == 'GrowthByModules'){
  315. $aValues = array('labels' => array(), 'values1' => array(), 'values2' => array(), 'links' => array());
  316. $sModuleName = $sTableName = bx_get('m');
  317. $sModuleTitle = "";
  318. $aTmp = $this->getSelectedModulesGrowth();
  319. if (empty($sModuleName)){
  320. $sModuleName = $sTableName = $aTmp[0][0];
  321. }
  322. $sModuleTitle = $aTmp[1][$sModuleName];
  323. $oModule = BxDolModule::getInstance($sModuleName);
  324. if (isset($oModule->_oConfig->CNF['TABLE_ENTRIES']))
  325. $sTableName = $oModule->_oConfig->CNF['TABLE_ENTRIES'];
  326. $aData = $this->_oDb->getGrowth($sTableName);
  327. $aTmp2 = $this->_oDb->getGrowthInitValue($sTableName);
  328. $iValuePrev = $aTmp2[0];
  329. $sGroupBy = getParam('bx_charts_chart_growth_group_by');
  330. foreach ($aData as $aValue) {
  331. $aValue['count1'] = $iValuePrev + $aValue['count'];
  332. $iX = $this->getXValueByParams($aValue, getParam('bx_charts_chart_growth_group_by'));
  333. array_push($aValues['values1'], array('x' => $iX , 'y' => $aValue['count1']));
  334. array_push($aValues['values2'], array('x' => $iX, 'y' => $aValue['count']));
  335. $iValuePrev = $aValue['count1'];
  336. }
  337. echo json_encode([
  338. "type" => "line",
  339. "data" => [
  340. "datasets" => [[
  341. "label" => $sModuleTitle . ':' . _t('_bx_charts_growth_speed_legend'),
  342. "fill" => false,
  343. "backgroundColor" => $this->aColors[2],
  344. "borderColor" => $this->aColors[2],
  345. "borderWidth" => 1,
  346. "data" => $aValues['values2'],
  347. ], [
  348. "label" => $sModuleTitle . ':' . _t('_bx_charts_growth_legend'),
  349. "fill" => false,
  350. "backgroundColor" => $this->aColors[3],
  351. "borderColor" => $this->aColors[3],
  352. "borderWidth" => 1,
  353. "data" => $aValues['values1'],
  354. ]]
  355. ],
  356. "options" => [
  357. "plugins" => [
  358. "legend" => [
  359. "position" => "bottom",
  360. ],
  361. ],
  362. "scales" => [
  363. "x" => [
  364. "type" => "time",
  365. "time" => [
  366. "unit" => $sGroupBy == 'month' ? 'month' : 'day',
  367. ],
  368. ]
  369. ]
  370. ]
  371. ]);
  372. }
  373. }
  374. public function getSelectedModulesGrowth()
  375. {
  376. $aModulesList = $this->serviceGetModules();
  377. $aModules = array_keys($aModulesList);
  378. $sModulesDisabled = explode(',', getParam('bx_charts_chart_growth_modules'));
  379. return array(array_diff($aModules, $sModulesDisabled), $aModulesList);
  380. }
  381. private function getItemName($sString)
  382. {
  383. $sString = strip_tags($sString);
  384. if ($sString == ''){
  385. $sString = _t('_bx_charts_txt_empty_title_item');
  386. }
  387. else{
  388. $sString = strmaxtextlen($sString, 50, '...');
  389. }
  390. return $sString;
  391. }
  392. private function getXValueByParams($aValue, $sMode)
  393. {
  394. switch ($sMode) {
  395. case 'date':
  396. return strtotime($aValue['period']) * 1000;
  397. case 'week':
  398. $mixedTime = strtotime("01.01." . $aValue['year'], time());
  399. $iDay = date('w', $mixedTime);
  400. $mixedTime += ((7 * $aValue['period']) + 1 - $iDay) * 24 * 3600;
  401. return $mixedTime * 1000;
  402. case 'month':
  403. return strtotime('01.' . $aValue['period'] . '.' . $aValue['year']) * 1000;
  404. }
  405. }
  406. }
  407. /** @} */