Commit 72e337a3 authored by Corentin Bettiol's avatar Corentin Bettiol 💻

add tag system

parent 2306c5b1
/*
====
CSS
*/
*{
color: #1f1f2e;
}
......@@ -65,6 +59,10 @@ h2{
font-size: 1.5em;
font-weight: normal;
display: inline-block;
max-width: 280px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
h3 a{
......@@ -86,6 +84,7 @@ h3 a:hover{
flex-direction: row;
list-style-type: none;
justify-content: space-around;
flex-wrap: wrap;
}
#listMenu a{
......@@ -341,6 +340,16 @@ form .small{
text-align: right;
}
.tag{
padding: 2px 7px;
background-color: #ddd;
margin: 2px;
}
.tag a{
text-decoration: none;
}
/*
====
Media Queries
......@@ -349,6 +358,7 @@ Media Queries
@media all and (max-width: 1350px){
#header{
justify-content: space-around;
font-size: 0.7em;
}
}
......@@ -359,6 +369,9 @@ Media Queries
#page{
font-size: 1.2em;
}
#header{
font-size: 1em;
}
#header, #title{
padding-top: 5px;
flex-direction: column;
......@@ -388,6 +401,11 @@ Media Queries
flex-direction: column;
justify-content: center;
}
#listMenu a{
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
#listMenu li, #listMenu h2, #listMenu a{
display: block;
width: 90%;
......
......@@ -29,6 +29,7 @@
RewriteEngine On
RewriteRule ^/(about|changelog|rss|projects|contact|patate|eplucher)$ /index.php?action=$1
RewriteRule ^/rss/(blog|projets)$ /rss/$1.xml
RewriteRule ^/tag/([-a-z0-9]+)$ /index.php?action=tags&title=$1
RewriteRule ^/page/([0-9]+)$ /index.php?action=blog&page=$1
RewriteRule ^/article/([-a-z0-9]+)$ /index.php?action=blog&title=$1
RewriteRule ^/project/([-a-z0-9]+)$ /index.php?action=project&title=$1
......
......@@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Client : localhost:3306
-- Généré le : Mar 19 Mars 2019 à 21:55
-- Généré le : Mer 20 Mars 2019 à 13:12
-- Version du serveur : 5.7.25-0ubuntu0.18.04.2
-- Version de PHP : 7.2.15-0ubuntu0.18.04.1
......@@ -19,6 +19,7 @@ SET time_zone = "+00:00";
--
-- Base de données : `l3m`
--
DROP DATABASE IF EXISTS `l3m`;
CREATE DATABASE IF NOT EXISTS `l3m` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `l3m`;
......@@ -38,15 +39,6 @@ CREATE TABLE `l3m_blog` (
`shortContent` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Ce billet de blog ne dispose d''aucune description.'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Vider la table avant d'insérer `l3m_blog`
--
TRUNCATE TABLE `l3m_blog`;
--
-- Contenu de la table `l3m_blog`
--
-- --------------------------------------------------------
--
......@@ -63,15 +55,30 @@ CREATE TABLE `l3m_projects` (
`shortContent` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Pas de description.'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Vider la table avant d'insérer `l3m_projects`
-- Structure de la table `l3m_tag`
--
TRUNCATE TABLE `l3m_projects`;
DROP TABLE IF EXISTS `l3m_tag`;
CREATE TABLE `l3m_tag` (
`id` int(11) NOT NULL,
`tag` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Contenu de la table `l3m_projects`
-- Structure de la table `l3m_tag_post`
--
DROP TABLE IF EXISTS `l3m_tag_post`;
CREATE TABLE `l3m_tag_post` (
`id` int(11) NOT NULL,
`tag` int(11) NOT NULL,
`post` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Index pour les tables exportées
......@@ -89,6 +96,18 @@ ALTER TABLE `l3m_blog`
ALTER TABLE `l3m_projects`
ADD PRIMARY KEY (`id`);
--
-- Index pour la table `l3m_tag`
--
ALTER TABLE `l3m_tag`
ADD PRIMARY KEY (`id`);
--
-- Index pour la table `l3m_tag_post`
--
ALTER TABLE `l3m_tag_post`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT pour les tables exportées
--
......@@ -97,12 +116,22 @@ ALTER TABLE `l3m_projects`
-- AUTO_INCREMENT pour la table `l3m_blog`
--
ALTER TABLE `l3m_blog`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT pour la table `l3m_projects`
--
ALTER TABLE `l3m_projects`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT pour la table `l3m_tag`
--
ALTER TABLE `l3m_tag`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT pour la table `l3m_tag_post`
--
ALTER TABLE `l3m_tag_post`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
......@@ -7,8 +7,10 @@ require_once('site/model/AdminManager.php');
require_once('site/model/RssManager.php');
// tente de connecter l'administrateur
function loginAdminPage($pageName){
if(password_verify($_POST['pass'], pass) && $_POST['user'] == user){
function loginAdminPage($pageName)
{
if(password_verify($_POST['pass'], pass) && $_POST['user'] == user)
{
$_SESSION['user'] = user;
header('Location:/patate');
}
......@@ -17,15 +19,19 @@ function loginAdminPage($pageName){
}
// afficher le panneau d'administration & gère l'administration du site
function writeAdminPage($pageName){
function writeAdminPage($pageName)
{
$admin = new AdminManager();
if($admin->verifyAdminConnect()){
if(isset($_GET['type'])){
switch ($_GET['type']){
if($admin->verifyAdminConnect())
{
if(isset($_GET['type']))
{
switch ($_GET['type'])
{
case 'blogedit':
if(!isset($_POST['title'])){
if(!isset($_POST['title']))
{
$title = htmlspecialchars($_GET['title']);
if($blogpost = $admin->getBlogPost($title))
require('site/view/private/editblogView.php');
......@@ -39,7 +45,8 @@ function writeAdminPage($pageName){
break;
case 'blogsubmit':
if(isset($_POST['title'])){
if(isset($_POST['title']))
{
$admin->sendBlogPost();
header('Location:/patate');
}
......@@ -48,14 +55,16 @@ function writeAdminPage($pageName){
break;
case 'blogdelete':
if(isset($_GET['title'])){
if(isset($_GET['title']))
{
$admin->deleteBlogPost($_GET['title']);
header('Location:/patate');
}
break;
case 'projectedit':
if(!isset($_POST['name'])){
if(!isset($_POST['name']))
{
$title = urldecode($_GET['title']);
$project = $admin->getProject($title);
require('site/view/private/editProjectView.php');
......@@ -67,7 +76,8 @@ function writeAdminPage($pageName){
break;
case 'projectsubmit':
if(isset($_POST['name'])){
if(isset($_POST['name']))
{
$admin->sendProject();
header('Location:/patate');
}
......@@ -76,7 +86,8 @@ function writeAdminPage($pageName){
break;
case 'projectdelete':
if(isset($_GET['title'])){
if(isset($_GET['title']))
{
$admin->deleteProject($_GET['title']);
header('Location:/patate');
}
......@@ -99,7 +110,8 @@ function writeAdminPage($pageName){
}
// déconnexion de la partie administration
function decoAdminPage(){
function decoAdminPage()
{
$_SESSION = array();
header('Location:/');
}
\ No newline at end of file
......@@ -12,13 +12,19 @@ require_once('site/model/BlogManager.php');
// classe pour la gestion des projets
require_once('site/model/ProjectsManager.php');
// classe pour la gestion des tags
require_once('site/model/TagManager.php');
// récupérer le nom de la page en fonction de l'url
// premier texte = affiché dans l'onglet (title), second texte = voir le switch dans site/layout/layout.php :
// c'est LA valeur utilisée pour savoir quoi faire depuis le layout
function getPageName(){
function getPageName()
{
// si on a une url personnalisée
if(isset($_GET['action'])){
switch ($_GET['action']) {
if(isset($_GET['action']))
{
switch ($_GET['action'])
{
case 'blog' : // le blog
if(isset($_GET['page']))
return ['Blog – page '. $_GET['page'], 'blog'];
......@@ -52,19 +58,22 @@ function getPageName(){
case 'patate': // administration
return ['Administration', 'administration'];
break;
case 'tags': // voir les posts de blog avec le tag
return ['Tag', 'tag'];
break;
default: // si l'argument n'est pas compris et qu'il passe les rewriterules d'apache on affiche le blog (ne devrait jamais se produire)
return ['Blog', 'blog'];
break;
}
}
// si on est sur l'accueil du site
else{
else
return ['Blog', 'blog'];
}
}
// affiche le menu du site (avec le titre de la page)
function writeMenu($pageName){
function writeMenu($pageName)
{
?>
<input id="toggle" type="checkbox" />
<ul id='listMenu'>
......@@ -77,6 +86,9 @@ function writeMenu($pageName){
<?php }
if($pageName == 'changelog'){ ?>
<li><h2><a href="/changelog" class="selected">Changelog</a></h2></li>
<?php }
if($pageName == 'tag'){ ?>
<li><h2><a href="/tag/<?php echo $_GET['title']; ?>" class="selected">Tag <?php echo $_GET['title']; ?></a></h2></li>
<?php } ?>
<li><h2><a href="/" title="Blog" <?php if($pageName == 'blog'){ ?>class="selected"<?php } ?>>Blog</a></h2></li>
<li><h2><a href="/about" title="À propos" <?php if($pageName == 'about'){ ?>class="selected"<?php } ?>>À propos</a></h2></li>
......@@ -87,29 +99,25 @@ function writeMenu($pageName){
<?php }
// affiche la page du blog (accueil par défaut)
function writeBlogPage($pageName){
// si on n'accède pas au fichier mais bien au site (var définie)
if($pageName[1] == 'blog'){
function writeBlogPage($pageName)
{
$blog = new BlogManager(NULL, NULL, NULL, NULL, NULL, NULL); // on a un blogmanager
// si on affiche un post en particulier
if(isset($_GET['title'])){
if(isset($_GET['title']))
{
$blogpost = $blog->get($_GET['title']);
if($blogpost != false){
if($blogpost != false)
{
// articles précédent/suivant
//$paging = $blog->pagingSingle($_GET['title']);
//require('site/view/publie/blogPostPagingView.php')
require('site/view/public/blogPostView.php');
}
else
header('Location:/404');
}
// si on affiche la liste des posts
else{
else
{
$page = 1;
if(isset($_GET['page']))
// normalement via le rewriterule on envoie forcément un int, mais un cast ça coûte pas grand chose donc bon
......@@ -123,18 +131,20 @@ function writeBlogPage($pageName){
$blogposts = $blog->getPage($page);
require('site/view/public/blogView.php');
}
}
}
// affiche la liste des projets
function writeProjectPage($pageName){
function writeProjectPage($pageName)
{
$project = new ProjectManager(NULL, NULL, NULL, NULL, NULL); // on a un projectmanager
// si on affiche un projet en particulier
if(isset($_GET['title'])){
if(isset($_GET['title']))
{
$project = $project->get($_GET['title']);
if($project != false){
if($project != false)
{
require('site/view/public/projectView.php');
}
else
......@@ -146,3 +156,17 @@ function writeProjectPage($pageName){
require('site/view/public/projectsView.php');
}
}
// affiche la liste des posts de blog avec le tag correspondant
function writeTagPage($pageName){
$blog = new BlogManager(NULL, NULL, NULL, NULL, NULL, NULL); // on a un blogmanager
// liste des articles avec le tag
if(isset($_GET['title']))
{
$blogposts = $blog->getTag($_GET['title']);
require('site/view/public/blogView.php');
}
else
header("Location:/");
}
\ No newline at end of file
......@@ -80,6 +80,9 @@
writeAdminPage($pageName);
}
break;
case 'tag':
writeTagPage($pageName);
break;
default: // page non connue (n'est pas senser arriver)
header('Location:/404');
break;
......
......@@ -3,7 +3,7 @@ class AdminManager extends Base
{
public function getBlogPosts()
{
$blogpost = new BlogManager(null, null, null, null, null, null);
$blogpost = new BlogManager(null, null, null, null, null, null, null);
return $blogpost->getAll();
}
......@@ -15,7 +15,7 @@ class AdminManager extends Base
public function getBlogPost($link)
{
$blogpost = new BlogManager(null, null, null, null, null, null);
$blogpost = new BlogManager(null, null, null, null, null, null, null);
return $blogpost->get($link);
}
......@@ -26,19 +26,22 @@ class AdminManager extends Base
}
public function verifyAdminConnect(){
public function verifyAdminConnect()
{
return isset($_SESSION['user']) && $_SESSION['user'] == user;
}
public function updateBlogPost(){
$blog = new BlogManager($_POST['time'], $_POST['title'], $_POST['content'], $_POST['shortContent'], $_POST['id']);
public function updateBlogPost()
{
$blog = new BlogManager($_POST['time'], $_POST['title'], $_POST['content'], $_POST['shortContent'], $_POST['id'], $_POST['tags']);
$blog->update();
$rss = new RssManager();
$rss->updateRssBlog();
}
public function updateProject(){
public function updateProject()
{
$project = new ProjectManager($_POST['name'], $_POST['link'], $_POST['content'], $_POST['shortContent'], $_POST['id']);
$project->update();
......@@ -46,8 +49,9 @@ class AdminManager extends Base
$rss->updateRssProjects();
}
public function sendBlogPost(){
$blog = new BlogManager($_POST['time'], $_POST['title'], $_POST['content'], $_POST['shortContent'], null);
public function sendBlogPost()
{
$blog = new BlogManager($_POST['time'], $_POST['title'], $_POST['content'], $_POST['shortContent'], null, $_POST['tags']);
$blog->send();
$rss = new RssManager();
......@@ -55,7 +59,8 @@ class AdminManager extends Base
}
public function sendProject(){
public function sendProject()
{
$project = new ProjectManager($_POST['name'], $_POST['link'], $_POST['content'], $_POST['shortContent'], null);
$project->send();
......@@ -63,15 +68,17 @@ class AdminManager extends Base
$rss->updateRssProjects();
}
public function deleteBlogPost($link){
$blog = new BlogManager(null, null, null, null, null, null);
public function deleteBlogPost($link)
{
$blog = new BlogManager(null, null, null, null, null, null, null);
$blog->delete($link);
$rss = new RssManager();
$rss->updateRssBlog();
}
public function deleteProject($link){
public function deleteProject($link)
{
$project = new ProjectManager(null, null, null, null, null);
$project->delete($link);
......
......@@ -7,7 +7,8 @@ class Base
protected static $db;
// we try to connect to db at each instantiation
function __construct(){
function __construct()
{
$this->dbConnect();
}
......@@ -20,7 +21,8 @@ class Base
// thanks https://www.matthecat.com/supprimer-les-accents-dune-chaine-en-php/ & http://php.net/manual/fr/transliterator.transliterate.php#110598
protected function slugify($text, $table){
protected function slugify($text, $table)
{
$slug = preg_replace('/&amp;/', 'et', $text);
$slug = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $slug);
$slug = preg_replace('/[-\s]+/', '-', $slug);
......@@ -37,7 +39,8 @@ class Base
$req = self::$db->prepare('SELECT slug FROM l3m_'. $table .' WHERE slug = ?');
$req->execute([$slug]);
$slugExist = $req->fetch();
if($slugExist){
if($slugExist)
{
$i++;
$slug = $old . "-" . $i;
}
......
......@@ -6,14 +6,17 @@ class BlogManager extends Base
private $content;
private $shortContent;
private $id;
private $tags;
function __construct($time, $title, $content, $shortContent, $id){
function __construct($time, $title, $content, $shortContent, $id, $tags)
{
parent::__construct(); // like antibiotics
$this->time = htmlspecialchars($time);
$this->title = htmlspecialchars($title);
$this->content = htmlspecialchars($content);
$this->shortContent = htmlspecialchars($shortContent);
$this->id = htmlspecialchars($id);
$this->tags = htmlspecialchars($tags);
}
public function getPage($page)
......@@ -42,6 +45,9 @@ class BlogManager extends Base
$post['datetime'] = new DateTime($post['time']);
$tag = new TagManager();
$post['tags'] = $tag->getTags($post['id']);
return $post;
}
......@@ -51,6 +57,19 @@ class BlogManager extends Base
return $req;
}
public function getTag($tag)
{
$req = self::$db->prepare('SELECT l3m_blog.* FROM l3m_blog, l3m_tag_post, l3m_tag WHERE l3m_blog.id = l3m_tag_post.post AND l3m_tag.id = l3m_tag_post.tag AND l3m_tag.tag = ? ORDER BY id DESC');
$req->execute([$tag]);
$posts = $req->fetchAll();
if(!$posts)
return false;
return $posts;
}
public function getBlogRss()
{
$req = self::$db->query('SELECT time, title, shortContent, slug FROM l3m_blog ORDER BY id DESC LIMIT 10');
......@@ -95,7 +114,8 @@ class BlogManager extends Base
}
// todo vérifier qu'on poste pas un truc vide
public function update(){
public function update()
{
$req = self::$db->prepare('UPDATE l3m_blog SET time = :time, title = :title, content = :content, shortContent = :shortContent WHERE id = :id');
$req->execute([
'time' => $this->time,
......@@ -104,15 +124,25 @@ class BlogManager extends Base
'shortContent' => $this->shortContent,
'id' => $this->id
]);
$tag = new TagManager();
// on enlève les vieux tags
$tag->deleteTag($this->id);
// on ajout les nouveaux
$tag->addTag($this->tags, $this->id);
}
// envoi d'un post de blog
public function send(){
if(strlen($this->time) < 1){
public function send()
{
if(strlen($this->time) < 1)
{
$this->time = new datetime();
$this->time = $this->time->format('Y-m-d H:i:s');
}
if(strlen($this->title) < 1){
if(strlen($this->title) < 1)
{
header('Location:/patate');
exit();
}
......@@ -129,13 +159,26 @@ class BlogManager extends Base
'content' => $this->content,
'shortContent' => $this->shortContent
]);
$tag = new TagManager();
// on ajoute les nouveaux tags
$tag->addTag($this->tags, self::$db->lastInsertId());
}
// suppression d'un post de blog
public function delete($link){
public function delete($link)
{
$link = htmlspecialchars($link);
$req = self::$db->prepare('SELECT id FROM l3m_blog WHERE slug = ?');
$req->execute([$link]);
$id = $req->fetch();
$req = self::$db->prepare('DELETE FROM l3m_blog WHERE slug = ?');
$req->execute([$link]);
$tag = new TagManager();
// on n'oublie pas d'enlever les tags
$tag->deleteTag($id['id']);
}
}
\ No newline at end of file
......@@ -7,7 +7,8 @@ class ProjectManager extends Base
private $shortContent;
private $id;
function __construct($name, $link, $content, $shortContent, $id){
function __construct($name, $link, $content, $shortContent, $id)
{
parent::__construct(); // like antibiotics
$this->name = htmlspecialchars($name);
$this->link = htmlspecialchars($link);
......@@ -38,7 +39,8 @@ class ProjectManager extends Base
}
// todo vérifier qu'on poste pas un truc vide
public function update(){
public function update()
{
$req = self::$db->prepare('UPDATE l3m_projects SET name = :name, link = :link, content = :content, shortContent = :shortContent WHERE id = :id');
$req->execute([
'name' => $this->name,
......@@ -55,12 +57,15 @@ class ProjectManager extends Base
return $req;
}
public function send(){
if(strlen($this->name) < 1){
public function send()
{
if(strlen($this->name) < 1)
{
header('Location:/patate');
exit();
}
if(strlen($this->shortContent) < 1){
if(strlen($this->shortContent) < 1)
{
$this->shortContent = 'Pas de description.';
}
$this->slug = $this->slugify($this->name, "projects");
......@@ -75,7 +80,8 @@ class ProjectManager extends Base
]);
}
public function delete($link){
public function delete($link)
{
$link = htmlspecialchars($link);
$req = self::$db->prepare('DELETE FROM l3m_projects WHERE slug = ?');
......
<?php
class RssManager extends Base
{
function updateRssBlog(){
function updateRssBlog()
{
$contentBlog =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\">
......@@ -12,7 +13,7 @@ class RssManager extends Base
<link>https://l3m.in</link>";
$blog = new BlogManager(null, null, null, null, null);
$blog = new BlogManager(null, null, null, null, null, null);
$posts = $blog->getBlogRss();
foreach ($posts as $post) {
......@@ -31,7 +32,8 @@ class RssManager extends Base
file_put_contents(RSSBLOG, $contentBlog);
}
function updateRssProjects(){
function updateRssProjects()
{
$contentProject =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\">
......
<?php
class TagManager extends Base
{
// envoi des tags d'un post de blog
public function addTag($tags, $post)
{
// donne tags = [[tag1], [tag2], ...]
$tags = str_replace(' ', '', $tags);
$tags = explode(',', $tags);