SidebarAjax.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * do not overwrite this function
  40. * @return type \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\RawDataJsonResponse
  41. */
  42. public function returnAjaxData() {
  43. $this->prepareAjaxData();
  44. $returnData = $this->parseProvidedData();
  45. return (new ResponseTemplates\RawDataJsonResponse($returnData))->setCallBackFunction($this->callBackFunction);
  46. }
  47. protected function parseProvidedData() {
  48. $this->loadLang();
  49. $data = [];
  50. foreach ($this->ajaxMenuElements as $mItem) {
  51. $data[] = [
  52. 'id' => $mItem->getId(),
  53. 'namespace' => $mItem->getNamespace(),
  54. 'icon' => $mItem->getIcon(),
  55. 'href' => method_exists($mItem, 'getHref') ? $mItem->getHref() : null,
  56. 'htmlAtributes' => $mItem->getHtmlAttributes(),
  57. 'class' => $mItem->getClasses(),
  58. 'clickAction' => $this->parseOnClickAction($mItem->getHtmlAttributes()['@click']),
  59. 'title' => $this->lang->tr($this->id, $mItem->getTitle())
  60. ];
  61. }
  62. return $data;
  63. }
  64. public function add($sidebar) {
  65. $this->ajaxMenuElements[$sidebar->getId()] = $sidebar;
  66. if (method_exists($sidebar, 'setParent')) {
  67. $sidebar->setParent($this);
  68. }
  69. return $this;
  70. }
  71. public function parseOnClickAction($actionString) {
  72. if (stripos($actionString, '(') > 0) {
  73. $actions = explode('(', $actionString);
  74. $action = $actions[0];
  75. $paramsString = trim(trim(trim($actions[1], ';'), ')'), "'");
  76. $params = explode(',', $paramsString);
  77. foreach ($params as $key => $param) {
  78. $params[$key] = trim(trim(trim($param), "'"), '"');
  79. }
  80. return ['action' => $action, 'params' => $params];
  81. }
  82. return ['action' => $actionString, 'params' => []];
  83. }
  84. }