EditRessourceDataProvider.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace ModulesGarden\Servers\KerioEmail\App\UI\Client\Ressource\Providers;
  3. use ModulesGarden\Servers\KerioEmail\App\Enums\Kerio;
  4. use ModulesGarden\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ModulesGarden\Servers\KerioEmail\App\Libs\Product\ProductManager;
  6. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource;
  7. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  8. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  9. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  10. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response;
  11. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update\UpdateRessource;
  12. use ModulesGarden\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  13. use function ModulesGarden\Servers\KerioEmail\Core\Helper\di;
  14. use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  15. use ModulesGarden\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  16. use ModulesGarden\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  17. /**
  18. *
  19. * Created by PhpStorm.
  20. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  21. * Date: 18.09.19
  22. * Time: 09:35
  23. * Class EditRessourceDataProvider
  24. */
  25. class EditRessourceDataProvider extends BaseDataProvider
  26. {
  27. /**
  28. *
  29. */
  30. public function read()
  31. {
  32. /**
  33. * hosting id
  34. */
  35. $hid = $this->getRequestValue('id');
  36. /**
  37. * load hosting
  38. */
  39. $hosting = Hosting::where('id', $hid)->first();
  40. /**
  41. * load api
  42. */
  43. $api = (new KerioManager())->getApiByServer($hosting->server);
  44. $repository = new Repository($api->soap);
  45. $result = $repository->ressources->getRessourceOptionsById($this->actionElementId);
  46. if($result instanceof Response && $result->getLastError())
  47. {
  48. throw new \Exception($result->getLastError());
  49. }
  50. $mailBoxParams = explode('@', $result->getName());
  51. $this->data['id'] = $result->getId();
  52. $this->data['username'] = $mailBoxParams[0];
  53. $this->data['domain'] = $mailBoxParams[1];
  54. $this->data['status'] = $result->getDataResourceA(Ressource::ATTR_STATUS);
  55. $this->data['type'] = $result->getDataResourceA(Ressource::ATTR_TYPE);
  56. $this->data['display_name'] = $result->getDataResourceA(Ressource::ATTR_DISPLAY_NAME);
  57. $this->data['description'] = $result->getDataResourceA(Ressource::ATTR_DESC);
  58. $this->data['capacity'] = $result->getDataResourceA(Ressource::ATTR_CAPACITY);
  59. $this->data['notes'] = $result->getDataResourceA(Ressource::ATTR_NOTE);
  60. $this->data['contact'] = $result->getDataResourceA(Ressource::ATTR_CONT);
  61. $this->data['site'] = $result->getDataResourceA(Ressource::ATTR_SITE);
  62. $this->data['contact_mail'] = $result->getDataResourceA(Ressource::ATTR_CONT_EMAIL);
  63. $this->data['contact_phone'] = $result->getDataResourceA(Ressource::ATTR_CONT_PHONE);
  64. $this->data['street'] = $result->getDataResourceA(Ressource::ATTR_STREET);
  65. $this->data['building'] = $result->getDataResourceA(Ressource::ATTR_BUILDING);
  66. $this->data['floor'] = $result->getDataResourceA(Ressource::ATTR_FLOOR);
  67. $this->data['room'] = $result->getDataResourceA(Ressource::ATTR_ROOM);
  68. $this->data['post_code'] = $result->getDataResourceA(Ressource::ATTR_POSTAL_CODE);
  69. $this->data['town'] = $result->getDataResourceA(Ressource::ATTR_TOWN);
  70. $this->data['state'] = $result->getDataResourceA(Ressource::ATTR_STATE);
  71. $this->data['county'] = $result->getDataResourceA(Ressource::ATTR_COUNTY);
  72. $this->data['auto_accept'] = $result->getDataResourceA(Ressource::ATTR_AUTO) == 'TRUE' ? 'on' : 'off';
  73. $this->data['auto_busy'] = $result->getDataResourceA(Ressource::ATTR_BUSY) == 'TRUE' ? 'on' : 'off';
  74. $lang = di('lang');
  75. $this->availableValues['status'] = [
  76. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  77. Kerio::ACC_STATUS_LOCKED => $lang->absoluteT('kerio','account','status','locked'),
  78. Kerio::ACC_STATUS_MAINTENANCE => $lang->absoluteT('kerio','account','status','maintenance'),
  79. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  80. Kerio::ACC_STATUS_LOCKOUT => $lang->absoluteT('kerio','account','status','lockout'),
  81. Kerio::ACC_STATUS_PENDING => $lang->absoluteT('kerio','account','status','pending')
  82. ];
  83. $this->availableValues['type'] = [
  84. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  85. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  86. ];
  87. }
  88. /**
  89. * @return HtmlDataJsonResponse
  90. */
  91. public function update()
  92. {
  93. /**
  94. * hosting id
  95. */
  96. $hid = $this->request->get('id');
  97. $fieldToProtection = [
  98. 'display_name',
  99. 'status',
  100. 'type',
  101. 'capacity',
  102. 'description',
  103. 'notes',
  104. 'contact',
  105. 'site',
  106. 'contact_mail',
  107. 'contact_phone',
  108. 'street',
  109. 'building',
  110. 'floor',
  111. 'room',
  112. 'post_code',
  113. 'town',
  114. 'state',
  115. 'county',
  116. 'auto_accept',
  117. 'auto_busy'
  118. ];
  119. foreach ($this->formData as $field => &$value)
  120. {
  121. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  122. }
  123. /**
  124. * product manager allow to check product settings
  125. */
  126. $productManager = new ProductManager();
  127. $productManager->loadByHostingId($hid);
  128. /**
  129. *
  130. * get soap create domain service
  131. */
  132. $service =(new KerioManager())
  133. ->getApiByHosting($hid)
  134. ->soap
  135. ->service()
  136. ->updateRessource();
  137. /**
  138. *
  139. * set product manager & form data to service
  140. */
  141. $service
  142. ->setProductManager($productManager)
  143. ->setFormData($this->formData);
  144. /**
  145. * run service
  146. */
  147. $result = $service->run();
  148. /**
  149. * return success or error response
  150. */
  151. if(!$result)
  152. {
  153. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  154. }
  155. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  156. }
  157. /**
  158. * @return HtmlDataJsonResponse
  159. */
  160. public function updateStatus()
  161. {
  162. /**
  163. * hosting id
  164. */
  165. $hid = $this->request->get('id');
  166. /**
  167. * product manager allow to check product settings
  168. */
  169. $productManager = new ProductManager();
  170. $productManager->loadByHostingId($hid);
  171. /**
  172. *
  173. * get soap create domain service
  174. */
  175. $service =(new KerioManager())
  176. ->getApiByHosting($hid)
  177. ->soap
  178. ->service()
  179. ->updateRessourceStatus()
  180. ->setProductManager($productManager)
  181. ;
  182. /**
  183. *
  184. * set product manager & form data to service
  185. */
  186. /**
  187. * run service for each id
  188. */
  189. $service->setFormData($this->formData);
  190. $result = $service->run();
  191. if(!$result)
  192. {
  193. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  194. }
  195. /**
  196. * return success
  197. */
  198. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  199. }
  200. /**
  201. * @return HtmlDataJsonResponse
  202. */
  203. public function changePassword()
  204. {
  205. /**
  206. * hosting id
  207. */
  208. $hid = $this->request->get('id');
  209. /**
  210. * product manager allow to check product settings
  211. */
  212. $productManager = new ProductManager();
  213. $productManager->loadByHostingId($hid);
  214. /**
  215. *
  216. * get soap create domain service
  217. */
  218. $service =(new KerioManager())
  219. ->getApiByHosting($hid)
  220. ->soap
  221. ->service()
  222. ->updateRessourcePassword()
  223. ->setProductManager($productManager)
  224. ;
  225. /**
  226. *
  227. * set product manager & form data to service
  228. */
  229. /**
  230. * run service for each id
  231. */
  232. $service->setFormData($this->formData);
  233. $result = $service->run();
  234. if(!$result)
  235. {
  236. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  237. }
  238. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  239. }
  240. }