ProductController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\SaveProductRequest;
  4. use App\Models\Product;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Redirect;
  7. use Illuminate\Support\Str;
  8. class ProductController extends Controller
  9. {
  10. public function index(Request $request)
  11. {
  12. $data = $request->validate([
  13. 's' => 'nullable|string',
  14. 's_series' => 'nullable|string',
  15. 's_price_min' => 'nullable|integer',
  16. 's_price_max' => 'nullable|integer',
  17. 'perpage' => 'nullable|string',
  18. ]);
  19. $query = Product::query();
  20. if (!empty($data['s'])) {
  21. $query->whereRaw(" (`article` LIKE '%{$data['s']}%' OR `name` LIKE '%{$data['s']}%' OR
  22. `name_for_form` LIKE '%{$data['s']}%' OR `product_group` LIKE '%{$data['s']}%' OR
  23. `characteristics` LIKE '%{$data['s']}%' OR `tech_description` LIKE '%{$data['s']}%'
  24. OR `tech_description_short` LIKE '%{$data['s']}%') ");
  25. }
  26. if (!empty($data['s_series'])) {
  27. $query->where('series', '=', $data['s_series']);
  28. }
  29. if (!empty($data['s_price_min'])) {
  30. $query->where('price', '>=', $data['s_price_min']);
  31. }
  32. if (!empty($data['s_price_max'])) {
  33. $query->where('price', '<=', $data['s_price_max']);
  34. }
  35. $data['products'] = $query->paginate($data['perpage'] ?? 20)->withQueryString();
  36. // get all series for select field
  37. $data['series'] = Product::query()->select('series')->distinct()->OrderBy('series')->get();
  38. return view('products.index', $data);
  39. }
  40. // todo вынести в job, чтобы работала в фоне
  41. public function upload_xls(Request $request)
  42. {
  43. $xls = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($request->file('file'));
  44. $xls->setReadDataOnly(true);
  45. $sheet = $xls->load($request->file('file'));
  46. // read xls to array
  47. $goods = $sheet->setActiveSheetIndex(0)->toArray();
  48. unset($sheet, $xls);
  49. $series = '';
  50. $i = 0;
  51. $created = 0;
  52. $updated = 0;
  53. $no_image = [];
  54. foreach ($goods as $good) {
  55. // check first line and skip it
  56. if ($good[0] === '№п/п' && $good['3'] === 'Наименование') continue;
  57. // check the line is name of series
  58. if ($good[0] == NULL && $good[1] == NULL && $good[2] == NULL && is_string($good[3])) {
  59. $series = $good[3];
  60. continue;
  61. }
  62. $tmp = explode("\n", $good[3]);
  63. if (!isset($tmp[1])) {
  64. $good[3] = preg_replace('!\s+!', ' ', $good[3]);
  65. $tmp = explode(' ', $good[3], 2);
  66. }
  67. $images = $this->find_images($tmp[0]);
  68. $data = [
  69. // 'article' => $tmp[0],
  70. 'series' => $series,
  71. 'name' => (isset($tmp[1])) ? $tmp[1] : 'error',
  72. 'name_for_form' => $good[2],
  73. 'product_group' => $good[1],
  74. 'price' => $good[5],
  75. 'characteristics' => $good[4],
  76. 'tech_description' => $good[7],
  77. 'tech_description_short' => $good[8],
  78. 'image_path' => (!empty($images)) ? $images[0] : $images,
  79. ];
  80. $a = Product::query()->updateOrCreate(['article' => $tmp[0]], $data);
  81. if ($a->wasRecentlyCreated) $created++; else $updated++;
  82. //echo $i++ . '. Серия: ' . $series . ', артикул: ' . $tmp[0] . '<br>';
  83. $i++;
  84. if ($data['image_path'] == '') $no_image[] = $tmp[0];
  85. }
  86. return view('products.import_result', ['count' => $i, 'updated' => $updated, 'created' => $created, 'no_image' => $no_image]);
  87. }
  88. private $allfiles; // remember files list
  89. private function find_images($article)
  90. {
  91. $path_to_files = './' . env('IMAGES_PATH', '---') . '/';
  92. if (!isset($this->allfiles) || empty($this->allfiles)) {
  93. $this->allfiles = scandir($path_to_files);
  94. }
  95. $images = [];
  96. foreach ($this->allfiles as $filename) {
  97. if ((mb_strpos($filename, $article) === 0) || (
  98. mb_strpos(Str::lower($filename), Str::slug($article)) === 0))
  99. $images[] = $filename;
  100. }
  101. return (!empty($images)) ? $images : '';
  102. }
  103. public function product(Request $request, $id)
  104. {
  105. $data['product'] = Product::query()->findOrFail($id);
  106. $data['images'] = $this->find_images($data['product']->article);
  107. return view('products.product', $data);
  108. }
  109. public function save_product(SaveProductRequest $request)
  110. {
  111. Product::query()->where('id', $request->validated('id'))->update($request->validated());
  112. return redirect()->route('index');
  113. }
  114. public function upload_image(Request $request){
  115. $path = public_path() . '/' . env('IMAGES_PATH');
  116. // dd($path);
  117. $file = $request->file('filename');
  118. $new_filename = $request->article . '.' . date('YmdHis') . '.' . $file->extension();
  119. $file->move($path, $new_filename);
  120. Product::query()->where('article', $request->article)->update(['image_path' => $new_filename]);
  121. return redirect()->back();
  122. }
  123. public function update_image(Request $request, $id){
  124. $validated = $request->validate(['image_path' => 'required|string']);
  125. Product::query()->where('id', $id)->update($validated);
  126. return redirect()->route('view_product', $id);
  127. }
  128. }