PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/report/lib/visualconstructor/views/component/base.php

https://gitlab.com/alexprowars/bitrix
PHP | 248 lines | 161 code | 26 blank | 61 comment | 8 complexity | 396eb5fda30ebc194efc5425bbc0836e MD5 | raw file
  1. <?php
  2. namespace Bitrix\Report\VisualConstructor\Views\Component;
  3. use Bitrix\Report\VisualConstructor\Config\Common;
  4. use Bitrix\Report\VisualConstructor\Entity\Widget;
  5. use Bitrix\Report\VisualConstructor\Helper\Widget as WidgetHelper;
  6. use Bitrix\Report\VisualConstructor\View;
  7. /**
  8. * Base class for component content type for widgets in report dashboard.
  9. *
  10. * @package Bitrix\Report\VisualConstructor\Views\Component
  11. */
  12. abstract class Base extends View
  13. {
  14. private $componentName;
  15. private $componentTemplateName = '';
  16. private $componentParameters;
  17. /**
  18. * Base component type view constructor.
  19. */
  20. public function __construct()
  21. {
  22. $this->setJsClassName('BX.Report.Dashboard.Content.Html');
  23. }
  24. /**
  25. * @return mixed
  26. */
  27. public function getComponentName()
  28. {
  29. return $this->componentName;
  30. }
  31. /**
  32. * Setter for component name.
  33. *
  34. * @param string $componentName Component name.
  35. * @return void
  36. */
  37. public function setComponentName($componentName)
  38. {
  39. $this->componentName = $componentName;
  40. }
  41. /**
  42. * @return string
  43. */
  44. public function getComponentParameters()
  45. {
  46. return $this->componentParameters;
  47. }
  48. /**
  49. * Component parameters setter.
  50. *
  51. * @param array $componentParameters Parameters which pass to component.
  52. * @return void
  53. */
  54. public function setComponentParameters($componentParameters)
  55. {
  56. $this->componentParameters = $componentParameters;
  57. }
  58. /**
  59. * @param $key
  60. * @param $value
  61. */
  62. public function addComponentParameters($key, $value)
  63. {
  64. $this->componentParameters[$key] = $value;
  65. }
  66. /**
  67. * Handle all data prepared for this view.
  68. *
  69. * @param array $calculatedPerformedData Performed data from report handler.
  70. * @return array
  71. */
  72. public function handlerFinallyBeforePassToView($calculatedPerformedData)
  73. {
  74. $result['data'] = $calculatedPerformedData;
  75. return $result;
  76. }
  77. /**
  78. * Method to modify Content which pass to widget view, in absolute end.
  79. *
  80. * @param Widget $widget Widget entity.
  81. * @param bool $withCalculatedData Marker for calculate or no data in widget.
  82. * @return array
  83. */
  84. public function prepareWidgetContent(Widget $widget, $withCalculatedData = false)
  85. {
  86. $resultWidget = parent::prepareWidgetContent($widget, $withCalculatedData);
  87. if (!$withCalculatedData)
  88. {
  89. return $resultWidget;
  90. }
  91. if ($withCalculatedData)
  92. {
  93. $resultWidget['content']['params']['color'] = $widget->getWidgetHandler()->getFormElement('color')->getValue();
  94. }
  95. try
  96. {
  97. $result = $this->getCalculatedPerformedData($widget, $withCalculatedData);
  98. }
  99. catch (\Throwable $exception)
  100. {
  101. $result = [];
  102. $error = $exception->getMessage();
  103. }
  104. if (!empty($result['data']) && static::MAX_RENDER_REPORT_COUNT > 1)
  105. {
  106. foreach ($result['data'] as $num => &$reportResult)
  107. {
  108. if (!isset($reportResult['config']['color']))
  109. {
  110. $reportResult['config']['color'] = $widget->getWidgetHandler()->getReportHandlers()[$num]->getFormElement('color')->getValue();
  111. }
  112. if (!isset($reportResult['config']['title']))
  113. {
  114. $reportResult['title'] = $widget->getWidgetHandler()->getReportHandlers()[$num]->getFormElement('label')->getValue();
  115. }
  116. else
  117. {
  118. $reportResult['title'] = $reportResult['config']['title'];
  119. }
  120. }
  121. }
  122. elseif (!empty($result['data']))
  123. {
  124. $reportResult['config']['color'] = $widget->getWidgetHandler()->getReportHandlers()[0]->getFormElement('color')->getValue();
  125. $reportResult['title'] = $widget->getWidgetHandler()->getReportHandlers()[0]->getFormElement('label')->getValue();
  126. }
  127. $this->addComponentParameters('WIDGET', $widget);
  128. $this->addComponentParameters('RESULT', $result);
  129. if (!isset($error))
  130. {
  131. $componentResult = $this->includeComponent();
  132. $resultWidget['content']['params']['html'] = $componentResult['html'];
  133. $resultWidget['content']['params']['css'] = $componentResult['css'];
  134. $resultWidget['content']['params']['js'] = $componentResult['js'];
  135. }
  136. else
  137. {
  138. $errorResult = static::GetErrorHTML($error);
  139. $resultWidget['content']['params']['html'] = $errorResult['html'];
  140. $resultWidget['content']['params']['css'] = $errorResult['css'];
  141. $resultWidget['content']['params']['js'] = $errorResult['js'];
  142. }
  143. return $resultWidget;
  144. }
  145. protected static function GetErrorHTML($errorText)
  146. {
  147. global $APPLICATION;
  148. ob_start();
  149. ShowError($errorText);
  150. $result['html'] = ob_get_clean();;
  151. $result['js'] = $APPLICATION->arHeadScripts;
  152. $result['css'] = $APPLICATION->sPath2css;
  153. foreach ($result['js'] as $key => $value)
  154. {
  155. $result['js'][$key] = \CUtil::GetAdditionalFileURL($value);
  156. }
  157. foreach ($result['css'] as $key => $value)
  158. {
  159. $result['css'][$key] = \CUtil::GetAdditionalFileURL($value);
  160. }
  161. return $result;
  162. }
  163. /**
  164. * Get calculated and format data.
  165. *
  166. * @param Widget $widget Widget Entity.
  167. * @param bool $withCalculatedData Marker for calculate or no data in widget.
  168. * @return array|null
  169. */
  170. protected function getCalculatedPerformedData(Widget $widget, $withCalculatedData)
  171. {
  172. static $data;
  173. if (!$data)
  174. {
  175. $data = $withCalculatedData ? WidgetHelper::getCalculatedPerformedData($this, $widget) : array();
  176. $data = $this->handlerFinallyBeforePassToView($data);
  177. }
  178. return $data;
  179. }
  180. /**
  181. * @param $componentName
  182. * @param array $params
  183. * @return mixed
  184. */
  185. private function includeComponent()
  186. {
  187. global $APPLICATION;
  188. ob_start();
  189. $APPLICATION->IncludeComponent(
  190. $this->getComponentName(),
  191. $this->getComponentTemplateName(),
  192. $this->getComponentParameters()
  193. );
  194. $componentContent = ob_get_clean();
  195. $result['html'] = $componentContent;
  196. $result['js'] = $APPLICATION->arHeadScripts;
  197. $result['css'] = $APPLICATION->sPath2css;
  198. foreach ($result['js'] as $key => $value)
  199. {
  200. $result['js'][$key] = \CUtil::GetAdditionalFileURL($value);
  201. }
  202. foreach ($result['css'] as $key => $value)
  203. {
  204. $result['css'][$key] = \CUtil::GetAdditionalFileURL($value);
  205. }
  206. return $result;
  207. }
  208. /**
  209. * @return string
  210. */
  211. public function getComponentTemplateName()
  212. {
  213. return $this->componentTemplateName;
  214. }
  215. /**
  216. * @param string $componentTemplateName
  217. */
  218. public function setComponentTemplateName($componentTemplateName)
  219. {
  220. $this->componentTemplateName = $componentTemplateName;
  221. }
  222. }