EditRessourceDataProvider.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $attr = array(
  73. "id",
  74. "loginName",
  75. "isEnabled");
  76. try {
  77. $users = $api->getUsers($attr,$domainID);
  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. $api->logout();
  89. foreach($users as $user) {
  90. if($user['isEnabled']){
  91. $this->availableValues['manager'][ $user['id']] = $user['loginName'] . '@' . $this->getWhmcsParamByKey('domain');
  92. }
  93. }
  94. $lang = di('lang');
  95. $this->data['id'] = $ressource[0]['id'];
  96. $this->data['name'] = $ressource[0]['name'];
  97. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  98. $this->data['status'] = $ressource[0]['isEnabled'] == true ? Kerio::ACC_STATUS_ACTIVE : Kerio::ACC_STATUS_CLOSED;
  99. $this->data['type'] = $ressource[0]['type'] === 'Room' ? di('lang')->absoluteT('kerio','ressource','type','location') : di('lang')->absoluteT('kerio','ressource','type','equipment');;
  100. $this->data['description'] = $ressource[0]['description'];
  101. $this->data['manager'] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  102. logModuleCall(
  103. 'kerioEmail',
  104. __FUNCTION__,
  105. $this->data,
  106. 'Debug Error',
  107. $ressource
  108. );
  109. $this->availableValues['status'] = [
  110. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  111. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  112. ];
  113. $this->availableValues['type'] = [
  114. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  115. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  116. ];
  117. }
  118. /**
  119. * @return HtmlDataJsonResponse
  120. */
  121. public function update()
  122. {
  123. $fieldToProtection = [
  124. 'name',
  125. 'status',
  126. 'type',
  127. 'description',
  128. 'managert'
  129. ];
  130. logModuleCall(
  131. 'kerioEmail',
  132. __FUNCTION__,
  133. $this->formData,
  134. 'Debug Error',
  135. $this->actionElementId
  136. );
  137. foreach ($this->formData as $field => &$value)
  138. {
  139. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  140. }
  141. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  142. }
  143. /**
  144. * @return HtmlDataJsonResponse
  145. */
  146. public function updateStatus()
  147. {
  148. /**
  149. * hosting id
  150. */
  151. $hid = $this->request->get('id');
  152. /**
  153. * product manager allow to check product settings
  154. */
  155. $productManager = new ProductManager();
  156. $productManager->loadByHostingId($hid);
  157. /**
  158. *
  159. * get soap create domain service
  160. */
  161. $service =(new KerioManager())
  162. ->getApiByHosting($hid)
  163. ->soap
  164. ->service()
  165. ->updateRessourceStatus()
  166. ->setProductManager($productManager)
  167. ;
  168. /**
  169. *
  170. * set product manager & form data to service
  171. */
  172. /**
  173. * run service for each id
  174. */
  175. $service->setFormData($this->formData);
  176. $result = $service->run();
  177. if(!$result)
  178. {
  179. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  180. }
  181. /**
  182. * return success
  183. */
  184. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  185. }
  186. /**
  187. * @return HtmlDataJsonResponse
  188. */
  189. public function changePassword()
  190. {
  191. /**
  192. * hosting id
  193. */
  194. $hid = $this->request->get('id');
  195. /**
  196. * product manager allow to check product settings
  197. */
  198. $productManager = new ProductManager();
  199. $productManager->loadByHostingId($hid);
  200. /**
  201. *
  202. * get soap create domain service
  203. */
  204. $service =(new KerioManager())
  205. ->getApiByHosting($hid)
  206. ->soap
  207. ->service()
  208. ->updateRessourcePassword()
  209. ->setProductManager($productManager)
  210. ;
  211. /**
  212. *
  213. * set product manager & form data to service
  214. */
  215. /**
  216. * run service for each id
  217. */
  218. $service->setFormData($this->formData);
  219. $result = $service->run();
  220. if(!$result)
  221. {
  222. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  223. }
  224. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  225. }
  226. }