EditListDataProvider.php 12 KB

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