Browse Source

新增导航管理

Moell 9 years ago
parent
commit
c91de1c0d9

+ 113 - 0
app/Http/Controllers/Backend/NavigationController.php

@@ -0,0 +1,113 @@
+<?php
+
+namespace App\Http\Controllers\Backend;
+
+use Illuminate\Http\Request;
+
+use App\Http\Requests;
+use App\Http\Controllers\Controller;
+use App\Repositories\NavigationRepositoryEloquent;
+use App\Http\Requests\Backend\Navigation\CreateRequest;
+use App\Http\Requests\Backend\Navigation\UpdateRequest;
+
+class NavigationController extends Controller
+{
+    protected $navigation;
+
+    public function __construct(NavigationRepositoryEloquent $navigation)
+    {
+        $this->navigation = $navigation;
+    }
+
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        $navigations = $this->navigation->orderBy('sequence', 'desc')->all();
+        return view('backend.navigation.index', compact('navigations'));
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        return view('backend.navigation.create');
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(CreateRequest $request)
+    {
+        if ($this->navigation->create($request->all())) {
+            return redirect('backend/navigation')->with('success', '导航添加成功');
+        }
+        return redirect()->back()->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)
+    {
+        $navigation = $this->navigation->find($id);
+        if ($navigation) {
+            return view('backend.navigation.edit', compact('navigation'));
+        }
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function update(UpdateRequest $request, $id)
+    {
+        if ($this->navigation->update($request->all(), $id)) {
+            return redirect('backend/navigation')->with('success', '导航修改成功');
+        }
+        return redirect()->back()->withErrors('系统异常,修改导航失败');
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  int  $id
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy($id)
+    {
+        $navigation = $this->navigation->find($id);
+        if ($navigation) {
+            if ($this->navigation->delete($id)) {
+                return response()->json(['status' => 0]);
+            }
+        }
+        return response()->json(['status' => 1]);
+    }
+}

+ 40 - 0
app/Http/Requests/Backend/Navigation/CreateRequest.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Http\Requests\Backend\Navigation;
+
+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',
+            'url'   => 'required | url'
+        ];
+    }
+
+    public function messages()
+    {
+        return [
+            'name.required' => '请输入导航名',
+            'url.reqiured' => '请输入URL',
+            'url.url' => '请输入合法的URL'
+        ];
+    }
+}

+ 40 - 0
app/Http/Requests/Backend/Navigation/UpdateRequest.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Http\Requests\Backend\Navigation;
+
+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()
+    {
+        return [
+            'name'  => 'required',
+            'url'   => 'required | url'
+        ];
+    }
+
+    public function messages()
+    {
+        return [
+            'name.required' => '请输入导航名',
+            'url.reqiured' => '请输入URL',
+            'url.url' => '请输入合法的URL'
+        ];
+    }
+}

+ 1 - 0
app/Http/routes.php

@@ -30,5 +30,6 @@ Route::group(['prefix'=>'backend'], function(){
         Route::resource('user', 'Backend\UserController');
         Route::resource('tag', 'Backend\TagController');
         Route::resource('link', 'Backend\LinkController');
+        Route::resource('navigation', 'Backend\NavigationController');
     });
 });

+ 15 - 0
app/Models/Navigation.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Prettus\Repository\Contracts\Transformable;
+use Prettus\Repository\Traits\TransformableTrait;
+
+class Navigation extends Model implements Transformable
+{
+    use TransformableTrait;
+
+    protected $fillable = ['name','url','sequence','state'];
+
+}

+ 17 - 0
app/Presenters/CategoryPresenter.php

@@ -52,6 +52,23 @@ class CategoryPresenter extends FractalPresenter
         $select .= "</select>";
 
         return $select;
+    }
 
+    /**
+     * 获取分类的名称
+     *
+     * @param $categoryId
+     * @return string
+     */
+    public function getIdName($categoryId)
+    {
+        $name = '';
+        if ($categoryId > 0) {
+            $data = $this->category->find($categoryId, ['name']);
+            if ($data) {
+                $name = $data->name;
+            }
+        }
+        return $name;
     }
 }

+ 14 - 0
app/Repositories/NavigationRepository.php

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

+ 36 - 0
app/Repositories/NavigationRepositoryEloquent.php

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

+ 0 - 33
database/migrations/2016_08_31_144116_create_links_table.php

@@ -1,33 +0,0 @@
-<?php
-
-use Illuminate\Database\Schema\Blueprint;
-use Illuminate\Database\Migrations\Migration;
-
-class CreateLinksTable extends Migration
-{
-
-	/**
-	 * Run the migrations.
-	 *
-	 * @return void
-	 */
-	public function up()
-	{
-		Schema::create('links', function(Blueprint $table) {
-            $table->increments('id');
-
-            $table->timestamps();
-		});
-	}
-
-	/**
-	 * Reverse the migrations.
-	 *
-	 * @return void
-	 */
-	public function down()
-	{
-		Schema::drop('links');
-	}
-
-}

+ 38 - 0
database/migrations/2016_09_03_051144_create_navigations_table.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateNavigationsTable extends Migration
+{
+
+	/**
+	 * Run the migrations.
+	 *
+	 * @return void
+	 */
+	public function up()
+	{
+		Schema::create('navigations', function(Blueprint $table) {
+            $table->increments('id');
+			$table->string('name');
+			$table->string('url');
+			$table->tinyInteger('state')->default(0)->comment('0-正常显示;1-隐藏');
+			$table->smallInteger('sequence')->comment('排序');
+            $table->tinyInteger('nav_type')->default(0)->comment('导航类型;0-自定义;1-分类导航');
+            $table->integer('article_cate_id')->default(0)->comment('文章分类id');
+            $table->timestamps();
+		});
+	}
+
+	/**
+	 * Reverse the migrations.
+	 *
+	 * @return void
+	 */
+	public function down()
+	{
+		Schema::drop('navigations');
+	}
+
+}

+ 28 - 0
public/js/backend.js

@@ -192,4 +192,32 @@ $(function() {
         $("#link-form")[0].reset();
     });
 
+    $("#navigation-form").bootstrapValidator({
+        validatorDefaultParam,
+        fields : {
+            name: {
+                validators: {
+                    notEmpty: {
+                        message: "请输入导航名"
+                    }
+                }
+            },
+            url : {
+                validators: {
+                    notEmpty: {
+                        message: "URL不能为空"
+                    },
+                    uri: {
+                        message: '请输入合法的URL地址'
+                    }
+                }
+            }
+        }
+    });
+
+    $("#navigation-form #reset-btn").click(function(){
+        $("#navigation-form").data('bootstrapValidator').resetForm(true);
+        $("#navigation-form")[0].reset();
+    });
+
 });

+ 15 - 9
resources/views/backend/article/index.blade.php

@@ -14,16 +14,22 @@
             <div class="box box-solid">
                 @include('backend.alert.success')
                 <div class="box-header">
-                    <h3 class="box-title"></h3>
-                    <div class="box-tools">
-                        <div class="input-group input-group-sm" style="width: 150px;">
-                            <input type="text" name="table_search" class="form-control pull-right" placeholder="Search">
-
-                            <div class="input-group-btn">
-                                <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
-                            </div>
+                    <form class="form-inline">
+                        <div class="form-group">
+                            <label for="title">标题</label>&nbsp;
+                            <input name='title' type="text" class="form-control" id="title" placeholder="请输入文章标题">&nbsp;
+                        </div>
+                        <div class="form-group">
+                            <label for="tag">标签</label>&nbsp;
+                            <input name='tag' type="text" class="form-control" id="tag" placeholder="请输入文章标签">
+                        </div>
+                        <div class="form-group">
+                            <label for="cate_id">分类</label>&nbsp;
+                            @inject('categoryPresenter', 'App\Presenters\CategoryPresenter')
+                            {!! $categoryPresenter->getSelect(0, '请选择', '') !!}
                         </div>
-                    </div>
+                        <button type="submit" class="btn btn-default">搜索</button>
+                    </form>
                 </div>
                 <!-- /.box-header -->
                 <div class="box-body table-responsive no-padding ">

+ 67 - 0
resources/views/backend/navigation/create.blade.php

@@ -0,0 +1,67 @@
+@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/navigation') }}" id="navigation-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 class="form-group">
+                            <label for="url">URL</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <input type='text' class='form-control' name="url" id='url' placeholder='请输入合法链接地址'>
+                                </div>
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <label for="sequence">显示顺序</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <input type='text' value="0" class='form-control' name="sequence" id='sequence' placeholder='请输入整形的数值'>
+                                </div>
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <label for="state">状态</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <select name="state" id="state" class="form-control">
+                                        <option value="0">显示</option>
+                                        <option value="1">隐藏</option>
+                                    </select>
+                                </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

+ 67 - 0
resources/views/backend/navigation/edit.blade.php

@@ -0,0 +1,67 @@
+@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.navigation.update', ['id' => $navigation->id]) }}" id="navigation-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="{{ $navigation->name }}" class='form-control' name="name" id='name' placeholder='请输入分类名称'>
+                                </div>
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <label for="url">URL</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <input type='text' value="{{ $navigation->url }}" class='form-control' name="url" id='url' placeholder='请输入合法链接地址'>
+                                </div>
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <label for="sequence">显示顺序</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <input type='text' value="{{ $navigation->sequence }}" class='form-control' name="sequence" id='sequence' placeholder='请输入整形的数值'>
+                                </div>
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <label for="state">状态</label>
+                            <div class="row">
+                                <div class='col-md-6'>
+                                    <select name="state" id="state" class="form-control">
+                                        <option value="0" {{ $navigation->state == 0 ? 'selected' : '' }}>显示</option>
+                                        <option value="1" {{ $navigation->state == 1 ? 'selected' : '' }}>隐藏</option>
+                                    </select>
+                                </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

+ 96 - 0
resources/views/backend/navigation/index.blade.php

@@ -0,0 +1,96 @@
+@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.navigation.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>URL</th>
+                            <th>类型</th>
+                            <th>顺序</th>
+                            <th>状态</th>
+                            <th>文章分类</th>
+                            <th>操作</th>
+                        </tr>
+                        @if ($navigations)
+                            @inject('articleCategory', 'App\Presenters\CategoryPresenter')
+                            <?php $line = 1  ?>
+                            @foreach($navigations as $navigation)
+                                <tr>
+                                    <td>{{ $line }}</td>
+                                    <td>{{ $navigation->name }}</td>
+                                    <td>{{ $navigation->url }}</td>
+                                    <td>
+                                        @if ($navigation->nav_type == 1)
+                                            文章分类
+                                            @else
+                                            自定义
+                                        @endif
+                                    </td>
+                                    <td>{{ $navigation->sequence }}</td>
+                                    <td>
+                                        @if ($navigation->state == 1)
+                                            隐藏
+                                            @else
+                                            显示
+                                        @endif
+                                    </td>
+                                    <td>
+                                        @if ($navigation->article_cate_id > 0)
+                                            {{ $articleCategory->getIdName($navigation->article_cate_id) }}
+                                        @endif
+                                    </td>
+                                    <td>
+                                        @if ($navigation->nav_type == 0)
+                                        <a href='{{ route("backend.navigation.edit", ["id" => $navigation->id]) }}' class='btn btn-info btn-xs'>
+                                            <i class="fa fa-pencil"></i> 修改</a>
+                                        @endif
+                                        <a data-href='{{ route("backend.navigation.destroy", ["id" => $navigation->id]) }}'
+                                           class='btn btn-danger btn-xs navigation-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() {
+            $(".navigation-delete").click(function(){
+                var url = $(this).attr('data-href');
+                Moell.ajax.delete(url);
+            });
+        });
+    </script>
+@endsection

+ 2 - 9
resources/views/layouts/backend.blade.php

@@ -223,18 +223,11 @@
                         <li><a href="pages/charts/morris.html">添加</a></li>
                     </ul>
                 </li>
-                <li class="treeview">
-                    <a href="#">
+                <li>
+                    <a href="{{ url('backend/navigation') }}">
                         <i class="fa fa-navicon"></i>
                         <span>导航</span>
-                        <i class="fa fa-angle-left pull-right"></i>
                     </a>
-                    <ul class="treeview-menu">
-                        <li><a href="pages/charts/chartjs.html"><i class="fa fa-circle-o"></i> ChartJS</a></li>
-                        <li><a href="pages/charts/morris.html"><i class="fa fa-circle-o"></i> Morris</a></li>
-                        <li><a href="pages/charts/flot.html"><i class="fa fa-circle-o"></i> Flot</a></li>
-                        <li><a href="pages/charts/inline.html"><i class="fa fa-circle-o"></i> Inline charts</a></li>
-                    </ul>
                 </li>
                 <li class="treeview">
                     <a href="#">