|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use App\Http\Requests\PostCreateRequest; |
| 6 | +use App\Http\Requests\PostUpdateRequest; |
| 7 | +use App\Jobs\PostFormFields; |
| 8 | +use App\Models\Post; |
| 9 | +use App\Models\Tag; |
| 10 | +use Carbon\Carbon; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use App\Http\Controllers\Controller; |
| 13 | +use Illuminate\Http\Response; |
| 14 | + |
| 15 | +class PostController extends Controller |
| 16 | +{ |
| 17 | + protected $fieldList = [ |
| 18 | + 'title' => '', |
| 19 | + 'subtitle' => '', |
| 20 | + 'page_image' => '', |
| 21 | + 'content' => '', |
| 22 | + 'meta_description' => '', |
| 23 | + 'is_draft' => "0", |
| 24 | + 'publish_date' => '', |
| 25 | + 'publish_time' => '', |
| 26 | + 'layout' => 'blog.layouts.post', |
| 27 | + 'tags' => [], |
| 28 | + ]; |
| 29 | + |
| 30 | + /** |
| 31 | + * Display a listing of the posts. |
| 32 | + */ |
| 33 | + public function index() |
| 34 | + { |
| 35 | + return view('admin.post.index', ['posts' => Post::all()]); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Show the new post form |
| 40 | + */ |
| 41 | + public function create() |
| 42 | + { |
| 43 | + $fields = $this->fieldList; |
| 44 | + |
| 45 | + $when = Carbon::now()->addHour(); |
| 46 | + $fields['publish_date'] = $when->format('Y-m-d'); |
| 47 | + $fields['publish_time'] = $when->format('g:i A'); |
| 48 | + |
| 49 | + foreach ($fields as $fieldName => $fieldValue) { |
| 50 | + $fields[$fieldName] = old($fieldName, $fieldValue); |
| 51 | + } |
| 52 | + |
| 53 | + $data = array_merge( |
| 54 | + $fields, |
| 55 | + ['allTags' => Tag::all()->pluck('tag')->all()] |
| 56 | + ); |
| 57 | + |
| 58 | + return view('admin.post.create', $data); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Store a newly created Post |
| 63 | + * |
| 64 | + * @param PostCreateRequest $request |
| 65 | + */ |
| 66 | + public function store(PostCreateRequest $request) |
| 67 | + { |
| 68 | + $post = Post::create($request->postFillData()); |
| 69 | + $post->syncTags($request->get('tags', [])); |
| 70 | + |
| 71 | + return redirect() |
| 72 | + ->route('post.index') |
| 73 | + ->with('success', '新文章创建成功.'); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Show the post edit form |
| 78 | + * |
| 79 | + * @param int $id |
| 80 | + * @return Response |
| 81 | + */ |
| 82 | + public function edit($id) |
| 83 | + { |
| 84 | + $fields = $this->fieldsFromModel($id, $this->fieldList); |
| 85 | + |
| 86 | + foreach ($fields as $fieldName => $fieldValue) { |
| 87 | + $fields[$fieldName] = old($fieldName, $fieldValue); |
| 88 | + } |
| 89 | + |
| 90 | + $data = array_merge( |
| 91 | + $fields, |
| 92 | + ['allTags' => Tag::all()->pluck('tag')->all()] |
| 93 | + ); |
| 94 | + |
| 95 | + return view('admin.post.edit', $data); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * 更新文章 |
| 100 | + * |
| 101 | + * @param PostUpdateRequest $request |
| 102 | + * @param $id |
| 103 | + * @return \Illuminate\Http\RedirectResponse |
| 104 | + */ |
| 105 | + public function update(PostUpdateRequest $request, $id) |
| 106 | + { |
| 107 | + $post = Post::findOrFail($id); |
| 108 | + $post->fill($request->postFillData()); |
| 109 | + $post->save(); |
| 110 | + $post->syncTags($request->get('tags', [])); |
| 111 | + |
| 112 | + if ($request->action === 'continue') { |
| 113 | + return redirect() |
| 114 | + ->back() |
| 115 | + ->with('success', '文章已保存.'); |
| 116 | + } |
| 117 | + |
| 118 | + return redirect() |
| 119 | + ->route('post.index') |
| 120 | + ->with('success', '文章已保存.'); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Remove the specified resource from storage. |
| 125 | + * |
| 126 | + * @param int $id |
| 127 | + * @return Response |
| 128 | + */ |
| 129 | + public function destroy($id) |
| 130 | + { |
| 131 | + $post = Post::findOrFail($id); |
| 132 | + $post->tags()->detach(); |
| 133 | + $post->delete(); |
| 134 | + |
| 135 | + return redirect() |
| 136 | + ->route('post.index') |
| 137 | + ->with('success', '文章已删除.'); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Return the field values from the model |
| 142 | + * |
| 143 | + * @param integer $id |
| 144 | + * @param array $fields |
| 145 | + * @return array |
| 146 | + */ |
| 147 | + private function fieldsFromModel($id, array $fields) |
| 148 | + { |
| 149 | + $post = Post::findOrFail($id); |
| 150 | + |
| 151 | + $fieldNames = array_keys(array_except($fields, ['tags'])); |
| 152 | + |
| 153 | + $fields = ['id' => $id]; |
| 154 | + foreach ($fieldNames as $field) { |
| 155 | + $fields[$field] = $post->{$field}; |
| 156 | + } |
| 157 | + |
| 158 | + $fields['tags'] = $post->tags->pluck('tag')->all(); |
| 159 | + |
| 160 | + return $fields; |
| 161 | + } |
| 162 | +} |
0 commit comments