TemplateTrait.php 3.7 KB

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