diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index c233eda..d95b9b8 100644 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php @@ -249,4 +249,75 @@ class Database { public function getLastId($column = '') { return $this->getHandle()->lastInsertId($column); } + + public function retrieveAllTags() { + $sql = "SELECT * FROM tags"; + $query = $this->executeQuery($sql, array()); + $tags = $query->fetchAll(); + + return $tags; + } + + public function retrieveTag($id) { + $tag = NULL; + $sql = "SELECT * FROM tags WHERE id=?"; + $params = array(intval($id)); + $query = $this->executeQuery($sql, $params); + $tag = $query->fetchAll(); + + return isset($tag[0]) ? $tag[0] : null; + } + + public function retrieveEntriesByTag($tag_id) { + $sql = + "SELECT * FROM entries + LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id + WHERE tags_entries.tag_id = ?"; + $query = $this->executeQuery($sql, array($tag_id)); + $entries = $query->fetchAll(); + + return $entries; + } + + public function retrieveTagsByEntry($entry_id) { + $sql = + "SELECT * FROM tags + LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id + WHERE tags_entries.entry_id = ?"; + $query = $this->executeQuery($sql, array($entry_id)); + $tags = $query->fetchAll(); + + return $tags; + } + + public function removeTagForEntry($entry_id, $tag_id) { + $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?"; + $params_action = array($tag_id, $entry_id); + $query = $this->executeQuery($sql_action, $params_action); + return $query; + } + + public function retrieveTagByValue($value) { + $tag = NULL; + $sql = "SELECT * FROM tags WHERE value=?"; + $params = array($value); + $query = $this->executeQuery($sql, $params); + $tag = $query->fetchAll(); + + return isset($tag[0]) ? $tag[0] : null; + } + + public function createTag($value) { + $sql_action = 'INSERT INTO tags ( value ) VALUES (?)'; + $params_action = array($value); + $query = $this->executeQuery($sql_action, $params_action); + return $query; + } + + public function setTagToEntry($tag_id, $entry_id) { + $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)'; + $params_action = array($tag_id, $entry_id); + $query = $this->executeQuery($sql_action, $params_action); + return $query; + } } diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index d45d0c4..d415dd0 100644 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php @@ -397,6 +397,36 @@ class Poche Tools::redirect(); } break; + case 'add_tag' : + $tags = explode(',', $_POST['value']); + $entry_id = $_POST['entry_id']; + foreach($tags as $key => $tag_value) { + $value = trim($tag_value); + $tag = $this->store->retrieveTagByValue($value); + + if (is_null($tag)) { + # we create the tag + $tag = $this->store->createTag($value); + $sequence = ''; + if (STORAGE == 'postgres') { + $sequence = 'tags_id_seq'; + } + $tag_id = $this->store->getLastId($sequence); + } + else { + $tag_id = $tag['id']; + } + + # we assign the tag to the article + $this->store->setTagToEntry($tag_id, $entry_id); + } + Tools::redirect(); + break; + case 'remove_tag' : + $tag_id = $_GET['tag_id']; + $this->store->removeTagForEntry($id, $tag_id); + Tools::redirect(); + break; default: break; } @@ -430,6 +460,31 @@ class Poche ); Tools::logm('config view'); break; + case 'edit-tags': + # tags + $tags = $this->store->retrieveTagsByEntry($id); + $tpl_vars = array( + 'entry_id' => $id, + 'tags' => $tags, + ); + break; + case 'tag': + $entries = $this->store->retrieveEntriesByTag($id); + $tag = $this->store->retrieveTag($id); + $tpl_vars = array( + 'tag' => $tag, + 'entries' => $entries, + ); + break; + case 'tags': + $token = $this->user->getConfigValue('token'); + $tags = $this->store->retrieveAllTags(); + $tpl_vars = array( + 'token' => $token, + 'user_id' => $this->user->getId(), + 'tags' => $tags, + ); + break; case 'view': $entry = $this->store->retrieveOneById($id, $this->user->getId()); if ($entry != NULL) { @@ -443,12 +498,16 @@ class Poche # flattr checking $flattr = new FlattrItem(); - $flattr->checkItem($entry['url'],$entry['id']); + $flattr->checkItem($entry['url'], $entry['id']); + + # tags + $tags = $this->store->retrieveTagsByEntry($entry['id']); $tpl_vars = array( - 'entry' => $entry, - 'content' => $content, - 'flattr' => $flattr + 'entry' => $entry, + 'content' => $content, + 'flattr' => $flattr, + 'tags' => $tags ); } else { @@ -859,9 +918,9 @@ class Poche $_SESSION['poche_user']->setConfig($currentConfig); } - public function generateFeeds($token, $user_id, $type = 'home') + public function generateFeeds($token, $user_id, $tag_id, $type = 'home') { - $allowed_types = array('home', 'fav', 'archive'); + $allowed_types = array('home', 'fav', 'archive', 'tag'); $config = $this->store->getConfigUser($user_id); if (!in_array($type, $allowed_types) || @@ -876,7 +935,13 @@ class Poche $feed->setChannelElement('updated', date(DATE_RSS , time())); $feed->setChannelElement('author', 'poche'); - $entries = $this->store->getEntriesByView($type, $user_id); + if ($type == 'tag') { + $entries = $this->store->retrieveEntriesByTag($tag_id); + } + else { + $entries = $this->store->getEntriesByView($type, $user_id); + } + if (count($entries) > 0) { foreach ($entries as $entry) { $newItem = $feed->createNewItem(); diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php index 9d8e1fd..6391658 100644 --- a/inc/poche/Tools.class.php +++ b/inc/poche/Tools.class.php @@ -88,39 +88,16 @@ class Tools public static function getTplFile($view) { - $default_tpl = 'home.twig'; - - switch ($view) { - case 'install': - $tpl_file = 'install.twig'; - break; - case 'import'; - $tpl_file = 'import.twig'; - break; - case 'export': - $tpl_file = 'export.twig'; - break; - case 'config': - $tpl_file = 'config.twig'; - break; - case 'view': - $tpl_file = 'view.twig'; - break; - - case 'login': - $tpl_file = 'login.twig'; - break; - - case 'error': - $tpl_file = 'error.twig'; - break; - - default: - $tpl_file = $default_tpl; - break; + $views = array( + 'install', 'import', 'export', 'config', 'tags', + 'edit-tags', 'view', 'login', 'error', 'tag' + ); + + if (in_array($view, $views)) { + return $view . '.twig'; } - - return $tpl_file; + + return 'home.twig'; } public static function getFile($url) diff --git a/index.php b/index.php index d2b363b..145da77 100644 --- a/index.php +++ b/index.php @@ -77,7 +77,8 @@ if (isset($_GET['login'])) { $poche->generateToken(); } else { - $poche->generateFeeds($_GET['token'], $_GET['user_id'], $_GET['type']); + $tag_id = (isset($_GET['tag_id']) ? intval($_GET['tag_id']) : 0); + $poche->generateFeeds($_GET['token'], $_GET['user_id'], $tag_id, $_GET['type']); } } diff --git a/install/mysql.sql b/install/mysql.sql index 9b01e32..66c4bb3 100644 --- a/install/mysql.sql +++ b/install/mysql.sql @@ -31,4 +31,19 @@ CREATE TABLE IF NOT EXISTS `users_config` ( `name` varchar(255) NOT NULL, `value` varchar(255) NOT NULL, PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE tags ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `value` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE tags_entries ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `entry_id` int(11) NOT NULL, + `tag_id` int(11) NOT NULL, + FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, + FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE, + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/install/poche.sqlite b/install/poche.sqlite index 7abf1f6..b911f27 100755 Binary files a/install/poche.sqlite and b/install/poche.sqlite differ diff --git a/install/postgres.sql b/install/postgres.sql index 9e0e827..fe8f559 100644 --- a/install/postgres.sql +++ b/install/postgres.sql @@ -27,4 +27,15 @@ CREATE TABLE users_config ( user_id integer NOT NULL, name varchar(255) NOT NULL, value varchar(255) NOT NULL -); \ No newline at end of file +); + +CREATE TABLE tags ( + id bigserial primary key, + value varchar(255) NOT NULL +); + +CREATE TABLE tags_entries ( + id bigserial primary key, + entry_id integer NOT NULL, + tag_id integer NOT NULL +) \ No newline at end of file diff --git a/themes/default/_menu.twig b/themes/default/_menu.twig index 699d6a0..02bec1d 100644 --- a/themes/default/_menu.twig +++ b/themes/default/_menu.twig @@ -2,6 +2,7 @@
  • {% trans "home" %}
  • {% trans "favorites" %}
  • {% trans "archive" %}
  • +
  • {% trans "tags" %}
  • {% trans "config" %}
  • {% trans "logout" %}
  • \ No newline at end of file diff --git a/themes/default/css/style.css b/themes/default/css/style.css index 670eb50..2088ee2 100644 --- a/themes/default/css/style.css +++ b/themes/default/css/style.css @@ -176,6 +176,12 @@ a:visited { text-decoration: none; } +#article .tags { + font-size: 0.8em; + color: #888; + padding-bottom: 5px; +} + .backhome { display: inline; } diff --git a/themes/default/edit-tags.twig b/themes/default/edit-tags.twig new file mode 100644 index 0000000..7116bba --- /dev/null +++ b/themes/default/edit-tags.twig @@ -0,0 +1,20 @@ +{% extends "layout.twig" %} +{% block title %}edit tags{% endblock %} +{% block menu %} +{% include '_menu.twig' %} +{% endblock %} +{% block content %} +{% if tags is empty %} +no tags +{% endif %} + +
    + +

    {% trans "you can type several tags, separated by comma" %}

    + + +
    +{% trans "back to the article" %} +{% endblock %} \ No newline at end of file diff --git a/themes/default/img/default/rss.png b/themes/default/img/default/rss.png new file mode 100644 index 0000000..21bad1a Binary files /dev/null and b/themes/default/img/default/rss.png differ diff --git a/themes/default/tag.twig b/themes/default/tag.twig new file mode 100644 index 0000000..364c7cd --- /dev/null +++ b/themes/default/tag.twig @@ -0,0 +1,33 @@ +{% extends "layout.twig" %} +{% block title %}tag {% endblock %} +{% block menu %} +{% include '_menu.twig' %} +{% endblock %} +{% block content %} +

    {% trans "Tag" %} {{ tag.value }}

    + {% if entries is empty %} +

    {% trans "No link available here!" %}

    + {% else %} + {% block pager %} + {% if nb_results > 1 %} +
    +
    {{ nb_results }} {% trans "results" %}
    + {{ page_links | raw }} +
    + {% endif %} + {% endblock %} + {% for entry in entries %} +
    +

    {{ entry.title|raw }}

    + +

    {{ entry.content|striptags|slice(0, 300) }}...

    +
    + {% endfor %} + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/themes/default/tags.twig b/themes/default/tags.twig new file mode 100644 index 0000000..cff6b1d --- /dev/null +++ b/themes/default/tags.twig @@ -0,0 +1,8 @@ +{% extends "layout.twig" %} +{% block title %}tags{% endblock %} +{% block menu %} +{% include '_menu.twig' %} +{% endblock %} +{% block content %} +{% for tag in tags %}{{ tag.value }} {% if token != '' %}{% endif %} {% endfor %} +{% endblock %} \ No newline at end of file diff --git a/themes/default/view.twig b/themes/default/view.twig index 1e54ae3..64672b6 100644 --- a/themes/default/view.twig +++ b/themes/default/view.twig @@ -20,6 +20,9 @@

    {{ entry.title|raw }}

    +
    {{ content | raw }}