EditRessourceDataProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. logModuleCall(
  51. 'zimbraEmail',
  52. __FUNCTION__,
  53. $result,
  54. 'Debug Ressource',
  55. $this->availableValues
  56. );
  57. $mailBoxParams = explode('@', $result->getName());
  58. $this->data['id'] = $result->getId();
  59. $this->data['username'] = $mailBoxParams[0];
  60. $this->data['domain'] = $mailBoxParams[1];
  61. $this->data['display_name'] = $result->getDataResourceA(Ressource::ATTR_DISPLAY_NAME);
  62. $this->data['status'] = $result->getDataResourceA(Ressource::ATTR_STATUS);
  63. $this->data['company'] = $result->getDataResourceA(Ressource::ATTR_COMPANY);
  64. $this->data['title'] = $result->getDataResourceA(Ressource::ATTR_PROF_TITLE);
  65. $this->data['phone'] = $result->getDataResourceA(Ressource::ATTR_PHONE);
  66. $this->data['home_phone'] = $result->getDataResourceA(Ressource::ATTR_HOME_PHONE);
  67. $this->data['mobile_phone'] = $result->getDataResourceA(Ressource::ATTR_MOBILE_PHONE);
  68. $this->data['fax'] = $result->getDataResourceA(Ressource::ATTR_FAX);
  69. $this->data['pager'] = $result->getDataResourceA(Ressource::ATTR_PAGER);
  70. $this->data['country'] = $result->getDataResourceA(Ressource::ATTR_COUNTRY);
  71. $this->data['city'] = $result->getDataResourceA(Ressource::ATTR_CITY);
  72. $this->data['street'] = $result->getDataResourceA(Ressource::ATTR_STREET);
  73. $this->data['post_code'] = $result->getDataResourceA(Ressource::ATTR_POSTAL_CODE);
  74. $this->data['state'] = $result->getDataResourceA(Ressource::ATTR_STATE);
  75. $lang = di('lang');
  76. $this->availableValues['status'] = [
  77. Zimbra::ACC_STATUS_ACTIVE => $lang->absoluteT('zimbra','account','status','active'),
  78. Zimbra::ACC_STATUS_LOCKED => $lang->absoluteT('zimbra','account','status','locked'),
  79. Zimbra::ACC_STATUS_MAINTENANCE => $lang->absoluteT('zimbra','account','status','maintenance'),
  80. Zimbra::ACC_STATUS_CLOSED => $lang->absoluteT('zimbra','account','status','closed'),
  81. Zimbra::ACC_STATUS_LOCKOUT => $lang->absoluteT('zimbra','account','status','lockout'),
  82. Zimbra::ACC_STATUS_PENDING => $lang->absoluteT('zimbra','account','status','pending')
  83. ];
  84. $this->readCosParams();
  85. }
  86. /**
  87. * @return HtmlDataJsonResponse
  88. */
  89. public function update()
  90. {
  91. /**
  92. * hosting id
  93. */
  94. $hid = $this->request->get('id');
  95. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'company', 'title', 'country', 'state', 'city', 'street', 'post_code' ];
  96. foreach ($this->formData as $field => &$value)
  97. {
  98. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  99. }
  100. /**
  101. * product manager allow to check product settings
  102. */
  103. $productManager = new ProductManager();
  104. $productManager->loadByHostingId($hid);
  105. /**
  106. *
  107. * get soap create domain service
  108. */
  109. $service =(new ZimbraManager())
  110. ->getApiByHosting($hid)
  111. ->soap
  112. ->service()
  113. ->updateRessource($productManager->get('cos_name'));
  114. /**
  115. *
  116. * set product manager & form data to service
  117. */
  118. $service
  119. ->setProductManager($productManager)
  120. ->setFormData($this->formData);
  121. /**
  122. * run service
  123. */
  124. $result = $service->run();
  125. /**
  126. * return success or error response
  127. */
  128. if(!$result)
  129. {
  130. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  131. }
  132. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  133. }
  134. /**
  135. * @return HtmlDataJsonResponse
  136. */
  137. public function updateStatus()
  138. {
  139. /**
  140. * hosting id
  141. */
  142. $hid = $this->request->get('id');
  143. /**
  144. * product manager allow to check product settings
  145. */
  146. $productManager = new ProductManager();
  147. $productManager->loadByHostingId($hid);
  148. /**
  149. *
  150. * get soap create domain service
  151. */
  152. $service =(new ZimbraManager())
  153. ->getApiByHosting($hid)
  154. ->soap
  155. ->service()
  156. ->updateRessourceStatus()
  157. ->setProductManager($productManager)
  158. ;
  159. /**
  160. *
  161. * set product manager & form data to service
  162. */
  163. /**
  164. * run service for each id
  165. */
  166. $service->setFormData($this->formData);
  167. $result = $service->run();
  168. if(!$result)
  169. {
  170. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  171. }
  172. /**
  173. * return success
  174. */
  175. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  176. }
  177. /**
  178. * @return HtmlDataJsonResponse
  179. */
  180. public function changePassword()
  181. {
  182. /**
  183. * hosting id
  184. */
  185. $hid = $this->request->get('id');
  186. /**
  187. * product manager allow to check product settings
  188. */
  189. $productManager = new ProductManager();
  190. $productManager->loadByHostingId($hid);
  191. /**
  192. *
  193. * get soap create domain service
  194. */
  195. $service =(new ZimbraManager())
  196. ->getApiByHosting($hid)
  197. ->soap
  198. ->service()
  199. ->updateRessourcePassword()
  200. ->setProductManager($productManager)
  201. ;
  202. /**
  203. *
  204. * set product manager & form data to service
  205. */
  206. /**
  207. * run service for each id
  208. */
  209. $service->setFormData($this->formData);
  210. $result = $service->run();
  211. if(!$result)
  212. {
  213. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  214. }
  215. return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
  216. }
  217. /**
  218. *
  219. */
  220. public function readCosParams()
  221. {
  222. $hid = $this->getRequestValue('id');
  223. /**
  224. * product manager allow to check product settings
  225. */
  226. $productManager = new ProductManager();
  227. $productManager->loadByHostingId($hid);
  228. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  229. {
  230. /**
  231. *
  232. * get soap create domain service
  233. */
  234. $api =(new ZimbraManager())
  235. ->getApiByHosting($hid)
  236. ->soap;
  237. /**
  238. *
  239. * get cos from API
  240. */
  241. $classOfServices = $api->repository()->cos->all();
  242. /**
  243. *
  244. * load configurable options coses
  245. */
  246. $supportedCos = $productManager->getSettingCos();
  247. /**
  248. *
  249. * add COS to array
  250. */
  251. $configoptions = $this->getFilteredCosConfigurableOptions();
  252. foreach($classOfServices as $cos)
  253. {
  254. /**
  255. *
  256. *
  257. * skip COS which is not used in configurable options
  258. */
  259. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  260. {
  261. continue;
  262. }
  263. /**
  264. *
  265. * skip if class of services doesnt exists in config option list
  266. */
  267. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  268. {
  269. continue;
  270. }
  271. /**
  272. * skip not purchased as CO
  273. */
  274. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  275. {
  276. continue;
  277. }
  278. /**
  279. * 1. check if config opts are not available
  280. * 2. skip if quantity === 0
  281. */
  282. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  283. {
  284. continue;
  285. }
  286. /* @var $cos ClassOfService*/
  287. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  288. }
  289. }
  290. }
  291. /**
  292. * @return bool|mixed
  293. */
  294. protected function getFilteredCosConfigurableOptions()
  295. {
  296. $configoptions = $this->getWhmcsParamByKey('configoptions');
  297. foreach($configoptions as $key => $value)
  298. {
  299. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  300. {
  301. unset($configoptions[$key]);
  302. }
  303. }
  304. return $configoptions;
  305. }
  306. }