EditRessourceDataProvider.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\Ressource\Providers;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
  4. use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
  5. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
  6. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Ressource;
  7. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\ClassOfService;
  8. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
  9. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository\ClassOfServices;
  10. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Response;
  11. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Update\UpdateRessource;
  12. use ModulesGarden\Servers\ZimbraEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  13. use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\di;
  14. use ModulesGarden\Servers\ZimbraEmail\Core\Models\Whmcs\Hosting;
  15. use ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  16. use ModulesGarden\Servers\ZimbraEmail\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 ZimbraManager())->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. $lang = di('lang');
  73. $this->availableValues['status'] = [
  74. Zimbra::ACC_STATUS_ACTIVE => $lang->absoluteT('zimbra','account','status','active'),
  75. Zimbra::ACC_STATUS_LOCKED => $lang->absoluteT('zimbra','account','status','locked'),
  76. Zimbra::ACC_STATUS_MAINTENANCE => $lang->absoluteT('zimbra','account','status','maintenance'),
  77. Zimbra::ACC_STATUS_CLOSED => $lang->absoluteT('zimbra','account','status','closed'),
  78. Zimbra::ACC_STATUS_LOCKOUT => $lang->absoluteT('zimbra','account','status','lockout'),
  79. Zimbra::ACC_STATUS_PENDING => $lang->absoluteT('zimbra','account','status','pending')
  80. ];
  81. $this->availableValues['type'] = [
  82. Zimbra::RES_TYPE_LOCATION => $lang->absoluteT('zimbra','ressource','type','location'),
  83. Zimbra::RES_TYPE_EQUIPMENT => $lang->absoluteT('zimbra','ressource','type','equipment')
  84. ];
  85. }
  86. /**
  87. * @return HtmlDataJsonResponse
  88. */
  89. public function update()
  90. {
  91. /**
  92. * hosting id
  93. */
  94. $hid = $this->request->get('id');
  95. $fieldToProtection = [
  96. 'display_name',
  97. 'status',
  98. 'type',
  99. 'capacity',
  100. 'description',
  101. 'notes',
  102. 'contact',
  103. 'site',
  104. 'contact_mail',
  105. 'contact_phone',
  106. 'street',
  107. 'building',
  108. 'floor',
  109. 'room',
  110. 'post_code',
  111. 'town',
  112. 'state',
  113. 'county'
  114. ];
  115. foreach ($this->formData as $field => &$value)
  116. {
  117. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  118. }
  119. /**
  120. * product manager allow to check product settings
  121. */
  122. $productManager = new ProductManager();
  123. $productManager->loadByHostingId($hid);
  124. /**
  125. *
  126. * get soap create domain service
  127. */
  128. $service =(new ZimbraManager())
  129. ->getApiByHosting($hid)
  130. ->soap
  131. ->service()
  132. ->updateRessource();
  133. /**
  134. *
  135. * set product manager & form data to service
  136. */
  137. $service
  138. ->setProductManager($productManager)
  139. ->setFormData($this->formData);
  140. logModuleCall(
  141. 'zimbraEmail',
  142. __FUNCTION__,
  143. $this->formData,
  144. 'Debug Update Form',
  145. $service->getFormData()
  146. );
  147. /**
  148. * run service
  149. */
  150. $result = $service->run();
  151. /**
  152. * return success or error response
  153. */
  154. if(!$result)
  155. {
  156. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  157. }
  158. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  159. }
  160. /**
  161. * @return HtmlDataJsonResponse
  162. */
  163. public function updateStatus()
  164. {
  165. /**
  166. * hosting id
  167. */
  168. $hid = $this->request->get('id');
  169. /**
  170. * product manager allow to check product settings
  171. */
  172. $productManager = new ProductManager();
  173. $productManager->loadByHostingId($hid);
  174. /**
  175. *
  176. * get soap create domain service
  177. */
  178. $service =(new ZimbraManager())
  179. ->getApiByHosting($hid)
  180. ->soap
  181. ->service()
  182. ->updateRessourceStatus()
  183. ->setProductManager($productManager)
  184. ;
  185. /**
  186. *
  187. * set product manager & form data to service
  188. */
  189. /**
  190. * run service for each id
  191. */
  192. $service->setFormData($this->formData);
  193. $result = $service->run();
  194. if(!$result)
  195. {
  196. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  197. }
  198. /**
  199. * return success
  200. */
  201. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  202. }
  203. /**
  204. * @return HtmlDataJsonResponse
  205. */
  206. public function changePassword()
  207. {
  208. /**
  209. * hosting id
  210. */
  211. $hid = $this->request->get('id');
  212. /**
  213. * product manager allow to check product settings
  214. */
  215. $productManager = new ProductManager();
  216. $productManager->loadByHostingId($hid);
  217. /**
  218. *
  219. * get soap create domain service
  220. */
  221. $service =(new ZimbraManager())
  222. ->getApiByHosting($hid)
  223. ->soap
  224. ->service()
  225. ->updateRessourcePassword()
  226. ->setProductManager($productManager)
  227. ;
  228. /**
  229. *
  230. * set product manager & form data to service
  231. */
  232. /**
  233. * run service for each id
  234. */
  235. $service->setFormData($this->formData);
  236. $result = $service->run();
  237. if(!$result)
  238. {
  239. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  240. }
  241. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  242. }
  243. }