RessourceDataProvider.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\Ressource\Providers;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Enums\ProductParams;
  4. use ModulesGarden\Servers\ZimbraEmail\App\Enums\Size;
  5. use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
  6. use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
  7. use ModulesGarden\Servers\ZimbraEmail\App\Http\Admin\ProductConfiguration;
  8. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
  9. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Helpers\ServiceFactory;
  10. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Ressource;
  11. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\ClassOfService;
  12. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository\ClassOfServices;
  13. use ModulesGarden\Servers\ZimbraEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  14. use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\di;
  15. use ModulesGarden\Servers\ZimbraEmail\Core\Http\JsonResponse;
  16. use ModulesGarden\Servers\ZimbraEmail\Core\Models\Whmcs\Hosting;
  17. use ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  18. use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  19. /**
  20. *
  21. * Created by PhpStorm.
  22. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  23. * Date: 10.09.19
  24. * Time: 13:06
  25. * Class RessourceDataProvider
  26. */
  27. class RessourceDataProvider extends BaseDataProvider
  28. {
  29. public function read()
  30. {
  31. /**
  32. * hosting id
  33. */
  34. $hid = $this->request->get('id');
  35. $hosting = Hosting::where('id', $hid)->first();
  36. //todo refactor
  37. $this->data['domain'] = $hosting->domain;
  38. $lang = di('lang');
  39. $this->availableValues['status'] = [
  40. Zimbra::ACC_STATUS_ACTIVE => $lang->absoluteT('zimbra','account','status','active'),
  41. Zimbra::ACC_STATUS_LOCKED => $lang->absoluteT('zimbra','account','status','locked'),
  42. Zimbra::ACC_STATUS_MAINTENANCE => $lang->absoluteT('zimbra','account','status','maintenance'),
  43. Zimbra::ACC_STATUS_CLOSED => $lang->absoluteT('zimbra','account','status','closed'),
  44. Zimbra::ACC_STATUS_LOCKOUT => $lang->absoluteT('zimbra','account','status','lockout'),
  45. Zimbra::ACC_STATUS_PENDING => $lang->absoluteT('zimbra','account','status','pending')
  46. ];
  47. $this->availableValues['type'] = [
  48. Zimbra::RES_TYPE_LOCATION => $lang->absoluteT('zimbra','account','status','active'),
  49. Zimbra::RES_TYPE_EQUIPMENT => $lang->absoluteT('zimbra','account','status','locked')
  50. ];
  51. /**
  52. * product manager allow to check product settings
  53. */
  54. $this->readCosParams();
  55. }
  56. public function create()
  57. {
  58. /**
  59. * hosting id
  60. */
  61. $hid = $this->request->get('id');
  62. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'company', 'title', 'country', 'state', 'city', 'street', 'post_code' ];
  63. foreach ($this->formData as $field => &$value)
  64. {
  65. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  66. }
  67. /**
  68. * product manager allow to check product settings
  69. */
  70. $productManager = new ProductManager();
  71. $productManager->loadByHostingId($hid);
  72. /**
  73. *
  74. * get soap create domain service
  75. */
  76. $service =(new ZimbraManager())
  77. ->getApiByHosting($hid)
  78. ->soap
  79. ->service()
  80. ->createRessource($productManager->get('cos_name'));
  81. /**
  82. *
  83. * set product manager & form data to service
  84. */
  85. $service
  86. ->setProductManager($productManager)
  87. ->setFormData($this->formData);
  88. /**
  89. *
  90. * run service
  91. */
  92. $result = $service->run();
  93. /**
  94. *
  95. * return success or error response
  96. */
  97. if(!$result)
  98. {
  99. return (new HtmlDataJsonResponse())->setMessageAndTranslate($service->getError())->setStatusError();
  100. }
  101. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailRessourceHasBeenAdded')->setStatusSuccess();
  102. }
  103. public function updateStatus()
  104. {
  105. }
  106. public function update()
  107. {
  108. /**
  109. * hosting id
  110. */
  111. $hid = $this->request->get('id');
  112. /**
  113. * product manager allow to check product settings
  114. */
  115. $productManager = new ProductManager();
  116. $productManager->loadByHostingId($hid);
  117. /**
  118. *
  119. * get soap create domain service
  120. */
  121. $service =(new ZimbraManager())
  122. ->getApiByHosting($hid)
  123. ->soap
  124. ->service()
  125. ->updateRessourceStatus()
  126. ->setProductManager($productManager)
  127. ;
  128. /**
  129. *
  130. * set product manager & form data to service
  131. */
  132. /**
  133. * run service for each id
  134. */
  135. foreach($this->request->get('massActions') as $id)
  136. {
  137. $service->setFormData(['status' => $this->formData['status'], 'id' => $id]);
  138. $result = $service->run();
  139. }
  140. /**
  141. * return success
  142. */
  143. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massRessourceStatusHasBeenUpdated')->setStatusSuccess();
  144. }
  145. /**
  146. *
  147. * read data per cos_name
  148. */
  149. protected function readCosParams()
  150. {
  151. $hid = $this->request->get('id');
  152. /**
  153. *
  154. * load product manager
  155. */
  156. $productManager = new ProductManager();
  157. $productManager->loadByHostingId($hid);
  158. /**
  159. *
  160. * check if class of services should be selectable
  161. */
  162. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  163. {
  164. /**
  165. *
  166. * get soap create domain service
  167. */
  168. $api =(new ZimbraManager())
  169. ->getApiByHosting($hid)
  170. ->soap;
  171. /**
  172. *
  173. * get cos from API
  174. */
  175. $classOfServices = $api->repository()->cos->all();
  176. /**
  177. *
  178. * load configurable options coses
  179. */
  180. $supportedCos = $productManager->getSettingCos();
  181. /**
  182. *
  183. * add COS to array
  184. */
  185. $configoptions = $this->getFilteredCosConfigurableOptions();
  186. foreach($classOfServices as $cos)
  187. {
  188. /**
  189. *
  190. *
  191. * skip COS which is not used in configurable options
  192. */
  193. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  194. {
  195. continue;
  196. }
  197. /**
  198. * 1. check if config opts are available
  199. * 2. skip if class of services doesnt exists in config option list
  200. */
  201. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  202. {
  203. continue;
  204. }
  205. /**
  206. * 1. check if config opts are available
  207. * 2. skip not purchased as CO
  208. */
  209. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  210. {
  211. continue;
  212. }
  213. /**
  214. * 1. check if config opts are not available
  215. * 2. skip if quantity === 0
  216. */
  217. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  218. {
  219. continue;
  220. }
  221. /* @var $cos ClassOfService*/
  222. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  223. }
  224. return $this;
  225. }
  226. /**
  227. *
  228. * check if class of service is choosen by config opt
  229. */
  230. if($productManager->get('cos_name') === ClassOfServices::ZIMBRA_CONFIG_OPTIONS)
  231. {
  232. $this->data['cosId'] = key($productManager->getSettingCos());
  233. return $this;
  234. }
  235. /**
  236. *
  237. * if cos_name is dedicated (loaded from API)
  238. */
  239. if($productManager->get('cos_name') !== ClassOfServices::CUSTOM_ZIMBRA)
  240. {
  241. /**
  242. * if dedicated class of service has been selected
  243. */
  244. $this->data['cosId'] = $productManager->get('cos_name');
  245. return $this;
  246. }
  247. return $this;
  248. }
  249. /**
  250. * @return bool|mixed
  251. */
  252. protected function getFilteredCosConfigurableOptions()
  253. {
  254. $configoptions = $this->getWhmcsParamByKey('configoptions');
  255. foreach($configoptions as $key => $value)
  256. {
  257. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  258. {
  259. unset($configoptions[$key]);
  260. }
  261. }
  262. return $configoptions;
  263. }
  264. }