AccountDataProvider.php 9.0 KB

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