AddListDataProvider.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. foreach($accounts as $account)
  67. {
  68. $this->availableValues['memberList'][$account['loginName']] = $account['loginName'] . '@' . $this->maildomain;
  69. $this->availableValues['moderator'][$account['loginName']] = $account['loginName'] . '@' . $this->maildomain;
  70. }
  71. /**
  72. *
  73. * load lang
  74. */
  75. $lang = di('lang');
  76. /**
  77. * subscription requests
  78. */
  79. $this->availableValues['subscriptionRequest'] = [
  80. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  81. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  82. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  83. ];
  84. /**
  85. * unsubscription requests
  86. */
  87. $this->availableValues['unsubscriptionRequest'] = [
  88. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  89. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  90. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  91. ];
  92. if($this->formData)
  93. {
  94. $this->loadReloadedData();
  95. }
  96. }
  97. /**
  98. *
  99. */
  100. public function loadReloadedData()
  101. {
  102. foreach($this->formData as $key => $value)
  103. {
  104. $this->data[$key] = $value;
  105. }
  106. }
  107. public function create()
  108. {
  109. $myFormData = $this->formData;
  110. /**
  111. *
  112. * provided aliases
  113. */
  114. $customEmails = explode(',',$this->formData['emailAliases']);
  115. $this->formData['emailAliases'] = [];
  116. foreach($customEmails as $email)
  117. {
  118. if ($email !== '')
  119. {
  120. $this->formData['emailAliases'][] = $email;
  121. }
  122. }
  123. /**
  124. * provided owners
  125. */
  126. $owners = explode(',',$this->formData['owners']);
  127. $this->formData['owners'] = [];
  128. foreach($owners as $email)
  129. {
  130. if ($email !== '')
  131. {
  132. $this->formData['owners'][] = $email;
  133. }
  134. }
  135. /**
  136. * custom members
  137. */
  138. $customMembers = explode(',',$this->formData['customMember']);
  139. foreach($customMembers as $customMember)
  140. {
  141. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  142. {
  143. $this->formData['memberList'][] = $customMember;
  144. }
  145. }
  146. /**
  147. * display name
  148. */
  149. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  150. /**
  151. * reply display name
  152. */
  153. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  154. /**
  155. * hosting id
  156. */
  157. $hid = $this->request->get('id');
  158. logModuleCall(
  159. 'kerioEmail',
  160. __FUNCTION__,
  161. $myFormData,
  162. 'Debug add ML',
  163. $this->formData
  164. );
  165. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenAdded')->setStatusSuccess();
  166. }
  167. public function update()
  168. {
  169. // TODO: Implement update() method.
  170. }
  171. }