Service.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Packages\WhmcsService;
  3. use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\ProductConfig\Pages\ConfigurableOptions;
  4. use ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Exceptions\Exception;
  5. use ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  6. use ModulesGarden\Servers\ProxmoxCloudVps\Core\Models\Whmcs\CustomFieldValue;
  7. use WHMCS\Database\Capsule;
  8. class Service
  9. {
  10. protected $serviceId = null;
  11. protected $service = null;
  12. public function __construct($serviceId = null)
  13. {
  14. $this->setServiceId($serviceId);
  15. $this->loadWhmcsService();
  16. }
  17. /**
  18. * @return null
  19. */
  20. public function getServiceId()
  21. {
  22. return $this->serviceId;
  23. }
  24. public function getProductId()
  25. {
  26. return $this->service->packageid;
  27. }
  28. public function getServiceInstanceId()
  29. {
  30. $customFields = $this->getCustomFieldsValues();
  31. return $customFields['InstanceId']['value'];
  32. }
  33. public function getCustomFieldsValues()
  34. {
  35. $model = new CustomFieldValue();
  36. $fields = $model->select('tblcustomfields.fieldname as name', 'value')
  37. ->join('tblcustomfields', 'tblcustomfields.id', '=', 'fieldid')
  38. ->where('type', 'product')
  39. ->where('tblcustomfields.relid', $this->getProductId())
  40. ->where('tblcustomfieldsvalues.relid', $this->serviceId)->get();
  41. $fields = $fields ? $fields->toArray() : [];
  42. $parsedFields = [];
  43. foreach($fields as $field)
  44. {
  45. $nameParts = explode('|', $field['name']);
  46. $parsedFields[$nameParts[0]] = [
  47. 'rawName' => $nameParts[0],
  48. 'name' => $field['name'],
  49. 'value' => $field['value'],
  50. 'displayName' => $nameParts[1] ? $nameParts[1] : $nameParts[0]
  51. ];
  52. }
  53. return $parsedFields;
  54. }
  55. protected function setServiceId($serviceId = null)
  56. {
  57. $relationId = (int)$serviceId;
  58. if ($relationId <= 0)
  59. {
  60. throw new Exception(ErrorCodesLib::CORE_WS_000001);
  61. }
  62. $this->serviceId = $relationId;
  63. }
  64. protected function loadWhmcsService()
  65. {
  66. $this->service = \WHMCS\Service\Service::find($this->serviceId);
  67. }
  68. public function getConfigurableOptionsValues()
  69. {
  70. $options = Capsule::table('tblhostingconfigoptions')->select(['*', 'tblproductconfigoptionssub.optionname as subname'])
  71. ->join('tblproductconfigoptionssub', 'tblproductconfigoptionssub.id', '=', 'tblhostingconfigoptions.optionid')
  72. ->join('tblproductconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
  73. ->where('tblhostingconfigoptions.relid', $this->serviceId)->get();
  74. if (!($options))
  75. {
  76. return [];
  77. }
  78. $optionsData = [];
  79. foreach ($options as $option)
  80. {
  81. $rawOptionName = $this->configOptionNameToRaw($option->optionname) ? : $option->optionname;
  82. $optionsData[$rawOptionName] = [
  83. 'optionNameRaw' => $rawOptionName,
  84. 'optionName' => $option->optionname,
  85. 'subOptionName' => $option->subname,
  86. 'subOptionNameRaw' => $this->configOptionNameToRaw($option->subname) ? : $option->subname,
  87. 'value' => $option->optiontype == 4 ? $option->qty : $this->configOptionNameToRaw($option->subname) ? : $option->subname
  88. ];
  89. }
  90. return $optionsData;
  91. }
  92. public function configOptionNameToRaw($optionName)
  93. {
  94. if (!is_string($optionName) || trim($optionName) === '' || substr($optionName, 0, 1) === '|')
  95. {
  96. return false;
  97. }
  98. if (strpos($optionName, '|') > 0)
  99. {
  100. $parts = explode('|', $optionName);
  101. return $parts[0];
  102. }
  103. return $optionName;
  104. }
  105. }