1
0
Moell 9 жил өмнө
parent
commit
75dda0361e

+ 121 - 0
app/Http/Controllers/Backend/TagController.php

@@ -0,0 +1,121 @@
+<?php
+
+namespace App\Http\Controllers\Backend;
+
+use Illuminate\Http\Request;
+
+use App\Http\Requests;
+use App\Http\Controllers\Controller;
+use App\Repositories\TagRepositoryEloquent;
+use App\Http\Requests\Backend\Tag\CreateRequest;
+use App\Http\Requests\Backend\Tag\UpdateRequest;
+
+class TagController extends Controller
+{
+    protected $tag;
+
+    public function __construct(TagRepositoryEloquent $tag)
+    {
+        $this->tag = $tag;
+    }
+
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        $tags = $this->tag->all();
+        return view('backend.tag.index', compact('tags'));
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        return view('backend.tag.create');
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(CreateRequest $request)
+    {
+        $data = [];
+        $data['tag_name'] = $request->name;
+        if ($this->tag->create($data)) {
+            return redirect('backend/tag')
+                ->with('success', '标签添加成功');
+        }
+        return redirect(route('backend.tag.create'))->withErrors('系统异常,标签添加失败');
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function show($id)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function edit($id)
+    {
+        $tag = $this->tag->find($id);
+        return view("backend.tag.edit", compact('tag'));
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function update(UpdateRequest $request, $id)
+    {
+        $tag = $this->tag->find($id);
+        if ($tag) {
+            $data = [];
+            $data['tag_name'] = $request->name;
+            if ($this->tag->update($data, $id)) {
+                return redirect('backend/tag')
+                    ->with('success', '标签修改成功');
+            }
+        }
+        return redirect()
+            ->back()
+            ->withErrors('系统异常,标签添加失败');
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy($id)
+    {
+        if ($this->tag->find($id)) {
+            if ($this->tag->delete($id)) {
+                return response()->json(['status' => 0]);
+            }
+        }
+        return response()->json(['status' => 1]);
+    }
+}

+ 38 - 0
app/Http/Requests/Backend/Tag/CreateRequest.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Http\Requests\Backend\Tag;
+
+use App\Http\Requests\Request;
+
+class CreateRequest extends Request
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            'name' => 'required|unique:tags,tag_name',
+        ];
+    }
+
+    public function messages()
+    {
+        return [
+            'name.required' => '标签不能为空',
+            'name.unique'   => '标签名必须唯一'
+        ];
+    }
+}

+ 39 - 0
app/Http/Requests/Backend/Tag/UpdateRequest.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Http\Requests\Backend\Tag;
+
+use App\Http\Requests\Request;
+
+class UpdateRequest extends Request
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        $id = $this->route('tag');
+        return [
+            'name' => 'required|unique:tags,tag_name,'.$id,
+        ];
+    }
+
+    public function messages()
+    {
+        return [
+            'name.required' => '请填写分类名称',
+            'name.unique'   => '标签名必须唯一'
+        ];
+    }
+}

+ 1 - 0
app/Http/routes.php

@@ -28,5 +28,6 @@ Route::group(['prefix'=>'backend'], function(){
         Route::resource('article', 'Backend\ArticleController');
         Route::resource('category', 'Backend\CategoryController');
         Route::resource('user', 'Backend\UserController');
+        Route::resource('tag', 'Backend\TagController');
     });
 });

+ 17 - 0
app/Models/Tag.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Prettus\Repository\Contracts\Transformable;
+use Prettus\Repository\Traits\TransformableTrait;
+
+class Tag extends Model implements Transformable
+{
+    use TransformableTrait;
+
+    protected $fillable = ['tag_name'];
+
+    protected $table = 'tags';
+
+}

+ 24 - 0
app/Presenters/TagPresenter.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Presenters;
+
+use App\Transformers\TagTransformer;
+use Prettus\Repository\Presenter\FractalPresenter;
+
+/**
+ * Class TagPresenter
+ *
+ * @package namespace App\Presenters;
+ */
+class TagPresenter extends FractalPresenter
+{
+    /**
+     * Transformer
+     *
+     * @return \League\Fractal\TransformerAbstract
+     */
+    public function getTransformer()
+    {
+        return new TagTransformer();
+    }
+}

+ 14 - 0
app/Repositories/TagRepository.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Repositories;
+
+use Prettus\Repository\Contracts\RepositoryInterface;
+
+/**
+ * Interface TagRepository
+ * @package namespace App\Repositories;
+ */
+interface TagRepository extends RepositoryInterface
+{
+    //
+}

+ 36 - 0
app/Repositories/TagRepositoryEloquent.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace App\Repositories;
+
+use Prettus\Repository\Eloquent\BaseRepository;
+use Prettus\Repository\Criteria\RequestCriteria;
+use App\Repositories\TagRepository;
+use App\Models\Tag;
+use App\Validators\TagValidator;
+
+/**
+ * Class TagRepositoryEloquent
+ * @package namespace App\Repositories;
+ */
+class TagRepositoryEloquent extends BaseRepository implements TagRepository
+{
+    /**
+     * Specify Model class name
+     *
+     * @return string
+     */
+    public function model()
+    {
+        return Tag::class;
+    }
+
+    
+
+    /**
+     * Boot up the repository, pushing criteria
+     */
+    public function boot()
+    {
+        $this->pushCriteria(app(RequestCriteria::class));
+    }
+}

+ 32 - 0
app/Transformers/TagTransformer.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Transformers;
+
+use League\Fractal\TransformerAbstract;
+use App\Models\Tag;
+
+/**
+ * Class TagTransformer
+ * @package namespace App\Transformers;
+ */
+class TagTransformer extends TransformerAbstract
+{
+
+    /**
+     * Transform the \Tag entity
+     * @param \Tag $model
+     *
+     * @return array
+     */
+    public function transform(Tag $model)
+    {
+        return [
+            'id'         => (int) $model->id,
+
+            /* place your other model properties here */
+
+            'created_at' => $model->created_at,
+            'updated_at' => $model->updated_at
+        ];
+    }
+}

+ 18 - 0
public/js/backend.js

@@ -146,4 +146,22 @@ $(function() {
         $("#user-form")[0].reset();
     });
 
+    $("#tag-form").bootstrapValidator({
+        validatorDefaultParam,
+        fields : {
+            name: {
+                validators: {
+                    notEmpty: {
+                        message: "请输入文章标签名"
+                    }
+                }
+            }
+        }
+    });
+
+    $("#tag-form #reset-btn").click(function(){
+        $("#tag-form").data('bootstrapValidator').resetForm(true);
+        $("#tag-form")[0].reset();
+    });
+
 });

BIN
public/uploads/avatar/5f0bc863c5814c013888200d9010c045.jpg


+ 40 - 0
resources/views/backend/tag/create.blade.php

@@ -0,0 +1,40 @@
+@extends('layouts.backend')
+
+@section('title', '文章标签添加')
+
+@section('header')
+    <h1>
+        文章标签添加
+    </h1>
+@endsection
+
+@section('content')
+    <div class="row">
+        <div class="col-xs-12">
+            @include('backend.alert.warning')
+            <div class="box box-solid">
+                <form role="form" method="post" action="{{ url('backend/tag') }}" id="tag-form">
+                    <div class="box-body">
+                        <div class="form-group">
+                            <label for="name">分类名称</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <input type='text' class='form-control' name="name" id='name' placeholder='请输入标签名称'>
+                                </div>
+                            </div>
+                        </div>
+                    </div>
+
+                    {{ csrf_field() }}
+
+
+                    <div class="box-footer">
+                        <button type="submit" class="btn btn-primary">确定</button>
+                        <button type="button" class="btn btn-warning" id="reset-btn">重置</button>
+                    </div>
+                </form>
+            </div>
+            <!-- /.box -->
+        </div>
+    </div>
+@endsection

+ 40 - 0
resources/views/backend/tag/edit.blade.php

@@ -0,0 +1,40 @@
+@extends('layouts.backend')
+
+@section('title', '文章标签修改')
+
+@section('header')
+    <h1>
+        文章标签修改
+    </h1>
+@endsection
+
+@section('content')
+    <div class="row">
+        @include('backend.alert.warning')
+        <div class="col-xs-12">
+            <div class="box box-solid">
+                <form role="form" method="post" action="{{ route('backend.tag.update', ['id' => $tag->id]) }}" id="tag-form">
+                    <div class="box-body">
+                        <div class="form-group">
+                            <label for="name">标签名称</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <input type='text' value="{{ $tag->tag_name }}" class='form-control' name="name" id='name' placeholder='请输入标签名称'>
+                                </div>
+                            </div>
+                        </div>
+                    </div>
+
+                    {{ csrf_field() }}
+                    {{ method_field('PUT') }}
+
+                    <div class="box-footer">
+                        <button type="submit" class="btn btn-primary">确定</button>
+                        <button type="button" class="btn btn-warning" id="reset-btn">重置</button>
+                    </div>
+                </form>
+            </div>
+            <!-- /.box -->
+        </div>
+    </div>
+@endsection

+ 69 - 0
resources/views/backend/tag/index.blade.php

@@ -0,0 +1,69 @@
+@extends('layouts.backend')
+
+@section('title', '文章标签')
+
+@section('header')
+    <h1>
+        文章标签
+    </h1>
+@endsection
+
+@section('content')
+    <div class="row">
+        <div class="col-xs-12">
+            @include('backend.alert.success')
+            <div class="box box-solid">
+                <!-- /.box-header -->
+                <div class="box-header">
+                    <div class="pull-right">
+                        <div class="btn-group">
+                            <a href="{{ route('backend.tag.create') }}" class="btn btn-white tooltips"
+                               data-toggle="tooltip" data-original-title="新增"><i
+                                        class="glyphicon glyphicon-plus"></i></a>
+                        </div>
+                    </div><!-- pull-right -->
+                </div>
+                <div class="box-body table-responsive no-padding ">
+                    <table class="table table-hover">
+                        <tr>
+                            <th>序号</th>
+                            <th>标签名</th>
+                            <th>文章数</th>
+                            <th>操作</th>
+                        </tr>
+                        @if ($tags)
+                            <?php $line = 1  ?>
+                            @foreach($tags as $tag)
+                                <tr>
+                                    <td>{{ $line }}</td>
+                                    <td>{{ $tag->tag_name }}</td>
+                                    <td>{{ $tag->article_number }}</td>
+                                    <td>
+                                        <a href='{{ route("backend.tag.edit", ["id" => $tag->id]) }}' class='btn btn-info btn-xs'>
+                                            <i class="fa fa-pencil"></i> 修改</a>
+                                        <a data-href='{{ route("backend.tag.destroy", ["id" => $tag->id]) }}'
+                                           class='btn btn-danger btn-xs tag-delete'><i class="fa fa-trash-o"></i> 删除</a>
+                                    </td>
+                                </tr>
+                                <?php $line++ ?>
+                            @endforeach
+                        @endif
+                    </table>
+                </div>
+                <!-- /.box-body -->
+            </div>
+            <!-- /.box -->
+        </div>
+    </div>
+@endsection
+
+@section('javascript')
+    <script>
+        $(function() {
+            $(".tag-delete").click(function(){
+                var url = $(this).attr('data-href');
+                Moell.ajax.delete(url);
+            });
+        });
+    </script>
+@endsection

+ 12 - 0
resources/views/layouts/backend.blade.php

@@ -205,6 +205,7 @@
                         <li><a href="{{ url('backend/article') }}">文章管理</a></li>
                         <li><a href="{{ url('backend/article/create') }}">发布文章</a></li>
                         <li><a href="{{ url('backend/category') }}">文章分类</a></li>
+                        <li><a href="{{ url('backend/tag') }}">文章标签</a></li>
                     </ul>
                 </li>
                 <li>
@@ -212,6 +213,17 @@
                         <i class="fa fa-tags"></i> <span>标签</span>
                     </a>
                 </li>
+                <li class="treeview">
+                    <a href="#">
+                        <i class="fa fa-file-image-o"></i>
+                        <span>多媒体</span>
+                        <i class="fa fa-angle-left pull-right"></i>
+                    </a>
+                    <ul class="treeview-menu">
+                        <li><a href="pages/charts/chartjs.html">媒体库</a></li>
+                        <li><a href="pages/charts/morris.html">添加</a></li>
+                    </ul>
+                </li>
                 <li class="treeview">
                     <a href="#">
                         <i class="fa fa-navicon"></i>