HostingService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace ModulesGarden\Servers\KerioEmail\App\Traits;
  3. use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\CustomField;
  4. use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\CustomFieldValue;
  5. use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  6. /**
  7. *
  8. * Created by PhpStorm.
  9. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  10. * Date: 10.09.19
  11. * Time: 10:43
  12. * Class HostingService
  13. */
  14. trait HostingService
  15. {
  16. /**
  17. * @return mixed
  18. */
  19. public function getHostingId()
  20. {
  21. if (!$this->hostingId)
  22. {
  23. $this->hostingId = $this->getWhmcsParamByKey("serviceid");
  24. }
  25. return $this->hostingId;
  26. }
  27. /**
  28. * @param mixed $hostingId
  29. */
  30. public function setHostingId($hostingId)
  31. {
  32. $this->hostingId = $hostingId;
  33. return $this;
  34. }
  35. /**
  36. * @return Hosting
  37. */
  38. public function hosting()
  39. {
  40. if ($this->hosting instanceof Hosting)
  41. {
  42. return $this->hosting;
  43. }
  44. return $this->hosting = Hosting::where("id", $this->getHostingId())->firstOrFail();
  45. }
  46. public function isActive()
  47. {
  48. return Hosting::ofId($this->getHostingId())->active()->count() == 1;
  49. }
  50. public function isSupportedModule()
  51. {
  52. return Hosting::ofServerType($this->getHostingId(), "proxmoxVPS")->count() == 1;
  53. }
  54. private function getCustomFieldId($fieldName)
  55. {
  56. return CustomField::select("id")
  57. ->where("type", "product")
  58. ->where("relid", $this->hosting()->packageid)
  59. ->where("fieldname", "like", $fieldName . "%")
  60. ->value("id");
  61. }
  62. public function customFieldUpdate($name, $value = '')
  63. {
  64. $fieldId = $this->getCustomFieldId($name);
  65. //Update
  66. if (CustomFieldValue::where('fieldid', $fieldId)->where("relid", $this->getHostingId())->count())
  67. {
  68. return CustomFieldValue::where('fieldid', $fieldId)->where("relid", $this->getHostingId())->update(['value' => $value]);
  69. }
  70. //Create
  71. $customFiledValue = new CustomFieldValue();
  72. $customFiledValue->fill([
  73. 'fieldid' => $fieldId,
  74. 'relid' => $this->getHostingId(),
  75. 'value' => $value
  76. ]);
  77. return $customFiledValue->save();
  78. }
  79. }