OsTemplateSelect.php 4.5 KB

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