AddListDataProvider.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\DistributionList\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\ServiceFactory;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Create\CreateDistributionList;
  9. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  10. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  11. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  12. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  13. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  14. /**
  15. *
  16. * Created by PhpStorm.
  17. * User: ThurData
  18. * Date: 18.09.19
  19. * Time: 13:32
  20. * Class AddListDataProvider
  21. */
  22. class AddListDataProvider extends BaseDataProvider
  23. {
  24. public function read()
  25. {
  26. $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();
  27. $this->data['domain'] = $hosting->domain;
  28. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  29. try {
  30. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  31. $domains = $api->getDomains(['id','name']);
  32. } catch (KerioApiException $error) {
  33. logModuleCall(
  34. 'kerioEmail',
  35. __FUNCTION__,
  36. $error,
  37. 'Debug Error',
  38. $error->getMessage()
  39. );
  40. return ['error' => $error->getMessage()];
  41. }
  42. foreach($domains as $maildomain) {
  43. if(($maildomain['name']) === $this->getWhmcsParamByKey('domain')){
  44. $this->maildomainID = $maildomain['id'];
  45. $this->maildomain = $maildomain['name'];
  46. }
  47. }
  48. $productManager = new ProductManager();
  49. $productManager->loadByHostingId($hosting->id);
  50. $fields = array(
  51. "id",
  52. "loginName");
  53. try {
  54. $accounts = $api->getUsers($fields,$this->maildomainID);
  55. } catch (KerioApiException $error) {
  56. logModuleCall(
  57. 'kerioEmail',
  58. __FUNCTION__,
  59. $error,
  60. 'Debug Error',
  61. $error->getMessage()
  62. );
  63. return ['error' => $error->getMessage()];
  64. }
  65. $api->logout();
  66. /**
  67. *
  68. * load lang
  69. */
  70. $lang = di('lang');
  71. $this->availableValues['moderator']['none'] = $lang->absoluteT('none');
  72. foreach($accounts as $account)
  73. {
  74. $this->availableValues['memberList'][$account['loginName']] = $account['loginName'] . '@' . $this->maildomain;
  75. $this->availableValues['moderator'][$account['loginName']] = $account['loginName'] . '@' . $this->maildomain;
  76. }
  77. /**
  78. * subscription requests
  79. */
  80. $this->availableValues['subscriptionRequest'] = [
  81. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  82. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  83. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  84. ];
  85. /**
  86. * unsubscription requests
  87. */
  88. $this->availableValues['unsubscriptionRequest'] = [
  89. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  90. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  91. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  92. ];
  93. $this->availableValues['lang'] = [ 'de' => $lang->absoluteT('german'), 'en' => $lang->absoluteT('english') ];
  94. if($this->formData)
  95. {
  96. $this->loadReloadedData();
  97. }
  98. }
  99. /**
  100. *
  101. */
  102. public function loadReloadedData()
  103. {
  104. foreach($this->formData as $key => $value)
  105. {
  106. $this->data[$key] = $value;
  107. }
  108. }
  109. public function create()
  110. {
  111. $myFormData = $this->formData;
  112. /**
  113. * custom members
  114. */
  115. $customMembers = explode(',',$this->formData['customMember']);
  116. foreach($this->formData['memberList'] as $member) {
  117. $memberList[] = $member . '@' . $this->getWhmcsParamByKey('domain');
  118. }
  119. $this->formData['memberList'] = $memberList;
  120. foreach($customMembers as $customMember)
  121. {
  122. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  123. {
  124. $this->formData['memberList'][] = $customMember;
  125. }
  126. }
  127. /**
  128. * display name
  129. */
  130. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  131. /**
  132. * reply display name
  133. */
  134. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  135. /**
  136. * hosting id
  137. */
  138. $hid = $this->request->get('id');
  139. logModuleCall(
  140. 'kerioEmail',
  141. __FUNCTION__,
  142. $myFormData,
  143. 'Debug add ML',
  144. $this->formData
  145. );
  146. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenAdded')->setStatusSuccess();
  147. }
  148. public function update()
  149. {
  150. // TODO: Implement update() method.
  151. }
  152. }