VmCleanerDataTable.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /* * ********************************************************************
  3. * WordPress Manager product developed. (Feb 5, 2018)
  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\ProxmoxAddon\App\UI\VmCleaner\Pages;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use ModulesGarden\ProxmoxAddon\App\UI\VmCleaner\Buttons\DeleteButton;
  23. use ModulesGarden\ProxmoxAddon\App\UI\VmCleaner\Buttons\DeleteMassButton;
  24. use ModulesGarden\ProxmoxAddon\Core\Helper;
  25. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  26. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
  27. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\RawDataTable\RawDataTable;
  28. use WHMCS\Database\Capsule;
  29. use WHMCS\Service\Service;
  30. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  31. use Illuminate\Database\Capsule\Manager as DB;
  32. /**
  33. * Description of PluginInstalled
  34. *
  35. * @author Pawel Kopec <pawelk@modulesgardne.com>
  36. */
  37. class VmCleanerDataTable extends RawDataTable implements AdminArea
  38. {
  39. use main\App\Services\BaseService;
  40. protected $id = 'vmCleaner';
  41. protected $name = 'vmCleaner';
  42. protected $title = 'vmCleanerTitle';
  43. private $proxmoxCloudTable;
  44. public function isRawTitle()
  45. {
  46. return false;
  47. }
  48. public function initContent()
  49. {
  50. $this->addActionButton(new DeleteButton);
  51. $this->addMassActionButton(new DeleteMassButton);
  52. try
  53. {
  54. $this->setServerId($this->getRequestValue('id'))->getApi();
  55. }
  56. catch (\Exception $ex)
  57. {
  58. $this->setInternalAlertMessage($ex->getMessage())
  59. ->setInternalAlertMessageType('danger');
  60. }
  61. }
  62. public function replaceFieldVirtualization($key, $row)
  63. {
  64. return Helper\sl('lang')->absoluteT($row['type']);
  65. }
  66. public function replaceFieldStatus($key, $row)
  67. {
  68. switch ($row['status'])
  69. {
  70. case 'stopped':
  71. return '<span class="lu-label lu-label--default lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
  72. case 'running':
  73. return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
  74. break;
  75. case 'unknown':
  76. return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
  77. break;
  78. case 'io-error':
  79. return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
  80. break;
  81. default:
  82. return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
  83. }
  84. }
  85. protected function loadHtml()
  86. {
  87. $this->addColumn((new Column('node'))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
  88. ->addColumn((new Column('vmid'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  89. ->addColumn((new Column('name'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  90. ->addColumn((new Column('virtualization'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  91. ->addColumn((new Column('status'))->setSearchable(true, Column::TYPE_STRING)->setOrderable());
  92. }
  93. protected function loadData()
  94. {
  95. session_write_close();
  96. $this->setServerId($this->getRequestValue('id'))->getApi()->setInstance();
  97. $dataProv = new main\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider();
  98. $data = [];
  99. $clusterRepository = new proxmox\repository\ClusterResourcesRepository();
  100. $this->proxmoxCloudVpsTable = Capsule::schema()->hasTable('ProxmoxAddon_Vm');
  101. foreach ($clusterRepository->fetch() as $resurce)
  102. {
  103. if (!in_array($resurce->getType(), ['qemu', 'lxc']) || $resurce->getTemplate() == '1' || $resurce->getStatus() == "unknown" ||
  104. $this->exist($resurce))
  105. {
  106. continue;
  107. }
  108. $data[] = array_merge($resurce->toArray(), ["id" => base64_encode(json_encode($resurce->toArray()))]);
  109. }
  110. $dataProv->setDefaultSorting("node", 'ASC');
  111. $dataProv->setData((array)$data);
  112. $this->setDataProvider($dataProv);
  113. }
  114. private function exist(proxmox\models\ClusterResource $resurce)
  115. {
  116. //cloud
  117. if ($this->proxmoxCloudVpsTable)
  118. {
  119. if (Capsule::table('ProxmoxAddon_Vm')->where("vmid", $resurce->getVmid())->where("node", $resurce->getNode())->count())
  120. {
  121. return true;
  122. }
  123. }
  124. //vps
  125. $h = 'tblhosting';
  126. $fv = "tblcustomfieldsvalues";
  127. $f = "tblcustomfields";
  128. $query = main\App\Models\Whmcs\Hosting::rightJoin($fv, "{$fv}.relid", "=","{$h}.id")
  129. ->rightJoin($f, "{$f}.id", "=","{$fv}.fieldid")
  130. ->where("{$h}.server", $this->getServerId())
  131. ->whereIn("{$h}.domainstatus", ["Active", "Suspended"])
  132. ->where("{$f}.type", "product")
  133. ->where("{$f}.fieldname", "LIKE", "%vmid")
  134. ->where("{$fv}.value", $resurce->getVmid());
  135. return $query->count() > 0;
  136. }
  137. }