Aliases.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAlias\Pages;
  3. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  4. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\AccountHelper;
  5. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\AccountAlias;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  8. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAlias\Buttons\AddEmailAliasButton;
  9. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAlias\Buttons\DeleteEmailAliasButton;
  10. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAlias\Buttons\MassDeleteEmailAliasButton;
  11. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  12. use ThurData\Servers\KerioEmail\Core\UI\Interfaces\ClientArea;
  13. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\Column;
  14. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\DataProvider;
  15. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
  16. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataTable;
  17. use ThurData\Servers\KerioEmail\Core\UI\Widget\Others\Label;
  18. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  19. /**
  20. *
  21. * Created by PhpStorm.
  22. * User: ThurData
  23. * Date: 18.09.19
  24. * Time: 11:46
  25. * Class Aliases
  26. */
  27. class Aliases extends DataTable implements ClientArea
  28. {
  29. protected $id = 'emailAliases';
  30. protected $name = 'emailAliases';
  31. protected $title = null;
  32. protected function loadHtml()
  33. {
  34. $this
  35. ->addColumn((new Column('email_alias'))
  36. ->setOrderable(DataProvider::SORT_ASC)
  37. ->setSearchable(true, Column::TYPE_STRING))
  38. ->addColumn((new Column('account'))
  39. ->setOrderable()
  40. ->setSearchable(true));
  41. }
  42. public function initContent()
  43. {
  44. $this->addMassActionButton(new MassDeleteEmailAliasButton());
  45. $addEmailAliasesButton = new AddEmailAliasButton();
  46. if($this->isCreateButtonDisabled())
  47. {
  48. $addEmailAliasesButton->addHtmlAttribute('disabled', true);
  49. }
  50. $this->addButton($addEmailAliasesButton);
  51. $this->addActionButton(new DeleteEmailAliasButton());
  52. }
  53. public function loadData()
  54. {
  55. /**
  56. * load hosting
  57. */
  58. $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();
  59. $fields = array(
  60. "id",
  61. "name",
  62. "deliverToId",
  63. "deliverTo",
  64. "type");
  65. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  66. try {
  67. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  68. $domains = $api->getDomains(['id','name']);
  69. } catch (KerioApiException $error) {
  70. logModuleCall(
  71. 'kerioEmail',
  72. __FUNCTION__,
  73. $error,
  74. 'Debug Error',
  75. $error->getMessage()
  76. );
  77. return ['error' => $error->getMessage()];
  78. }
  79. foreach($domains as $maildomain) {
  80. if(($maildomain['name']) === $this->getWhmcsParamByKey('domain')){
  81. $this->maildomainID = $maildomain['id'];
  82. $this->maildomain = $maildomain['name'];
  83. }
  84. }
  85. try {
  86. $aliases = $api->getAliases($fields,$this->maildomainID);
  87. } catch (KerioApiException $error) {
  88. logModuleCall(
  89. 'kerioEmail',
  90. __FUNCTION__,
  91. $error,
  92. 'Debug Error',
  93. $error->getMessage()
  94. );
  95. return ['error' => $error->getMessage()];
  96. }
  97. logModuleCall(
  98. 'kerioEmail',
  99. __FUNCTION__,
  100. $this->maildomainID,
  101. 'Debug Error',
  102. $aliases
  103. );
  104. $api->logout();
  105. /*
  106. $aliases = (new KerioManager())
  107. ->getApiByServer($hosting->server)
  108. ->soap->repository()
  109. ->accounts
  110. ->getAccountAliasesByDomainName($hosting->domain);
  111. */
  112. /**
  113. * format model to array
  114. */
  115. $data = [];
  116. // foreach ($aliases as $alias)
  117. // {
  118. // /* @var $alias AccountAlias */
  119. // $tmp = [
  120. // 'id' => base64_encode(json_encode(['alias' => $alias->getAlias(), 'accId' => $alias->getAccountId()])),
  121. // 'email_alias' => $alias->getAlias(),
  122. // 'account' => $alias->getAccountName()
  123. // ];
  124. // $data[] = $tmp;
  125. // }
  126. $dataProv = new ArrayDataProvider();
  127. $dataProv->setDefaultSorting('account', 'ASC')->setData($data);
  128. $this->setDataProvider($dataProv);
  129. }
  130. /**
  131. *
  132. * check if add button should be disabled
  133. * @return bool
  134. */
  135. private function isCreateButtonDisabled()
  136. {
  137. $hid = $this->getRequestValue('id');
  138. /**
  139. * hosting model
  140. */
  141. $hosting = Hosting::where('id', $hid)->first();
  142. // $accounts =(new KerioManager())
  143. // ->getApiByHosting($hid)
  144. // ->soap
  145. // ->repository()
  146. // ->accounts
  147. // ->getByDomainName($hosting->domain);
  148. return !(count($accounts) > 0);
  149. }
  150. }