SidebarService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /* * ********************************************************************
  3. * KerioEmail product developed. (Nov 19, 2018)
  4. * *
  5. *
  6. * CREATED BY THURDATA -> http://thurdata.com
  7. * CONTACT -> contact@thurdata.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ThurData\Servers\KerioEmail\Core\UI\Widget\Sidebar;
  20. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  21. use \ThurData\Servers\KerioEmail\Core\ModuleConstants;
  22. use \ThurData\Servers\KerioEmail\Core\FileReader\Reader;
  23. /**
  24. * Description of SidebarService
  25. *
  26. * @autor ThurData <info@thrudata.ch>
  27. */
  28. class SidebarService
  29. {
  30. use SidebarTrait;
  31. protected $id;
  32. /**
  33. *
  34. * @var Sidebar[]
  35. */
  36. protected $children = [];
  37. public function __construct()
  38. {
  39. $this->load();
  40. }
  41. private function load()
  42. {
  43. if (!file_exists(ModuleConstants::getDevConfigDir() . DS . 'sidebars.yml'))
  44. {
  45. return;
  46. }
  47. $data = Reader::read(ModuleConstants::getDevConfigDir() . DS . 'sidebars.yml');
  48. foreach ($data->get() as $parent => $sidebars)
  49. {
  50. $this->add(new Sidebar($parent));
  51. foreach($sidebars as $id => $sidebar)
  52. {
  53. $sidebarItem = new SidebarItem($id, $sidebar['uri'], $sidebar['order'], $sidebar['target_blank']);
  54. /**
  55. * check if sidebar is active
  56. */
  57. if($sidebarItem->getTitle() === di('request')->get('mg-page'))
  58. {
  59. $sidebarItem->setActive(true);
  60. }
  61. $this->getSidebar($parent)->add($sidebarItem);
  62. }
  63. }
  64. }
  65. /**
  66. *
  67. * @param type $id
  68. * @return Sidebar
  69. * @throws \Exception
  70. */
  71. public function getSidebar($id){
  72. if(!isset($this->children[$id])){
  73. throw new \Exception(sprintf("Sidebar %s does not exist", $id));
  74. }
  75. return $this->children[$id];
  76. }
  77. public function isEmpty(){
  78. return empty($this->children);
  79. }
  80. public function get(){
  81. $children =[];
  82. foreach($this->children as $child){
  83. if(!$child->getOrder()){
  84. $children[]= $child;
  85. continue;
  86. }
  87. $children[$child->getOrder()]= $child;
  88. }
  89. ksort($children);
  90. return $children;
  91. }
  92. }