ConfigOptions.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\Http\Actions;
  3. use ThurData\Servers\KerioEmail\App\Http\Admin\ProductConfiguration;
  4. use ThurData\Servers\KerioEmail\App\Libs\Restrictions\Restriction;
  5. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Api;
  6. use ThurData\Servers\KerioEmail\App\Traits\ExtensionsCheckerTrait;
  7. use ThurData\Servers\KerioEmail\App\UI\Admin\ProductConfiguration\Pages\ConfigForm;
  8. use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController;
  9. use ThurData\Servers\KerioEmail\Core\Traits\OutputBuffer;
  10. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  11. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product;
  12. /**
  13. *
  14. * Created by PhpStorm.
  15. * User: ThurData
  16. * Date: 28.08.19
  17. * Time: 09:07
  18. * Class ConfigOptions
  19. */
  20. class ConfigOptions extends AddonController
  21. {
  22. use ExtensionsCheckerTrait;
  23. use OutputBuffer;
  24. /**
  25. * @param null $params
  26. * @return array
  27. * @throws \Exception
  28. */
  29. public function execute($params = null)
  30. {
  31. try{
  32. $this->throwErrorIfServerInvalid();
  33. $this->throwErrorIfExtensionsInvalid();
  34. if ($this->getRequestValue('action') === 'module-settings' && !$this->getRequestValue('loadData') )
  35. {
  36. if(empty($this->getRequestValue('magic')))
  37. {
  38. return [
  39. "configoption" => [
  40. "Type" => "",
  41. "Description" => $this->getJsCode(),
  42. ],
  43. ];
  44. }
  45. if (isset($_REQUEST['magic']))
  46. {
  47. $this->cleanOutputBuffer();
  48. return [ProductConfiguration::class, 'index'];
  49. }
  50. return [ProductConfiguration::class, 'index'];
  51. }
  52. elseif ($this->getRequestValue('action') === 'save')
  53. {
  54. $form = new ConfigForm();
  55. $form->runInitContentProcess();
  56. $form->returnAjaxData();
  57. }
  58. elseif (($this->getRequestValue('loadData') && $this->getRequestValue('ajax') == '1'))
  59. {
  60. return [ProductConfiguration::class, 'index'];
  61. }
  62. }catch (\Exception $e)
  63. {
  64. //todo log
  65. throw new \Exception($e->getMessage());
  66. }
  67. }
  68. /**
  69. * check if server is configured properly
  70. * @throws \Exception
  71. */
  72. protected function throwErrorIfServerInvalid()
  73. {
  74. $productId = di('request')->get('id');
  75. $product = Product::where('id', $productId)->first();
  76. if(!$product->servergroup)
  77. {
  78. throw new \Exception(di('lang')->absoluteT('error', 'invalidServer'));
  79. }
  80. }
  81. /**
  82. * check if extension is instlled
  83. * @throws \Exception
  84. */
  85. protected function throwErrorIfExtensionsInvalid()
  86. {
  87. $extensions = [
  88. \SoapClient::class => 'soap'
  89. ];
  90. foreach($extensions as $class => $extension)
  91. {
  92. if(!class_exists($class))
  93. {
  94. throw new \Exception(di('lang')->addReplacementConstant('extension', $extension)->absoluteT('error', 'extensionRequired'));
  95. }
  96. }
  97. }
  98. private function getJsCode()
  99. {
  100. $params = array_merge($this->request->request->all(), $this->request->query->all());
  101. $dataQuery = http_build_query($params);
  102. return "
  103. <script>
  104. $('#layers').remove();
  105. $('.lu-alert').remove();
  106. $('#tblModuleSettings').addClass('hidden');
  107. $('#tblMetricSettings').before('<img style=\"margin-left: 50%; margin-top: 15px; margin-bottom: 15px; height: 20px\" id=\"mg-configoptionLoader\" src=\"images/loading.gif\">');
  108. $.post({
  109. url: '{$_SERVER['HTTP_ORIGIN']}{$_SERVER['PHP_SELF']}?$dataQuery&magic=1'
  110. })
  111. .done(function( data ){
  112. var json = JSON.parse(data);
  113. $('#mg-configoptionLoader').remove();
  114. if ({$this->getRequestValue('servergroup')} == 0)
  115. {
  116. $('#tblModuleSettings').html(json.content).removeClass('hidden');
  117. }
  118. else
  119. {
  120. $('#tblModuleSettings').html(json.content).removeClass('hidden');
  121. }
  122. //
  123. });
  124. </script>";
  125. }
  126. }