From e12ada1095cd5ee90a4a209e39cc24ba20b1753c Mon Sep 17 00:00:00 2001 From: Summer Date: Wed, 1 Nov 2017 08:33:05 +0000 Subject: [PATCH] Admin can manage content --- bootstrap/helpers.php | 35 ++++++++++ config/.DS_Store | Bin 0 -> 6148 bytes config/administrator.php | 5 ++ config/administrator/categories.php | 63 +++++++++++++++++ config/administrator/replies.php | 92 ++++++++++++++++++++++++ config/administrator/topics.php | 105 ++++++++++++++++++++++++++++ 6 files changed, 300 insertions(+) create mode 100644 config/.DS_Store create mode 100644 config/administrator/categories.php create mode 100644 config/administrator/replies.php create mode 100644 config/administrator/topics.php diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index 424ea35df..0a39e68c3 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -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 '' . $title . ''; +} + +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); } \ No newline at end of file diff --git a/config/.DS_Store b/config/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 [ + 'categories', + 'topics', + 'replies', + ], ], /* diff --git a/config/administrator/categories.php b/config/administrator/categories.php new file mode 100644 index 000000000..e88511162 --- /dev/null +++ b/config/administrator/categories.php @@ -0,0 +1,63 @@ + '分类', + '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' => '请确保名字至少一个字符以上', + ], +]; \ No newline at end of file diff --git a/config/administrator/replies.php b/config/administrator/replies.php new file mode 100644 index 000000000..6c6f89ef1 --- /dev/null +++ b/config/administrator/replies.php @@ -0,0 +1,92 @@ + '回复', + 'single' => '回复', + 'model' => Reply::class, + + 'columns' => [ + + 'id' => [ + 'title' => 'ID', + ], + 'content' => [ + 'title' => '内容', + 'sortable' => false, + 'output' => function ($value, $model) { + return '
' . $value . '
'; + }, + ], + 'user' => [ + 'title' => '作者', + 'sortable' => false, + 'output' => function ($value, $model) { + $avatar = $model->user->avatar; + $value = empty($avatar) ? 'N/A' : ' ' . $model->user->name; + return model_link($value, $model); + }, + ], + 'topic' => [ + 'title' => '话题', + 'sortable' => false, + 'output' => function ($value, $model) { + return '
' . model_admin_link($model->topic->title, $model->topic) . '
'; + }, + ], + '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' => '请填写回复内容', + ], +]; \ No newline at end of file diff --git a/config/administrator/topics.php b/config/administrator/topics.php new file mode 100644 index 000000000..c7b6e7a57 --- /dev/null +++ b/config/administrator/topics.php @@ -0,0 +1,105 @@ + '话题', + 'single' => '话题', + 'model' => Topic::class, + + 'columns' => [ + + 'id' => [ + 'title' => 'ID', + ], + 'title' => [ + 'title' => '话题', + 'sortable' => false, + 'output' => function ($value, $model) { + return '
' . model_link($value, $model) . '
'; + }, + ], + 'user' => [ + 'title' => '作者', + 'sortable' => false, + 'output' => function ($value, $model) { + $avatar = $model->user->avatar; + $value = empty($avatar) ? 'N/A' : ' ' . $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' => '请填写标题', + ], +]; \ No newline at end of file