RessourceDataProvider.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\ProductParams;
  4. use ThurData\Servers\KerioEmail\App\Enums\Size;
  5. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  6. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  7. use ThurData\Servers\KerioEmail\App\Http\Admin\ProductConfiguration;
  8. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\ServiceFactory;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  12. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  13. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  14. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  15. use ThurData\Servers\KerioEmail\Core\Http\JsonResponse;
  16. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  17. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  18. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  19. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  20. /**
  21. *
  22. * Created by PhpStorm.
  23. * User: ThurData
  24. * Date: 10.09.19
  25. * Time: 13:06
  26. * Class RessourceDataProvider
  27. */
  28. class RessourceDataProvider extends BaseDataProvider
  29. {
  30. public function read()
  31. {
  32. /**
  33. * hosting id
  34. */
  35. $hid = $this->request->get('id');
  36. $hosting = Hosting::where('id', $hid)->first();
  37. //todo refactor
  38. $this->data['domain'] = $hosting->domain;
  39. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  40. try {
  41. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  42. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  43. } catch (KerioApiException $error) {
  44. logModuleCall(
  45. 'kerioEmail',
  46. __FUNCTION__,
  47. $error,
  48. 'Debug Error',
  49. $error->getMessage()
  50. );
  51. return ['error' => $error->getMessage()];
  52. }
  53. $fields = array(
  54. "id",
  55. "loginName",
  56. "isEnabled");
  57. try {
  58. $users = $api->getUsers($fields,$domainID);
  59. } catch (KerioApiException $error) {
  60. logModuleCall(
  61. 'kerioEmail',
  62. __FUNCTION__,
  63. $error,
  64. 'Debug Error',
  65. $error->getMessage()
  66. );
  67. return ['error' => $error->getMessage()];
  68. }
  69. $api->logout();
  70. foreach($users as $user) {
  71. if($user['isEnabled']){
  72. $this->availableValues['manager'][ $user['id']] = $user['loginName'] . '@' . $this->getWhmcsParamByKey('domain');
  73. }
  74. }
  75. $lang = di('lang');
  76. $this->availableValues['status'] = [
  77. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  78. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  79. ];
  80. $this->availableValues['type'] = [
  81. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  82. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  83. ];
  84. }
  85. public function create()
  86. {
  87. $fieldToProtection = [
  88. 'name',
  89. 'status',
  90. 'manager',
  91. 'type',
  92. 'description'
  93. ];
  94. foreach ($this->formData as $field => &$value)
  95. {
  96. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  97. }
  98. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  99. try {
  100. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  101. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  102. } catch (KerioApiException $error) {
  103. logModuleCall(
  104. 'kerioEmail',
  105. __FUNCTION__,
  106. $error,
  107. 'Debug Error',
  108. $error->getMessage()
  109. );
  110. return ['error' => $error->getMessage()];
  111. }
  112. try {
  113. $ressourceID = $api->createResouce($this->formData, $domainID);
  114. } catch (KerioApiException $error) {
  115. logModuleCall(
  116. 'kerioEmail',
  117. __FUNCTION__,
  118. $error,
  119. 'Debug Error',
  120. $error->getMessage()
  121. );
  122. return ['error' => $error->getMessage()];
  123. }
  124. $api->logout();
  125. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenAdded')->setStatusSuccess();
  126. }
  127. public function updateStatus()
  128. {
  129. }
  130. public function update()
  131. {
  132. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  133. try {
  134. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  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. $isEnabled = $this->formData['status'] === Kerio::ACC_STATUS_ACTIVE ? true : false;
  146. /**
  147. * run service for each id
  148. */
  149. foreach($this->request->get('massActions') as $id)
  150. {
  151. try {
  152. $result = $api->updateResouce(['isEnabled' => $isEnabled], $id);
  153. } catch (KerioApiException $error) {
  154. logModuleCall(
  155. 'kerioEmail',
  156. __FUNCTION__,
  157. $error,
  158. 'Debug Error',
  159. $error->getMessage()
  160. );
  161. return ['error' => $error->getMessage()];
  162. }
  163. }
  164. $api->logout();
  165. /**
  166. * return success
  167. */
  168. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massRessourceStatusHasBeenUpdated')->setStatusSuccess();
  169. }
  170. }