AddListDataProvider.php 6.1 KB

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