EmptyGraph.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\UI\Widget\Graphs;
  3. use ThurData\Servers\KerioEmail\Core\UI\Builder\BaseContainer;
  4. use ThurData\Servers\KerioEmail\Core\UI\Interfaces\AjaxElementInterface;
  5. use \ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception;
  6. use \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  7. use ThurData\Servers\KerioEmail\Core\Models\ModuleSettings\Model;
  8. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates;
  9. use ThurData\Servers\KerioEmail\Core\UI\Widget\Graphs\Models\Data;
  10. use ThurData\Servers\KerioEmail\Core\UI\Widget\Graphs\Models\DataSet;
  11. use \ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Hidden;
  12. use function ThurData\Servers\KerioEmail\Core\Helper\sl;
  13. /**
  14. * Description of EmptyGraph
  15. *
  16. * @autor ThurData <info@thrudata.ch>
  17. */
  18. class EmptyGraph extends BaseContainer implements AjaxElementInterface
  19. {
  20. use \ThurData\Servers\KerioEmail\Core\UI\Traits\TableRowCol;
  21. use \ThurData\Servers\KerioEmail\Core\UI\Traits\Icon;
  22. use \ThurData\Servers\KerioEmail\Core\UI\Traits\TitleButtons;
  23. const GRAPH_FILTER_TYPE_INT = 'int';
  24. const GRAPH_FILTER_TYPE_STRING = 'string';
  25. const GRAPH_FILTER_TYPE_DATE = 'date';
  26. const GRAPH_OPTIONS_MODE_POINT = 'point';
  27. const GRAPH_OPTIONS_MODE_NEAREST = 'nearest';
  28. const GRAPH_OPTIONS_MODE_INDEX = 'index';
  29. const GRAPH_OPTIONS_MODE_DATASET = 'dataset';
  30. const GRAPH_OPTIONS_MODE_X = 'x';
  31. const GRAPH_OPTIONS_MODE_Y = 'y';
  32. protected $id = 'emptyGraph';
  33. protected $name = 'emptyGraph';
  34. protected $title = 'title';
  35. protected $configCharts = [
  36. 'type' => '',
  37. 'data' => [],
  38. 'dataParse' => [],
  39. 'options' => [],
  40. 'filter' => []
  41. ];
  42. protected $tooltips = [
  43. 'mode' => self::GRAPH_OPTIONS_MODE_INDEX
  44. ];
  45. protected $responsive = true;
  46. protected $responsiveAnimationDuration = null;
  47. protected $maintainAspectRatio = null;
  48. protected $onResize = null;
  49. protected $filterInfo = [
  50. 'displayEditColor' => true,
  51. 'type' => self::GRAPH_FILTER_TYPE_INT,
  52. 'default' => [
  53. 'start' => 0,
  54. 'end' => 100
  55. ]
  56. ];
  57. protected $animation = [];
  58. protected $layout = [];
  59. protected $scales = [
  60. 'xAxes' => [
  61. [
  62. 'ticks' => [
  63. 'beginAtZero' => true
  64. ]
  65. ]
  66. ]
  67. ];
  68. protected $legend = [
  69. 'display' => true,
  70. 'position' => 'top'
  71. ];
  72. protected $titleGraph = [
  73. 'display' => false,
  74. 'text' => '',
  75. 'position' => 'top'
  76. ];
  77. protected $graphWidth = 1200;
  78. protected $graphHeight = 400;
  79. protected $vueComponent = true;
  80. protected $defaultVueComponentName = 'mg-graph';
  81. protected $graphData = null;
  82. protected $graphSettingsKey = null;
  83. protected $graphSettingsEnabled = false;
  84. protected $configurationFields = [];
  85. public function returnAjaxData()
  86. {
  87. if ($this->getRequestValue('loadData', false) === 'settingButton')
  88. {
  89. $settingButton = $this->titleButtons['settingButton'];
  90. return $settingButton->returnAjaxData();
  91. }
  92. $this->loadData();
  93. $this->setTitleText(sl('lang')->T($this->name));
  94. $this->reloadConfigCharts();
  95. $this->addColorForLabels();
  96. $this->useFilter();
  97. return (new ResponseTemplates\RawDataJsonResponse($this->configCharts))->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds);
  98. }
  99. protected function disableEditColor()
  100. {
  101. $this->filterInfo['displayEditColor'] = false;
  102. return $this;
  103. }
  104. protected function enableEditColor()
  105. {
  106. $this->filterInfo['displayEditColor'] = true;
  107. return $this;
  108. }
  109. protected function setGraphFilterInfo($type = self::GRAPH_FILTER_TYPE_INT, $defaultStart = 0, $defaultStop = 100)
  110. {
  111. if ($type !== null && in_array($type, [self::GRAPH_FILTER_TYPE_INT, self::GRAPH_FILTER_TYPE_DATE, self::GRAPH_FILTER_TYPE_STRING], true))
  112. {
  113. $this->filterInfo['type'] = $type;
  114. }
  115. if ($defaultStart !== null)
  116. {
  117. $this->filterInfo['default']['start'] = $defaultStart;
  118. }
  119. if ($defaultStop !== null)
  120. {
  121. $this->filterInfo['default']['end'] = $defaultStop;
  122. }
  123. return $this;
  124. }
  125. protected function getActionId()
  126. {
  127. return $this->getRequestValue('index', false);
  128. }
  129. protected function reloadConfigCharts()
  130. {
  131. if (is_array($this->animation) && count($this->animation) > 0)
  132. {
  133. $this->configCharts['options']['animation'] = $this->animation;
  134. }
  135. elseif (is_string($this->animation) && strlen($this->animation) > 0)
  136. {
  137. $this->configCharts['options']['animation'] = json_decode($this->animation, true);
  138. }
  139. if (is_array($this->layout) && count($this->layout) > 0)
  140. {
  141. $this->configCharts['options']['layout'] = $this->layout;
  142. }
  143. if (is_array($this->legend) && count($this->legend) > 0)
  144. {
  145. $this->configCharts['options']['legend'] = $this->legend;
  146. }
  147. if (is_array($this->titleGraph) && count($this->titleGraph) > 0)
  148. {
  149. $this->configCharts['options']['title'] = $this->titleGraph;
  150. }
  151. if (is_array($this->scales) && count($this->scales) > 0)
  152. {
  153. $this->configCharts['options']['scales'] = $this->scales;
  154. }
  155. if (is_array($this->filterInfo) && count($this->filterInfo) > 0)
  156. {
  157. $this->configCharts['filter'] = $this->filterInfo;
  158. }
  159. if ($this->responsive !== null)
  160. {
  161. $this->configCharts['options']['responsive'] = $this->responsive;
  162. }
  163. if ($this->responsiveAnimationDuration!== null)
  164. {
  165. $this->configCharts['options']['responsiveAnimationDuration'] = $this->responsiveAnimationDuration;
  166. }
  167. if ($this->maintainAspectRatio!== null)
  168. {
  169. $this->configCharts['options']['maintainAspectRatio'] = $this->maintainAspectRatio;
  170. }
  171. if ($this->onResize !== null)
  172. {
  173. $this->configCharts['options']['onResize'] = $this->onResize;
  174. }
  175. return $this;
  176. }
  177. protected function setChartScales(array $scales = [])
  178. {
  179. $this->scales = $scales;
  180. return $this;
  181. }
  182. protected function addChartScale($key = null, array $scales = [])
  183. {
  184. if (trim($key) !== '' && is_string($key) && !isset($this->scales[$key]))
  185. {
  186. $this->scales[$key] = $scales;
  187. }
  188. return $this;
  189. }
  190. protected function updateChartScale($key = null, array $scales = [])
  191. {
  192. if (trim($key) !== '' && is_string($key))
  193. {
  194. $this->scales[$key] = $scales;
  195. }
  196. return $this;
  197. }
  198. protected function setTooltipsMode($tooltipMode = 'index')
  199. {
  200. if (!in_array($tooltipMode, [self::GRAPH_OPTIONS_MODE_DATASET, self::GRAPH_OPTIONS_MODE_INDEX, self::GRAPH_OPTIONS_MODE_NEAREST, self::GRAPH_OPTIONS_MODE_POINT, self::GRAPH_OPTIONS_MODE_X, self::GRAPH_OPTIONS_MODE_Y]))
  201. {
  202. throw new Exception(ErrorCodesLib::CORE_GRA_000001, ['mode' => $tooltipMode]);
  203. }
  204. $this->tooltips['mode'] = $tooltipMode;
  205. return $this;
  206. }
  207. protected function setTitleDisplay($display = true)
  208. {
  209. $this->titleGraph['display'] = (bool)$display;
  210. return $this;
  211. }
  212. protected function setTitlePosition($position = 'top')
  213. {
  214. $this->titleGraph['position'] = (string)$position;
  215. return $this;
  216. }
  217. protected function setTitleFontSize($fontSize = 12)
  218. {
  219. $this->titleGraph['fontSize'] = (int)$fontSize;
  220. return $this;
  221. }
  222. protected function setTitleFontFamily($fontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif")
  223. {
  224. $this->titleGraph['fontFamily'] = (string)$fontFamily;
  225. return $this;
  226. }
  227. protected function setTitleFontColor($fontColor = '#666')
  228. {
  229. $this->titleGraph['fontColor'] = (string)$fontColor;
  230. return $this;
  231. }
  232. protected function setTitleFontStyle($fontStyle = '')
  233. {
  234. $this->titleGraph['fontStyle'] = (string)$fontStyle;
  235. return $this;
  236. }
  237. protected function setTitlePadding($padding = '')
  238. {
  239. $this->titleGraph['padding'] = (string)$padding;
  240. return $this;
  241. }
  242. protected function setTitleLineHeight($lineHeight = 1.2)
  243. {
  244. $this->titleGraph['lineHeight'] = (float)$lineHeight;
  245. return $this;
  246. }
  247. protected function setTitleText($text = '')
  248. {
  249. $this->titleGraph['text'] = (string)$text;
  250. return $this;
  251. }
  252. protected function unsetLegend()
  253. {
  254. $this->legend = [];
  255. return $this;
  256. }
  257. protected function setLegendLabelsBoxWidth($boxWidth = 40)
  258. {
  259. $this->legend['labels']['boxWidth'] = (int)$boxWidth;
  260. return $this;
  261. }
  262. protected function setLegendLabelsFontSize($fontSize = 12)
  263. {
  264. $this->legend['labels']['fontSize'] = (int)$fontSize;
  265. return $this;
  266. }
  267. protected function setLegendLabelsFontStyle($fontStyle = 'normal')
  268. {
  269. $this->legend['labels']['fontStyle'] = (string)$fontStyle;
  270. return $this;
  271. }
  272. protected function setLegendLabelsFontColor($fontColor = '#666') // Demon Color 3:-]
  273. {
  274. $this->legend['labels']['fontColor'] = (string)$fontColor;
  275. return $this;
  276. }
  277. protected function setLegendLabelsFontFamily($fontFamily = "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif") // Demon Color 3:-]
  278. {
  279. $this->legend['labels']['fontFamily'] = (string)$fontFamily;
  280. return $this;
  281. }
  282. protected function setLegendLabelsPadding($padding = 10)
  283. {
  284. $this->legend['labels']['padding'] = (int)$padding;
  285. return $this;
  286. }
  287. protected function setLegendLabelsGenerateLabels($generateLabels = "function () {}")
  288. {
  289. $this->legend['labels']['generateLabels'] = (string)$generateLabels;
  290. return $this;
  291. }
  292. protected function setLegendLabelsFilter($filter = "function () {}")
  293. {
  294. $this->legend['labels']['filter'] = (string)$filter;
  295. return $this;
  296. }
  297. protected function setLegendLabelsUsePointStyle($usePointStyle = false)
  298. {
  299. $this->legend['labels']['usePointStyle'] = (bool)$usePointStyle;
  300. return $this;
  301. }
  302. protected function setLegendReverse($isReverse = false)
  303. {
  304. $this->legend['reverse'] = (bool)$isReverse;
  305. return $this;
  306. }
  307. protected function setLegendOnHover($eventFunction = "function (event, legendItem) {}")
  308. {
  309. $this->legend['onHover'] = (string)$eventFunction;
  310. return $this;
  311. }
  312. protected function setLegendOnClick($eventFunction = "function (event, legendItem) {}")
  313. {
  314. $this->legend['onClick'] = (string)$eventFunction;
  315. return $this;
  316. }
  317. protected function setLegendFullWidth($isFullWidth = true)
  318. {
  319. $this->legend['fullWidth'] = (bool)$isFullWidth;
  320. return $this;
  321. }
  322. protected function setLegendDisplay($isDisplay = true)
  323. {
  324. $this->legend['display'] = (bool)$isDisplay;
  325. return $this;
  326. }
  327. protected function setLegendPosition($position = 'top')
  328. {
  329. $this->legend['position'] = (string)$position;
  330. return $this;
  331. }
  332. protected function unsetLayout()
  333. {
  334. $this->layout = [];
  335. return $this;
  336. }
  337. protected function setLayoutPadding($left = 0, $right = 0, $top = 0, $bottom = 0)
  338. {
  339. $this->layout['padding'] = [
  340. 'left' => $left,
  341. 'right' => $right,
  342. 'top' => $top,
  343. 'bottom'=> $bottom
  344. ];
  345. return $this;
  346. }
  347. protected function setAnimation($animation)
  348. {
  349. $this->animation = $animation;
  350. return $this;
  351. }
  352. protected function addAnimation($event, $animation)
  353. {
  354. if (is_array($this->animation))
  355. {
  356. $this->animation[$event] = $animation;
  357. }
  358. elseif (is_string($this->animation))
  359. {
  360. $animations = json_decode($this->animation, true);
  361. $animations[$event] = $animation;
  362. $this->animation = json_encode($animations);
  363. }
  364. return $this;
  365. }
  366. public function setGraphWidth($graphWidth = 0)
  367. {
  368. if (!is_numeric($graphWidth))
  369. {
  370. throw new Exception(ErrorCodesLib::CORE_GRA_000002, ['width' => $graphWidth], ['width' => $graphWidth]);
  371. }
  372. $this->graphWidth = $graphWidth;
  373. return $this;
  374. }
  375. public function setGraphHeight($graphHeight = 0)
  376. {
  377. if (!is_numeric($graphHeight))
  378. {
  379. throw new Exception(ErrorCodesLib::CORE_GRA_000003, ['height' => $graphHeight], ['height' => $graphHeight]);
  380. }
  381. $this->graphHeight = $graphHeight;
  382. return $this;
  383. }
  384. public function getGraphWidth()
  385. {
  386. return $this->graphWidth;
  387. }
  388. public function getGraphHeight()
  389. {
  390. return $this->graphHeight;
  391. }
  392. public function setChartOptions (array $chartOptions = [])
  393. {
  394. $this->configCharts['options'] = $chartOptions;
  395. return $this;
  396. }
  397. public function updateChartOption($key = null, $value = null)
  398. {
  399. if(is_string($key) && trim($key) !== '')
  400. {
  401. $this->configCharts['options'][$key] = $value;
  402. }
  403. return $this;
  404. }
  405. public function getChartOptions()
  406. {
  407. return $this->configChartsOptions;
  408. }
  409. public function setChartType($type = '')
  410. {
  411. $this->configCharts['type'] = $type;
  412. return $this;
  413. }
  414. public function getChartType()
  415. {
  416. return $this->configCharts['type'];
  417. }
  418. public function setChartTypeToLine()
  419. {
  420. return $this->setChartType('line');
  421. }
  422. public function setChartTypeToBar()
  423. {
  424. return $this->setChartType('bar');
  425. }
  426. public function setChartTypeToRader()
  427. {
  428. return $this->setChartType('radar');
  429. }
  430. public function setChartTypeToPolarArea()
  431. {
  432. return $this->setChartType('polarArea');
  433. }
  434. public function setChartTypeToPie()
  435. {
  436. return $this->setChartType('pie');
  437. }
  438. public function setChartTypeToDoughnut()
  439. {
  440. return $this->setChartType('doughnut');
  441. }
  442. public function setChartTypeToBubble()
  443. {
  444. return $this->setChartType('bubble');
  445. }
  446. public function setChartData($data = [])
  447. {
  448. if(is_object($data) && $data instanceof Data)
  449. {
  450. $this->configCharts['data'] = $data->toArray();
  451. }
  452. else
  453. {
  454. $this->configCharts['data'] = $data;
  455. }
  456. return $this;
  457. }
  458. public function getChartData()
  459. {
  460. return $this->configCharts['data'];
  461. }
  462. protected function loadSettings()
  463. {
  464. $this->configChartsSettings = json_decode(Model::where('setting', $this->graphSettingsKey)->first()->value);
  465. if ($this->configChartsSettings)
  466. {
  467. $this->setGraphFilterInfo(null, $this->configChartsSettings->start, $this->configChartsSettings->end);
  468. }
  469. return $this;
  470. }
  471. protected function useFilter()
  472. {
  473. if ($this->configCharts['data'] && $this->configCharts['data']['labels'] && $this->configCharts['filter']['default']['start'] && $this->configCharts['filter']['default']['end'])
  474. {
  475. $this->configCharts['dataParse'] = $this->configCharts['data'];
  476. $removeIndex = [];
  477. $isRemove = true;
  478. foreach ($this->configCharts['data']['labels'] as $index => $value)
  479. {
  480. if ($value == $this->configCharts['filter']['default']['start'])
  481. {
  482. $isRemove = false;
  483. continue;
  484. }
  485. if ($value == $this->configCharts['filter']['default']['end'])
  486. {
  487. $isRemove = true;
  488. continue;
  489. }
  490. if ($isRemove)
  491. {
  492. $removeIndex[] = $index;
  493. }
  494. }
  495. foreach ($removeIndex as $index)
  496. {
  497. if (isset($this->configCharts['data']['labels'][$index]))
  498. unset($this->configCharts['data']['labels'][$index]);
  499. if (isset($this->configCharts['data']['datasets']))
  500. {
  501. foreach ($this->configCharts['data']['datasets'] as &$dataset)
  502. {
  503. foreach($dataset as $key => $value)
  504. {
  505. if (is_array($value) && isset($dataset[$key][$index]))
  506. unset($dataset[$key][$index]);
  507. }
  508. }
  509. }
  510. }
  511. }
  512. return $this;
  513. }
  514. protected function addColorForLabels()
  515. {
  516. $indexColor = [];
  517. if ($this->configCharts['data'] && $this->configCharts['data']['labels'] && $this->filterInfo['displayEditColor'])
  518. {
  519. foreach ($this->configCharts['data']['labels'] as $labelName)
  520. {
  521. if (isset($this->configChartsSettings->{$labelName}))
  522. {
  523. $indexColor[] = "#" . $this->configChartsSettings->{$labelName};
  524. }
  525. }
  526. if (!$indexColor)
  527. {
  528. return $this;
  529. }
  530. foreach ($this->configCharts['data']['datasets'] as &$dataset)
  531. {
  532. $dataset['backgroundColor'] = $indexColor;
  533. $dataset['borderColor'] = $indexColor;
  534. $dataset['hoverBackgroundColor'] = $indexColor;
  535. $dataset['hoverBorderColor'] = $indexColor;
  536. $dataset['pointBackgroundColor'] = $indexColor;
  537. $dataset['pointBorderColor'] = $indexColor;
  538. $dataset['pointHoverBackgroundColor'] = $indexColor;
  539. $dataset['pointHoverBorderColor'] = $indexColor;
  540. }
  541. }
  542. return $this;
  543. }
  544. /**
  545. *
  546. * @return Data
  547. */
  548. public function generateDataObject()
  549. {
  550. return (new Data());
  551. }
  552. /**
  553. * @return DataSet
  554. */
  555. public function generateDataSetObject()
  556. {
  557. return (new DataSet());
  558. }
  559. public function prepareAjaxData()
  560. {
  561. //to be overwritten
  562. //load the data for graph drawing in this function
  563. }
  564. public function setLabels($labels = [])
  565. {
  566. $this->initData();
  567. if ($labels)
  568. {
  569. $this->graphData->setLabels($labels);
  570. }
  571. return $this;
  572. }
  573. public function initData()
  574. {
  575. if (!$this->graphData)
  576. {
  577. $this->graphData = new Data();
  578. }
  579. return $this;
  580. }
  581. public function addDataSet($dataSet)
  582. {
  583. $this->graphData->addDataSet($dataSet);
  584. return $this;
  585. }
  586. protected function loadData()
  587. {
  588. $this->addGraphSettings();
  589. $this->prepareAjaxData();
  590. $this->setChartData($this->graphData);
  591. }
  592. protected function addGraphSettings()
  593. {
  594. if (!$this->getRequestValue('mgformtype', false) && $this->getRequestValue('ajax', false))
  595. {
  596. return $this;
  597. }
  598. if (!$this->graphSettingsEnabled)
  599. {
  600. return $this;
  601. }
  602. if (!$this->graphSettingsKey)
  603. {
  604. $this->graphSettingsKey = 'graph_' . $this->id;
  605. }
  606. if ($this->configurationFields)
  607. {
  608. $field = new Hidden('setting');
  609. $field->setDefaultValue($this->graphSettingsKey);
  610. $this->addSettingField($field);
  611. }
  612. $btn = (new Settings\SettingButton())
  613. ->setIndex($this->graphSettingsKey)
  614. ->addNamespaceScope($this->namespace)
  615. ->setConfigFields($this->configurationFields);
  616. $this->addTitleButton($btn);
  617. }
  618. public function enableGraphSettings($settingsKey = null)
  619. {
  620. $this->graphSettingsEnabled = true;
  621. if ((string)$settingsKey !== '')
  622. {
  623. $this->graphSettingsKey = (string)$settingsKey;
  624. }
  625. $this->addGraphSettings();
  626. return $this;
  627. }
  628. public function addSettingField($field)
  629. {
  630. if (is_object($field))
  631. {
  632. $this->configurationFields[$field->getId()] = $field;
  633. }
  634. return $this;
  635. }
  636. public function getGraphSettingsKey()
  637. {
  638. return $this->graphSettingsKey;
  639. }
  640. }