EditListDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. $accountFields = array(
  97. "id",
  98. "loginName");
  99. try {
  100. $accounts = $api->getUsers($accountFields,$domainID);
  101. } catch (KerioApiException $error) {
  102. logModuleCall(
  103. 'kerioEmail',
  104. __FUNCTION__,
  105. $error,
  106. 'Debug Error',
  107. $error->getMessage()
  108. );
  109. return ['error' => $error->getMessage()];
  110. }
  111. $api->logout();
  112. foreach($members as $member) {
  113. if($member['kind'] === 'Moderator') {
  114. $this->data['moderator'] = $member['emailAddress'];
  115. } elseif ($member['hasId'] == TRUE) {
  116. $intMembers[] = $member['emailAddress'];
  117. } else {
  118. $extMembers[] = $member['emailAddress'];
  119. }
  120. }
  121. foreach($accounts as $account) {
  122. $users[] = $account['loginName'] . '@' . $this->getWhmcsParamByKey('domain');
  123. }
  124. $availableMembers = array_values(array_diff($users, $intMembers));
  125. logModuleCall(
  126. 'kerioEmail',
  127. __FUNCTION__,
  128. $intMembers,
  129. 'Debug Members',
  130. $extMembers
  131. );
  132. logModuleCall(
  133. 'kerioEmail',
  134. __FUNCTION__,
  135. $availableMembers,
  136. 'Debug Moderator',
  137. $this->data['moderator']
  138. );
  139. /**
  140. * load hosting
  141. */
  142. $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();
  143. /**
  144. * load lang
  145. */
  146. $lang = di('lang');
  147. $this->data['id'] = $list[0]['id'];
  148. $this->data['listmail'] = $list[0]['name'];
  149. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  150. $this->data['description'] = $list[0]['description'];
  151. $this->data['lang'] = $list[0]['languageId'];
  152. $this->availableValues['lang'] = [ 'de' => $lang->absoluteT('german'), 'en' => $lang->absoluteT('english') ];
  153. $this->data['subscriptionRequest'] = $list[0]['subscription']['type'];
  154. $this->availableValues['subscriptionRequest'] = [
  155. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject'),
  156. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  157. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept')
  158. ];
  159. $this->data['subscriptionNotify'] = $list[0]['subscription']['moderatorNotification'] === TRUE ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  160. $this->data['memberPosting'] = $list[0]['posting']['memberPosting'];
  161. $this->availableValues['memberPosting'] = [
  162. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept'),
  163. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  164. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject')
  165. ];
  166. $this->data['nonMemberPosting'] = $list[0]['posting']['nonMemberPosting'];
  167. $this->availableValues['nonMemberPosting'] = [
  168. Kerio::STATUS_REJECT => $lang->absoluteT('Automatically reject'),
  169. Kerio::STATUS_APPROVAL => $lang->absoluteT('Require moderator approval'),
  170. Kerio::STATUS_ACCEPT => $lang->absoluteT('Automatically accept')
  171. ];
  172. $this->data['sendErrorsToModerator'] = $list[0]['posting']['sendErrorsToModerator'] === TRUE ? ProductParams::SWITCHER_ENABLED : ProductParams::SWITCHER_DISABLED;
  173. $this->data['replyEmailAddress'] = $list[0]['replyTo'];
  174. $this->availableValues['replyEmailAddress'] = [
  175. 'Sender' => $lang->absoluteT('Sender'),
  176. 'ThisList' => $lang->absoluteT('ThisList'),
  177. 'SenderThisList' => $lang->absoluteT('SenderThisList')
  178. ];
  179. $this->data['customMember'] = implode(',',$extMembers);
  180. foreach($intMembers as $intMember) {
  181. $this->data['memberList'][$intMember] = $intMember;
  182. }
  183. $this->availableValues['memberListActually'] = $this->data['memberList'];
  184. foreach($availableMembers as $availableMember) {
  185. $this->availableValues['memberList'][$availableMember] = $availableMember;
  186. }
  187. logModuleCall(
  188. 'kerioEmail',
  189. __FUNCTION__,
  190. $this->data,
  191. 'Debug Data',
  192. $this->availableValues
  193. );
  194. /**
  195. * allow to check which should be removed
  196. */
  197. // $this->availableValues['memberListActually'] = $this->data['memberListActually'] = $list->getResourceMembers();
  198. // $this->availableValues['emailAliasesActually'] = $this->data['emailAliasesActually'] = $list->getResourceAliases();
  199. // $this->availableValues['ownersActually'] = $this->data['ownersActually'] = $list->getResourceOwners();
  200. /**
  201. * available members
  202. */
  203. // $accounts = $repository->accounts->getByDomainName($hosting->domain);
  204. // $members = $list->getResourceMembers();
  205. /**
  206. *
  207. */
  208. // foreach ($accounts as $account)
  209. // {
  210. // if(in_array($account->getName(),$members))
  211. // {
  212. // $this->data['memberList'][] = $account->getName();
  213. // }
  214. // $this->availableValues['memberList'][$account->getName()] = $account->getName();
  215. // }
  216. /**
  217. * add custom members to array
  218. */
  219. // $ownMembers = $this->data['memberList'] ? $this->data['memberList'] : [];
  220. // $this->data['customMember'] = implode(',',array_diff($members, $ownMembers));
  221. if($this->formData)
  222. {
  223. $this->loadReloadedData();
  224. }
  225. }
  226. /**
  227. *
  228. */
  229. public function update()
  230. {
  231. /**
  232. *
  233. * provided aliases
  234. */
  235. $customEmails = explode(',',$this->formData['emailAliases']);
  236. $this->formData['emailAliases'] = [];
  237. foreach($customEmails as $email)
  238. {
  239. if ($email !== '')
  240. {
  241. $this->formData['emailAliases'][] = $email;
  242. }
  243. }
  244. /**
  245. * provided owners
  246. */
  247. $owners = explode(',',$this->formData['owners']);
  248. $this->formData['owners'] = [];
  249. foreach($owners as $email)
  250. {
  251. if ($email !== '')
  252. {
  253. $this->formData['owners'][] = $email;
  254. }
  255. }
  256. /**
  257. * display name
  258. */
  259. $this->formData['displayName'] = htmlentities($this->formData['displayName']);
  260. /**
  261. * reply display name
  262. */
  263. $this->formData['replyDisplayName'] = htmlentities($this->formData['replyDisplayName']);
  264. /**
  265. * custom members
  266. */
  267. $customMembers = explode(',',$this->formData['customMember']);
  268. foreach($customMembers as $customMember)
  269. {
  270. if($customMember !== '' && !in_array($customMember, $this->formData['memberList']))
  271. {
  272. $this->formData['memberList'][] = $customMember;
  273. }
  274. }
  275. $members = $this->formData['memberList'] ? $this->formData['memberList'] : [];
  276. $this->formData['memberList'] = array_merge($customMembers, $members);
  277. /**
  278. * hosting id
  279. */
  280. $hid = $this->request->get('id');
  281. /**
  282. * get product manager
  283. */
  284. $productManager = new ProductManager();
  285. $productManager->loadByHostingId($hid);
  286. /**
  287. * load kerio manager by hosting id
  288. */
  289. $service = (new KerioManager())
  290. ->getApiByHosting($hid)
  291. ->soap
  292. ->service()
  293. ->updateDistributionList()
  294. ->setFormData($this->formData)
  295. ->setProductManager($productManager)
  296. ;
  297. /**
  298. *
  299. */
  300. $result = $service->run();
  301. if(!$result)
  302. {
  303. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  304. }
  305. return (new HtmlDataJsonResponse())->setMessageAndTranslate('distributionListHasBeenUpdated')->setStatusSuccess();
  306. }
  307. }