OsTemplateSelect.php 4.8 KB

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