/** * @param array $data */ public function seed($data = []) { DB::table('enums')->truncate(); $models = []; foreach ($data as $resource => $enums) { foreach ($enums as $type => $items) { foreach ($items as $i => $item) { if (strpos($item, '|')) { list($item, $params) = explode('|', $item, 2); } else { $params = null; } $models[] = ['title' => $item, 'slug' => VnString::to_slug($item), 'position' => $i + 1, 'type' => "{$resource}.{$type}", 'params' => $params]; } } } DB::table('enums')->insert($models); }
/** * - Tự động lấy trang đầu của PDF làm cover * - Thứ tự data: * 0 title, 1 filename(ko đuôi file), 2 summary, 3 pyear, 4 pages, 5 category_slug, 6 security_slug, * 7 language, 8 writer, 9 publisher, 10 pplace, 11 user_id, 12 status, 13 hit, 14 featured * * @param string $dir * @param array $data * * @throws \Spatie\PdfToImage\Exceptions\InvalidFormat */ public function seed($dir, $data) { DB::table('ebooks')->truncate(); $data_dir = storage_path('data/' . config('ebook.data_dir')); $tmp_dir = storage_path('tmp'); $img_dir = public_path(setting('system.public_files') . '/' . config('ebook.featured_image.dir')); $ebooks = []; foreach ($data as $ebook) { $category = Category::findBy('slug', $ebook[5]); $security = Enum::where('slug', $ebook[6])->where('type', 'ebook.security')->first(); if ($security && $category && is_file("{$dir}/{$ebook[1]}.pdf")) { $pdf = new Pdf("{$dir}/{$ebook[1]}.pdf"); $pdf->saveImage("{$tmp_dir}/{$ebook[1]}.png"); $image_file = save_new_image("{$ebook[1]}.png", "{$tmp_dir}/{$ebook[1]}.png", $img_dir, ['main' => ['width' => config('ebook.featured_image.width'), 'height' => config('ebook.featured_image.height'), 'method' => 'resize'], 'sm' => ['width' => config('ebook.featured_image.width_sm'), 'height' => config('ebook.featured_image.height_sm'), 'method' => 'resize']]); @unlink("{$tmp_dir}/{$ebook[1]}.png"); File::copy("{$dir}/{$ebook[1]}.pdf", "{$data_dir}/{$ebook[1]}.pdf"); $ebooks[] = ['title' => $ebook[0], 'slug' => VnString::to_slug($ebook[0]), 'filename' => $ebook[1] . '.pdf', 'filemime' => 'application/pdf', 'filesize' => File::size("{$dir}/{$ebook[1]}.pdf"), 'summary' => $ebook[2], 'featured_image' => $image_file, 'pyear' => $ebook[3], 'pages' => $ebook[4], 'category_id' => $category->id, 'security_id' => $security->id, 'language_id' => Enum::firstOrCreate(['title' => $ebook[7], 'type' => 'ebook.language'], ['slug' => VnString::to_slug($ebook[7])])->id, 'writer_id' => Enum::firstOrCreate(['title' => $ebook[8], 'type' => 'ebook.writer'], ['slug' => VnString::to_slug($ebook[8])])->id, 'publisher_id' => Enum::firstOrCreate(['title' => $ebook[9], 'type' => 'ebook.publisher'], ['slug' => VnString::to_slug($ebook[9])])->id, 'pplace_id' => Enum::firstOrCreate(['title' => $ebook[10], 'type' => 'ebook.pplace'], ['slug' => VnString::to_slug($ebook[10])])->id, 'user_id' => $ebook[11], 'status' => $ebook[12], 'hit' => $ebook[13], 'featured' => $ebook[14], 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]; } } if ($ebooks) { DB::table('ebooks')->insert($ebooks); } }
/** * @param \Minhbang\ILib\UploadRequest $request * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function store(UploadRequest $request) { $ebook = new Ebook(); $ebook->fill($request->only(['title', 'summary'])); $ebook->fileFill($request); $ebook->user_id = user('id'); $ebook->status = $this->statusManager->valueStatus('uploaded'); $ebook->slug = VnString::to_slug($ebook->title); $ebook->enumNotRestore = true; $ebook->save(); return view('message', ['module' => trans('ilib::common.ilib'), 'type' => 'success', 'content' => trans('ilib::common.messages.upload_success'), 'buttons' => [[route('ilib.index'), trans('ilib::common.back_ilib_home'), ['icon' => 'home', 'size' => 'sm', 'type' => 'success']]]]); }
/** * Các attributes cho phép quick-update * * @return array */ protected function quickUpdateAttributes() { return ['title' => ['rules' => ['required|max:255', 'slug', function ($title) { return VnString::to_slug($title); }, 'required|max:255|alpha_dash'], 'label' => trans('ebook::common.title')]]; }