SidebarAjax.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxCloudVps 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\ProxmoxCloudVps\Core\UI\Widget\Sidebar;
  20. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates;
  21. /**
  22. * Description of SidebarAjax
  23. *
  24. * @author Sławomir Miśkowicz <slawomir@modulesgardne.com>
  25. */
  26. class SidebarAjax extends Sidebar implements \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AjaxElementInterface
  27. {
  28. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\Traits\Lang;
  29. protected $id = 'sidebarAjax';
  30. protected $name = 'sidebarAjax';
  31. protected $vueComponent = true;
  32. protected $defaultVueComponentName = 'mg-ajax-sidebar';
  33. protected $ajaxMenuElements = [];
  34. /**
  35. * overwrite this function, use add function to add ajax elements
  36. */
  37. public function prepareAjaxData()
  38. {
  39. }
  40. /**
  41. * do not overwrite this function
  42. * @return type \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\RawDataJsonResponse
  43. */
  44. public function returnAjaxData()
  45. {
  46. $this->prepareAjaxData();
  47. $returnData = $this->parseProvidedData();
  48. return (new ResponseTemplates\RawDataJsonResponse($returnData))->setCallBackFunction($this->callBackFunction);
  49. }
  50. protected function parseProvidedData()
  51. {
  52. $this->loadLang();
  53. $data = [];
  54. foreach ($this->ajaxMenuElements as $mItem)
  55. {
  56. $data[] = [
  57. 'id' => $mItem->getId(),
  58. 'namespace' => $mItem->getNamespace(),
  59. 'icon' => $mItem->getIcon(),
  60. 'href' => method_exists($mItem, 'getHref') ? $mItem->getHref() : null,
  61. 'htmlAtributes' => $mItem->getHtmlAttributes(),
  62. 'class' => $mItem->getClasses(),
  63. 'clickAction' => $this->parseOnClickAction($mItem->getHtmlAttributes()['@click']),
  64. 'title' => $this->lang->tr($this->id, $mItem->getTitle())
  65. ];
  66. }
  67. return $data;
  68. }
  69. public function add($sidebar)
  70. {
  71. $this->ajaxMenuElements[$sidebar->getId()] = $sidebar;
  72. if (method_exists($sidebar, 'setParent'))
  73. {
  74. $sidebar->setParent($this);
  75. }
  76. return $this;
  77. }
  78. public function parseOnClickAction($actionString)
  79. {
  80. if (stripos($actionString, '(') > 0)
  81. {
  82. $actions = explode('(', $actionString);
  83. $action = $actions[0];
  84. $paramsString = trim(trim(trim($actions[1], ';'), ')'), "'");
  85. $params = explode(',', $paramsString);
  86. foreach ($params as $key => $param)
  87. {
  88. $params[$key] = trim(trim(trim($param), "'"), '"');
  89. }
  90. return ['action' => $action, 'params' => $params];
  91. }
  92. return ['action' => $actionString, 'params' => []];
  93. }
  94. }