TemplatesDataTable.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /* * ********************************************************************
  3. * WordPress Manager product developed. (Feb 5, 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\App\UI\Templates\Pages;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use ModulesGarden\ProxmoxAddon\App\UI\Templates\Buttons\CreateButton;
  23. use ModulesGarden\ProxmoxAddon\App\UI\Templates\Buttons\DeleteButton;
  24. use ModulesGarden\ProxmoxAddon\App\UI\Templates\Buttons\DeleteMassButton;
  25. use ModulesGarden\ProxmoxAddon\Core\FileReader\Reader\Json;
  26. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  27. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  28. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
  29. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\RawDataTable\RawDataTable;
  30. /**
  31. * Description of PluginInstalled
  32. *
  33. * @author Pawel Kopec <pawelk@modulesgardne.com>
  34. */
  35. class TemplatesDataTable extends RawDataTable implements AdminArea
  36. {
  37. use main\App\Services\BaseService;
  38. protected $id = 'template';
  39. protected $name = 'template';
  40. protected $title = 'templateTitle';
  41. public function isRawTitle()
  42. {
  43. return false;
  44. }
  45. public function initContent()
  46. {
  47. //Create
  48. $this->addButton(new CreateButton);
  49. //Update
  50. $this->addActionButton(new main\App\UI\Templates\Buttons\UpdateButton());
  51. //Delete
  52. $this->addActionButton(new DeleteButton);
  53. //Delete Mass
  54. $this->addMassActionButton(new DeleteMassButton);
  55. try
  56. {
  57. $this->setServerId($this->getRequestValue('id'))->getApi();
  58. }
  59. catch (\Exception $ex)
  60. {
  61. $this->setInternalAlertMessage($ex->getMessage())
  62. ->setInternalAlertMessageType('danger');
  63. }
  64. }
  65. public function replaceFieldOstype($key, $row)
  66. {
  67. $osType = new Json('ostype.json', ModuleConstants::getFullPath('storage', 'app'));
  68. if (is_null($row['ostype']))
  69. {
  70. return "Unspecified OS";
  71. }
  72. else
  73. {
  74. if ($osType->get($row['ostype']))
  75. {
  76. return $osType->get($row['ostype']);
  77. }
  78. }
  79. return $row['ostype'];
  80. }
  81. public function replaceFieldCiuser($key, $row)
  82. {
  83. if ($row[$key])
  84. {
  85. return $row[$key];
  86. }
  87. return "-";
  88. }
  89. public function replaceFieldDescription($key, $row)
  90. {
  91. if ($row[$key])
  92. {
  93. return $row[$key];
  94. }
  95. return "-";
  96. }
  97. protected function loadHtml()
  98. {
  99. $this->addColumn((new Column('node'))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
  100. ->addColumn((new Column('vmid'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  101. ->addColumn((new Column('name'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  102. ->addColumn((new Column('description'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  103. ->addColumn((new Column('ostype'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  104. ->addColumn((new Column('ciuser'))->setSearchable(true, Column::TYPE_STRING)->setOrderable());
  105. }
  106. protected function loadData()
  107. {
  108. $this->setServerId($this->getRequestValue('id'))->getApi()->setInstance();
  109. $dataProv = new main\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider();
  110. $data = [];
  111. $clusterRepository = new proxmox\repository\ClusterResourcesRepository();
  112. $clusterRepository->findKvmTemplate();
  113. foreach ($clusterRepository->fetch() as $resurce)
  114. {
  115. if ($resurce->getTemplate() != '1')
  116. {
  117. continue;
  118. }
  119. $vm = array_merge($resurce->toArray(), $resurce->getVm()->config());
  120. $data[] = array_merge($vm, ["id" => base64_encode(json_encode($vm))]);
  121. }
  122. $dataProv->setDefaultSorting("node", 'ASC');
  123. $dataProv->setData((array)$data);
  124. $this->setDataProvider($dataProv);
  125. }
  126. }