Skip to content

Commit

Permalink
Admin can manage content
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Nov 1, 2017
1 parent 60111c2 commit e12ada1
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 0 deletions.
35 changes: 35 additions & 0 deletions bootstrap/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,39 @@ function make_excerpt($value, $length = 200)
{
$excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value)));
return str_limit($excerpt, $length);
}

function model_admin_link($title, $model)
{
return model_link($title, $model, 'admin');
}

function model_link($title, $model, $prefix = '')
{
// 获取数据模型的复数蛇形命名
$model_name = model_plural_name($model);

// 初始化前缀
$prefix = $prefix ? "/$prefix/" : '/';

// 使用站点 URL 拼接全量 URL
$url = config('app.url') . $prefix . $model_name . '/' . $model->id;

// 拼接 HTML A 标签,并返回
return '<a href="' . $url . '" target="_blank">' . $title . '</a>';
}

function model_plural_name($model)
{
// 从实体中获取完整类名,例如:App\Models\User
$full_class_name = get_class($model);

// 获取基础类名,例如:传参 `App\Models\User` 会得到 `User`
$class_name = class_basename($full_class_name);

// 蛇形命名,例如:传参 `User` 会得到 `user`, `FooBar` 会得到 `foo_bar`
$snake_case_name = snake_case($class_name);

// 获取子串的复数形式,例如:传参 `user` 会得到 `users`
return str_plural($snake_case_name);
}
Binary file added config/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions config/administrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
'roles',
'permissions',
],
'内容管理' => [
'categories',
'topics',
'replies',
],
],

/*
Expand Down
63 changes: 63 additions & 0 deletions config/administrator/categories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use App\Models\Category;

return [
'title' => '分类',
'single' => '分类',
'model' => Category::class,

// 对 CRUD 动作的单独权限控制,其他动作不指定默认为通过
'action_permissions' => [
// 删除权限控制
'delete' => function () {
// 只有站长才能删除话题分类
return Auth::user()->hasRole('Founder');
},
],

'columns' => [
'id' => [
'title' => 'ID',
],
'name' => [
'title' => '名称',
'sortable' => false,
],
'description' => [
'title' => '描述',
'sortable' => false,
],
'operation' => [
'title' => '管理',
'sortable' => false,
],
],
'edit_fields' => [
'name' => [
'title' => '名称',
],
'description' => [
'title' => '描述',
'type' => 'textarea',
],
],
'filters' => [
'id' => [
'title' => '分类 ID',
],
'name' => [
'title' => '名称',
],
'description' => [
'title' => '描述',
],
],
'rules' => [
'name' => 'required|min:1|unique:categories'
],
'messages' => [
'name.unique' => '分类名在数据库里有重复,请选用其他名称。',
'name.required' => '请确保名字至少一个字符以上',
],
];
92 changes: 92 additions & 0 deletions config/administrator/replies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

use App\Models\Reply;

return [
'title' => '回复',
'single' => '回复',
'model' => Reply::class,

'columns' => [

'id' => [
'title' => 'ID',
],
'content' => [
'title' => '内容',
'sortable' => false,
'output' => function ($value, $model) {
return '<div style="max-width:220px">' . $value . '</div>';
},
],
'user' => [
'title' => '作者',
'sortable' => false,
'output' => function ($value, $model) {
$avatar = $model->user->avatar;
$value = empty($avatar) ? 'N/A' : '<img src="'.$avatar.'" style="height:22px;width:22px"> ' . $model->user->name;
return model_link($value, $model);
},
],
'topic' => [
'title' => '话题',
'sortable' => false,
'output' => function ($value, $model) {
return '<div style="max-width:260px">' . model_admin_link($model->topic->title, $model->topic) . '</div>';
},
],
'operation' => [
'title' => '管理',
'sortable' => false,
],
],
'edit_fields' => [
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
'topic' => [
'title' => '话题',
'type' => 'relationship',
'name_field' => 'title',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', title)"),
'options_sort_field' => 'id',
],
'content' => [
'title' => '回复内容',
'type' => 'textarea',
],
],
'filters' => [
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
'topic' => [
'title' => '话题',
'type' => 'relationship',
'name_field' => 'title',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', title)"),
'options_sort_field' => 'id',
],
'content' => [
'title' => '回复内容',
],
],
'rules' => [
'content' => 'required'
],
'messages' => [
'content.required' => '请填写回复内容',
],
];
105 changes: 105 additions & 0 deletions config/administrator/topics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

use App\Models\Topic;

return [
'title' => '话题',
'single' => '话题',
'model' => Topic::class,

'columns' => [

'id' => [
'title' => 'ID',
],
'title' => [
'title' => '话题',
'sortable' => false,
'output' => function ($value, $model) {
return '<div style="max-width:260px">' . model_link($value, $model) . '</div>';
},
],
'user' => [
'title' => '作者',
'sortable' => false,
'output' => function ($value, $model) {
$avatar = $model->user->avatar;
$value = empty($avatar) ? 'N/A' : '<img src="'.$avatar.'" style="height:22px;width:22px"> ' . $model->user->name;
return model_link($value, $model);
},
],
'category' => [
'title' => '分类',
'sortable' => false,
'output' => function ($value, $model) {
return model_admin_link($model->category->name, $model->category);
},
],
'reply_count' => [
'title' => '评论',
],
'operation' => [
'title' => '管理',
'sortable' => false,
],
],
'edit_fields' => [
'title' => [
'title' => '标题',
],
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',

// 自动补全,对于大数据量的对应关系,推荐开启自动补全,
// 可防止一次性加载对系统造成负担
'autocomplete' => true,

// 自动补全的搜索字段
'search_fields' => ["CONCAT(id, ' ', name)"],

// 自动补全排序
'options_sort_field' => 'id',
],
'category' => [
'title' => '分类',
'type' => 'relationship',
'name_field' => 'name',
'search_fields' => ["CONCAT(id, ' ', name)"],
'options_sort_field' => 'id',
],
'reply_count' => [
'title' => '评论',
],
'view_count' => [
'title' => '查看',
],
],
'filters' => [
'id' => [
'title' => '内容 ID',
],
'user' => [
'title' => '用户',
'type' => 'relationship',
'name_field' => 'name',
'autocomplete' => true,
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
'category' => [
'title' => '分类',
'type' => 'relationship',
'name_field' => 'name',
'search_fields' => array("CONCAT(id, ' ', name)"),
'options_sort_field' => 'id',
],
],
'rules' => [
'title' => 'required'
],
'messages' => [
'title.required' => '请填写标题',
],
];

0 comments on commit e12ada1

Please sign in to comment.