EditSettingDataProvider.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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: 18.09.19
  13. * Time: 09:35
  14. * Class EditSettingDataProvider
  15. */
  16. class EditSettingDataProvider extends BaseDataProvider
  17. {
  18. /**
  19. *
  20. */
  21. public function read()
  22. {
  23. $fields = array(
  24. 'id',
  25. "domain",
  26. "test",
  27. "notes",
  28. "2FA",
  29. "AD"
  30. );
  31. $cond = array(
  32. "fieldName" => "id",
  33. "comparator" => "Eq",
  34. "value" => $this->actionElementId
  35. );
  36. $this->availableValues['manager'][$ressource[0]['manager']['id']] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  37. $this->data['name'] = $ressource[0]['name'];
  38. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  39. $this->data['status'] = $ressource[0]['isEnabled'] == true ? Kerio::ACC_STATUS_ACTIVE : Kerio::ACC_STATUS_CLOSED;
  40. $this->data['type'] = $ressource[0]['type'] === 'Room' ? Kerio::RES_TYPE_LOCATION : Kerio::RES_TYPE_EQUIPMENT;
  41. $this->data['description'] = $ressource[0]['description'];
  42. $this->data['manager'] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  43. $this->availableValues['status'] = [
  44. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  45. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  46. ];
  47. $this->availableValues['type'] = [
  48. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  49. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  50. ];
  51. }
  52. /**
  53. * @return HtmlDataJsonResponse
  54. */
  55. public function update()
  56. {
  57. $fieldToProtection = [
  58. 'name',
  59. 'status',
  60. 'type',
  61. 'description',
  62. 'managert'
  63. ];
  64. foreach ($this->formData as $field => &$value)
  65. {
  66. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  67. }
  68. $attr = array(
  69. 'description' => $this->formData['description'],
  70. 'type' => $this->formData['type'],
  71. 'isEnabled' => $this->formData['status'] === 'active' ? true : false,
  72. 'manager' => array(
  73. 'id' => $this->formData['manager'],
  74. 'type' => 'UserPrincipal'
  75. )
  76. );
  77. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  78. try {
  79. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  80. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  81. } catch (KerioApiException $error) {
  82. logModuleCall(
  83. 'kerioEmail',
  84. __FUNCTION__,
  85. $error,
  86. 'Debug Error',
  87. $error->getMessage()
  88. );
  89. return ['error' => $error->getMessage()];
  90. }
  91. try {
  92. $result = $api->updateResouce($attr,$this->formData['id']);
  93. } catch (KerioApiException $error) {
  94. logModuleCall(
  95. 'kerioEmail',
  96. __FUNCTION__,
  97. $error,
  98. 'Debug Error',
  99. $error->getMessage()
  100. );
  101. return ['error' => $error->getMessage()];
  102. }
  103. $api->logout();
  104. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  105. }
  106. /**
  107. * @return HtmlDataJsonResponse
  108. */
  109. public function updateStatus()
  110. {
  111. $attr = array(
  112. 'isEnabled' => $this->formData['status'] === 'active' ? true : false,
  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. try {
  128. $result = $api->updateResouce($attr,$this->formData['id']);
  129. } catch (KerioApiException $error) {
  130. logModuleCall(
  131. 'kerioEmail',
  132. __FUNCTION__,
  133. $error,
  134. 'Debug Error',
  135. $error->getMessage()
  136. );
  137. return ['error' => $error->getMessage()];
  138. }
  139. $api->logout();
  140. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  141. }
  142. /**
  143. * @return HtmlDataJsonResponse
  144. */
  145. public function changePassword()
  146. {
  147. }
  148. }