BuildTlds.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Helper;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use ModulesGarden\ProxmoxAddon\App\Models\BuildTlds\Collection;
  5. use ModulesGarden\ProxmoxAddon\App\Models\Doe\DomainCategory;
  6. use ModulesGarden\ProxmoxAddon\App\Models\Doe\DomainCategoryRelations;
  7. use ModulesGarden\ProxmoxAddon\App\Models\Doe\DomainLabel;
  8. use ModulesGarden\ProxmoxAddon\App\UI\DoeSectionsSettings\Helpers\DoeSearchTypes;
  9. use ModulesGarden\ProxmoxAddon\App\UI\DoeSectionsSettings\Providers\DoeSettingsDataProvider;
  10. use ModulesGarden\ProxmoxAddon\Core\Helper\DomainHelper;
  11. use ModulesGarden\ProxmoxAddon\Core\Http\Request;
  12. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Client;
  13. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Currency;
  14. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\DomainPricing;
  15. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Pricing;
  16. /**
  17. * Description of BuildTlds
  18. *
  19. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  20. */
  21. class BuildTlds
  22. {
  23. /**
  24. *
  25. * @var DomainCategory
  26. */
  27. protected $domainCategory;
  28. /**
  29. * @var DomainPricing
  30. */
  31. protected $domainPricing;
  32. /**
  33. * @var Pricing
  34. */
  35. protected $pricing;
  36. /**
  37. * @var DomainCategoryRelations
  38. */
  39. protected $domainCategoryRelations;
  40. /**
  41. * @var Client
  42. */
  43. protected $client;
  44. /**
  45. * @var Request
  46. */
  47. protected $request;
  48. /**
  49. * @var Currency
  50. */
  51. protected $currency;
  52. protected $specialTld;
  53. /**
  54. * @var Collection
  55. */
  56. protected $collection;
  57. /**
  58. * @var DomainLabel
  59. */
  60. protected $domainLabel;
  61. /**
  62. * @var DoeSettingsDataProvider
  63. */
  64. protected $doeSettingsDataProvider;
  65. /**
  66. * @var string
  67. */
  68. protected $domainSearchType;
  69. /**
  70. * @var array
  71. */
  72. protected $tlds = [];
  73. /**
  74. * @var array
  75. */
  76. protected $categoresTld = [];
  77. protected $subCategores = [];
  78. public function __construct(DoeSettingsDataProvider $doeSettingsDataProvider, DomainLabel $domainLabel, Collection $collection, Currency $currency, Request $request, Client $client, Pricing $pricing, DomainPricing $domainPricing, DomainCategory $domainCategory, DomainCategoryRelations $domainCategoryRelations)
  79. {
  80. $this->domainCategoryRelations = $domainCategoryRelations;
  81. $this->doeSettingsDataProvider = $doeSettingsDataProvider;
  82. $this->domainLabel = $domainLabel;
  83. $this->collection = $collection;
  84. $this->currency = $currency;
  85. $this->request = $request;
  86. $this->client = $client;
  87. $this->pricing = $pricing;
  88. $this->domainCategory = $domainCategory;
  89. $this->domainPricing = $domainPricing;
  90. $this->loadData();
  91. }
  92. protected function loadData()
  93. {
  94. $sessionDomain = $this->getDomainWithSession();
  95. $this->collection->addSessionDomain($sessionDomain)
  96. ->setSessionSearchDomain($this->request->get('formData')['search'])
  97. ->setSessionCartDomain($this->request->getSession('cart', ['domains' => []])['domains']);
  98. if ($this->getDomainSearchType() === DoeSearchTypes::CATEGORY_SEARCH || $this->getDomainSearchType() === DoeSearchTypes::MULTIPLE_DOMAIN_SEARCH)
  99. {
  100. $this->buildCategoryTree($this->domainCategory->orderBy('suggested', 'DESC')->orderBy('order', 'ASC')->get()->toArray());
  101. $this->collection->generateCollection($this->categoresTld);
  102. }
  103. $princing = $this->pricing->getTable();
  104. $curency = $this->getCurrency();
  105. $this->collection->addCurrentName($this->getCurrencyName($curency));
  106. $query = $this->domainCategory
  107. ->WithDomainCategoresRelations()
  108. ->WithDomainPrincing()
  109. ->WithPrincing()
  110. ->WithDomainLabelRelations()
  111. ->WithDomainLabel()
  112. ->select(
  113. $this->domainCategory->getTable() . '.id', $this->domainCategory->getTable() . '.parent_id', $this->domainCategory->getTable() . '.header', $this->domainCategory->getTable() . '.title', $this->domainCategory->getTable() . '.type', $this->domainCategory->getTable() . '.order', $this->domainCategory->getTable() . '.suggested', "{$this->domainPricing->getTable()}.id as tld_id", "{$this->domainCategoryRelations->getTable()}.order as tld_order", "{$this->domainCategoryRelations->getTable()}.suggested as tld_suggested", "{$this->domainPricing->getTable()}.extension as tld_extension", "{$princing}.id as prince_id", "{$princing}.currency as tld_currency", "{$princing}.type as prince_type", "{$princing}.msetupfee as prince_one", "{$princing}.qsetupfee as prince_two", "{$princing}.ssetupfee as prince_three", "{$princing}.asetupfee as prince_four", "{$princing}.bsetupfee as prince_five", "{$princing}.monthly as prince_six", "{$princing}.quarterly as prince_seven", "{$princing}.semiannually as prince_eight", "{$princing}.annually as prince_nine", "{$princing}.biennially as prince_ten", "{$this->domainLabel->getTable()}.id as label_id", "{$this->domainLabel->getTable()}.title as label_title", "{$this->domainLabel->getTable()}.message as label_message", "{$this->domainLabel->getTable()}.order as label_order", "{$this->domainLabel->getTable()}.color as label_color", "{$this->domainLabel->getTable()}.background_color as label_background_color"
  114. )
  115. ->where("{$this->pricing->getTable()}.currency", "LIKE", $curency)
  116. ->where(DB::raw("(IF(`{$princing}`.`msetupfee` >= 0, `{$princing}`.`msetupfee`, 0) "
  117. . "+ IF(`{$princing}`.`qsetupfee` >= 0, `{$princing}`.`qsetupfee`, 0) "
  118. . "+ IF(`{$princing}`.`ssetupfee` >= 0, `{$princing}`.`ssetupfee`, 0) "
  119. . "+ IF(`{$princing}`.`asetupfee` >= 0, `{$princing}`.`asetupfee`, 0) "
  120. . "+ IF(`{$princing}`.`bsetupfee` >= 0, `{$princing}`.`bsetupfee`, 0) "
  121. . "+ IF(`{$princing}`.`monthly` >= 0, `{$princing}`.`monthly`, 0) "
  122. . "+ IF(`{$princing}`.`quarterly` >= 0, `{$princing}`.`quarterly`, 0) "
  123. . "+ IF(`{$princing}`.`annually` >= 0, `{$princing}`.`annually`, 0) "
  124. . "+ IF(`{$princing}`.`biennially` >= 0, `{$princing}`.`biennially`, 0) "
  125. . "+ IF(`{$princing}`.`semiannually` >= 0, `{$princing}`.`semiannually`, 0))"), '>', 0.00);
  126. if ($this->getDomainSearchType() === DoeSearchTypes::CATEGORY_SEARCH || $this->getDomainSearchType() === DoeSearchTypes::MULTIPLE_DOMAIN_SEARCH)
  127. {
  128. $query = $query->orderBy('suggested', 'DESC')
  129. ->orderBy('order', 'ASC')
  130. ->orderBy("{$this->domainCategoryRelations->getTable()}.suggested", 'DESC')
  131. ->orderBy("{$this->domainLabel->getTable()}.order", 'DESC')
  132. ->orderBy("{$this->domainCategoryRelations->getTable()}.order", 'ASC');
  133. $return = $query->get()->toArray();
  134. $this->collection->addTlds($return)->setDomainSearchType($this->getDomainSearchType());
  135. }
  136. elseif ($this->getDomainSearchType() === DoeSearchTypes::SINGLE_DOMAIN_SEARCH)
  137. {
  138. $query = $query->orderBy("{$this->domainCategoryRelations->getTable()}.suggested", 'DESC')
  139. ->orderBy("{$this->domainLabel->getTable()}.order", 'DESC')
  140. ->orderBy("{$this->domainCategoryRelations->getTable()}.order", 'ASC');
  141. $return = $query->get()->toArray();
  142. $this->collection->addTldsWithoutCategory($return)->setDomainSearchType($this->getDomainSearchType());
  143. }
  144. $this->buildTld($return);
  145. }
  146. protected function getDomainWithSession()
  147. {
  148. $domains = [];
  149. try
  150. {
  151. $domainsSession = $this->request->getSession('cart')['domains'];
  152. foreach ($domainsSession as $domain)
  153. {
  154. $domain = new DomainHelper($domain['domain']);
  155. $domains[$domain->getDomain()][] = $domain->getTLDWithDot();
  156. }
  157. }
  158. catch (\Exception $exc)
  159. {
  160. }
  161. return $domains;
  162. }
  163. public function getDomainSearchType()
  164. {
  165. if (isset($this->domainSearchType) === false)
  166. {
  167. $this->domainSearchType = $this->doeSettingsDataProvider->getValueById('domainSearchType')['value'];
  168. }
  169. return $this->domainSearchType;
  170. }
  171. protected function buildCategoryTree($data)
  172. {
  173. foreach ($data as $record)
  174. {
  175. if ($record['parent_id'] !== 0)
  176. {
  177. $this->subCategores[] = $record;
  178. }
  179. else
  180. {
  181. $this->createCategory($record);
  182. }
  183. }
  184. foreach ($this->subCategores as $record)
  185. {
  186. $this->createSubCategory($record);
  187. }
  188. return $this;
  189. }
  190. private function createCategory($data)
  191. {
  192. $this->categoresTld[$data['id']] = [
  193. 'id' => $data['id'],
  194. 'parent_id' => $data['parent_id'],
  195. 'title' => $data['title'],
  196. 'header' => $data['header'],
  197. 'type' => $data['type'],
  198. 'suggested' => $data['suggested'],
  199. 'order' => $data['order']
  200. ];
  201. if (isset($this->categoresTld[$data['id']]['subCategory']) === false)
  202. {
  203. $this->categoresTld[$data['id']]['subCategory'] = [];
  204. }
  205. return $this;
  206. }
  207. private function createSubCategory($data)
  208. {
  209. $this->categoresTld[$data['parent_id']]['subCategory'][$data['id']] = [
  210. 'id' => $data['id'],
  211. 'parent_id' => $data['parent_id'],
  212. 'title' => $data['title'],
  213. 'header' => $data['header'],
  214. 'type' => $data['type'],
  215. 'suggested' => $data['suggested'],
  216. 'order' => $data['order']
  217. ];
  218. return $this;
  219. }
  220. protected function getCurrency()
  221. {
  222. if ($userId = $this->request->getSession("uid", false))
  223. {
  224. $currency = $this->client->find($userId)->toArray()['currency'];
  225. }
  226. elseif ($currencyId = $this->request->getSession("currency", false))
  227. {
  228. $currency = $currencyId;
  229. }
  230. else
  231. {
  232. $currency = $this->currency->where("default", "LIKE", 1)->first()->toArray()['id'];
  233. }
  234. return $currency;
  235. }
  236. protected function getCurrencyName($id)
  237. {
  238. return $this->currency->where("id", "LIKE", $id)->first()->toArray()['code'];
  239. }
  240. protected function buildTld($data)
  241. {
  242. foreach ($data as $record)
  243. {
  244. if (in_array($record['tld_extension'], $this->tlds, true) === false)
  245. {
  246. $this->tlds[$record['tld_extension']] = $record['tld_extension'];
  247. }
  248. }
  249. return $this;
  250. }
  251. public function setSpecialChoice($specialChoice)
  252. {
  253. $this->specialTld = $specialChoice;
  254. return $this;
  255. }
  256. public function getSpecialTld()
  257. {
  258. return $this->collection->getSpecialTld($this->specialTld);
  259. }
  260. public function getTlds()
  261. {
  262. $tlds = $this->tlds;
  263. foreach ($this->collection->getRemoveTlds() as $tld)
  264. {
  265. if (array_key_exists($tld, $tlds))
  266. {
  267. unset($tlds[$tld]);
  268. }
  269. }
  270. return $tlds;
  271. }
  272. public function getTldsCount()
  273. {
  274. return (count($this->tlds) - $this->collection->getSessionFindTld());
  275. }
  276. public function getCategoresTld()
  277. {
  278. return $this->collection->getDataCollection();
  279. }
  280. protected function buildCategoresTld($data)
  281. {
  282. foreach ($data as $record)
  283. {
  284. if ($record['parent_id'] !== 0)
  285. {
  286. $this->addTldToSubCategory($record);
  287. }
  288. else
  289. {
  290. $this->addTldToCategory($record);
  291. }
  292. }
  293. return $this;
  294. }
  295. private function addTldToSubCategory($data)
  296. {
  297. if (in_array(
  298. [
  299. 'id' => $data['id_tld'],
  300. 'name' => $data['extension']
  301. ], $this->categoresTld[$data['parent_id']]['subCategory'][$data['id']]['tlds'], true
  302. ) === false
  303. )
  304. {
  305. $this->categoresTld[$data['parent_id']]['subCategory'][$data['id']]['tlds'][] = ['id' => $data['id_tld'], 'name' => $data['extension']];
  306. }
  307. return $this;
  308. }
  309. private function addTldToCategory($data)
  310. {
  311. if (in_array(
  312. [
  313. 'id' => $data['id_tld'],
  314. 'name' => $data['extension']
  315. ], $this->categoresTld[$data['id']]['tlds'], true
  316. ) === false
  317. )
  318. {
  319. $this->categoresTld[$data['id']]['tlds'][] = ['id' => $data['id_tld'], 'name' => $data['extension']];
  320. }
  321. return $this;
  322. }
  323. }