AddEmailAliasDataProvider.php 3.8 KB

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