EditRessourceDataProvider.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update\UpdateRessource;
  12. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  13. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  14. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  15. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  16. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  17. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  18. /**
  19. *
  20. * Created by PhpStorm.
  21. * User: ThurData
  22. * Date: 18.09.19
  23. * Time: 09:35
  24. * Class EditRessourceDataProvider
  25. */
  26. class EditRessourceDataProvider extends BaseDataProvider
  27. {
  28. /**
  29. *
  30. */
  31. public function read()
  32. {
  33. $fields = array(
  34. "id",
  35. "name",
  36. "description",
  37. "type",
  38. "isEnabled",
  39. "manager"
  40. );
  41. $cond = array(
  42. "fieldName" => "id",
  43. "comparator" => "Eq",
  44. "value" => $this->actionElementId
  45. );
  46. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  47. try {
  48. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  49. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  50. } catch (KerioApiException $error) {
  51. logModuleCall(
  52. 'kerioEmail',
  53. __FUNCTION__,
  54. $error,
  55. 'Debug Error',
  56. $error->getMessage()
  57. );
  58. return ['error' => $error->getMessage()];
  59. }
  60. try {
  61. $ressource = $api->getResources($fields,$domainID,[ $cond ]);
  62. } catch (KerioApiException $error) {
  63. logModuleCall(
  64. 'kerioEmail',
  65. __FUNCTION__,
  66. $error,
  67. 'Debug Error',
  68. $error->getMessage()
  69. );
  70. return ['error' => $error->getMessage()];
  71. }
  72. $api->logout();
  73. $lang = di('lang');
  74. $this->data['id'] = $ressource[0]['id'];
  75. $this->data['name'] = $ressource[0]['name'];
  76. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  77. $this->data['status'] = $ressource[0]['isEnabled'] == true ? Kerio::ACC_STATUS_ACTIVE : Kerio::ACC_STATUS_CLOSED;
  78. $this->data['type'] = $ressource[0]['type'] === 'Room' ? di('lang')->absoluteT('kerio','ressource','type','location') : di('lang')->absoluteT('kerio','ressource','type','equipment');;
  79. $this->data['description'] = $ressource[0]['description'];
  80. $this->data['manager'] = $ressource[0]['magager']['name'] . '@' . $ressource[0]['magager']['domainName'];
  81. logModuleCall(
  82. 'kerioEmail',
  83. __FUNCTION__,
  84. $this->data,
  85. 'Debug Error',
  86. $ressource
  87. );
  88. $this->availableValues['status'] = [
  89. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  90. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  91. ];
  92. $this->availableValues['type'] = [
  93. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  94. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  95. ];
  96. }
  97. /**
  98. * @return HtmlDataJsonResponse
  99. */
  100. public function update()
  101. {
  102. $fieldToProtection = [
  103. 'name',
  104. 'status',
  105. 'type',
  106. 'description',
  107. 'managert'
  108. ];
  109. logModuleCall(
  110. 'kerioEmail',
  111. __FUNCTION__,
  112. $this->formData,
  113. 'Debug Error',
  114. $this->actionElementId
  115. );
  116. foreach ($this->formData as $field => &$value)
  117. {
  118. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  119. }
  120. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  121. }
  122. /**
  123. * @return HtmlDataJsonResponse
  124. */
  125. public function updateStatus()
  126. {
  127. /**
  128. * hosting id
  129. */
  130. $hid = $this->request->get('id');
  131. /**
  132. * product manager allow to check product settings
  133. */
  134. $productManager = new ProductManager();
  135. $productManager->loadByHostingId($hid);
  136. /**
  137. *
  138. * get soap create domain service
  139. */
  140. $service =(new KerioManager())
  141. ->getApiByHosting($hid)
  142. ->soap
  143. ->service()
  144. ->updateRessourceStatus()
  145. ->setProductManager($productManager)
  146. ;
  147. /**
  148. *
  149. * set product manager & form data to service
  150. */
  151. /**
  152. * run service for each id
  153. */
  154. $service->setFormData($this->formData);
  155. $result = $service->run();
  156. if(!$result)
  157. {
  158. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  159. }
  160. /**
  161. * return success
  162. */
  163. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  164. }
  165. /**
  166. * @return HtmlDataJsonResponse
  167. */
  168. public function changePassword()
  169. {
  170. /**
  171. * hosting id
  172. */
  173. $hid = $this->request->get('id');
  174. /**
  175. * product manager allow to check product settings
  176. */
  177. $productManager = new ProductManager();
  178. $productManager->loadByHostingId($hid);
  179. /**
  180. *
  181. * get soap create domain service
  182. */
  183. $service =(new KerioManager())
  184. ->getApiByHosting($hid)
  185. ->soap
  186. ->service()
  187. ->updateRessourcePassword()
  188. ->setProductManager($productManager)
  189. ;
  190. /**
  191. *
  192. * set product manager & form data to service
  193. */
  194. /**
  195. * run service for each id
  196. */
  197. $service->setFormData($this->formData);
  198. $result = $service->run();
  199. if(!$result)
  200. {
  201. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  202. }
  203. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  204. }
  205. }