EditListDataProvider.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\DistributionList\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
  4. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  5. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  6. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Create\CreateDistributionList;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update\UpdateDistributionList;
  11. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  12. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  13. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  14. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  15. /**
  16. *
  17. * Created by PhpStorm.
  18. * User: ThurData
  19. * Date: 02.10.19
  20. * Time: 08:36
  21. * Class EditListDataProvider
  22. */
  23. class EditListDataProvider extends AddListDataProvider
  24. {
  25. public function read()
  26. {
  27. /**
  28. * load hosting
  29. */
  30. $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();
  31. /**
  32. * load api
  33. */
  34. $api = (new KerioManager())->getApiByServer($hosting->server);
  35. $repository = new Repository($api->soap);
  36. $list = $repository->lists()->getDistributionListBydId($this->actionElementId);
  37. $this->data['id'] = $list->getId();
  38. $res = explode('@', $list->getName());
  39. $this->data['listmail'] = $res[0];
  40. $this->data['domain'] = $res[1];
  41. $this->data['emailAliases'] = implode(',', $list->getResourceAliases());
  42. $this->data['owners'] = implode(',', $list->getResourceOwners());
  43. /**
  44. * allow to check which should be removed
  45. */
  46. $this->availableValues['memberListActually'] = $this->data['memberListActually'] = $list->getResourceMembers();
  47. $this->availableValues['emailAliasesActually'] = $this->data['emailAliasesActually'] = $list->getResourceAliases();
  48. $this->availableValues['ownersActually'] = $this->data['ownersActually'] = $list->getResourceOwners();
  49. $this->data['displayName'] = $list->getDataResourceA(DistributionList::ATTR_DISPLAY_NAME);
  50. $this->data['description'] = $list->getDataResourceA(DistributionList::ATTR_DESCRIPTION);
  51. $this->data['subscriptionRequest'] = $list->getDataResourceA(DistributionList::ATTR_SUBSCRIPTION_REQUEST);
  52. $this->data['unsubscriptionRequest'] = $list->getDataResourceA(DistributionList::ATTR_UNSUBSCRIPTION_REQUEST);
  53. $this->data['replyDisplayName'] = $list->getDataResourceA(DistributionList::REPLY_TO_DISPLAY);
  54. $this->data['replyEmailAddress'] = $list->getDataResourceA(DistributionList::REPLY_TO_ADDRESS);
  55. $this->data['sharesNotify'] = $list->getDataResourceA(DistributionList::ATTR_NOTIFY_SHARES) === Kerio::ATTR_ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  56. $this->data['hideGal'] = $list->getDataResourceA(DistributionList::ATTR_HIDE_IN_GAL) === Kerio::ATTR_ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  57. $this->data['receiveMail'] = $list->getDataResourceA(DistributionList::ATTR_MAIL_STATUS) === Kerio::ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  58. $this->data['replyEmail'] = $list->getDataResourceA(DistributionList::REPLY_TO_ENABLED) === Kerio::ATTR_ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  59. /**
  60. * is dynamic group
  61. */
  62. $this->data['dynamicGroup'] = (int)$list->isDynamic() === Kerio::ENABLED_AS_INT ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  63. /**
  64. * available members
  65. */
  66. $accounts = $repository->accounts->getByDomainName($hosting->domain);
  67. /**
  68. * load lang
  69. */
  70. $lang = di('lang');
  71. /**
  72. * subscription requests
  73. */
  74. $this->availableValues['subscriptionRequest'] = [
  75. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  76. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require list owner approval'),
  77. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  78. ];
  79. /**
  80. * unsubscription requests
  81. */
  82. $this->availableValues['unsubscriptionRequest'] = [
  83. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  84. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require list owner approval'),
  85. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  86. ];
  87. $members = $list->getResourceMembers();
  88. /**
  89. *
  90. */
  91. foreach ($accounts as $account)
  92. {
  93. if(in_array($account->getName(),$members))
  94. {
  95. $this->data['memberList'][] = $account->getName();
  96. }
  97. $this->availableValues['memberList'][$account->getName()] = $account->getName();
  98. }
  99. /**
  100. * add custom members to array
  101. */
  102. $ownMembers = $this->data['memberList'] ? $this->data['memberList'] : [];
  103. $this->data['customMember'] = implode(',',array_diff($members, $ownMembers));
  104. if($this->formData)
  105. {
  106. $this->loadReloadedData();
  107. }
  108. }
  109. /**
  110. *
  111. */
  112. public function update()
  113. {
  114. /**
  115. *
  116. * provided aliases
  117. */
  118. $customEmails = explode(',',$this->formData['emailAliases']);
  119. $this->formData['emailAliases'] = [];
  120. foreach($customEmails as $email)
  121. {
  122. if ($email !== '')
  123. {
  124. $this->formData['emailAliases'][] = $email;
  125. }
  126. }
  127. /**
  128. * provided owners
  129. */
  130. $owners = explode(',',$this->formData['owners']);
  131. $this->formData['owners'] = [];
  132. foreach($owners as $email)
  133. {
  134. if ($email !== '')
  135. {
  136. $this->formData['owners'][] = $email;
  137. }
  138. }
  139. /**
  140. * display name
  141. */
  142. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  143. /**
  144. * reply display name
  145. */
  146. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  147. /**
  148. * custom members
  149. */
  150. $customMembers = explode(',',$this->formData['customMember']);
  151. foreach($customMembers as $customMember)
  152. {
  153. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  154. {
  155. $this->formData['memberList'][] = $customMember;
  156. }
  157. }
  158. $members = $this->formData['memberList'] ? $this->formData['memberList'] : [];
  159. $this->formData['memberList'] = array_merge($customMembers, $members);
  160. /**
  161. * hosting id
  162. */
  163. $hid = $this->request->get('id');
  164. /**
  165. * get product manager
  166. */
  167. $productManager = new ProductManager();
  168. $productManager->loadByHostingId($hid);
  169. /**
  170. * load kerio manager by hosting id
  171. */
  172. $service = (new KerioManager())
  173. ->getApiByHosting($hid)
  174. ->soap
  175. ->service()
  176. ->updateDistributionList()
  177. ->setFormData($this->formData)
  178. ->setProductManager($productManager)
  179. ;
  180. /**
  181. *
  182. */
  183. $result = $service->run();
  184. if(!$result)
  185. {
  186. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  187. }
  188. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenUpdated')->setStatusSuccess();
  189. }
  190. }