PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/PagesController.php

https://bitbucket.org/vutmat281191/topluxury_shop
PHP | 191 lines | 153 code | 26 blank | 12 comment | 23 complexity | a92fdb12ad99f0025e3ea971f0e2436d MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Categories;
  6. use App\Products;
  7. use App\Video;
  8. use App\News;
  9. use App\Collection;
  10. use App\ContactUs;
  11. class PagesController extends Controller
  12. {
  13. public function __construct()
  14. {
  15. $categories = Categories::where('level', 1)->get();
  16. // $categories = Categories::all();
  17. view()->share('categories', $categories);
  18. }
  19. public function home()
  20. {
  21. return view('pages.home');
  22. }
  23. public function promotion()
  24. {
  25. return view('pages.promotion');
  26. }
  27. public function cart()
  28. {
  29. $arr_cart = [];
  30. $product_cart = [];
  31. if (isset($_COOKIE["list-in-cart"]) && !empty($_COOKIE["list-in-cart"])) {
  32. $list_cart = $_COOKIE["list-in-cart"];
  33. $arr_cart = explode(",", $list_cart);
  34. $arr_cart = array_unique($arr_cart);
  35. $arr_cart = array_filter($arr_cart);
  36. }
  37. $product_cart = Products::whereIn('id', $arr_cart)
  38. // ->join('color', 'users.id', '=', 'contacts.user_id')
  39. ->get();
  40. return view('pages.cart', ['product_cart' => $product_cart]);
  41. }
  42. public function policy()
  43. {
  44. return view('pages.policy');
  45. }
  46. public function aboutUs()
  47. {
  48. return view('pages.aboutUs');
  49. }
  50. public function contactUs()
  51. {
  52. return view('pages.contactUs');
  53. }
  54. public function submitContactUs(Request $request)
  55. {
  56. $contact = new ContactUs();
  57. $contact->full_name = $request->input('full_name');
  58. $contact->email = $request->input('email');
  59. $contact->phone_number = $request->input('phone_number');
  60. $contact->message = $request->input('message');
  61. $contact->save();
  62. return redirect('/');
  63. }
  64. public function storeCartGet($id) {
  65. $this->setCookieProduct("list-in-cart", $id);
  66. // return $_COOKIE["list-in-cart"];
  67. $arr_viewed = explode(",", $_COOKIE["list-in-cart"]);
  68. $arr_viewed = array_unique($arr_viewed);
  69. $arr_viewed = array_filter($arr_viewed);
  70. $this->setCookieProduct("cart-num", count($arr_viewed));
  71. $String_viewed = implode(",", $arr_viewed);
  72. // setcookie("list-in-cart", $String_viewed , time()+36000);
  73. return $String_viewed;
  74. }
  75. public function setCookieProduct($name, $val)
  76. {
  77. if (isset($_COOKIE[$name])) {
  78. setcookie($name, $_COOKIE[$name] . ',' . $val, time()+36000);
  79. } else setcookie($name, $val , time()+36000);
  80. }
  81. public function products($link)
  82. {
  83. $product = Products::where('link', $link)->first();
  84. $this->setCookieProduct("list-product", $link);
  85. $arr_viewed = [];
  86. if (isset($_COOKIE["list-product"])) {
  87. $list_viewed = $_COOKIE["list-product"];
  88. $arr_viewed = explode(",", $list_viewed);
  89. $arr_viewed = array_unique($arr_viewed);
  90. $arr_viewed = array_filter($arr_viewed);
  91. }
  92. $product_viewed = Products::whereIn('link', $arr_viewed)->get();
  93. return view('pages.product', ['product' => $product, 'product_viewed' => $product_viewed]);
  94. }
  95. public function getIdCategory($Obj)
  96. {
  97. return $Obj->id;
  98. }
  99. public function category($link)
  100. {
  101. //list category relate
  102. $list_cate = [];
  103. $cate = Categories::where('link', $link)->first();
  104. $template = $cate->template;
  105. if ($template == 'video') {
  106. $video = Video::all();
  107. return view('pages.category', ['video' => $video, 'link' => $link, 'template' => $template]);
  108. }
  109. if ($template == 'collection') {
  110. $collection = Collection::all();
  111. return view('pages.category', ['collection' => $collection, 'link' => $link, 'template' => $template]);
  112. }
  113. if ($template == 'new') {
  114. $News = News::all();
  115. return view('pages.category', ['News' => $News, 'link' => $link, 'template' => $template]);
  116. }
  117. $list_cate[] = $cate->id;
  118. $level2 = Categories::where('parent_id', $cate->id)->get();
  119. if ($level2->count() > 0) {
  120. foreach ($level2 as $l2) {
  121. $list_cate[] = $l2->id;
  122. $level3 = Categories::where('parent_id', $l2->id)->get();
  123. if ($level3->count() > 0) {
  124. foreach ($level3 as $l3) {
  125. $list_cate[] = $l3->id;
  126. }
  127. }
  128. }
  129. }
  130. // dd($list_cate);exit();
  131. $list_cate = array_unique($list_cate);
  132. $list_cate = array_filter($list_cate);
  133. //list product in list category
  134. $list_products = Products::whereIn('id', $list_cate);
  135. // order
  136. if (isset($_GET["price"])) {
  137. $list_products = $list_products->orderBy('price', $_GET["price"]);
  138. } else {
  139. $list_products = $list_products->orderBy('created_at', 'desc');
  140. }
  141. //filter by color
  142. if (isset($_GET["color"]) && isset(\App\Color::where('variable', $_GET["color"])->first()->id)) {
  143. $id_color = \App\Color::where('variable', $_GET["color"])->first()->id;
  144. $list_products = $list_products->where('color', $id_color);
  145. }
  146. //filter by size
  147. if (isset($_GET["size"]) && isset(\App\Size::where('name', $_GET["size"])->first()->id)) {
  148. $id_size = \App\Size::where('name', $_GET["size"])->first()->id;
  149. $list_products = $list_products->where('name', 'LIKE', "%$id_size%");;
  150. }
  151. //filter grid
  152. $grid = 3;
  153. $class_grid = ' col-sm-4 col-xs-4 ';
  154. if (isset($_GET["grid"])) {
  155. $grid = $_GET["grid"];
  156. if ((int)$grid == 5) {
  157. $class_grid = ' col-sm-3 grid-5 ';
  158. } else {
  159. $class_grid = ' col-sm-4 col-xs-4 ';
  160. }
  161. }
  162. // dd($list_products->get());exit();
  163. return view('pages.category', ['list_products' => $list_products->get(), 'link' => $link, 'class_grid' => $class_grid, 'template' => $cate->template, 'cat_content' => $cate->content]);
  164. }
  165. }