PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/ProductsController.php

https://gitlab.com/fayimora/pp
PHP | 343 lines | 245 code | 80 blank | 18 comment | 42 complexity | 1521c1b4666f5c1b7ae036fbded6458e MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use Auth;
  7. use File;
  8. use App\ProductsCategories;
  9. use App\Products;
  10. use App\ProductsBackup;
  11. use Intervention\Image\ImageManagerStatic as Image;
  12. class ProductsController extends Controller
  13. {
  14. public function singleProduct($slug)
  15. {
  16. $slug = addslashes($slug);
  17. $product = Products::where('slug', $slug)->firstOrFail();
  18. $related = Products::where('id', '!=', $product->id)->where('category', '=', $product->category)->take(5)->get();
  19. return view('front.pages.single-product', ['product' => $product, 'related' => $related]);
  20. }
  21. public function products()
  22. {
  23. $categories = ProductsCategories::all();
  24. return view('back.pages.products', ['categories' => $categories]);
  25. }
  26. public function productsTrash()
  27. {
  28. $categories = ProductsCategories::all();
  29. return view('back.pages.products_trash', ['categories' => $categories]);
  30. }
  31. public function addproduct()
  32. {
  33. $products_categories = ProductsCategories::all();
  34. return view('back.pages.add-product', ['categories' => $products_categories]);
  35. }
  36. public function addproductPost(Request $request)
  37. {
  38. $this->validate($request, [
  39. 'name' => 'required',
  40. 'weight_points' => 'required|integer|min:0',
  41. 'category' => 'required|integer|exists:products_categories,id',
  42. 'price' => 'required|numeric|min:0',
  43. 'cost_price' => 'numeric|min:0',
  44. 'picture' => 'required|image'
  45. ]);
  46. $product = new Products;
  47. $product->name = $request->name;
  48. $product->slug = str_slug($request->name." ".$request->capacity);
  49. $product->weight_points = $request->weight_points;
  50. $product->category = $request->category;
  51. $product->cost_price = $request->cost_price;
  52. $product->price = $request->price;
  53. $product->capacity = $request->capacity;
  54. $product->description = $request->description;
  55. if ($request->meta_title == "") $product->meta_title = $request->name; else $product->meta_title = $request->meta_title;
  56. $product->meta_keywords = $request->meta_keywords;
  57. $product->meta_description = $request->meta_description;
  58. if ($request->hasFile('picture')) {
  59. $filename = snake_case($request->name)."_orig_".uniqid().".".$request->file('picture')->getClientOriginalExtension();
  60. $img = Image::make($request->file('picture'));
  61. // background 462x620
  62. // img size: 400x537
  63. if ($img->width() > $img->height()) {
  64. $img->resize(400, null, function ($constraint) {
  65. $constraint->aspectRatio();
  66. $constraint->upsize();
  67. });
  68. } else {
  69. $img->resize(null, 537, function ($constraint) {
  70. $constraint->aspectRatio();
  71. $constraint->upsize();
  72. });
  73. }
  74. $img->save();
  75. $filenameok = snake_case($request->name)."_".uniqid().".jpg";
  76. $imgok = Image::make('images/products/back_products.png');
  77. $imgok->insert($request->file('picture'), 'center');
  78. $imgok->save('products_pictures/'.$filenameok, 95);
  79. if ($request->file('picture')->move('products_pictures/', $filename)) {
  80. $product->path_img = "products_pictures/".$filenameok;
  81. $product->path_img_orig = "products_pictures/".$filename;
  82. }
  83. }
  84. $order_product = Products::where('category', '=', $request->category)->whereRaw('position = (select max(position) from products where category = "'.$request->category.'")')->first();
  85. // $product->position = $order_product->position + 1;
  86. if ($order_product) $product->position = $order_product->position + 1; else $product->position = 1;
  87. $product->save();
  88. return redirect()->route('products')->with('message', 'Products added successfully');
  89. }
  90. public function editProduct($id)
  91. {
  92. $products_categories = ProductsCategories::all();
  93. $product = Products::findOrFail($id);
  94. return view('back.pages.edit-product', ['categories' => $products_categories, 'product' => $product]);
  95. }
  96. public function editProductPost(Request $request)
  97. {
  98. if (!Auth::user()->canEdit()) return redirect()->route('products')->withErrors(['You don\'t have the rights to edit this product']);
  99. $this->validate($request, [
  100. 'prodid' => 'required|exists:products,id',
  101. 'name' => 'required',
  102. 'weight_points' => 'required|integer|min:0',
  103. 'category' => 'required|integer|exists:products_categories,id',
  104. 'price' => 'required|numeric|min:0',
  105. 'cost_price' => 'numeric|min:0',
  106. 'picture' => 'image'
  107. ]);
  108. $product = Products::findOrFail($request->prodid);
  109. $product->name = $request->name;
  110. $product->weight_points = $request->weight_points;
  111. $product->category = $request->category;
  112. $product->cost_price = $request->cost_price;
  113. $product->price = $request->price;
  114. $product->capacity = $request->capacity;
  115. $product->description = $request->description;
  116. if ($request->meta_title == "") $product->meta_title = $request->name; else $product->meta_title = $request->meta_title;
  117. $product->meta_keywords = $request->meta_keywords;
  118. $product->meta_description = $request->meta_description;
  119. if ($request->hasFile('picture')) {
  120. /* Delete old pictures */
  121. if (File::exists(public_path()."/".$product->path_img)) {
  122. File::delete(public_path()."/".$product->path_img);
  123. }
  124. if (File::exists(public_path()."/".$product->path_img_orig)) {
  125. File::delete(public_path()."/".$product->path_img_orig);
  126. }
  127. $filename = snake_case($request->name)."_orig_".uniqid().".".$request->file('picture')->getClientOriginalExtension();
  128. $img = Image::make($request->file('picture'));
  129. // background 462x620
  130. // img size: 400x537
  131. if ($img->width() > $img->height()) {
  132. $img->resize(400, null, function ($constraint) {
  133. $constraint->aspectRatio();
  134. $constraint->upsize();
  135. });
  136. } else {
  137. $img->resize(null, 537, function ($constraint) {
  138. $constraint->aspectRatio();
  139. $constraint->upsize();
  140. });
  141. }
  142. $img->save();
  143. $filenameok = snake_case($request->name)."_".uniqid().".jpg";
  144. $imgok = Image::make('images/products/back_products.png');
  145. $imgok->insert($request->file('picture'), 'center');
  146. $imgok->save('products_pictures/'.$filenameok, 95);
  147. if ($request->file('picture')->move('products_pictures/', $filename)) {
  148. $product->path_img = "products_pictures/".$filenameok;
  149. $product->path_img_orig = "products_pictures/".$filename;
  150. }
  151. }
  152. $product->save();
  153. return redirect()->route('edit-product', $product->id)->with('message', 'Product edited successfully');
  154. }
  155. public function removeProduct($id)
  156. {
  157. if (!Auth::user()->canEdit()) return redirect()->route('products')->withErrors(['You don\'t have the rights to delete this product']);
  158. $product = Products::findOrFail($id);
  159. /* if (File::exists(public_path()."/".$product->path_img)) {
  160. File::delete(public_path()."/".$product->path_img);
  161. }
  162. if (File::exists(public_path()."/".$product->path_img_orig)) {
  163. File::delete(public_path()."/".$product->path_img_orig);
  164. } */
  165. $product->delete();
  166. return redirect()->route('products')->with('message', 'Product deleted successfully');
  167. }
  168. public function restoreProduct($id)
  169. {
  170. if (!Auth::user()->canEdit()) return redirect()->route('products')->withErrors(['You don\'t have the rights to recover this product']);
  171. $product = Products::withTrashed()->findOrFail($id);
  172. /* if (File::exists(public_path()."/".$product->path_img)) {
  173. File::delete(public_path()."/".$product->path_img);
  174. }
  175. if (File::exists(public_path()."/".$product->path_img_orig)) {
  176. File::delete(public_path()."/".$product->path_img_orig);
  177. } */
  178. $product->restore();
  179. return redirect()->route('products-trash')->with('message', 'Product restored successfully');
  180. }
  181. public function addProductBackupPost(Request $request)
  182. {
  183. $this->validate($request, [
  184. 'name' => 'required',
  185. 'product_id' => 'required|exists:products,id'
  186. ]);
  187. $related = Products::findOrFail($request->product_id);
  188. $product = new ProductsBackup;
  189. $product->name = $request->name;
  190. $product->product_id = $request->product_id;
  191. if ($request->cost_price > 0)
  192. $product->cost_price = $request->cost_price; else $product->cost_price = $related->cost_price;
  193. if ($request->price > 0)
  194. $product->price = $request->price; else $product->price = $related->price;
  195. if ($request->capacity != "")
  196. $product->capacity = $request->capacity;
  197. $product->save();
  198. return redirect()->route('edit-product', $request->product_id)->with('message', 'Backup Product added successfully');
  199. }
  200. public function deleteBackupProduct(Request $request)
  201. {
  202. $this->validate($request, [
  203. 'bpid' => 'required|exists:products_backup,id'
  204. ]);
  205. if (!Auth::user()->canEdit()) return redirect()->back()->withErrors(['You don\'t have the rights to delete this product']);
  206. $product = ProductsBackup::findOrFail($request->bpid);
  207. $product->delete();
  208. return redirect()->back()->with('message', 'Backup product deleted successfully');
  209. }
  210. public function editProductBackupPost(Request $request)
  211. {
  212. $this->validate($request, [
  213. 'name' => 'required',
  214. 'product_id' => 'required|exists:products,id'
  215. ]);
  216. if (!Auth::user()->canEdit()) return redirect()->back()->withErrors(['You don\'t have the rights to delete this product']);
  217. $product = ProductsBackup::findOrFail($request->product_id);
  218. $related = Products::findOrFail($product->id);
  219. $product->name = $request->name;
  220. if ($request->cost_price > 0)
  221. $product->cost_price = $request->cost_price; else $product->cost_price = $related->cost_price;
  222. if ($request->price > 0)
  223. $product->price = $request->price; else $product->price = $related->price;
  224. if ($request->capacity != "")
  225. $product->capacity = $request->capacity;
  226. $product->save();
  227. return redirect()->back()->with('message', 'Backup product changed successfully');
  228. }
  229. public function changeProductPosition(Request $request)
  230. {
  231. $this->validate($request, [
  232. 'pos' => 'required|in:up,down',
  233. 'idprod' => 'required|exists:products,id'
  234. ]);
  235. $product = Products::findOrFail($request->idprod);
  236. $position = $product->position;
  237. $category = $product->category;
  238. if ($request->pos == "up") {
  239. if ($position > 1) {
  240. $product_pre = Products::where('category', '=', $category)->where('position', '=', $position - 1)->firstOrFail();
  241. $product_pre->position = $position;
  242. $product->position = $position - 1;
  243. $product->save();
  244. $product_pre->save();
  245. } else {
  246. return array('result' => 'err', 'message' => 'Position not changed');
  247. }
  248. } elseif ($request->pos == "down") {
  249. $category_count = Products::where('category', '=', $category)->get()->count();
  250. error_log($category_count, 0);
  251. if ($position < $category_count) {
  252. $product_pre = Products::where('category', '=', $category)->where('position', '=', $position + 1)->firstOrFail();
  253. $product_pre->position = $position;
  254. $product->position = $position + 1;
  255. $product->save();
  256. $product_pre->save();
  257. } else {
  258. return array('result' => 'err', 'message' => 'Position not changed');
  259. }
  260. }
  261. return array('result' => 'ok', 'message' => 'Position changed successfully');
  262. }
  263. }