OsTemplateSelect.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. use ProductService;
  14. use ApiService;
  15. public function prepareAjaxData() {
  16. session_write_close();
  17. $advancedUser = new AdvancedUserHelper($this->getWhmcsParamByKey('userid'));
  18. if ($this->configuration()->isQemu()) {
  19. $this->loadQemuTemplates();
  20. /* if ($advancedUser->isAdvanced() || $this->configuration()->isPermissionIsoImage())
  21. {
  22. $this->availableValues[] = [
  23. "key" => 'installationFromIso',
  24. "value" => sl('lang')->abtr('Installation From ISO'),
  25. ];
  26. }
  27. if($this->availableValues[0]['key']=="installationFromIso"){
  28. $this->callBackFunction = 'pcOsTemplateLoadShowIsoFields';
  29. } */
  30. }
  31. if ($this->configuration()->isLxc()) {
  32. $this->loadLxcTemplates();
  33. }
  34. if ( $this->configuration()->isPermissionArchive()) {
  35. $this->availableValues[] = [
  36. "key" => 'installationFromArchive',
  37. "value" => sl('lang')->abtr('template','InstallationFromArchive'),
  38. ];
  39. }
  40. if($this->availableValues[0]['key']){
  41. $this->value = $this->availableValues[0]['key'];
  42. }
  43. $this->data['additionalData']['showItemDescription'] = true;
  44. }
  45. private function defaultNode() {
  46. if ($this->getRequestValue('location')) {
  47. return $this->getRequestValue('location');
  48. } else {
  49. return $this->getNode()->getNode();
  50. }
  51. }
  52. private function loadQemuTemplates() {
  53. $clusterResurces = new ClusterResourcesRepository();
  54. $clusterResurces->findKvmTemplate();
  55. //default node
  56. $defaultNode = $this->defaultNode();
  57. //search templates on specific node
  58. if (!$this->configuration()->isOsTemplatesInAllNodes()) {
  59. $clusterResurces->findByNodes($defaultNode);
  60. }
  61. $VmList = $clusterResurces->fetchWithUniqueNames($defaultNode);
  62. usort($VmList, function($a, $b) {return strcmp($a->getName(), $b->getName());});
  63. foreach ($VmList as $entity) {
  64. if ($entity->isCustom() && !$entity->matchName($this->getWhmcsParamByKey("serviceid"))) {
  65. continue;
  66. }
  67. if (!$entity->isCustom() && $this->configuration()->isPermissionOsTemplates() && !in_array($entity->getName(), $this->configuration()->getPermissionOsTemplates())) {
  68. continue;
  69. }
  70. $id = "{$entity->getNode()}/{$entity->getVmid()}";
  71. $name = sl('lang')->abtr('template', $entity->getName());
  72. $description = $entity->getVm()->config()['description'];
  73. if(!$description){
  74. $description = $entity->getName();
  75. }
  76. $description = str_replace('\n','<br />', $description);
  77. $desc = sl('lang')->abtr('template_desc',$description );
  78. $desc = nl2br($desc);
  79. $this->availableValues[] = [
  80. "key" => $id,
  81. "value" => $name,
  82. 'description' => $name != $desc ? $desc : null,
  83. ];
  84. }
  85. }
  86. private function loadLxcTemplates() {
  87. $defaultNode = $this->defaultNode();
  88. $storageRepository = new StorageRepository();
  89. $storageRepository->findByNodes([$defaultNode])->findEnabed();
  90. $fileRepository = new FileRepository();
  91. $fileRepository->setApi($this->api());
  92. $fileRepository->findLxcTemplates();
  93. $fileRepository->findByNodes([$defaultNode]);
  94. $fileRepository->findByStorages($storageRepository->fetchAsArray());
  95. foreach ($fileRepository->fetch() as $file) {
  96. if ($this->configuration()->isPermissionOsTemplates() && !in_array($file->getVolid(), $this->configuration()->getPermissionOsTemplates())) {
  97. continue;
  98. }
  99. $this->availableValues[] = [
  100. "key" => $file->getVolid(),
  101. "value" => sl('lang')->abtr('template', $file->getFriendlyName()),
  102. ];
  103. }
  104. }
  105. }