TemplateTrait.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\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\Cloud\ProductService;
  8. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Reinstall\Buttons\TemplateInstallButton;
  9. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
  10. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
  11. use function ModulesGarden\Servers\ProxmoxCloudVps\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. $this->unsetShowTitle();
  20. //Update
  21. $this->addActionButton(new TemplateInstallButton());
  22. }
  23. protected function loadHtml()
  24. {
  25. //name
  26. $this->addColumn((new Column('name'))->setSearchable(true, "string")->setOrderable('ASC'));
  27. //description
  28. if($this->configuration()->isQemu()){
  29. $this->addColumn((new Column('description'))->setSearchable(true, "string"));
  30. }
  31. }
  32. public function replaceFieldName($key, $row)
  33. {
  34. return sl("lang")->abtr("template", $row[$key]);
  35. }
  36. protected function loadData()
  37. {
  38. $data = [];
  39. $fileRepository = new FileRepository();
  40. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  41. $nodes = [$vm->getNode()];
  42. //LXC
  43. if ($this->configuration()->isLxc())
  44. {
  45. $storageRepository = new StorageRepository();
  46. $storageRepository->findByNodes($nodes);
  47. $storageRepository->findEnabed();
  48. $storages = $storageRepository->fetchAsArray();
  49. $fileRepository->findByNodes($nodes)
  50. ->findByStorages($storages);
  51. $fileRepository->findByContent('vztmpl');
  52. foreach ($fileRepository->fetch() as $entity)
  53. {
  54. if ($this->configuration()->isPermissionOsTemplates() && !in_array($entity->getVolid(), $this->configuration()->getPermissionOsTemplates()))
  55. {
  56. continue;
  57. }
  58. $data[] = [
  59. "id" => $entity->getVolid(),
  60. "name" => $entity->getFriendlyName(),
  61. ];
  62. }
  63. }//KVM
  64. else
  65. {
  66. if ($this->configuration()->isQemu())
  67. {
  68. $clusterResurces = new ClusterResourcesRepository();
  69. $clusterResurces->findKvmTemplate();
  70. if (!$this->configuration()->isOsTemplatesInAllNodes())
  71. {
  72. $clusterResurces->findByNodes($vm->getNode());
  73. }
  74. foreach ($clusterResurces->fetchWithUniqueNames($vm->getNode()) as $entity)
  75. {
  76. if ($entity->isCustom() && !$entity->matchName($this->getWhmcsParamByKey("serviceid")))
  77. {
  78. continue;
  79. }
  80. if ($this->configuration()->isPermissionOsTemplates() && !in_array($entity->getName(), $this->configuration()->getPermissionOsTemplates()))
  81. {
  82. continue;
  83. }
  84. $id = "{$entity->getNode()}/{$entity->getVmid()}";
  85. $description = $entity->getVm()->config()['description'];
  86. if(!$description){
  87. $description = '-';
  88. }
  89. $data[] = [
  90. "id" => $id,
  91. "name" => $entity->getName(),
  92. 'description' => sl('lang')->abtr('template_desc' ,$description)
  93. ];
  94. }
  95. }
  96. }
  97. $dataProv = new ArrayDataProvider();
  98. $dataProv->setDefaultSorting("name", 'ASC');
  99. $dataProv->setData($data);
  100. $this->setDataProvider($dataProv);
  101. }
  102. }