AddDomainAliasDataProvider.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\DomainAlias\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: 13:39
  12. * Class AddDomainAliasDataProvider
  13. */
  14. class AddDomainAliasDataProvider extends BaseDataProvider
  15. {
  16. public function read()
  17. {
  18. $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. $domainID = $api->getDomainId($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. $api->logout();
  34. $this->data['domainId'] = $domainID;
  35. $this->data['domain'] = $domain;
  36. }
  37. public function update()
  38. {
  39. }
  40. public function create()
  41. {
  42. $maildomain = $this->getWhmcsParamByKey('domain');
  43. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  44. try {
  45. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  46. $domains = $api->getDomains(['id','name','aliasList']);
  47. } catch (KerioApiException $error) {
  48. logModuleCall(
  49. 'kerioEmail',
  50. __FUNCTION__,
  51. $error,
  52. 'Debug Error',
  53. $error->getMessage()
  54. );
  55. return ['error' => $error->getMessage()];
  56. }
  57. foreach($domains as $domain) {
  58. if($domain['name'] === $maildomain){
  59. $domainID = $domain['id'];
  60. $aliasList = $domain['aliasList'];
  61. }
  62. }
  63. array_push($aliasList, $this->formData['alias']);
  64. $attr = array('aliasList' => $aliasList);
  65. try {
  66. $result = $api->modifyDomain($domainID,$attr);
  67. } catch (KerioApiException $error) {
  68. logModuleCall(
  69. 'kerioEmail',
  70. __FUNCTION__,
  71. $error,
  72. 'Debug Error',
  73. $error->getMessage()
  74. );
  75. return ['error' => $error->getMessage()];
  76. }
  77. $api->logout();
  78. return (new HtmlDataJsonResponse())->setMessageAndTranslate('domainAliasHasBeenCreated')->setStatusSuccess();
  79. }
  80. }