OsTemplateSelect.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Fields;
  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\ProxmoxAddon\Core\Helper\AdvancedUserHelper;
  9. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  10. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\AjaxFields\Select;
  11. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  12. class OsTemplateSelect extends Select implements ClientArea
  13. {
  14. use ProductService;
  15. use ApiService;
  16. public function prepareAjaxData()
  17. {
  18. session_write_close();
  19. $advancedUser = new AdvancedUserHelper($this->getWhmcsParamByKey('userid'));
  20. if ($this->configuration()->isQemu())
  21. {
  22. $this->loadQemuTemplates();
  23. if ($advancedUser->isAdvanced() || $this->configuration()->isPermissionIsoImage())
  24. {
  25. $this->availableValues[] = [
  26. "key" => 'installationFromIso',
  27. "value" => sl('lang')->abtr('Installation From ISO'),
  28. ];
  29. }
  30. if($this->availableValues[0]['key']=="installationFromIso"){
  31. $this->callBackFunction = 'pcOsTemplateLoadShowIsoFields';
  32. }
  33. }
  34. if ($this->configuration()->isLxc())
  35. {
  36. $this->loadLxcTemplates();
  37. }
  38. if($this->availableValues[0]['key']){
  39. $this->value = $this->availableValues[0]['key'];
  40. }
  41. if ( $this->configuration()->isPermissionArchive())
  42. {
  43. $this->availableValues[] = [
  44. "key" => 'installationFromArchive',
  45. "value" => sl('lang')->abtr('template','InstallationFromArchive'),
  46. ];
  47. }
  48. $this->data['additionalData']['showItemDescription'] = true;
  49. }
  50. private function defaultNode()
  51. {
  52. if ($this->getRequestValue('location'))
  53. {
  54. return $this->getRequestValue('location');
  55. }
  56. else
  57. {
  58. return $this->getNode()->getNode();
  59. }
  60. }
  61. private function loadQemuTemplates()
  62. {
  63. $clusterResurces = new ClusterResourcesRepository();
  64. $clusterResurces->findKvmTemplate();
  65. //default node
  66. $defaultNode = $this->defaultNode();
  67. //search templates on specific node
  68. if (!$this->configuration()->isOsTemplatesInAllNodes())
  69. {
  70. $clusterResurces->findByNodes($defaultNode);
  71. }
  72. foreach ($clusterResurces->fetchWithUniqueNames($defaultNode) as $entity)
  73. {
  74. if ($entity->isCustom() && !$entity->matchName($this->getWhmcsParamByKey("serviceid")))
  75. {
  76. continue;
  77. }
  78. if ($this->configuration()->isPermissionOsTemplates() && !in_array($entity->getName(), $this->configuration()->getPermissionOsTemplates()))
  79. {
  80. continue;
  81. }
  82. $id = "{$entity->getNode()}/{$entity->getVmid()}";
  83. $name = sl('lang')->abtr('template', $entity->getName());
  84. $description = $entity->getVm()->config()['description'];
  85. if(!$description){
  86. $description = $entity->getName();
  87. }
  88. $desc = sl('lang')->abtr('template_desc',$description );
  89. $this->availableValues[] = [
  90. "key" => $id,
  91. "value" => $name,
  92. 'description' => $name != $desc ? $desc : null,
  93. ];
  94. }
  95. }
  96. private function loadLxcTemplates()
  97. {
  98. $defaultNode = $this->defaultNode();
  99. $storageRepository = new StorageRepository();
  100. $storageRepository->findByNodes([$defaultNode])
  101. ->findEnabed();
  102. $fileRepository = new FileRepository();
  103. $fileRepository->setApi($this->api());
  104. $fileRepository->findLxcTemplates();
  105. $fileRepository->findByNodes([$defaultNode]);
  106. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  107. foreach ($fileRepository->fetch() as $file)
  108. {
  109. if ($this->configuration()->isPermissionOsTemplates() && !in_array($file->getVolid(), $this->configuration()->getPermissionOsTemplates()))
  110. {
  111. continue;
  112. }
  113. $this->availableValues[] = [
  114. "key" => $file->getVolid(),
  115. "value" => sl('lang')->abtr('template', $file->getFriendlyName()),
  116. ];
  117. }
  118. }
  119. }