Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Feature tags #12

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions app/components/link-blog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<select id="form-link-blog" class="uk-width-1-1" v-model="link">
<option value="@blog">{{ 'Posts View' | trans }}</option>
<optgroup :label="'Posts' | trans">
<option v-for="p in posts" :value="p | link">{{ p.title }}</option>
<option v-for="p in posts" :value="p | postlink">{{ p.title }}</option>
</optgroup>
<optgroup :label="'Tags' | trans">
<option v-for="t in tags" :value="t | taglink">{{ t.name }}</option>
</optgroup>
</select>
</div>
Expand All @@ -26,7 +29,8 @@

data: function () {
return {
posts: []
posts: [],
tags: []
}
},

Expand All @@ -35,6 +39,9 @@
this.$http.get('api/blog/post', {filter: {limit: 1000}}).then(function (res) {
this.$set('posts', res.data.posts);
});
this.$http.get('api/blog/tag', {filter: {limit: 1000}}).then(function (res) {
this.$set('tags', res.data.tags);
});
},

ready: function() {
Expand All @@ -43,8 +50,11 @@

filters: {

link: function (post) {
postlink: function (post) {
return '@blog/id?id='+post.id;
},
taglink: function (tag) {
return '@blog/tag?id='+tag.id;
}

}
Expand Down
11 changes: 11 additions & 0 deletions app/components/post-settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
</div>
</div>

<div class="uk-form-row">
<span class="uk-form-label">{{ 'Tags' | trans }}</span>
<div class="uk-form-controls">
<ul class="uk-list">
<li v-for="tag in data.post_tags">
{{ tag.name }}
</li>
</ul>
</div>
</div>

<div class="uk-form-row">
<span class="uk-form-label">{{ 'Restrict Access' | trans }}</span>
<div class="uk-form-controls uk-form-controls-text">
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/post-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ window.Post = {
data: window.$data,
post: window.$data.post,
sections: []
}
};
},

created: function () {
Expand Down
21 changes: 21 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'name' => '@blog/api',
'controller' => [
'Pagekit\\Blog\\Controller\\PostApiController',
'Pagekit\\Blog\\Controller\\TagApiController',
'Pagekit\\Blog\\Controller\\CommentApiController'
]
]
Expand Down Expand Up @@ -159,6 +160,26 @@
new PostListener(),
new ReadmorePlugin
);
//temp add db in dev environment
//todo remove here, add to update/install scripts
$util = $app['db']->getUtility();
if ($util->tableExists('@blog_tag') === false) {
$util->createTable('@blog_tag', function ($table) {
$table->addColumn('id', 'integer', ['unsigned' => true, 'length' => 10, 'autoincrement' => true]);
$table->addColumn('name', 'string', ['length' => 255]);
$table->addColumn('slug', 'string', ['length' => 255]);
$table->setPrimaryKey(['id']);
});
}

if ($util->tableExists('@blog_post_tags') === false) {
$util->createTable('@blog_post_tags', function ($table) {
$table->addColumn('id', 'integer', ['unsigned' => true, 'length' => 10, 'autoincrement' => true]);
$table->addColumn('post_id', 'integer', ['unsigned' => true, 'length' => 10]);
$table->addColumn('tag_id', 'integer', ['unsigned' => true, 'length' => 10]);
$table->setPrimaryKey(['id']);
});
}
},

'view.scripts' => function ($event, $scripts) {
Expand Down
26 changes: 26 additions & 0 deletions scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@
$table->addIndex(['post_id', 'status'], '@BLOG_COMMENT_POST_ID_STATUS');
});
}

if ($util->tableExists('@blog_tag') === false) {
$util->createTable('@blog_tag', function ($table) {
$table->addColumn('id', 'integer', ['unsigned' => true, 'length' => 10, 'autoincrement' => true]);
$table->addColumn('name', 'string', ['length' => 255]);
$table->addColumn('slug', 'string', ['length' => 255]);
$table->setPrimaryKey(['id']);
});
}

if ($util->tableExists('@blog_post_tags') === false) {
$util->createTable('@blog_post_tags', function ($table) {
$table->addColumn('id', 'integer', ['unsigned' => true, 'length' => 10, 'autoincrement' => true]);
$table->addColumn('post_id', 'integer', ['unsigned' => true, 'length' => 10]);
$table->addColumn('tag_id', 'integer', ['unsigned' => true, 'length' => 10]);
$table->setPrimaryKey(['id']);
});
}

},

Expand All @@ -64,6 +82,14 @@
if ($util->tableExists('@blog_comment')) {
$util->dropTable('@blog_comment');
}

if ($util->tableExists('@blog_tag')) {
$util->dropTable('@blog_tag');
}

if ($util->tableExists('@blog_post_tags')) {
$util->dropTable('@blog_post_tags');
}
},

'updates' => [
Expand Down
5 changes: 4 additions & 1 deletion src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Pagekit\Application as App;
use Pagekit\Blog\Model\Comment;
use Pagekit\Blog\Model\Post;
use Pagekit\Blog\Model\Tag;
use Pagekit\User\Model\Role;

/**
Expand Down Expand Up @@ -44,7 +45,7 @@ public function editAction($id = 0)
{
try {

if (!$post = Post::where(compact('id'))->related('user')->first()) {
if (!$post = Post::where(compact('id'))->related('user', 'tags')->first()) {

if ($id) {
App::abort(404, __('Invalid post id.'));
Expand Down Expand Up @@ -88,6 +89,8 @@ public function editAction($id = 0)
],
'$data' => [
'post' => $post,
'post_tags' => array_values($post->tags ? : []),
'tags' => array_values(Tag::findAll()),
'statuses' => Post::getStatuses(),
'roles' => array_values(Role::findAll()),
'canEditAll' => $user->hasAccess('blog: manage all posts'),
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/PostApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function indexAction($filter = [], $page = 0)
$pages = ceil($count / $limit);
$page = max(0, min($pages - 1, $page));

$posts = array_values($query->offset($page * $limit)->related('user', 'comments')->limit($limit)->orderBy($order[1], $order[2])->get());
$posts = array_values($query->offset($page * $limit)->related('user', 'comments', 'tags')->limit($limit)->orderBy($order[1], $order[2])->get());

return compact('posts', 'pages', 'count');
}
Expand Down
26 changes: 20 additions & 6 deletions src/Controller/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@ public function __construct()
* @Route("/")
* @Route("/page/{page}", name="page", requirements={"page" = "\d+"})
*/
public function indexAction($page = 1)
public function indexAction($page = 1, $tag_id = 0)
{
if (!App::node()->hasAccess(App::user())) {
App::abort(403, __('Insufficient User Rights.'));
}

$query = Post::where(['status = ?', 'date < ?'], [Post::STATUS_PUBLISHED, new \DateTime])->where(function ($query) {
$query = Post::where(['status = ?', 'date < ?'], [Post::STATUS_PUBLISHED, new \DateTime])->from('@blog_post i')->where(function ($query) {
return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
})->related('user');
})->related('user', 'tags');

if ($tag_id) {
//taxonomy trick?
$query->join('@blog_post_tags t', 'post_id = i.id')->where('tag_id = ?', [$tag_id]);
}

if (!$limit = $this->blog->config('posts.posts_per_page')) {
$limit = 10;
}

$count = $query->count('id');
$count = $query->count('i.id');
$total = ceil($count / $limit);
$page = max(1, min($total, $page));

Expand Down Expand Up @@ -97,7 +102,7 @@ public function feedAction($type = '')

foreach (Post::where(['status = ?', 'date < ?'], [Post::STATUS_PUBLISHED, new \DateTime])->where(function ($query) {
return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
})->related('user')->limit($this->blog->config('feed.limit'))->orderBy('date', 'DESC')->get() as $post) {
})->related('user', 'tags')->limit($this->blog->config('feed.limit'))->orderBy('date', 'DESC')->get() as $post) {
$url = App::url('@blog/id', ['id' => $post->id], 0);
$feed->addItem(
$feed->createItem([
Expand All @@ -119,7 +124,7 @@ public function feedAction($type = '')
*/
public function postAction($id = 0)
{
if (!$post = Post::where(['id = ?', 'status = ?', 'date < ?'], [$id, Post::STATUS_PUBLISHED, new \DateTime])->related('user')->first()) {
if (!$post = Post::where(['id = ?', 'status = ?', 'date < ?'], [$id, Post::STATUS_PUBLISHED, new \DateTime])->related('user', 'tags')->first()) {
App::abort(404, __('Post not found!'));
}

Expand Down Expand Up @@ -168,4 +173,13 @@ public function postAction($id = 0)
'post' => $post
];
}

/**
* @Route("/tag/{id}", name="tag")
* @Route("/tag/{id}/page/{page}", name="tag/page", requirements={"page" = "\d+", "id" = "\d+"})
*/
public function tagAction($page = 1, $id = '')
{
return $this->indexAction($page, $id);
}
}
113 changes: 113 additions & 0 deletions src/Controller/TagApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Pagekit\Blog\Controller;

use Pagekit\Application as App;
use Pagekit\Blog\Model\Tag;

/**
* @Access("blog: manage own posts || blog: manage all posts")
* @Route("tag", name="tag")
*/
class TagApiController
{
/**
* @Route("/", methods="GET")
* @Request({"filter": "array", "page":"int"})
*/
public function indexAction($filter = [], $page = 0)
{
$query = Tag::query();
$filter = array_merge(array_fill_keys(['search', 'order', 'limit'], ''), $filter);

extract($filter, EXTR_SKIP);

if ($search) {
$query->where(function ($query) use ($search) {
$query->orWhere(['name LIKE :search', 'slug LIKE :search'], ['search' => "%{$search}%"]);
});
}

if (!preg_match('/^(name|slug)\s(asc|desc)$/i', $order, $order)) {
$order = [1 => 'name', 2 => 'asc'];
}

$limit = (int) $limit ?: App::module('blog')->config('posts.posts_per_page');
$count = $query->count();
$pages = ceil($count / $limit);
$page = max(0, min($pages - 1, $page));

$tags = array_values($query->offset($page * $limit)->limit($limit)->orderBy($order[1], $order[2])->get());

return compact('tags', 'pages', 'count');
}

/**
* @Route("/", methods="POST")
* @Route("/{id}", methods="POST", requirements={"id"="\d+"})
* @Request({"tag": "array", "id": "int"}, csrf=true)
*/
public function saveAction($data, $id = 0)
{
if (!$id || !$tag = Tag::find($id)) {

if ($id) {
App::abort(404, __('Post not found.'));
}

$tag = Tag::create();
}

if (!$data['slug'] = App::filter($data['slug'] ?: $data['name'], 'slugify')) {
App::abort(400, __('Invalid slug.'));
}

$tag->save($data);

return ['message' => 'success', 'tag' => $tag];
}

/**
* @Route("/{id}", methods="DELETE", requirements={"id"="\d+"})
* @Request({"id": "int"}, csrf=true)
*/
public function deleteAction($id)
{
if ($tag = Tag::find($id)) {

if(!App::user()->hasAccess('blog: manage all posts')) {
App::abort(400, __('Access denied.'));
}

$tag->delete();
}

return ['message' => 'success'];
}

/**
* @Route("/bulk", methods="POST")
* @Request({"tags": "array"}, csrf=true)
*/
public function bulkSaveAction($tags = [])
{
foreach ($tags as $data) {
$this->saveAction($data, isset($data['id']) ? $data['id'] : 0);
}

return ['message' => 'success'];
}

/**
* @Route("/bulk", methods="DELETE")
* @Request({"ids": "array"}, csrf=true)
*/
public function bulkDeleteAction($ids = [])
{
foreach (array_filter($ids) as $id) {
$this->deleteAction($id);
}

return ['message' => 'success'];
}
}
3 changes: 3 additions & 0 deletions src/Event/RouteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function onConfigureRoute($event, $route)
if ($route->getName() == '@blog/id' && UrlResolver::getPermalink()) {
App::routes()->alias(dirname($route->getPath()).'/'.ltrim(UrlResolver::getPermalink(), '/'), '@blog/id', ['_resolver' => 'Pagekit\Blog\UrlResolver']);
}
if ($route->getName() == '@blog/tag') {
App::routes()->alias(dirname($route->getPath()).'/{tag}/', '@blog/tag', ['_resolver' => 'Pagekit\Blog\TagUrlResolver']);
}
}

/**
Expand Down
Loading