AccountDataProvider.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  5. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  6. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  7. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  8. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  9. /**
  10. *
  11. * Created by PhpStorm.
  12. * User: ThurData
  13. * Date: 10.09.19
  14. * Time: 13:06
  15. * Class AccountDataProvider
  16. */
  17. class AccountDataProvider extends BaseDataProvider
  18. {
  19. public function read()
  20. {
  21. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  22. $this->data['quota'] = 0;
  23. $lang = di('lang');
  24. $this->availableValues['status'] = [
  25. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  26. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  27. ];
  28. $this->availableValues['unit'] = [
  29. 'MegaBytes' => 'MB',
  30. 'GigaBytes' => 'GB',
  31. ];
  32. }
  33. public function create()
  34. {
  35. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  36. foreach ($this->formData as $field => &$value)
  37. {
  38. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  39. }
  40. $maildomain = $this->getWhmcsParamByKey('domain');
  41. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  42. try {
  43. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  44. $domainID = $api->getDomainId($maildomain);
  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. if ($domainID === FALSE) {
  56. return "Error: Domain $maildomain not found";
  57. }
  58. $active = $this->formData['status'] === 'active' ? TRUE : FALSE;
  59. try {
  60. $userID = $api->createUser($domainID, $this->formData['username'], $this->formData['password'], $active)['result'][0]['id'];
  61. } catch (KerioApiException $error) {
  62. logModuleCall(
  63. 'kerioEmail',
  64. __FUNCTION__,
  65. $error,
  66. 'Debug Error',
  67. $error->getMessage()
  68. );
  69. return ['error' => $error->getMessage()];
  70. }
  71. $account['fullName'] = $this->formData['display_name'];
  72. if ($this->formData['quota'] > 0) {
  73. $account['diskSizeLimit']['isActive'] = TRUE;
  74. $account['diskSizeLimit']['limit']['value'] = intval($this->formData['quota']);
  75. $account['diskSizeLimit']['limit']['units'] = $this->formData['unit'];
  76. }
  77. try {
  78. $result = $api->modifyUser($userID, $account);
  79. } catch (KerioApiException $error) {
  80. logModuleCall(
  81. 'kerioEmail',
  82. __FUNCTION__,
  83. $error,
  84. 'Debug Error',
  85. $error->getMessage()
  86. );
  87. return ['error' => $error->getMessage()];
  88. }
  89. $fields['firstName'] = $this->formData['firstname'];
  90. $fields['surName'] = $this->formData['lastname'];
  91. $fields['commonName'] = $this->formData['display_name'];
  92. $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
  93. $fields['titleBefore'] = $this->formData['title'];
  94. $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
  95. $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
  96. $fields['departmentName'] = $this->formData['department'];
  97. $fields['profession'] = $this->formData['profession'];
  98. try {
  99. $result = $api->setAddress($userID, $fields);
  100. } catch (KerioApiException $error) {
  101. logModuleCall(
  102. 'kerioEmail',
  103. __FUNCTION__,
  104. $error,
  105. 'Debug Error',
  106. $error->getMessage()
  107. );
  108. return ['error' => $error->getMessage()];
  109. }
  110. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  111. }
  112. public function updateStatus()
  113. {
  114. }
  115. public function update()
  116. {
  117. $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
  118. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  119. try {
  120. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  121. } catch (KerioApiException $error) {
  122. logModuleCall(
  123. 'kerioEmail',
  124. __FUNCTION__,
  125. $error,
  126. 'Debug Error',
  127. $error->getMessage()
  128. );
  129. return ['error' => $error->getMessage()];
  130. }
  131. /**
  132. * run service for each id
  133. */
  134. foreach($this->request->get('massActions') as $id)
  135. {
  136. try {
  137. $result = $api->modifyUser($id, $status);
  138. } catch (KerioApiException $error) {
  139. logModuleCall(
  140. 'kerioEmail',
  141. __FUNCTION__,
  142. $error,
  143. 'Debug Error',
  144. $error->getMessage()
  145. );
  146. return ['error' => $error->getMessage()];
  147. }
  148. }
  149. $api->logout();
  150. /**
  151. * return success
  152. */
  153. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  154. }
  155. /**
  156. *
  157. * read data per cos_name
  158. */
  159. protected function readCosParams()
  160. {
  161. }
  162. /**
  163. * @return bool|mixed
  164. */
  165. protected function getFilteredCosConfigurableOptions()
  166. {
  167. }
  168. }