CustomerProductController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\CustomerProduct;
  5. use App\Models\CustomerProductTranslation;
  6. use App\Models\Category;
  7. use App\Models\SubCategory;
  8. use App\Models\Brand;
  9. use App\Models\SubSubCategory;
  10. use Auth;
  11. use ImageOptimizer;
  12. use Illuminate\Support\Str;
  13. use App\Utility\CategoryUtility;
  14. class CustomerProductController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. $products = CustomerProduct::where('user_id', Auth::user()->id)->orderBy('created_at', 'desc')->paginate(10);
  24. return view('frontend.user.customer.products', compact('products'));
  25. }
  26. public function customer_product_index()
  27. {
  28. $products = CustomerProduct::orderBy('created_at', 'desc')->paginate(10);
  29. return view('backend.customer.classified_products.index', compact('products'));
  30. }
  31. /**
  32. * Show the form for creating a new resource.
  33. *
  34. * @return \Illuminate\Http\Response
  35. */
  36. public function create()
  37. {
  38. $categories = Category::where('parent_id', 0)
  39. ->where('digital', 0)
  40. ->with('childrenCategories')
  41. ->get();
  42. if(Auth::user()->user_type == "customer" && Auth::user()->remaining_uploads > 0){
  43. return view('frontend.user.customer.product_upload', compact('categories'));
  44. }
  45. elseif (Auth::user()->user_type == "seller" && Auth::user()->remaining_uploads > 0) {
  46. return view('frontend.user.customer.product_upload', compact('categories'));
  47. }
  48. else{
  49. flash(translate('Your classified product upload limit has been reached. Please buy a package.'))->error();
  50. return redirect()->route('customer_packages_list_show');
  51. }
  52. }
  53. /**
  54. * Store a newly created resource in storage.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function store(Request $request)
  60. {
  61. $customer_product = new CustomerProduct;
  62. $customer_product->name = $request->name;
  63. $customer_product->added_by = $request->added_by;
  64. $customer_product->user_id = Auth::user()->id;
  65. $customer_product->category_id = $request->category_id;
  66. $customer_product->brand_id = $request->brand_id;
  67. $customer_product->conditon = $request->conditon;
  68. $customer_product->location = $request->location;
  69. $customer_product->photos = $request->photos;
  70. $customer_product->thumbnail_img = $request->thumbnail_img;
  71. $customer_product->unit = $request->unit;
  72. $tags = array();
  73. if($request->tags[0] != null){
  74. foreach (json_decode($request->tags[0]) as $key => $tag) {
  75. array_push($tags, $tag->value);
  76. }
  77. }
  78. $customer_product->tags = implode(',', $tags);
  79. $customer_product->description = $request->description;
  80. $customer_product->video_provider = $request->video_provider;
  81. $customer_product->video_link = $request->video_link;
  82. $customer_product->unit_price = $request->unit_price;
  83. $customer_product->meta_title = $request->meta_title;
  84. $customer_product->meta_description = $request->meta_description;
  85. $customer_product->meta_img = $request->meta_img;
  86. $customer_product->pdf = $request->pdf;
  87. $customer_product->slug = strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->name)).'-'.Str::random(5));
  88. if($customer_product->save()){
  89. $user = Auth::user();
  90. $user->remaining_uploads -= 1;
  91. $user->save();
  92. $customer_product_translation = CustomerProductTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'customer_product_id' => $customer_product->id]);
  93. $customer_product_translation->name = $request->name;
  94. $customer_product_translation->unit = $request->unit;
  95. $customer_product_translation->description = $request->description;
  96. $customer_product_translation->save();
  97. flash(translate('Product has been inserted successfully'))->success();
  98. return redirect()->route('customer_products.index');
  99. }
  100. else{
  101. flash(translate('Something went wrong'))->error();
  102. return back();
  103. }
  104. }
  105. /**
  106. * Display the specified resource.
  107. *
  108. * @param int $id
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function show($id)
  112. {
  113. //
  114. }
  115. /**
  116. * Show the form for editing the specified resource.
  117. *
  118. * @param int $id
  119. * @return \Illuminate\Http\Response
  120. */
  121. public function edit(Request $request, $id)
  122. {
  123. $categories = Category::where('parent_id', 0)
  124. ->where('digital', 0)
  125. ->with('childrenCategories')
  126. ->get();
  127. $product = CustomerProduct::find($id);
  128. $lang = $request->lang;
  129. return view('frontend.user.customer.product_edit', compact('categories', 'product','lang'));
  130. }
  131. /**
  132. * Update the specified resource in storage.
  133. *
  134. * @param \Illuminate\Http\Request $request
  135. * @param int $id
  136. * @return \Illuminate\Http\Response
  137. */
  138. public function update(Request $request, $id)
  139. {
  140. $customer_product = CustomerProduct::find($id);
  141. if($request->lang == env("DEFAULT_LANGUAGE")){
  142. $customer_product->name = $request->name;
  143. $customer_product->unit = $request->unit;
  144. $customer_product->description = $request->description;
  145. }
  146. $customer_product->status = '1';
  147. $customer_product->user_id = Auth::user()->id;
  148. $customer_product->category_id = $request->category_id;
  149. $customer_product->brand_id = $request->brand_id;
  150. $customer_product->conditon = $request->conditon;
  151. $customer_product->location = $request->location;
  152. $customer_product->photos = $request->photos;
  153. $customer_product->thumbnail_img = $request->thumbnail_img;
  154. $tags = array();
  155. if($request->tags[0] != null){
  156. foreach (json_decode($request->tags[0]) as $key => $tag) {
  157. array_push($tags, $tag->value);
  158. }
  159. }
  160. $customer_product->tags = implode(',', $tags);
  161. $customer_product->video_provider = $request->video_provider;
  162. $customer_product->video_link = $request->video_link;
  163. $customer_product->unit_price = $request->unit_price;
  164. $customer_product->meta_title = $request->meta_title;
  165. $customer_product->meta_description = $request->meta_description;
  166. $customer_product->meta_img = $request->meta_img;
  167. $customer_product->pdf = $request->pdf;
  168. $customer_product->slug = strtolower($request->slug);
  169. if($customer_product->save()){
  170. $customer_product_translation = CustomerProductTranslation::firstOrNew(['lang' => $request->lang, 'customer_product_id' => $customer_product->id]);
  171. $customer_product_translation->name = $request->name;
  172. $customer_product_translation->unit = $request->unit;
  173. $customer_product_translation->description = $request->description;
  174. $customer_product_translation->save();
  175. flash(translate('Product has been inserted successfully'))->success();
  176. return back();
  177. }
  178. else{
  179. flash(translate('Something went wrong'))->error();
  180. return back();
  181. }
  182. }
  183. /**
  184. * Remove the specified resource from storage.
  185. *
  186. * @param int $id
  187. * @return \Illuminate\Http\Response
  188. */
  189. public function destroy($id)
  190. {
  191. $product = CustomerProduct::findOrFail($id);
  192. foreach ($product->customer_product_translations as $key => $customer_product_translations) {
  193. $customer_product_translations->delete();
  194. }
  195. if (CustomerProduct::destroy($id)) {
  196. if(Auth::user()->user_type == "customer" || Auth::user()->user_type == "seller"){
  197. flash(translate('Product has been deleted successfully'))->success();
  198. return redirect()->route('customer_products.index');
  199. }
  200. else {
  201. return back();
  202. }
  203. }
  204. }
  205. public function updateStatus(Request $request)
  206. {
  207. $product = CustomerProduct::findOrFail($request->id);
  208. $product->status = $request->status;
  209. if($product->save()){
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. public function updatePublished(Request $request)
  215. {
  216. $product = CustomerProduct::findOrFail($request->id);
  217. $product->published = $request->status;
  218. if($product->save()){
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. public function customer_products_listing(Request $request)
  224. {
  225. return $this->search($request);
  226. }
  227. public function customer_product($slug)
  228. {
  229. $customer_product = CustomerProduct::where('slug', $slug)->first();
  230. if($customer_product!=null){
  231. return view('frontend.customer_product_details', compact('customer_product'));
  232. }
  233. abort(404);
  234. }
  235. public function search(Request $request)
  236. {
  237. // $query = $request->q;
  238. $brand_id = (Brand::where('slug', $request->brand)->first() != null) ? Brand::where('slug', $request->brand)->first()->id : null;
  239. $category_id = (Category::where('slug', $request->category)->first() != null) ? Category::where('slug', $request->category)->first()->id : null;
  240. $sort_by = $request->sort_by;
  241. $condition = $request->condition;
  242. $conditions = ['published' => 1, 'status' => 1];
  243. if($brand_id != null){
  244. $conditions = array_merge($conditions, ['brand_id' => $brand_id]);
  245. }
  246. $customer_products = CustomerProduct::where($conditions);
  247. if($category_id != null){
  248. $category_ids = CategoryUtility::children_ids($category_id);
  249. $category_ids[] = $category_id;
  250. $customer_products = $customer_products->whereIn('category_id', $category_ids);
  251. }
  252. // if($query != null){
  253. // $customer_products = $customer_products->where('name', 'like', '%'.$query.'%')->orWhere('tags', 'like', '%'.$query.'%');
  254. // }
  255. if($sort_by != null){
  256. switch ($sort_by) {
  257. case '1':
  258. $customer_products->orderBy('created_at', 'desc');
  259. break;
  260. case '2':
  261. $customer_products->orderBy('created_at', 'asc');
  262. break;
  263. case '3':
  264. $customer_products->orderBy('unit_price', 'asc');
  265. break;
  266. case '4':
  267. $customer_products->orderBy('unit_price', 'desc');
  268. break;
  269. case '5':
  270. $customer_products->where('conditon', 'new');
  271. break;
  272. case '6':
  273. $customer_products->where('conditon', 'used');
  274. break;
  275. default:
  276. // code...
  277. break;
  278. }
  279. }
  280. if($condition != null){
  281. $customer_products->where('conditon', $condition);
  282. }
  283. $customer_products = $customer_products->paginate(12)->appends(request()->query());
  284. return view('frontend.customer_product_listing', compact('customer_products', 'category_id', 'brand_id', 'sort_by', 'condition'));
  285. }
  286. }