SidebarService.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVps 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\Servers\ProxmoxVps\Core\UI\Widget\Sidebar;
  20. use \ModulesGarden\Servers\ProxmoxVps\Core\ModuleConstants;
  21. use \ModulesGarden\Servers\ProxmoxVps\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. $this->getSidebar($parent)->add(new SidebarItem($id, $sidebar['uri'], $sidebar['order']));
  52. }
  53. }
  54. }
  55. /**
  56. *
  57. * @param type $id
  58. * @return Sidebar
  59. * @throws \Exception
  60. */
  61. public function getSidebar($id){
  62. if(!isset($this->children[$id])){
  63. throw new \Exception(sprintf("Sidebar %s does not exist", $id));
  64. }
  65. return $this->children[$id];
  66. }
  67. public function isEmpty(){
  68. return empty($this->children);
  69. }
  70. public function get(){
  71. $children =[];
  72. foreach($this->children as $child){
  73. if(!$child->getOrder()){
  74. $children[]= $child;
  75. continue;
  76. }
  77. $children[$child->getOrder()]= $child;
  78. }
  79. ksort($children);
  80. return $children;
  81. }
  82. }