AccountDataProvider.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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'] = $this->getWhmcsParamByKey('customfields')['maildomain'];
  38. $lang = di('lang');
  39. $this->availableValues['status'] = [
  40. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  41. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  42. ];
  43. $this->availableValues['unit'] = [
  44. 'MegaBytes' => 'MB',
  45. 'GigaBytes' => 'GB',
  46. ];
  47. }
  48. public function create()
  49. {
  50. /**
  51. * hosting id
  52. */
  53. $hid = $this->request->get('id');
  54. $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
  55. foreach ($this->formData as $field => &$value)
  56. {
  57. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  58. }
  59. /**
  60. * product manager allow to check product settings
  61. */
  62. $productManager = new ProductManager();
  63. $productManager->loadByHostingId($hid);
  64. logModuleCall(
  65. 'kerioEmail',
  66. __FUNCTION__,
  67. $this->formData,
  68. 'Debug Add Account',
  69. $hid
  70. );
  71. return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenAdded')->setStatusSuccess();
  72. }
  73. public function updateStatus()
  74. {
  75. }
  76. public function update()
  77. {
  78. /**
  79. * hosting id
  80. */
  81. $hid = $this->request->get('id');
  82. /**
  83. * product manager allow to check product settings
  84. */
  85. $productManager = new ProductManager();
  86. $productManager->loadByHostingId($hid);
  87. /**
  88. * run service for each id
  89. */
  90. foreach($this->request->get('massActions') as $id)
  91. {
  92. $service->setFormData(['status' => $this->formData['status'], 'id' => $id]);
  93. }
  94. /**
  95. * return success
  96. */
  97. return (new HtmlDataJsonResponse())->setMessageAndTranslate('massEmailAccountStatusHasBeenUpdated')->setStatusSuccess();
  98. }
  99. /**
  100. *
  101. * read data per cos_name
  102. */
  103. protected function readCosParams()
  104. {
  105. $hid = $this->request->get('id');
  106. /**
  107. *
  108. * load product manager
  109. */
  110. $productManager = new ProductManager();
  111. $productManager->loadByHostingId($hid);
  112. /**
  113. *
  114. * check if class of services should be selectable
  115. */
  116. if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  117. {
  118. /**
  119. *
  120. * get soap create domain service
  121. */
  122. $api =(new KerioManager())
  123. ->getApiByHosting($hid)
  124. ->soap;
  125. /**
  126. *
  127. * get cos from API
  128. */
  129. $classOfServices = $api->repository()->cos->all();
  130. /**
  131. *
  132. * load configurable options coses
  133. */
  134. $supportedCos = $productManager->getSettingCos();
  135. /**
  136. *
  137. * add COS to array
  138. */
  139. $configoptions = $this->getFilteredCosConfigurableOptions();
  140. foreach($classOfServices as $cos)
  141. {
  142. /**
  143. *
  144. *
  145. * skip COS which is not used in configurable options
  146. */
  147. if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
  148. {
  149. continue;
  150. }
  151. /**
  152. * 1. check if config opts are available
  153. * 2. skip if class of services doesnt exists in config option list
  154. */
  155. if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
  156. {
  157. continue;
  158. }
  159. /**
  160. * 1. check if config opts are available
  161. * 2. skip not purchased as CO
  162. */
  163. if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
  164. {
  165. continue;
  166. }
  167. /**
  168. * 1. check if config opts are not available
  169. * 2. skip if quantity === 0
  170. */
  171. if(!$configoptions && $supportedCos[$cos->getId()] == 0)
  172. {
  173. continue;
  174. }
  175. /* @var $cos ClassOfService*/
  176. $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
  177. }
  178. return $this;
  179. }
  180. /**
  181. *
  182. * check if class of service is choosen by config opt
  183. */
  184. if($productManager->get('cos_name') === ClassOfServices::KERIO_CONFIG_OPTIONS)
  185. {
  186. $this->data['cosId'] = key($productManager->getSettingCos());
  187. return $this;
  188. }
  189. /**
  190. *
  191. * if cos_name is dedicated (loaded from API)
  192. */
  193. if($productManager->get('cos_name') !== ClassOfServices::CUSTOM_KERIO)
  194. {
  195. /**
  196. * if dedicated class of service has been selected
  197. */
  198. $this->data['cosId'] = $productManager->get('cos_name');
  199. return $this;
  200. }
  201. return $this;
  202. }
  203. /**
  204. * @return bool|mixed
  205. */
  206. protected function getFilteredCosConfigurableOptions()
  207. {
  208. $configoptions = $this->getWhmcsParamByKey('configoptions');
  209. foreach($configoptions as $key => $value)
  210. {
  211. if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
  212. {
  213. unset($configoptions[$key]);
  214. }
  215. }
  216. return $configoptions;
  217. }
  218. }