SettingDataProvider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Setting\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  5. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  6. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  7. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  8. /**
  9. *
  10. * Created by PhpStorm.
  11. * User: ThurData
  12. * Date: 10.09.19
  13. * Time: 13:06
  14. * Class SettingDataProvider
  15. */
  16. class SettingDataProvider extends BaseDataProvider
  17. {
  18. public function read()
  19. {
  20. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  21. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  22. try {
  23. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  24. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  25. } catch (KerioApiException $error) {
  26. logModuleCall(
  27. 'kerioEmail',
  28. __FUNCTION__,
  29. $error,
  30. 'Debug Error',
  31. $error->getMessage()
  32. );
  33. return ['error' => $error->getMessage()];
  34. }
  35. $fields = array(
  36. "id",
  37. "loginName",
  38. "isEnabled");
  39. try {
  40. $users = $api->getUsers($fields,$domainID);
  41. } catch (KerioApiException $error) {
  42. logModuleCall(
  43. 'kerioEmail',
  44. __FUNCTION__,
  45. $error,
  46. 'Debug Error',
  47. $error->getMessage()
  48. );
  49. return ['error' => $error->getMessage()];
  50. }
  51. $api->logout();
  52. foreach($users as $user) {
  53. if($user['isEnabled']){
  54. $this->availableValues['manager'][ $user['id']] = $user['loginName'] . '@' . $this->getWhmcsParamByKey('domain');
  55. }
  56. }
  57. $lang = di('lang');
  58. $this->availableValues['status'] = [
  59. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  60. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  61. ];
  62. $this->availableValues['type'] = [
  63. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  64. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  65. ];
  66. }
  67. public function create()
  68. {
  69. $fieldToProtection = [
  70. 'name',
  71. 'status',
  72. 'manager',
  73. 'type',
  74. 'description'
  75. ];
  76. foreach ($this->formData as $field => &$value)
  77. {
  78. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  79. }
  80. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  81. try {
  82. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  83. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  84. } catch (KerioApiException $error) {
  85. logModuleCall(
  86. 'kerioEmail',
  87. __FUNCTION__,
  88. $error,
  89. 'Debug Error',
  90. $error->getMessage()
  91. );
  92. return ['error' => $error->getMessage()];
  93. }
  94. try {
  95. $ressourceID = $api->createResouce($this->formData, $domainID);
  96. } catch (KerioApiException $error) {
  97. logModuleCall(
  98. 'kerioEmail',
  99. __FUNCTION__,
  100. $error,
  101. 'Debug Error',
  102. $error->getMessage()
  103. );
  104. return ['error' => $error->getMessage()];
  105. }
  106. $api->logout();
  107. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenAdded')->setStatusSuccess();
  108. }
  109. public function updateStatus()
  110. {
  111. }
  112. public function update()
  113. {
  114. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  115. try {
  116. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  117. } catch (KerioApiException $error) {
  118. logModuleCall(
  119. 'kerioEmail',
  120. __FUNCTION__,
  121. $error,
  122. 'Debug Error',
  123. $error->getMessage()
  124. );
  125. return ['error' => $error->getMessage()];
  126. }
  127. $isEnabled = $this->formData['status'] === Kerio::ACC_STATUS_ACTIVE ? true : false;
  128. /**
  129. * run service for each id
  130. */
  131. foreach($this->request->get('massActions') as $id)
  132. {
  133. try {
  134. $result = $api->updateResouce(['isEnabled' => $isEnabled], $id);
  135. } catch (KerioApiException $error) {
  136. logModuleCall(
  137. 'kerioEmail',
  138. __FUNCTION__,
  139. $error,
  140. 'Debug Error',
  141. $error->getMessage()
  142. );
  143. return ['error' => $error->getMessage()];
  144. }
  145. }
  146. $api->logout();
  147. /**
  148. * return success
  149. */
  150. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massSettingStatusHasBeenUpdated')->setStatusSuccess();
  151. }
  152. }