EditListDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  16. /**
  17. *
  18. * Created by PhpStorm.
  19. * User: ThurData
  20. * Date: 02.10.19
  21. * Time: 08:36
  22. * Class EditListDataProvider
  23. */
  24. class EditListDataProvider extends AddListDataProvider
  25. {
  26. public function read()
  27. {
  28. $fields = array(
  29. "id",
  30. "name",
  31. "description",
  32. "languageId",
  33. "subscription",
  34. "replyTo",
  35. "posting"
  36. );
  37. $cond = array(
  38. "fieldName" => "id",
  39. "comparator" => "Eq",
  40. "value" => $this->actionElementId
  41. );
  42. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  43. try {
  44. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  45. } catch (KerioApiException $error) {
  46. logModuleCall(
  47. 'kerioEmail',
  48. __FUNCTION__,
  49. $error,
  50. 'Debug Error',
  51. $error->getMessage()
  52. );
  53. return ['error' => $error->getMessage()];
  54. }
  55. try {
  56. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  57. } catch (KerioApiException $error) {
  58. logModuleCall(
  59. 'kerioEmail',
  60. __FUNCTION__,
  61. $error,
  62. 'Debug Error',
  63. $error->getMessage()
  64. );
  65. return ['error' => $error->getMessage()];
  66. }
  67. try {
  68. $list = $api->getMailingLists($fields,$domainID,[ $cond ]);
  69. } catch (KerioApiException $error) {
  70. logModuleCall(
  71. 'kerioEmail',
  72. __FUNCTION__,
  73. $error,
  74. 'Debug Error',
  75. $error->getMessage()
  76. );
  77. return ['error' => $error->getMessage()];
  78. }
  79. $api->logout();
  80. logModuleCall(
  81. 'kerioEmail',
  82. __FUNCTION__,
  83. $list,
  84. 'Debug Error',
  85. $this->actionElementId
  86. );
  87. /**
  88. * load hosting
  89. */
  90. $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();
  91. /**
  92. * load lang
  93. */
  94. $lang = di('lang');
  95. $this->data['id'] = $list[0]['id'];
  96. $this->data['listmail'] = $list[0]['name'];
  97. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  98. $this->data['description'] = $list[0]['description'];
  99. $this->data['lang'] = $list[0]['languageId'];
  100. $this->availableValues['lang'] = [ 'de' => $lang->absoluteT('german'), 'en' => $lang->absoluteT('english') ];
  101. $this->data['subscriptionRequest'] = $list[0]['subscription']['type'];
  102. $this->availableValues['subscriptionRequest'] = [
  103. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject'),
  104. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  105. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept')
  106. ];
  107. $this->data['subscriptionNotify'] = $list[0]['subscription']['moderatorNotification'] === TRUE ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  108. $this->data['memberPosting'] = $list[0]['posting']['memberPosting'];
  109. $this->availableValues['memberPosting'] = [
  110. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  111. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  112. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  113. ];
  114. $this->data['nonMemberPosting'] = $list[0]['posting']['nonMemberPosting'];
  115. $this->availableValues['nonMemberPosting'] = [
  116. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject'),
  117. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  118. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept')
  119. ];
  120. $this->data['sendErrorsToModerator'] = $list[0]['posting']['sendErrorsToModerator'] === TRUE ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  121. $this->data['replyEmailAddress'] = $list[0]['replyTo'];
  122. $this->availableValues['replyEmailAddress'] = [
  123. 'Sender' => $lang->absoluteT('Sender'),
  124. 'ThisList' => $lang->absoluteT('ThisList'),
  125. 'SenderThisList' => $lang->absoluteT('SenderThisList')
  126. ];
  127. /**
  128. * allow to check which should be removed
  129. */
  130. // $this->availableValues['memberListActually'] = $this->data['memberListActually'] = $list->getResourceMembers();
  131. // $this->availableValues['emailAliasesActually'] = $this->data['emailAliasesActually'] = $list->getResourceAliases();
  132. // $this->availableValues['ownersActually'] = $this->data['ownersActually'] = $list->getResourceOwners();
  133. // $this->data['subscriptionRequest'] = $list->getDataResourceA(DistributionList::ATTR_SUBSCRIPTION_REQUEST);
  134. // $this->data['unsubscriptionRequest'] = $list->getDataResourceA(DistributionList::ATTR_UNSUBSCRIPTION_REQUEST);
  135. // $this->data['replyDisplayName'] = $list->getDataResourceA(DistributionList::REPLY_TO_DISPLAY);
  136. // $this->data['replyEmailAddress'] = $list->getDataResourceA(DistributionList::REPLY_TO_ADDRESS);
  137. // $this->data['sharesNotify'] = $list->getDataResourceA(DistributionList::ATTR_NOTIFY_SHARES) === Kerio::ATTR_ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  138. // $this->data['hideGal'] = $list->getDataResourceA(DistributionList::ATTR_HIDE_IN_GAL) === Kerio::ATTR_ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  139. // $this->data['receiveMail'] = $list->getDataResourceA(DistributionList::ATTR_MAIL_STATUS) === Kerio::ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  140. // $this->data['replyEmail'] = $list->getDataResourceA(DistributionList::REPLY_TO_ENABLED) === Kerio::ATTR_ENABLED ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  141. /**
  142. * is dynamic group
  143. */
  144. // $this->data['dynamicGroup'] = (int)$list->isDynamic() === Kerio::ENABLED_AS_INT ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  145. /**
  146. * available members
  147. */
  148. // $accounts = $repository->accounts->getByDomainName($hosting->domain);
  149. /**
  150. * subscription requests
  151. */
  152. // $this->availableValues['subscriptionRequest'] = [
  153. // Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  154. // Kerio::STATUS_APPROVAL => $lang->absoluteT('Require list owner approval'),
  155. // Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  156. // ];
  157. /**
  158. * unsubscription requests
  159. */
  160. // $this->availableValues['unsubscriptionRequest'] = [
  161. // Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  162. // Kerio::STATUS_APPROVAL => $lang->absoluteT('Require list owner approval'),
  163. // Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  164. // ];
  165. // $members = $list->getResourceMembers();
  166. /**
  167. *
  168. */
  169. // foreach ($accounts as $account)
  170. // {
  171. // if(in_array($account->getName(),$members))
  172. // {
  173. // $this->data['memberList'][] = $account->getName();
  174. // }
  175. // $this->availableValues['memberList'][$account->getName()] = $account->getName();
  176. // }
  177. /**
  178. * add custom members to array
  179. */
  180. // $ownMembers = $this->data['memberList'] ? $this->data['memberList'] : [];
  181. // $this->data['customMember'] = implode(',',array_diff($members, $ownMembers));
  182. if($this->formData)
  183. {
  184. $this->loadReloadedData();
  185. }
  186. }
  187. /**
  188. *
  189. */
  190. public function update()
  191. {
  192. /**
  193. *
  194. * provided aliases
  195. */
  196. $customEmails = explode(',',$this->formData['emailAliases']);
  197. $this->formData['emailAliases'] = [];
  198. foreach($customEmails as $email)
  199. {
  200. if ($email !== '')
  201. {
  202. $this->formData['emailAliases'][] = $email;
  203. }
  204. }
  205. /**
  206. * provided owners
  207. */
  208. $owners = explode(',',$this->formData['owners']);
  209. $this->formData['owners'] = [];
  210. foreach($owners as $email)
  211. {
  212. if ($email !== '')
  213. {
  214. $this->formData['owners'][] = $email;
  215. }
  216. }
  217. /**
  218. * display name
  219. */
  220. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  221. /**
  222. * reply display name
  223. */
  224. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  225. /**
  226. * custom members
  227. */
  228. $customMembers = explode(',',$this->formData['customMember']);
  229. foreach($customMembers as $customMember)
  230. {
  231. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  232. {
  233. $this->formData['memberList'][] = $customMember;
  234. }
  235. }
  236. $members = $this->formData['memberList'] ? $this->formData['memberList'] : [];
  237. $this->formData['memberList'] = array_merge($customMembers, $members);
  238. /**
  239. * hosting id
  240. */
  241. $hid = $this->request->get('id');
  242. /**
  243. * get product manager
  244. */
  245. $productManager = new ProductManager();
  246. $productManager->loadByHostingId($hid);
  247. /**
  248. * load kerio manager by hosting id
  249. */
  250. $service = (new KerioManager())
  251. ->getApiByHosting($hid)
  252. ->soap
  253. ->service()
  254. ->updateDistributionList()
  255. ->setFormData($this->formData)
  256. ->setProductManager($productManager)
  257. ;
  258. /**
  259. *
  260. */
  261. $result = $service->run();
  262. if(!$result)
  263. {
  264. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  265. }
  266. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenUpdated')->setStatusSuccess();
  267. }
  268. }