AddListDataProvider.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if($this->formData)
  94. {
  95. $this->loadReloadedData();
  96. }
  97. }
  98. /**
  99. *
  100. */
  101. public function loadReloadedData()
  102. {
  103. foreach($this->formData as $key => $value)
  104. {
  105. $this->data[$key] = $value;
  106. }
  107. }
  108. public function create()
  109. {
  110. $myFormData = $this->formData;
  111. /**
  112. * custom members
  113. */
  114. $customMembers = explode(',',$this->formData['customMember']);
  115. foreach($this->formData['memberList'] as $member) {
  116. $memberList[] = $member . '@' . $this->maildomain;
  117. }
  118. foreach($customMembers as $customMember)
  119. {
  120. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  121. {
  122. $this->formData['memberList'][] = $customMember;
  123. }
  124. }
  125. /**
  126. * display name
  127. */
  128. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  129. /**
  130. * reply display name
  131. */
  132. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  133. /**
  134. * hosting id
  135. */
  136. $hid = $this->request->get('id');
  137. logModuleCall(
  138. 'kerioEmail',
  139. __FUNCTION__,
  140. $memberList,
  141. 'Debug add ML',
  142. $this->formData
  143. );
  144. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenAdded')->setStatusSuccess();
  145. }
  146. public function update()
  147. {
  148. // TODO: Implement update() method.
  149. }
  150. }