AccountDataProvider.php 6.2 KB

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