AddEmailAliasDataProvider.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAlias\Providers;
  3. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  4. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  5. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  6. /**
  7. *
  8. * Created by PhpStorm.
  9. * User: ThurData
  10. * Date: 18.09.19
  11. * Time: 11:50
  12. * Class AddEmailAliasDataProvider
  13. */
  14. class AddEmailAliasDataProvider extends BaseDataProvider
  15. {
  16. public function read()
  17. {
  18. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  19. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  20. try {
  21. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  22. $domainsID = $api->getDomainId($this->data['domain']);
  23. } catch (KerioApiException $error) {
  24. logModuleCall(
  25. 'kerioEmail',
  26. __FUNCTION__,
  27. $error,
  28. 'Debug Error',
  29. $error->getMessage()
  30. );
  31. return ['error' => $error->getMessage()];
  32. }
  33. $fields = array(
  34. "id",
  35. "loginName"
  36. );
  37. $accounts = $api->getUsers($fields,$domainID);
  38. $api->logout();
  39. /**
  40. * available accounts
  41. */
  42. foreach($accounts as $account)
  43. {
  44. $this->availableValues['mailbox'][$account['loginName']] = $account['loginName'] . '@' . $this->data['domain'];
  45. }
  46. }
  47. public function create()
  48. {
  49. $domain = $this->getWhmcsParamByKey('domain');
  50. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  51. try {
  52. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  53. $domainID = $api->getDomainId($domain);
  54. } catch (KerioApiException $error) {
  55. logModuleCall(
  56. 'kerioEmail',
  57. __FUNCTION__,
  58. $error,
  59. 'Debug Error',
  60. $error->getMessage()
  61. );
  62. return ['error' => $error->getMessage()];
  63. }
  64. try {
  65. $result = $api->createAlias($domainID, $this->formData['aliasName'], $this->formData['mailbox'] . '@' . $domain);
  66. } catch (KerioApiException $error) {
  67. logModuleCall(
  68. 'kerioEmail',
  69. __FUNCTION__,
  70. $error,
  71. 'Debug Error',
  72. $error->getMessage()
  73. );
  74. return ['error' => $error->getMessage()];
  75. }
  76. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAliasHasBeenCreated')->setStatusSuccess();
  77. }
  78. public function update()
  79. {
  80. // TODO: Implement update() method.
  81. }
  82. }