AddEmailAliasDataProvider.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAlias\Providers;
  3. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  4. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\AccountAlias;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  7. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  8. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  9. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  10. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  11. /**
  12. *
  13. * Created by PhpStorm.
  14. * User: ThurData
  15. * Date: 18.09.19
  16. * Time: 11:50
  17. * Class AddEmailAliasDataProvider
  18. */
  19. class AddEmailAliasDataProvider extends BaseDataProvider
  20. {
  21. public function read()
  22. {
  23. /**
  24. * hosting id
  25. */
  26. $hid = $this->getRequestValue('id');
  27. /**
  28. * hosting model
  29. */
  30. $hosting = Hosting::where('id', $hid)->first();
  31. /**
  32. * hosting domain
  33. */
  34. $this->data['domain'] = $hosting->domain;
  35. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  36. try {
  37. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  38. $domains = $api->getDomains(['id','name']);
  39. } catch (KerioApiException $error) {
  40. logModuleCall(
  41. 'kerioEmail',
  42. __FUNCTION__,
  43. $error,
  44. 'Debug Error',
  45. $error->getMessage()
  46. );
  47. return ['error' => $error->getMessage()];
  48. }
  49. foreach($domains as $maildomain) {
  50. if(($maildomain['name']) === $this->getWhmcsParamByKey('domain')){
  51. $this->maildomainID = $maildomain['id'];
  52. $this->maildomain = $maildomain['name'];
  53. }
  54. }
  55. $productManager = new ProductManager();
  56. $productManager->loadByHostingId($hosting->id);
  57. $fields = array(
  58. "id",
  59. "loginName"
  60. );
  61. $accounts = $api->getUsers($fields,$this->maildomainID);
  62. $api->logout();
  63. logModuleCall(
  64. 'kerioEmail',
  65. __FUNCTION__,
  66. $accounts,
  67. 'Debug Aliases',
  68. $hosting->domain
  69. );
  70. /**
  71. * available accounts
  72. */
  73. foreach($accounts as $account)
  74. {
  75. $this->availableValues['mailbox'][$account['id']] = $account['loginName'] . '@' . $this->maildomain;
  76. }
  77. }
  78. public function create()
  79. {
  80. /**
  81. * hosting id
  82. */
  83. $hid = $this->request->get('id');
  84. /**
  85. * product manager allow to check product settings
  86. */
  87. $productManager = new ProductManager();
  88. $productManager->loadByHostingId($hid);
  89. /**
  90. *
  91. * get soap create alias service
  92. * set service configuration
  93. */
  94. $service =(new KerioManager())
  95. ->getApiByHosting($hid)
  96. ->soap
  97. ->service()
  98. ->createAccountAlias()
  99. ->setProductManager($productManager)
  100. ->setFormData($this->formData);
  101. /**
  102. *
  103. * run service
  104. */
  105. $result = $service->run();
  106. /**
  107. *
  108. * return success or error response
  109. */
  110. if(!$result)
  111. {
  112. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  113. }
  114. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAliasHasBeenCreated')->setStatusSuccess();
  115. }
  116. public function update()
  117. {
  118. // TODO: Implement update() method.
  119. }
  120. }