DeleteDomainAliasProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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: 02.10.19
  11. * Time: 14:41
  12. * Class DeleteDomainAliasProvider
  13. */
  14. class DeleteDomainAliasProvider extends BaseDataProvider
  15. {
  16. /**
  17. *
  18. */
  19. public function read()
  20. {
  21. $this->data['id'] = $this->actionElementId;
  22. }
  23. /**
  24. *
  25. */
  26. public function update()
  27. {
  28. // TODO: Implement update() method.
  29. }
  30. /**
  31. * @return HtmlDataJsonResponse|void
  32. */
  33. public function delete()
  34. {
  35. $maildomain = $this->getWhmcsParamByKey('domain');
  36. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  37. try {
  38. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  39. $domains = $api->getDomains(['id','name','aliasList']);
  40. } catch (KerioApiException $error) {
  41. logModuleCall(
  42. 'kerioEmail',
  43. __FUNCTION__,
  44. $error,
  45. 'Debug Error',
  46. $error->getMessage()
  47. );
  48. return ['error' => $error->getMessage()];
  49. }
  50. foreach($domains as $domain) {
  51. if($domain['name'] === $maildomain){
  52. $domainID = $domain['id'];
  53. $aliasList = $domain['aliasList'];
  54. }
  55. }
  56. $newList = array_values(array_diff($aliasList, [ $this->formData['id']]));
  57. $attr = array('aliasList' => $newList);
  58. try {
  59. $result = $api->modifyDomain($domainID,$attr);
  60. } catch (KerioApiException $error) {
  61. logModuleCall(
  62. 'kerioEmail',
  63. __FUNCTION__,
  64. $error,
  65. 'Debug Error',
  66. $error->getMessage()
  67. );
  68. return ['error' => $error->getMessage()];
  69. }
  70. $api->logout();
  71. return (new HtmlDataJsonResponse())->setMessageAndTranslate('domainAliasHasBeenDeleted')->setStatusSuccess();
  72. }
  73. public function massDelete()
  74. {
  75. $maildomain = $this->getWhmcsParamByKey('domain');
  76. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  77. try {
  78. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  79. $domains = $api->getDomains(['id','name','aliasList']);
  80. } catch (KerioApiException $error) {
  81. logModuleCall(
  82. 'kerioEmail',
  83. __FUNCTION__,
  84. $error,
  85. 'Debug Error',
  86. $error->getMessage()
  87. );
  88. return ['error' => $error->getMessage()];
  89. }
  90. foreach($domains as $domain) {
  91. if($domain['name'] === $maildomain){
  92. $domainID = $domain['id'];
  93. $aliasList = $domain['aliasList'];
  94. }
  95. }
  96. $newList = array_values(array_diff($aliasList, $this->request->get('massActions')));
  97. $attr = array('aliasList' => $newList);
  98. try {
  99. $result = $api->modifyDomain($domainID,$attr);
  100. } catch (KerioApiException $error) {
  101. logModuleCall(
  102. 'kerioEmail',
  103. __FUNCTION__,
  104. $error,
  105. 'Debug Error',
  106. $error->getMessage()
  107. );
  108. return ['error' => $error->getMessage()];
  109. }
  110. $api->logout();
  111. return (new HtmlDataJsonResponse())
  112. ->setMessageAndTranslate('massDomainAliasHasBeenDeleted')
  113. ->setStatusSuccess();
  114. }
  115. }