EditRessourceDataProvider.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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'];
  81. $this->availableValues['status'] = [
  82. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  83. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  84. ];
  85. $this->availableValues['type'] = [
  86. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  87. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  88. ];
  89. }
  90. /**
  91. * @return HtmlDataJsonResponse
  92. */
  93. public function update()
  94. {
  95. $fieldToProtection = [
  96. 'name',
  97. 'status',
  98. 'type',
  99. 'description',
  100. 'managert'
  101. ];
  102. logModuleCall(
  103. 'kerioEmail',
  104. __FUNCTION__,
  105. $this->formData,
  106. 'Debug Error',
  107. $this->actionElementId
  108. );
  109. foreach ($this->formData as $field => &$value)
  110. {
  111. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  112. }
  113. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  114. }
  115. /**
  116. * @return HtmlDataJsonResponse
  117. */
  118. public function updateStatus()
  119. {
  120. /**
  121. * hosting id
  122. */
  123. $hid = $this->request->get('id');
  124. /**
  125. * product manager allow to check product settings
  126. */
  127. $productManager = new ProductManager();
  128. $productManager->loadByHostingId($hid);
  129. /**
  130. *
  131. * get soap create domain service
  132. */
  133. $service =(new KerioManager())
  134. ->getApiByHosting($hid)
  135. ->soap
  136. ->service()
  137. ->updateRessourceStatus()
  138. ->setProductManager($productManager)
  139. ;
  140. /**
  141. *
  142. * set product manager & form data to service
  143. */
  144. /**
  145. * run service for each id
  146. */
  147. $service->setFormData($this->formData);
  148. $result = $service->run();
  149. if(!$result)
  150. {
  151. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  152. }
  153. /**
  154. * return success
  155. */
  156. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  157. }
  158. /**
  159. * @return HtmlDataJsonResponse
  160. */
  161. public function changePassword()
  162. {
  163. /**
  164. * hosting id
  165. */
  166. $hid = $this->request->get('id');
  167. /**
  168. * product manager allow to check product settings
  169. */
  170. $productManager = new ProductManager();
  171. $productManager->loadByHostingId($hid);
  172. /**
  173. *
  174. * get soap create domain service
  175. */
  176. $service =(new KerioManager())
  177. ->getApiByHosting($hid)
  178. ->soap
  179. ->service()
  180. ->updateRessourcePassword()
  181. ->setProductManager($productManager)
  182. ;
  183. /**
  184. *
  185. * set product manager & form data to service
  186. */
  187. /**
  188. * run service for each id
  189. */
  190. $service->setFormData($this->formData);
  191. $result = $service->run();
  192. if(!$result)
  193. {
  194. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  195. }
  196. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  197. }
  198. }