ProductManager.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace ModulesGarden\Servers\KerioEmail\App\Libs\Product;
  3. use ModulesGarden\Servers\KerioEmail\App\Enums\ControllerEnums;
  4. use ModulesGarden\Servers\KerioEmail\App\Enums\ProductParams;
  5. use ModulesGarden\Servers\KerioEmail\App\Enums\Size;
  6. use ModulesGarden\Servers\KerioEmail\App\Enums\Kerio;
  7. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Connection;
  8. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  9. use ModulesGarden\Servers\KerioEmail\App\Services\ConfigurableOptions\Helper\TypeConstans;
  10. use ModulesGarden\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  11. use ModulesGarden\Servers\KerioEmail\App\Traits\ConfigTrait;
  12. use ModulesGarden\Servers\KerioEmail\App\Traits\ModuleConfigurationHandler;
  13. use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  14. use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\HostingConfigOption;
  15. use ModulesGarden\Servers\KerioEmail\App\Models\ProductConfiguration;
  16. use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\Server;
  17. /**
  18. *
  19. * Created by PhpStorm.
  20. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  21. * Date: 11.09.19
  22. * Time: 12:26
  23. * Class ProductManager
  24. */
  25. class ProductManager
  26. {
  27. use ConfigTrait;
  28. use ModuleConfigurationHandler;
  29. /**
  30. *
  31. * @var array
  32. */
  33. protected $cos = [];
  34. /**
  35. * @var array
  36. */
  37. protected $configOptCosLimits = [];
  38. /**
  39. * @var bool
  40. */
  41. protected $cosConfigOptionEnabled = false;
  42. /**
  43. * @var Hosting
  44. */
  45. protected $hosting = null;
  46. /**
  47. * @param $id
  48. * @return $this
  49. */
  50. public function loadByHostingId($id)
  51. {
  52. /**
  53. *
  54. * load hosting model
  55. */
  56. $this->hosting = Hosting::with('configOptions')->where('id', $id)->first();
  57. /**
  58. *
  59. * load product configuration
  60. */
  61. $this->loadById($this->hosting->packageid);
  62. /**
  63. *
  64. *
  65. * load config options (e.g class of services)
  66. */
  67. $this->loadConfigOptions($this->hosting->configOptions);
  68. /**
  69. *
  70. */
  71. return $this;
  72. }
  73. /**
  74. * @param $id
  75. * @return $this
  76. */
  77. public function loadById($id)
  78. {
  79. $config = ProductConfiguration::where('product_id', $id)->get();
  80. foreach ($config as $conf)
  81. {
  82. $this->set($conf->setting, $conf->value);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * @return Hosting
  88. */
  89. public function getHosting()
  90. {
  91. return $this->hosting;
  92. }
  93. /**
  94. * @param $configOpt
  95. * @return $this
  96. */
  97. public function loadConfigOptions($configOpt)
  98. {
  99. /**
  100. * configurable options to config array
  101. */
  102. foreach ($configOpt as $opt)
  103. {
  104. /**
  105. *
  106. * continue if conf opt is hidden
  107. */
  108. if((bool) $opt->configOption->hidden === true)
  109. {
  110. continue;
  111. }
  112. if(!$opt->configOption->isLinkedToProduct($this->hosting->packageid))
  113. {
  114. continue;
  115. }
  116. /* @var $opt HostingConfigOption */
  117. $option = explode('|', $opt->configOption->optionname);
  118. if (false !== strpos( $option[0], ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX))
  119. {
  120. $this->addCosConfigOpt($option[0], $opt->qty);
  121. continue;
  122. }
  123. /**
  124. * base setting
  125. */
  126. if ((int) $opt->configOption->optiontype === TypeConstans::QUANTITY)
  127. {
  128. $this->set($option[0], $opt->qty);
  129. continue;
  130. }
  131. elseif ((int) $opt->configOption->optiontype === TypeConstans::DROPDOWN)
  132. {
  133. /**
  134. * selected class of services
  135. */
  136. $name = explode('|', $opt->subOption->optionname);
  137. if($option[0] === 'cos')
  138. {
  139. /**
  140. * add to available cos list
  141. */
  142. $this->addCosConfigOpt($name[0], Size::UNLIMITED);
  143. }
  144. $this->set($option[0], $name[0]);
  145. continue;
  146. }
  147. }
  148. return $this;
  149. }
  150. /**
  151. * @param $cosName
  152. * @param int $quantity
  153. * @return $this
  154. */
  155. public function addCosConfigOpt($cosName, $quantity= 0)
  156. {
  157. $cosName = str_replace(ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX,'',$cosName);
  158. $this->cos[$cosName] = $quantity;
  159. $this->configOptCosLimits[$cosName] = $quantity;
  160. $this->setCosConfigOptionEnabled(true);
  161. return $this;
  162. }
  163. /**
  164. * @return mixed
  165. */
  166. public function getKerioConfiguration()
  167. {
  168. foreach($this->config as $name => $value)
  169. {
  170. if(false !== strpos($name, ProductParams::ZIMBRA_PREFIX_SETTINGS))
  171. {
  172. $tmp[$name] = $value;
  173. }
  174. }
  175. return $tmp;
  176. }
  177. /**
  178. * @return mixed
  179. */
  180. public function getSettingCos()
  181. {
  182. /**
  183. *
  184. * if type is `cosQuota` return saved class of services list
  185. */
  186. if($this->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
  187. {
  188. /**
  189. *
  190. * return cos from config opt or decoded from database
  191. */
  192. return $this->cos ? $this->cos : json_decode($this->get('cos'), true);
  193. }
  194. /**
  195. *
  196. * if type is `kerioConfigurableOptions` return choosed cos id
  197. */
  198. if($this->get('cos_name') === ClassOfServices::ZIMBRA_CONFIG_OPTIONS)
  199. {
  200. return $this->cos;
  201. }
  202. /**
  203. *
  204. * return cos name if is other type
  205. */
  206. if($this->get('cos_name') !== ClassOfServices::CUSTOM_ZIMBRA)
  207. {
  208. return $this->get('cos_name');
  209. }
  210. return null;
  211. }
  212. /**
  213. *
  214. * is available controller actions
  215. * @param $controller
  216. * @return bool
  217. */
  218. public function isControllerAccessible($controller)
  219. {
  220. return $this->get($controller) === ProductParams::SWITCHER_ENABLED;
  221. }
  222. /**
  223. * @param $name
  224. * @return bool
  225. */
  226. public function isSidebarEnabled($name)
  227. {
  228. switch ($name)
  229. {
  230. case 'emailAccount':
  231. $controller = ControllerEnums::EMAIL_ACCOUNT_PAGE;
  232. break;
  233. case 'emailAlias':
  234. $controller = ControllerEnums::EMAIL_ALIAS_PAGE;
  235. break;
  236. case 'ressource':
  237. $controller = ControllerEnums::RESSOURCE_PAGE;
  238. break;
  239. case 'distributionList':
  240. $controller = ControllerEnums::DISTRIBUTION_MAIL_PAGE;
  241. break;
  242. case 'domainAlias':
  243. $controller = ControllerEnums::DOMAIN_ALIAS_PAGE;
  244. break;
  245. case 'goWebmail':
  246. $controller = ControllerEnums::WEBMAIL_PAGE;
  247. break;
  248. }
  249. return $this->isActionAccessible($controller);
  250. }
  251. /**
  252. *
  253. * @param $action
  254. * @return bool
  255. */
  256. public function isActionAccessible($action)
  257. {
  258. return $this->get($action) === ProductParams::SWITCHER_ENABLED;
  259. }
  260. /**
  261. * @return string|null
  262. */
  263. public function getServerUrl()
  264. {
  265. if($this->hosting)
  266. {
  267. $server = $this->hosting->server()->first();
  268. $hostname = $server->hostname ? $server->hostname : $server->ipaddress;
  269. $url = ($server->secure ? 'https://' : 'http://') . $hostname.($server->secure ? ':'.Kerio::SECURE_PORT: ':'.Kerio::PORT).'/';
  270. return $url;
  271. }
  272. return null;
  273. }
  274. /**
  275. * @return string|null
  276. */
  277. public function getClientUrl()
  278. {
  279. if($this->hosting)
  280. {
  281. /**
  282. *
  283. * load server
  284. */
  285. $server = $this->hosting->server()->first();
  286. /**
  287. * set hostname if exist or IP address
  288. */
  289. $hostname = $server->hostname ? $server->hostname : $server->ipaddress;
  290. /**
  291. *
  292. * load port from configuration or set default port
  293. */
  294. $port = $server->secure === 'on' ? $this->loadModuleData()->getAll()['clienPortSecure'] : $this->loadModuleData()->getAll()['clienPort'];
  295. $port = $port ? $port : Connection::CLIENT_PORT;
  296. /**
  297. *
  298. * prepare url
  299. */
  300. $url = ($server->secure ? 'https://' : 'http://') . $hostname.':'.$port.'/';
  301. return $url;
  302. }
  303. return null;
  304. }
  305. /**
  306. * @return array
  307. */
  308. public function getConfigOptCosLimits()
  309. {
  310. return $this->configOptCosLimits;
  311. }
  312. /**
  313. * @param array $configOptCosLimits
  314. */
  315. public function setConfigOptCosLimits($configOptCosLimits)
  316. {
  317. $this->configOptCosLimits = $configOptCosLimits;
  318. }
  319. /**
  320. * @return bool
  321. */
  322. public function isCosConfigOptionEnabled()
  323. {
  324. return $this->cosConfigOptionEnabled;
  325. }
  326. /**
  327. * @param bool $cosConfigOptionEnabled
  328. */
  329. public function setCosConfigOptionEnabled($cosConfigOptionEnabled)
  330. {
  331. $this->cosConfigOptionEnabled = $cosConfigOptionEnabled;
  332. }
  333. }