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

/app/Http/Controllers/HomeController.php

https://bitbucket.org/laravelte2018/firlaravelpro
PHP | 236 lines | 194 code | 41 blank | 1 comment | 9 complexity | 54e1d7b8e8ccab880acc4525980088de MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Comment;
  4. use App\Book;
  5. use App\Language;
  6. use App\Publisher;
  7. use App\Author;
  8. use App\Picture;
  9. use App\User;
  10. use App\Type;
  11. use App\Order_product;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Http\Request;
  15. use App\Mail\SendFeedback;
  16. class HomeController extends Controller {
  17. public function index() {
  18. $listBestSelling = DB::select('SELECT book_id, SUM(order_products.quantity) AS total '
  19. . 'FROM order_products GROUP BY book_id ORDER BY SUM(order_products.quantity) DESC LIMIT 10');
  20. $bestSellProducts = null;
  21. foreach ($listBestSelling as $key => $product) {
  22. $bestSellProducts[] = Book::with("pictures")
  23. ->where('id' ,$product->book_id)
  24. ->groupBy('id')
  25. ->get();
  26. }
  27. //echo var_dump($bestSellProducts[0]->first()->pictures->first()->url);
  28. $types = Type::where('is_thumbnail', '=', '0')
  29. ->paginate(5);
  30. $authors = Author::where('is_thumbnail', '=', '0')
  31. ->paginate(5);
  32. $languages = Language::where('is_thumbnail', '=', '0')
  33. ->paginate(5);
  34. $publishers = Publisher::where('is_thumbnail', '=', '0')
  35. ->paginate(5);
  36. $newProducts = Book::with('pictures')
  37. ->where('is_thumbnail', '=', '0')
  38. ->orderBy('created_at', 'desc')
  39. ->paginate(3);
  40. $randomProducts = Book::with('pictures')
  41. ->where('is_thumbnail', '=', '0')
  42. ->inRandomOrder()
  43. ->get();
  44. $numberProduct = Book::sum('origin');
  45. $numberUser = User::all();
  46. $numberproductsale = Order_product::sum('quantity');
  47. $saleProducts = Book::with('pictures')
  48. ->orderBy('promotion_price', 'desc')
  49. ->inRandomOrder()
  50. ->get();
  51. return view('home', compact('bestSellProducts', 'numberUser', 'saleProducts', 'numberproductsale', 'numberProduct', 'newProducts', 'randomProducts', 'types', 'authors', 'languages', 'publishers'));
  52. }
  53. public function productDetail($id) {
  54. $types = Type::where('is_thumbnail', '=', '0')
  55. ->paginate(5);
  56. $authors = Author::where('is_thumbnail', '=', '0')
  57. ->paginate(5);
  58. $languages = Language::where('is_thumbnail', '=', '0')
  59. ->paginate(5);
  60. $publishers = Publisher::where('is_thumbnail', '=', '0')
  61. ->paginate(5);
  62. $image = Picture::where('book_id', '=', $id)->orderBy('created_at', 'desc')->paginate(4);
  63. $user = Auth::user();
  64. $comments = Comment::where('book_id', '=', $id)->orderBy('created_at', 'desc')->paginate(6);
  65. $type = Type::pluck('name', 'id')->all();
  66. $language = Language::pluck('name', 'id')->all();
  67. $author = Author::pluck('name', 'id')->all();
  68. $publisher = Publisher::pluck('name', 'id')->all();
  69. $product = Book::with('pictures')->findOrFail($id);
  70. $getidauthor = $product->author_id;
  71. $proByAuthor = Book::with('pictures')->orderBy('created_at', 'desc')->where('author_id', '=', $getidauthor)->paginate(4);
  72. $allproducts = Book::with('pictures')
  73. ->where('is_thumbnail', '=', '0')
  74. ->orderBy('books.id', 'desc')->paginate(5);
  75. return view('product_detail', compact('comments', 'user', 'image', 'product', 'allproducts', 'types', 'proByAuthor', 'authors', 'languages', 'publishers', 'type', 'author', 'language', 'publisher'));
  76. }
  77. public function commentProduct(Request $request) {
  78. if (($request->get('comment_content') != '') || ($request->get('book_id') != '') || ($request->get('parent_id') != '')) {
  79. $user = Auth::user();
  80. if ($user) {
  81. $comment = new Comment();
  82. $comment->book_id = $request->get('book_id');
  83. $comment->parent_id = $request->get('parent_id');
  84. $comment->user_id = $user->id;
  85. $comment->content = $request->get('comment_content');
  86. $comment->save();
  87. return response()->json($comment);
  88. } else {
  89. return view("errors.submit-error", ["data" => "Please login as administrator!"]);
  90. }
  91. return response()->json(FALSE);
  92. }
  93. }
  94. public function contactUs() {
  95. $types = Type::where('is_thumbnail', '=', '0')
  96. ->paginate(5);
  97. $authors = Author::where('is_thumbnail', '=', '0')
  98. ->paginate(5);
  99. $languages = Language::where('is_thumbnail', '=', '0')
  100. ->paginate(5);
  101. $publishers = Publisher::where('is_thumbnail', '=', '0')
  102. ->paginate(5);
  103. $allproducts = Book::with('pictures')
  104. ->where('is_thumbnail', '=', '0')
  105. ->orderBy('books.id', 'desc')->paginate(5);
  106. return view('shop.contact', compact('allproducts', 'types', 'authors', 'languages', 'publishers'));
  107. }
  108. public function checkOut() {
  109. $types = Type::where('is_thumbnail', '=', '0')
  110. ->paginate(5);
  111. $authors = Author::where('is_thumbnail', '=', '0')
  112. ->paginate(5);
  113. $languages = Language::where('is_thumbnail', '=', '0')
  114. ->paginate(5);
  115. $publishers = Publisher::where('is_thumbnail', '=', '0')
  116. ->paginate(5);
  117. $allproducts = Book::with('pictures')
  118. ->where('is_thumbnail', '=', '0')
  119. ->orderBy('books.id', 'desc')->paginate(5);
  120. return view('checkout', compact('allproducts', 'types', 'authors', 'languages', 'publishers'));
  121. }
  122. public function aboutUs() {
  123. $types = Type::where('is_thumbnail', '=', '0')
  124. ->paginate(5);
  125. $authors = Author::where('is_thumbnail', '=', '0')
  126. ->paginate(5);
  127. $languages = Language::where('is_thumbnail', '=', '0')
  128. ->paginate(5);
  129. $publishers = Publisher::where('is_thumbnail', '=', '0')
  130. ->paginate(5);
  131. $allproducts = Book::with('pictures')
  132. ->where('is_thumbnail', '=', '0')
  133. ->orderBy('books.id', 'desc')->paginate(5);
  134. return view('shop.about', compact('allproducts', 'types', 'authors', 'languages', 'publishers'));
  135. }
  136. public function sendFeedback(Request $request) {
  137. $mailTo = "huycuong333@gmail.com";
  138. $mailFrom = $request->email;
  139. $subject = "Feedback from " . $request->name;
  140. $message = "<div>Hello Admin,</div>"
  141. . "<br><div>A product with name is " . $request->name
  142. . " and his or her email mail is <a href='mailto:"
  143. . $request->email . "' >"
  144. . $request->email . "</a></div>"
  145. . "<br><div>The feedback is below</div>"
  146. . "<br>"
  147. . $request->content;
  148. $send = new SendFeedback($mailTo, $mailFrom, $subject, $message);
  149. $send->sendMail();
  150. return redirect('/contact-us')->with('message', $message);
  151. }
  152. public function allProduct() {
  153. $types = Type::where('is_thumbnail', '=', '0')
  154. ->paginate(5);
  155. $authors = Author::where('is_thumbnail', '=', '0')
  156. ->paginate(5);
  157. $languages = Language::where('is_thumbnail', '=', '0')
  158. ->paginate(5);
  159. $publishers = Publisher::where('is_thumbnail', '=', '0')
  160. ->paginate(5);
  161. $type = Type::pluck('name', 'id')->all();
  162. $language = Language::pluck('name', 'id')->all();
  163. $author = Author::pluck('name', 'id')->all();
  164. $publisher = Publisher::pluck('name', 'id')->all();
  165. $allproducts = Book::with('pictures')
  166. ->where('is_thumbnail', '=', '0')
  167. ->orderBy('books.id', 'desc')->paginate(12)
  168. ;
  169. $randomProducts = Book::with('pictures')
  170. ->where('is_thumbnail', '=', '0')
  171. ->inRandomOrder()
  172. ->paginate(12);
  173. return view('all_product', compact('allproducts','randomProducts', 'types','authors', 'languages', 'publishers', 'type', 'author', 'language', 'publisher'));
  174. }
  175. public function sortProduct(Request $request) {
  176. $allproducts = $request->get('sort') == 1 ? Book::with('pictures', 'author', 'language', 'type', 'publisher')->orderByRaw('(final_price - (final_price*promotion_price)/100) desc')->get() : Book::with('pictures', 'author', 'language', 'type', 'publisher')->orderByRaw('(final_price - promotion_price) asc')->get();
  177. return response()->json($allproducts);
  178. }
  179. public function search(Request $request) {
  180. $keyword = $request->search;
  181. $products = Book::whereRaw("MATCH(name, description) AGAINST(? IN BOOLEAN MODE)", $keyword)->get();
  182. $authors = Author::whereRaw("MATCH(name) AGAINST(? IN BOOLEAN MODE)", $keyword)->get();
  183. return view('result', ['products' => $products, 'authors' => $authors, 'keyword' => $keyword]);
  184. }
  185. public function show(Request $request) {
  186. $keyword = $request->search;
  187. $products = Book::whereRaw("MATCH(name, description) AGAINST(? IN BOOLEAN MODE)", $keyword)->get();
  188. $authors = Author::whereRaw("MATCH(name) AGAINST(? IN BOOLEAN MODE)", $keyword)->get();
  189. $newProducts = Book::with('pictures')
  190. ->where('is_thumbnail', '=', '0')
  191. ->orderBy('created_at', 'desc')
  192. ->paginate(5);
  193. return view('search', ['products' => $products, 'authors' => $authors, 'newProducts' => $newProducts, 'keyword' => $keyword]);
  194. }
  195. }