AbstractAvailableExtensions.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Helper;
  3. use ModulesGarden\ProxmoxAddon\App\Models\Doe\SubmoduleSettings;
  4. use ModulesGarden\ProxmoxAddon\Core\Helper;
  5. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\DomainPricing;
  6. /**
  7. * Description of AbstractAvailableExtensions
  8. *
  9. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  10. */
  11. class AbstractAvailableExtensions
  12. {
  13. protected $submoduleConfigKey = null;
  14. protected $tlds = [];
  15. /**
  16. * @var DomainPricing
  17. */
  18. protected $domainPricing;
  19. public function __construct(DomainPricing $domainPricing)
  20. {
  21. $this->domainPricing = $domainPricing;
  22. }
  23. public function isExist($name)
  24. {
  25. return in_array($name, $this->tlds, true);
  26. }
  27. public function removeTldsById($ids)
  28. {
  29. if (is_array($ids))
  30. {
  31. $names = $this->getDomainPricingById($ids);
  32. foreach ($names as $name)
  33. {
  34. $this->unsetTld($name['tld']);
  35. }
  36. }
  37. else
  38. {
  39. $name = $this->getDomainPricingById($ids);
  40. $this->unsetTld($name['tld']);
  41. }
  42. return $this;
  43. }
  44. protected function getDomainPricingById($ids)
  45. {
  46. if (is_array($ids))
  47. {
  48. return $this->domainPricing
  49. ->select("{$this->domainPricing->getTable()}.extension as tld")
  50. ->whereIn("{$this->domainPricing->getTable()}.id", $ids)
  51. ->get()
  52. ->toArray();
  53. }
  54. else
  55. {
  56. return $this->domainPricing
  57. ->select("{$this->domainPricing->getTable()}.extension as tld")
  58. ->where("{$this->domainPricing->getTable()}.id", "LIKE", (int)$ids)
  59. ->first()
  60. ->toArray();
  61. }
  62. return null;
  63. }
  64. protected function unsetTld($name)
  65. {
  66. if ($key = array_search($name, $this->tlds, true))
  67. {
  68. unset($this->tlds[$key]);
  69. }
  70. return $this;
  71. }
  72. public function removeTldsByName($names)
  73. {
  74. if (is_array($names))
  75. {
  76. foreach ($names as $name)
  77. {
  78. $this->unsetTld($name);
  79. }
  80. }
  81. else
  82. {
  83. $this->unsetTld($names);
  84. }
  85. return $this;
  86. }
  87. public function getAllowedTldList()
  88. {
  89. return array_diff($this->getTlds(), $this->loadDisabledTldList());
  90. }
  91. public function getTlds()
  92. {
  93. return $this->tlds;
  94. }
  95. protected function loadDisabledTldList()
  96. {
  97. $model = Helper\di(SubmoduleSettings::class);
  98. $setting = $model->query()->getQuery()->select('value')
  99. ->where('submodule', '=', $this->submoduleConfigKey)
  100. ->where('setting', '=', 'disabledTlds')->value('value');
  101. if ($setting)
  102. {
  103. return json_decode($setting) ?: [];
  104. }
  105. return [];
  106. }
  107. }