VmsDataTable.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (26.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Pages;
  20. use MGProvision\Proxmox\v2\VmFactory;
  21. use ModulesGarden\ProxmoxAddon\App\Decorators\OsTemplateDecorator;
  22. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  23. use ModulesGarden\ProxmoxAddon\App\Repositories\ModuleSettingRepository;
  24. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  25. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  26. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Buttons\CreateButton;
  27. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Buttons\DeleteButton;
  28. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Buttons\EditButton;
  29. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Buttons\ImportButton;
  30. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Buttons\MigrateButton;
  31. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AdminArea;
  32. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  33. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Buttons\ButtonRedirect;
  34. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
  35. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\QueryDataProvider;
  36. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataTable;
  37. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\isAdmin;
  38. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  39. class VmsDataTable extends DataTable implements ClientArea, AdminArea
  40. {
  41. use ApiService;
  42. use ProductService;
  43. protected $id = 'vmsDataTable';
  44. protected $title = 'vmsDataTable';
  45. protected $searchable = false;
  46. protected $tableLength = "100";
  47. public function initContent()
  48. {
  49. $this->addClass('lu-text-left');
  50. $isAdmin = isAdmin();
  51. $moduleSettings = new ModuleSettingRepository();
  52. //create
  53. if(!$isAdmin){
  54. $createButton = new ButtonRedirect('createVmButton');
  55. $createButton->setShowTitle();
  56. $url ="clientarea.php?action=productdetails&id={$this->getWhmcsParamByKey('serviceid')}&modop=custom&a=management&mg-page=vm&mg-action=create";
  57. $createButton->setRawUrl($url);
  58. $createButton->setIcon('lu-zmdi lu-zmdi-plus');
  59. $createButton->replaceClasses(['lu-btn lu-btn--primary']);
  60. $this->addTitleButton($createButton);
  61. }
  62. //import
  63. if($isAdmin){
  64. $this->addButton(new ImportButton());
  65. }
  66. if($isAdmin && $moduleSettings->isPermissionMigrate()){
  67. //migrate
  68. $this->addActionButton(new MigrateButton());
  69. }
  70. if(!$isAdmin){
  71. //edit
  72. $this->addActionButton(new EditButton());
  73. }
  74. //delete
  75. $this->addActionButton(new DeleteButton());
  76. }
  77. protected function loadHtml()
  78. {
  79. if(isAdmin()){
  80. $this->addColumn((new Column('vmid')))
  81. ->addColumn((new Column('node')));
  82. }
  83. $this->addColumn((new Column('name')))
  84. ->addColumn((new Column('status')))
  85. ->addColumn((new Column('vcpus')))
  86. ->addColumn((new Column('memory')))
  87. ->addColumn((new Column('disk')))
  88. ->addColumn((new Column('osTemplate')));
  89. }
  90. public function replaceFieldStatus($key, $row)
  91. {
  92. try{
  93. //current
  94. if($row->vmid==0){
  95. return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->abtr( "Build") . '</span>';
  96. }
  97. $vmModel = VmModel::ofId($row->id)->firstOrFail();
  98. $this->api();
  99. $status = (new VmFactory())->fromVmModel($vmModel)->status()['status'];
  100. if ($status=='running')
  101. {
  102. return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->abtr($status) . '</span>';
  103. }
  104. return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->abtr($status) . '</span>';
  105. }catch (\Exception $ex){
  106. return '<span class="lu-label lu-label--danger lu-label--status">' . $ex->getMessage() . '</span>';
  107. }
  108. }
  109. public function replaceFieldOsTemplate($key, $row)
  110. {
  111. $json = \json_decode($row->data, true);
  112. if(!$json['osTemplate'] || $json['osTemplate'] =='installationFromIso'){
  113. return sl('lang')->abtr('template', "Installation From ISO Image");
  114. }
  115. $decorator = new OsTemplateDecorator($json['osTemplate']);
  116. return sl('lang')->abtr('template', $decorator->toFriendlyName());
  117. }
  118. public function replaceFieldDisk($key, $row)
  119. {
  120. return sprintf(" %s GB", $row->disk + $row->disks);
  121. }
  122. public function replaceFieldMemory($key, $row)
  123. {
  124. return sprintf(" %s MB", $row->memory);
  125. }
  126. protected function loadData()
  127. {
  128. $query = VmModel::select("id", "name", "vcpus", "memory", "disk","disks", "vmid","data","node")
  129. ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
  130. ->notTemplate()
  131. ->getQuery();
  132. $dataProv = new QueryDataProvider();
  133. $dataProv->setDefaultSorting("name", 'ASC');
  134. $dataProv->setData($query);
  135. $this->setDataProvider($dataProv);
  136. }
  137. public function isViewFooter()
  138. {
  139. return false;
  140. }
  141. public function isViewTopBody()
  142. {
  143. return isAdmin();
  144. }
  145. }