AddListDataProvider.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DistributionList\Providers;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
  4. use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
  5. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
  6. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Helpers\ServiceFactory;
  7. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
  8. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Create\CreateDistributionList;
  9. use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\di;
  10. use ModulesGarden\Servers\ZimbraEmail\Core\Models\Whmcs\Hosting;
  11. use ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  12. use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  13. /**
  14. *
  15. * Created by PhpStorm.
  16. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  17. * Date: 18.09.19
  18. * Time: 13:32
  19. * Class AddListDataProvider
  20. */
  21. class AddListDataProvider extends BaseDataProvider
  22. {
  23. public function read()
  24. {
  25. $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();
  26. $this->data['domain'] = $hosting->domain;
  27. /**
  28. * load api
  29. */
  30. $accounts = (new ZimbraManager())
  31. ->getApiByServer($hosting->server)
  32. ->soap
  33. ->repository()
  34. ->accounts
  35. ->getByDomainName($hosting->domain);
  36. /**
  37. *
  38. * load lang
  39. */
  40. $lang = di('lang');
  41. /**
  42. * subscription requests
  43. */
  44. $this->availableValues['subscriptionRequest'] = [
  45. Zimbra::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  46. Zimbra::STATUS_APPROVAL => $lang->absoluteT('Require list owner approval'),
  47. Zimbra::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  48. ];
  49. /**
  50. * unsubscription requests
  51. */
  52. $this->availableValues['unsubscriptionRequest'] = [
  53. Zimbra::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  54. Zimbra::STATUS_APPROVAL => $lang->absoluteT('Require list owner approval'),
  55. Zimbra::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  56. ];
  57. /**
  58. *
  59. */
  60. foreach($accounts as $account)
  61. {
  62. $this->availableValues['memberList'][$account->getName()] = $account->getName();
  63. }
  64. if($this->formData)
  65. {
  66. $this->loadReloadedData();
  67. }
  68. }
  69. /**
  70. *
  71. */
  72. public function loadReloadedData()
  73. {
  74. foreach($this->formData as $key => $value)
  75. {
  76. $this->data[$key] = $value;
  77. }
  78. }
  79. public function create()
  80. {
  81. /**
  82. *
  83. * provided aliases
  84. */
  85. $customEmails = explode(',',$this->formData['emailAliases']);
  86. $this->formData['emailAliases'] = [];
  87. foreach($customEmails as $email)
  88. {
  89. if ($email !== '')
  90. {
  91. $this->formData['emailAliases'][] = $email;
  92. }
  93. }
  94. /**
  95. * provided owners
  96. */
  97. $owners = explode(',',$this->formData['owners']);
  98. $this->formData['owners'] = [];
  99. foreach($owners as $email)
  100. {
  101. if ($email !== '')
  102. {
  103. $this->formData['owners'][] = $email;
  104. }
  105. }
  106. /**
  107. * custom members
  108. */
  109. $customMembers = explode(',',$this->formData['customMember']);
  110. foreach($customMembers as $customMember)
  111. {
  112. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  113. {
  114. $this->formData['memberList'][] = $customMember;
  115. }
  116. }
  117. /**
  118. * display name
  119. */
  120. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  121. /**
  122. * reply display name
  123. */
  124. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  125. /**
  126. * hosting id
  127. */
  128. $hid = $this->request->get('id');
  129. /**
  130. * load zimbra manager by hosting id
  131. */
  132. $service = (new ZimbraManager())
  133. ->getApiByHosting($hid)
  134. ->soap
  135. ->service()
  136. ->createDistributionList()
  137. ->setFormData($this->formData)
  138. ;
  139. /**
  140. * load product configuration
  141. */
  142. $productManager = new ProductManager();
  143. $productManager->loadByHostingId($hid);
  144. $service->setProductManager($productManager);
  145. $result= $service->run();
  146. if(!$result)
  147. {
  148. return (new HtmlDataJsonResponse())->setMessage($service->getError())->setStatusError();
  149. }
  150. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenAdded')->setStatusSuccess();
  151. }
  152. public function update()
  153. {
  154. // TODO: Implement update() method.
  155. }
  156. }