AddListDataProvider.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /**
  110. *
  111. * provided aliases
  112. */
  113. $customEmails = explode(',',$this->formData['emailAliases']);
  114. $this->formData['emailAliases'] = [];
  115. foreach($customEmails as $email)
  116. {
  117. if ($email !== '')
  118. {
  119. $this->formData['emailAliases'][] = $email;
  120. }
  121. }
  122. /**
  123. * provided owners
  124. */
  125. $owners = explode(',',$this->formData['owners']);
  126. $this->formData['owners'] = [];
  127. foreach($owners as $email)
  128. {
  129. if ($email !== '')
  130. {
  131. $this->formData['owners'][] = $email;
  132. }
  133. }
  134. /**
  135. * custom members
  136. */
  137. $customMembers = explode(',',$this->formData['customMember']);
  138. foreach($customMembers as $customMember)
  139. {
  140. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  141. {
  142. $this->formData['memberList'][] = $customMember;
  143. }
  144. }
  145. /**
  146. * display name
  147. */
  148. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  149. /**
  150. * reply display name
  151. */
  152. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  153. /**
  154. * hosting id
  155. */
  156. $hid = $this->request->get('id');
  157. /**
  158. * load kerio manager by hosting id
  159. */
  160. $service = (new KerioManager())
  161. ->getApiByHosting($hid)
  162. ->soap
  163. ->service()
  164. ->createDistributionList()
  165. ->setFormData($this->formData)
  166. ;
  167. /**
  168. * load product configuration
  169. */
  170. $productManager = new ProductManager();
  171. $productManager->loadByHostingId($hid);
  172. $service->setProductManager($productManager);
  173. $result= $service->run();
  174. if(!$result)
  175. {
  176. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  177. }
  178. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenAdded')->setStatusSuccess();
  179. }
  180. public function update()
  181. {
  182. // TODO: Implement update() method.
  183. }
  184. }