PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/report/lib/visualconstructor/helper/dashboard.php

https://gitlab.com/alexprowars/bitrix
PHP | 163 lines | 116 code | 17 blank | 30 comment | 9 complexity | e37f46613a66bcccc8eb9668d3614eb1 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Report\VisualConstructor\Helper;
  3. use Bitrix\Main\Config\Option;
  4. use Bitrix\Main\SystemException;
  5. use Bitrix\Report\VisualConstructor\Entity\Dashboard as DashboardEntity;
  6. use Bitrix\Report\VisualConstructor\Entity\Widget as WidgetEntity;
  7. use Bitrix\Report\VisualConstructor\RuntimeProvider\DefaultBoardProvider;
  8. /**
  9. * Class Dashboard
  10. * @package Bitrix\Report\VisualConstructor\Helper
  11. */
  12. class Dashboard
  13. {
  14. /**
  15. * Try find dashboard with key for this user, if not exist create it from copy of default board and return.
  16. *
  17. * @param string $boardKey Board key.
  18. * @return DashboardEntity
  19. */
  20. public static function getDashboardByKeyForCurrentUser($boardKey)
  21. {
  22. global $USER;
  23. $userId = $USER->getId();
  24. $dashboardForUser = DashboardEntity::loadByBoardKeyAndUserId($boardKey, $userId);
  25. if (!$dashboardForUser)
  26. {
  27. self::renewDefaultDashboard($boardKey);
  28. $defaultDashboard = DashboardEntity::getDefaultBoardWithEverythingByBoardKey($boardKey);
  29. $dashboardForUser = $defaultDashboard->getCopyForCurrentUser();
  30. $dashboardForUser->setVersion('');
  31. $dashboardForUser->setUserId($userId);
  32. $dashboardForUser->save();
  33. }
  34. return $dashboardForUser;
  35. }
  36. /**
  37. * Add this widget to end of all boards with key $boardKey.
  38. * will create new row and place there $widget.
  39. * @param string $boardKey Board key.
  40. * @param WidgetEntity $widget Widget entity.
  41. * @return array
  42. */
  43. public static function addWidgetToDashboardsWithKey($boardKey, WidgetEntity $widget)
  44. {
  45. $dashboards = DashboardEntity::loadByBoardKeyMultiple($boardKey);
  46. $dashboardIds = array();
  47. foreach ($dashboards as $dashboard)
  48. {
  49. $cellId = 'cell_' . randString(4);
  50. $row = Row::getRowDefaultEntity(array(
  51. 'cellIds' => array($cellId)
  52. ));
  53. $widget->setWeight($cellId);
  54. $widget->setBoardId($boardKey);
  55. $row->addWidgets($widget->getCopyForCurrentUser());
  56. $dashboard->addRows($row);
  57. $dashboard->save();
  58. $dashboardIds[] = $dashboard->getId();
  59. }
  60. return $dashboardIds;
  61. }
  62. /**
  63. * This method is for service.
  64. * Find all default dashboards in product.
  65. * Check if version change, then remove dashboard with all nested entities, and isnert new.
  66. *
  67. * @param string $boardKey Board key.
  68. * @throws SystemException
  69. * @return void
  70. */
  71. public static function renewDefaultDashboard($boardKey)
  72. {
  73. $board = new DefaultBoardProvider();
  74. $board->addFilter('boardKey', $boardKey);
  75. $board = $board->execute()->getFirstResult();
  76. if ($board)
  77. {
  78. if (!$board->getVersion())
  79. {
  80. throw new SystemException("To renew default dashboard in db state, version of dashboard should exist");
  81. }
  82. $boardFromDb = DashboardEntity::getDefaultBoardByBoardKey($boardKey);
  83. if ($boardFromDb && $boardFromDb->getVersion() !== $board->getVersion())
  84. {
  85. $boardFromDb->delete();
  86. $board->save();
  87. }
  88. elseif (!$boardFromDb)
  89. {
  90. $board->save();
  91. }
  92. }
  93. }
  94. /**
  95. * @param string $board Key Board key.
  96. * @return bool
  97. */
  98. public static function getBoardModeIsDemo($boardKey)
  99. {
  100. $boardModes = \CUserOptions::GetOption('report_dashboard', 'IS_DEMO_MODE_MARKERS', array());
  101. if (isset($boardModes[$boardKey]))
  102. {
  103. return $boardModes[$boardKey];
  104. }
  105. return self::getBoardCustomDefaultModeIsDemo($boardKey);
  106. }
  107. public static function setBoardModeIsDemo($boardKey, $mode)
  108. {
  109. $boardModes = \CUserOptions::GetOption('report_dashboard', 'IS_DEMO_MODE_MARKERS', array());
  110. $boardModes[$boardKey] = $mode;
  111. \CUserOptions::SetOption('report_dashboard', 'IS_DEMO_MODE_MARKERS', $boardModes);
  112. }
  113. public static function updateBoardCustomDefaultMode($boardKey, $demo = false)
  114. {
  115. if (self::checkBoardCustomDefaultModeIsExist($boardKey))
  116. {
  117. if (self::getBoardCustomDefaultModeIsDemo($boardKey) != $demo)
  118. {
  119. self::setBoardCustomDefaultModeIsDemo($boardKey, $demo);
  120. }
  121. }
  122. else
  123. {
  124. self::setBoardCustomDefaultModeIsDemo($boardKey, $demo);
  125. }
  126. }
  127. private static function setBoardCustomDefaultModeIsDemo($boardKey, $demo = false)
  128. {
  129. $modes = Option::get('report', 'BOARD_CUSTOM_DEFAULT_MODES', serialize(array()));
  130. $modes = unserialize($modes, ['allowed_classes' => false]);
  131. $modes[$boardKey] = $demo ? 1 : 0;
  132. Option::set('report', 'BOARD_CUSTOM_DEFAULT_MODES', serialize($modes));
  133. }
  134. private static function checkBoardCustomDefaultModeIsExist($boardKey)
  135. {
  136. $modes = Option::get('report', 'BOARD_CUSTOM_DEFAULT_MODES', serialize(array()));
  137. $modes = unserialize($modes, ['allowed_classes' => false]);
  138. return isset($modes[$boardKey]);
  139. }
  140. private static function getBoardCustomDefaultModeIsDemo($boardKey)
  141. {
  142. $modes = Option::get('report', 'BOARD_CUSTOM_DEFAULT_MODES', serialize(array()));
  143. $modes = unserialize($modes, ['allowed_classes' => false]);
  144. return !empty($modes[$boardKey]);
  145. }
  146. }