TemplateTrait.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Reinstall\Pages;
  3. use MGProvision\Proxmox\v2\repository\ClusterResourcesRepository;
  4. use MGProvision\Proxmox\v2\repository\FileRepository;
  5. use MGProvision\Proxmox\v2\repository\StorageRepository;
  6. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  7. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  8. use ModulesGarden\Servers\ProxmoxVps\App\UI\Reinstall\Buttons\TemplateInstallButton;
  9. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Buttons\ModalActionButtons\BaseCancelButton;
  10. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\Column;
  11. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
  12. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  13. trait TemplateTrait
  14. {
  15. use ApiService;
  16. use ProductService;
  17. public function initContent()
  18. {
  19. $this->setInternalAlertMessage("Select the template for reinstallation. If you proceed, all data located on the virtual machine will be lost.");
  20. //Update
  21. $this->addActionButton(new TemplateInstallButton());
  22. //Cancel
  23. $this->addActionButton(new BaseCancelButton());
  24. }
  25. protected function loadHtml()
  26. {
  27. $this->addColumn((new Column('name'))->setSearchable(true, "string")->setOrderable('ASC'));
  28. }
  29. public function replaceFieldName($key, $row)
  30. {
  31. return sl("lang")->abtr("template", $row[$key]);
  32. }
  33. protected function loadData()
  34. {
  35. $data = [];
  36. $fileRepository = new FileRepository();
  37. $nodes = [$this->vm()->getNode()];
  38. //LXC
  39. if ($this->configuration()->isLxc())
  40. {
  41. $storageRepository = new StorageRepository();
  42. $storageRepository->findByNodes($nodes);
  43. $storageRepository->findEnabed();
  44. $storages = $storageRepository->fetchAsArray();
  45. $fileRepository->findByNodes($nodes)
  46. ->findByStorages($storages);
  47. $fileRepository->findByContent('vztmpl');
  48. foreach ($fileRepository->fetch() as $entity)
  49. {
  50. if ($this->configuration()->isPermissionOsTemplates() && !in_array($entity->getVolid(), $this->configuration()->getPermissionOsTemplates()))
  51. {
  52. continue;
  53. }
  54. $data[] = [
  55. "id" => $entity->getVolid(),
  56. "name" => $entity->getFriendlyName(),
  57. ];
  58. }
  59. }//KVM
  60. else
  61. {
  62. if ($this->configuration()->isQemu())
  63. {
  64. $clusterResurces = new ClusterResourcesRepository();
  65. $clusterResurces->findKvmTemplate();
  66. if (!$this->configuration()->isOsTemplatesInAllNodes())
  67. {
  68. $clusterResurces->findByNodes($this->vm()->getNode());
  69. }
  70. foreach ($clusterResurces->fetchWithUniqueNames($this->vm()->getNode()) as $entity)
  71. {
  72. if ($entity->isCustom() && !$entity->matchName($this->getWhmcsParamByKey("serviceid")))
  73. {
  74. continue;
  75. }
  76. if ($this->configuration()->isPermissionOsTemplates() && !in_array($entity->getName(), $this->configuration()->getPermissionOsTemplates()))
  77. {
  78. continue;
  79. }
  80. $id = "{$entity->getNode()}/{$entity->getVmid()}";
  81. $data[] = [
  82. "id" => $id,
  83. "name" => $entity->getName(),
  84. ];
  85. }
  86. }
  87. }
  88. $dataProv = new ArrayDataProvider();
  89. $dataProv->setDefaultSorting("name", 'ASC');
  90. $dataProv->setData($data);
  91. $this->setDataProvider($dataProv);
  92. }
  93. }