| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace App\Http\Controllers;
- use App\Http\Requests\SaveProductRequest;
- use App\Models\Product;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Redirect;
- use Illuminate\Support\Str;
- class ProductController extends Controller
- {
- public function index(Request $request)
- {
- $data = $request->validate([
- 's' => 'nullable|string',
- 's_series' => 'nullable|string',
- 's_price_min' => 'nullable|integer',
- 's_price_max' => 'nullable|integer',
- 'perpage' => 'nullable|string',
- ]);
- $query = Product::query();
- if (!empty($data['s'])) {
- $query->whereRaw(" (`article` LIKE '%{$data['s']}%' OR `name` LIKE '%{$data['s']}%' OR
- `name_for_form` LIKE '%{$data['s']}%' OR `product_group` LIKE '%{$data['s']}%' OR
- `characteristics` LIKE '%{$data['s']}%' OR `tech_description` LIKE '%{$data['s']}%'
- OR `tech_description_short` LIKE '%{$data['s']}%') ");
- }
- if (!empty($data['s_series'])) {
- $query->where('series', '=', $data['s_series']);
- }
- if (!empty($data['s_price_min'])) {
- $query->where('price', '>=', $data['s_price_min']);
- }
- if (!empty($data['s_price_max'])) {
- $query->where('price', '<=', $data['s_price_max']);
- }
- $data['products'] = $query->paginate($data['perpage'] ?? 20)->withQueryString();
- // get all series for select field
- $data['series'] = Product::query()->select('series')->distinct()->OrderBy('series')->get();
- return view('products.index', $data);
- }
- // todo вынести в job, чтобы работала в фоне
- public function upload_xls(Request $request)
- {
- $xls = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($request->file('file'));
- $xls->setReadDataOnly(true);
- $sheet = $xls->load($request->file('file'));
- // read xls to array
- $goods = $sheet->setActiveSheetIndex(0)->toArray();
- unset($sheet, $xls);
- $series = '';
- $i = 0;
- $created = 0;
- $updated = 0;
- $no_image = [];
- foreach ($goods as $good) {
- // check first line and skip it
- if ($good[0] === '№п/п' && $good['3'] === 'Наименование') continue;
- // check the line is name of series
- if ($good[0] == NULL && $good[1] == NULL && $good[2] == NULL && is_string($good[3])) {
- $series = $good[3];
- continue;
- }
- $tmp = explode("\n", $good[3]);
- if (!isset($tmp[1])) {
- $good[3] = preg_replace('!\s+!', ' ', $good[3]);
- $tmp = explode(' ', $good[3], 2);
- }
- $images = $this->find_images($tmp[0]);
- $data = [
- // 'article' => $tmp[0],
- 'series' => $series,
- 'name' => (isset($tmp[1])) ? $tmp[1] : 'error',
- 'name_for_form' => $good[2],
- 'product_group' => $good[1],
- 'price' => $good[5],
- 'characteristics' => $good[4],
- 'tech_description' => $good[7],
- 'tech_description_short' => $good[8],
- 'image_path' => (!empty($images)) ? $images[0] : $images,
- ];
- $a = Product::query()->updateOrCreate(['article' => $tmp[0]], $data);
- if ($a->wasRecentlyCreated) $created++; else $updated++;
- //echo $i++ . '. Серия: ' . $series . ', артикул: ' . $tmp[0] . '<br>';
- $i++;
- if ($data['image_path'] == '') $no_image[] = $tmp[0];
- }
- return view('products.import_result', ['count' => $i, 'updated' => $updated, 'created' => $created, 'no_image' => $no_image]);
- }
- private $allfiles; // remember files list
- private function find_images($article)
- {
- $path_to_files = './' . env('IMAGES_PATH', '---') . '/';
- if (!isset($this->allfiles) || empty($this->allfiles)) {
- $this->allfiles = scandir($path_to_files);
- }
- $images = [];
- foreach ($this->allfiles as $filename) {
- if ((mb_strpos($filename, $article) === 0) || (
- mb_strpos(Str::lower($filename), Str::slug($article)) === 0))
- $images[] = $filename;
- }
- return (!empty($images)) ? $images : '';
- }
- public function product(Request $request, $id)
- {
- $data['product'] = Product::query()->findOrFail($id);
- $data['images'] = $this->find_images($data['product']->article);
- return view('products.product', $data);
- }
- public function save_product(SaveProductRequest $request)
- {
- Product::query()->where('id', $request->validated('id'))->update($request->validated());
- return redirect()->route('index');
- }
- public function upload_image(Request $request){
- $path = public_path() . '/' . env('IMAGES_PATH');
- // dd($path);
- $file = $request->file('filename');
- $new_filename = $request->article . '.' . date('YmdHis') . '.' . $file->extension();
- $file->move($path, $new_filename);
- Product::query()->where('article', $request->article)->update(['image_path' => $new_filename]);
- return redirect()->back();
- }
- public function update_image(Request $request, $id){
- $validated = $request->validate(['image_path' => 'required|string']);
- Product::query()->where('id', $id)->update($validated);
- return redirect()->route('view_product', $id);
- }
- }
|