SidebarService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /* * ********************************************************************
  3. * WordPress Manager product developed. (Nov 19, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.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 ModulesGarden\ProxmoxAddon\Core\UI\Widget\Sidebar;
  20. use \ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  21. use \ModulesGarden\ProxmoxAddon\Core\FileReader\Reader;
  22. /**
  23. * Description of SidebarService
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgardne.com>
  26. */
  27. class SidebarService
  28. {
  29. use SidebarTrait;
  30. protected $id;
  31. /**
  32. *
  33. * @var Sidebar[]
  34. */
  35. protected $children = [];
  36. public function __construct()
  37. {
  38. $this->load();
  39. }
  40. private function load()
  41. {
  42. if (!file_exists(ModuleConstants::getDevConfigDir() . DS . 'sidebars.yml'))
  43. {
  44. return;
  45. }
  46. $data = Reader::read(ModuleConstants::getDevConfigDir() . DS . 'sidebars.yml');
  47. foreach ($data->get() as $parent => $sidebars)
  48. {
  49. $this->add(new Sidebar($parent));
  50. foreach ($sidebars as $id => $sidebar)
  51. {
  52. $this->getSidebar($parent)->add(new SidebarItem($id, $sidebar['uri'], $sidebar['order']));
  53. }
  54. }
  55. }
  56. /**
  57. *
  58. * @param type $id
  59. * @return Sidebar
  60. * @throws \Exception
  61. */
  62. public function getSidebar($id)
  63. {
  64. if (!isset($this->children[$id]))
  65. {
  66. throw new \Exception(sprintf("Sidebar %s does not exist", $id));
  67. }
  68. return $this->children[$id];
  69. }
  70. public function isEmpty()
  71. {
  72. return empty($this->children);
  73. }
  74. public function get()
  75. {
  76. $children = [];
  77. foreach ($this->children as $child)
  78. {
  79. if (!$child->getOrder())
  80. {
  81. $children[] = $child;
  82. continue;
  83. }
  84. $children[$child->getOrder()] = $child;
  85. }
  86. ksort($children);
  87. return $children;
  88. }
  89. }