ZwiiCMS/module/news/news.php

490 lines
17 KiB
PHP
Raw Normal View History

2018-04-02 08:29:19 +02:00
<?php
/**
2021-02-17 13:51:12 +01:00
* This file is part of Zwii.
2018-04-02 08:29:19 +02:00
* For full copyright and license information, please see the LICENSE
* file that was distributed with this source code.
*
* @author Rémi Jean <remi.jean@outlook.com>
* @copyright Copyright (C) 2008-2018, Rémi Jean
2021-02-17 13:49:58 +01:00
* @author Frédéric Tempez <frederic.tempez@outlook.com>
* @copyright Copyright (C) 2018-2021, Frédéric Tempez
2018-04-02 08:29:19 +02:00
* @license GNU General Public License, version 3
* @link http://zwiicms.fr/
2018-04-02 08:29:19 +02:00
*/
class news extends common {
2021-04-05 08:59:24 +02:00
const VERSION = '3.0';
const REALNAME = 'Actualités';
const DELETE = true;
2021-02-25 07:53:57 +01:00
const UPDATE = '0.0';
2021-05-03 16:32:36 +02:00
const DATADIRECTORY = self::DATA_DIR . 'news/';
2018-04-02 08:29:19 +02:00
public static $actions = [
'add' => self::GROUP_MODERATOR,
'config' => self::GROUP_MODERATOR,
'delete' => self::GROUP_MODERATOR,
'edit' => self::GROUP_MODERATOR,
2020-11-15 18:39:03 +01:00
'index' => self::GROUP_VISITOR,
2020-11-20 19:52:44 +01:00
'rss' => self::GROUP_VISITOR
2018-04-02 08:29:19 +02:00
];
public static $news = [];
public static $comments = [];
public static $pages;
public static $states = [
false => 'Brouillon',
true => 'Publié'
];
public static $users = [];
2021-04-05 08:59:24 +02:00
// Nombre d'objets par page
2021-04-09 13:29:11 +02:00
public static $itemsList = [
2021-04-05 08:59:24 +02:00
4 => '4 articles',
8 => '8 articles',
12 => '12 articles',
16 => '16 articles',
22 => '22 articles'
];
// Nombre de colone par page
2021-04-09 13:29:11 +02:00
public static $columns = [
2021-04-05 08:59:24 +02:00
12 => '1 Colonne',
6 => '2 Colonnes',
4 => '3 Colonnes',
2 => '4 Colonnes'
];
public static $nbrCol = 1;
2021-04-09 13:29:11 +02:00
public static $itemsHeight = [
'200px' => 'Petite',
'300px' => 'Moyenne',
'400px' => 'Grande',
'auto' => 'Article complet'
];
public static $itemsBlur = [
'100%' => 'Aucun',
2021-04-09 14:42:21 +02:00
'90%' => 'Faible',
'75%' => 'Modéré',
'60%' => 'Important',
2021-04-05 08:59:24 +02:00
];
// Signature de l'article
public static $articleSignature = '';
2020-11-15 18:39:03 +01:00
/**
* Flux RSS
*/
public function rss() {
2020-11-16 14:33:59 +01:00
// Inclure les classes
include_once 'module/news/vendor/FeedWriter/Item.php';
include_once 'module/news/vendor/FeedWriter/Feed.php';
include_once 'module/news/vendor/FeedWriter/RSS2.php';
include_once 'module/news/vendor/FeedWriter/InvalidOperationException.php';
date_default_timezone_set('UTC');
$feeds = new \FeedWriter\RSS2();
2020-11-15 18:39:03 +01:00
// En-tête
2020-11-20 13:44:32 +01:00
$feeds->setTitle($this->getData (['page', $this->getUrl(0),'title']));
2020-11-16 14:33:59 +01:00
$feeds->setLink(helper::baseUrl() . $this->getUrl(0));
2020-11-20 13:44:32 +01:00
$feeds->setDescription($this->getData (['page', $this->getUrl(0), 'metaDescription']));
2020-11-16 14:33:59 +01:00
$feeds->setChannelElement('language', 'fr-FR');
2020-11-20 13:44:32 +01:00
$feeds->setDate(date('r',time()));
2020-11-16 14:33:59 +01:00
$feeds->addGenerator();
// Corps des articles
2020-11-17 09:47:45 +01:00
$newsIdsPublishedOns = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'publishedOn', 'SORT_DESC');
// Articles de la première page uniquement
2021-04-05 08:59:24 +02:00
$newsIdsPublishedOns = array_slice($newsIdsPublishedOns, 0, $this->getData(['module', $this->getUrl(0), 'config', 'itemsperPage']) );
2020-11-17 09:47:45 +01:00
$newsIdsStates = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'state', 'SORT_DESC');
2020-11-15 18:39:03 +01:00
foreach($newsIdsPublishedOns as $newsId => $newsPublishedOn) {
if($newsPublishedOn <= time() AND $newsIdsStates[$newsId]) {
2020-11-16 14:33:59 +01:00
$newsArticle = $feeds->createNewItem();
2020-11-20 13:44:32 +01:00
$author = $this->signature($this->getData(['module', $this->getUrl(0), 'posts', $newsId, 'userId']));
2020-11-16 14:33:59 +01:00
$newsArticle->addElementArray([
2020-11-20 13:44:32 +01:00
'title' => $this->getData(['module', $this->getUrl(0),'posts', $newsId, 'title']),
2020-11-21 03:57:27 +01:00
'link' => helper::baseUrl() . $this->getUrl(0) . '/' . $newsId . '#' . $newsId,
2020-11-20 17:32:38 +01:00
'description' => $this->getData(['module', $this->getUrl(0),'posts', $newsId, 'content'])
2020-11-16 14:33:59 +01:00
]);
2020-11-20 17:32:38 +01:00
$newsArticle->setAuthor($author,'no@mail.com');
2020-11-21 03:57:27 +01:00
$newsArticle->setId(helper::baseUrl() .$this->getUrl(0) . '/' . $newsId . '#' . $newsId);
2020-11-20 17:32:38 +01:00
$newsArticle->setDate(date('r', $this->getData(['module', $this->getUrl(0), 'posts', $newsId, 'publishedOn'])));
2020-11-16 14:33:59 +01:00
$feeds->addItem($newsArticle);
2020-11-15 18:39:03 +01:00
}
}
2020-11-16 14:33:59 +01:00
2020-11-15 18:39:03 +01:00
// Valeurs en sortie
$this->addOutput([
'display' => self::DISPLAY_RSS,
2020-11-16 14:33:59 +01:00
'content' => $feeds->generateFeed(),
2020-11-15 18:39:03 +01:00
'view' => 'rss'
]);
}
2018-04-02 08:29:19 +02:00
/**
2021-04-05 08:59:24 +02:00
* Ajout d'un article
2018-04-02 08:29:19 +02:00
*/
public function add() {
// Soumission du formulaire
if($this->isPost()) {
// Crée la news
$newsId = helper::increment($this->getInput('newsAddTitle', helper::FILTER_ID), (array) $this->getData(['module', $this->getUrl(0)]));
2020-11-17 09:47:45 +01:00
$this->setData(['module', $this->getUrl(0),'posts', $newsId, [
2018-04-02 08:29:19 +02:00
'content' => $this->getInput('newsAddContent', null),
'publishedOn' => $this->getInput('newsAddPublishedOn', helper::FILTER_DATETIME, true),
'state' => $this->getInput('newsAddState', helper::FILTER_BOOLEAN),
'title' => $this->getInput('newsAddTitle', helper::FILTER_STRING_SHORT, true),
'userId' => $this->getInput('newsAddUserId', helper::FILTER_ID, true)
]]);
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
'notification' => 'Nouvelle news créée',
'state' => true
]);
}
// Liste des utilisateurs
self::$users = helper::arrayCollumn($this->getData(['user']), 'firstname');
ksort(self::$users);
foreach(self::$users as $userId => &$userFirstname) {
$userFirstname = $userFirstname . ' ' . $this->getData(['user', $userId, 'lastname']);
}
unset($userFirstname);
// Valeurs en sortie
$this->addOutput([
'title' => 'Nouvelle news',
'vendor' => [
'flatpickr',
'tinymce'
],
'view' => 'add'
]);
}
/**
* Configuration
*/
public function config() {
2021-04-07 14:26:12 +02:00
2021-04-05 08:59:24 +02:00
// Mise à jour des données de module
$this->update();
2021-04-09 11:34:16 +02:00
// Initialisation d'un nouveau module
$this->init();
2021-04-09 11:34:16 +02:00
2020-11-17 09:47:45 +01:00
// Soumission du formulaire
if($this->isPost()) {
2021-04-05 08:59:24 +02:00
// Générer la feuille de CSS
2021-04-05 08:59:24 +02:00
$style = '.newsContent {height:' . $this->getInput('newsConfigItemsHeight',helper::FILTER_STRING_SHORT) . ';}';
$style .= '.newsBlur {background: linear-gradient(#333 ' . $this->getInput('newsConfigItemsBlur',helper::FILTER_STRING_SHORT) . ',rgba(255,255,255,0) );';
2021-04-09 13:29:11 +02:00
$style .= ' background-clip: text;-webkit-background-clip: text;-webkit-text-fill-color: transparent;}';
2021-04-05 08:59:24 +02:00
// Dossier de l'instance
2021-05-03 16:32:36 +02:00
if (!is_dir(self::DATADIRECTORY . 'pages/' . $this->getUrl(0))) {
mkdir (self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css', 0777, true);
2021-04-05 08:59:24 +02:00
}
2021-05-03 16:32:36 +02:00
$success = file_put_contents(self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css', $style );
2021-04-09 11:34:16 +02:00
2021-04-05 08:59:24 +02:00
// Fin feuille de style
2021-04-09 11:34:16 +02:00
$this->setData(['module', $this->getUrl(0), 'theme',[
2021-05-03 16:32:36 +02:00
'style' => $success ? self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css' : '',
2021-04-09 13:29:11 +02:00
'itemsHeight' => $this->getInput('newsConfigItemsHeight',helper::FILTER_STRING_SHORT),
'itemsBlur' => $this->getInput('newsConfigItemsBlur',helper::FILTER_STRING_SHORT)
2021-04-09 11:34:16 +02:00
]]);
2020-11-17 09:47:45 +01:00
$this->setData(['module', $this->getUrl(0), 'config',[
'feeds' => $this->getInput('newsConfigShowFeeds',helper::FILTER_BOOLEAN),
2021-04-05 08:59:24 +02:00
'feedsLabel' => $this->getInput('newsConfigFeedslabel',helper::FILTER_STRING_SHORT),
'itemsperPage' => $this->getInput('newsConfigItemsperPage', helper::FILTER_INT,true),
'itemsperCol' => $this->getInput('newsConfigItemsperCol', helper::FILTER_INT,true),
2021-04-09 11:34:16 +02:00
'versionData' => $this->getData(['module', $this->getUrl(0), 'config', 'versionData'])
]]);
2020-11-17 09:47:45 +01:00
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
'notification' => 'Modifications enregistrées',
'state' => true
]);
} else {
// Ids des news par ordre de publication
$newsIds = array_keys(helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'publishedOn', 'SORT_DESC'));
// Pagination
2021-04-05 08:59:24 +02:00
$pagination = helper::pagination($newsIds, $this->getUrl(),$this->getData(['module', $this->getUrl(0), 'config', 'itemsperPage']) );
2020-11-17 09:47:45 +01:00
// Liste des pages
self::$pages = $pagination['pages'];
// News en fonction de la pagination
for($i = $pagination['first']; $i < $pagination['last']; $i++) {
// Met en forme le tableau
$date = mb_detect_encoding(strftime('%d %B %Y', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])), 'UTF-8', true)
? strftime('%d %B %Y', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn']))
: utf8_encode(strftime('%d %B %Y', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])));
$heure = mb_detect_encoding(strftime('%H:%M', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])), 'UTF-8', true)
? strftime('%H:%M', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn']))
: utf8_encode(strftime('%H:%M', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])));
self::$news[] = [
$this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'title']),
$date .' à '. $heure,
self::$states[$this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'state'])],
template::button('newsConfigEdit' . $newsIds[$i], [
'href' => helper::baseUrl() . $this->getUrl(0) . '/edit/' . $newsIds[$i]. '/' . $_SESSION['csrf'],
'value' => template::ico('pencil')
]),
template::button('newsConfigDelete' . $newsIds[$i], [
'class' => 'newsConfigDelete buttonRed',
'href' => helper::baseUrl() . $this->getUrl(0) . '/delete/' . $newsIds[$i] . '/' . $_SESSION['csrf'],
'value' => template::ico('cancel')
])
];
}
// Valeurs en sortie
$this->addOutput([
'title' => 'Configuration du module',
'view' => 'config'
]);
2018-04-02 08:29:19 +02:00
}
}
/**
* Suppression
*/
2020-06-03 09:07:00 +02:00
public function delete() {
2018-04-02 08:29:19 +02:00
// La news n'existe pas
2020-11-17 09:47:45 +01:00
if($this->getData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]) === null) {
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
'access' => false
]);
}
2019-01-08 17:55:18 +01:00
// Jeton incorrect
2019-01-16 19:25:09 +01:00
elseif ($this->getUrl(3) !== $_SESSION['csrf']) {
2019-01-08 17:55:18 +01:00
// Valeurs en sortie
$this->addOutput([
2019-01-16 19:25:09 +01:00
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
'notification' => 'Action non autorisée'
2019-01-08 17:55:18 +01:00
]);
2020-06-03 09:07:00 +02:00
}
2018-04-02 08:29:19 +02:00
// Suppression
else {
2020-11-17 09:47:45 +01:00
$this->deleteData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]);
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
'notification' => 'News supprimée',
'state' => true
]);
}
}
/**
* Édition
*/
public function edit() {
2019-01-16 19:25:09 +01:00
// Jeton incorrect
if ($this->getUrl(3) !== $_SESSION['csrf']) {
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
'notification' => 'Action non autorisée'
]);
2020-06-03 09:07:00 +02:00
}
2018-04-02 08:29:19 +02:00
// La news n'existe pas
2020-11-17 09:47:45 +01:00
if($this->getData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]) === null) {
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
'access' => false
]);
}
// La news existe
else {
// Soumission du formulaire
if($this->isPost()) {
// Si l'id a changée
$newsId = $this->getInput('newsEditTitle', helper::FILTER_ID, true);
if($newsId !== $this->getUrl(2)) {
// Incrémente le nouvel id de la news
$newsId = helper::increment($newsId, $this->getData(['module', $this->getUrl(0)]));
// Supprime l'ancien news
2020-11-17 09:47:45 +01:00
$this->deleteData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]);
2018-04-02 08:29:19 +02:00
}
2020-11-17 09:47:45 +01:00
$this->setData(['module', $this->getUrl(0),'posts', $newsId, [
2018-04-02 08:29:19 +02:00
'content' => $this->getInput('newsEditContent', null),
'publishedOn' => $this->getInput('newsEditPublishedOn', helper::FILTER_DATETIME, true),
'state' => $this->getInput('newsEditState', helper::FILTER_BOOLEAN),
'title' => $this->getInput('newsEditTitle', helper::FILTER_STRING_SHORT, true),
'userId' => $this->getInput('newsEditUserId', helper::FILTER_ID, true)
]]);
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
'notification' => 'Modifications enregistrées',
'state' => true
]);
}
// Liste des utilisateurs
self::$users = helper::arrayCollumn($this->getData(['user']), 'firstname');
ksort(self::$users);
foreach(self::$users as $userId => &$userFirstname) {
$userFirstname = $userFirstname . ' ' . $this->getData(['user', $userId, 'lastname']);
}
unset($userFirstname);
// Valeurs en sortie
$this->addOutput([
2020-11-17 09:47:45 +01:00
'title' => $this->getData(['module', $this->getUrl(0),'posts', $this->getUrl(2), 'title']),
2018-04-02 08:29:19 +02:00
'vendor' => [
'flatpickr',
'tinymce'
],
'view' => 'edit'
]);
}
}
/**
* Accueil
*/
public function index() {
2021-04-07 14:26:12 +02:00
2021-04-05 08:59:24 +02:00
// Mise à jour des données de module
$this->update();
2021-04-09 11:34:16 +02:00
// Initialisation d'un nouveau module
$this->init();
2021-04-09 11:34:16 +02:00
2021-04-05 08:59:24 +02:00
// Affichage d'un article
if(
$this->getUrl(1)
// Protection pour la pagination, un ID ne peut pas être un entier, une page oui
AND intval($this->getUrl(1)) === 0
) {
// L'article n'existe pas
if($this->getData(['module', $this->getUrl(0), 'posts', $this->getUrl(1)]) === null) {
// Valeurs en sortie
$this->addOutput([
'access' => false
]);
2018-04-02 08:29:19 +02:00
}
2021-04-05 08:59:24 +02:00
// L'article existe
else {
self::$articleSignature = $this->signature($this->getData(['module', $this->getUrl(0), 'posts', $this->getUrl(1), 'userId']));
// Valeurs en sortie
$this->addOutput([
'showBarEditButton' => true,
'title' => $this->getData(['module', $this->getUrl(0), 'posts', $this->getUrl(1), 'title']),
'view' => 'article'
]);
}
} else {
// Affichage index
// Ids des news par ordre de publication
$newsIdsPublishedOns = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'publishedOn', 'SORT_DESC');
$newsIdsStates = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'state', 'SORT_DESC');
$newsIds = [];
foreach($newsIdsPublishedOns as $newsId => $newsPublishedOn) {
if($newsPublishedOn <= time() AND $newsIdsStates[$newsId]) {
$newsIds[] = $newsId;
}
}
// Pagination
//$pagination = helper::pagination($newsIds, $this->getUrl(),$this->getData(['config','itemsperPage']));
$pagination = helper::pagination($newsIds, $this->getUrl(),$this->getData(['module', $this->getUrl(0),'config', 'itemsperPage']));
// Nombre de colonnes
self::$nbrCol = $this->getData(['module', $this->getUrl(0),'config', 'itemsperCol']);
// Liste des pages
self::$pages = $pagination['pages'];
// News en fonction de la pagination
for($i = $pagination['first']; $i < $pagination['last']; $i++) {
self::$news[$newsIds[$i]] = $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i]]);
self::$news[$newsIds[$i]]['userId'] = $this->signature($this->getData(['module', $this->getUrl(0), 'posts', $newsIds[$i], 'userId']));
}
// Valeurs en sortie
$this->addOutput([
'showBarEditButton' => true,
'showPageContent' => true,
'view' => 'index',
2021-04-09 13:29:11 +02:00
'style' => $this->getData(['module', $this->getUrl(0),'theme', 'style'])
2021-04-05 08:59:24 +02:00
]);
2018-04-02 08:29:19 +02:00
}
}
2020-11-20 13:44:32 +01:00
/**
* Retourne la signature d'un utilisateur
*/
private function signature($userId) {
switch ($this->getData(['user', $userId, 'signature'])){
case 1:
return $userId;
break;
case 2:
return $this->getData(['user', $userId, 'pseudo']);
break;
case 3:
return $this->getData(['user', $userId, 'firstname']) . ' ' . $this->getData(['user', $userId, 'lastname']);
break;
case 4:
return $this->getData(['user', $userId, 'lastname']) . ' ' . $this->getData(['user', $userId, 'firstname']);
break;
default:
return $this->getData(['user', $userId, 'firstname']);
}
}
2021-04-07 14:26:12 +02:00
/**
* Mise à jour du module
* Appelée par les fonctions index et config
*/
private function update() {
2021-04-08 11:33:35 +02:00
2021-04-07 14:26:12 +02:00
// Version 3.0
2021-04-09 11:34:16 +02:00
if ($this->getData(['module', $this->getUrl(0), 'config']) === NULL ) {
// Données config et theme absentes du précédent module
$this->init();
2021-04-07 14:26:12 +02:00
}
}
/**
* Initialisation du thème d'un nouveau module
*/
private function init() {
2021-05-03 16:32:36 +02:00
$fileCSS = self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css';
// Données du module
if ($this->getData(['module', $this->getUrl(0) ]) === null) {
require_once('module/news/ressource/defaultdata.php');
$this->setData(['module', $this->getUrl(0), 'config', init::$defaultData]);
// Données de thème
$this->setData(['module', $this->getUrl(0), 'theme', init::$defaultTheme]);
2021-05-03 16:32:36 +02:00
$this->setData(['module', $this->getUrl(0), 'theme', 'style', self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css' ]);
2021-04-08 11:33:35 +02:00
}
2021-04-07 14:26:12 +02:00
// Dossier de l'instance
2021-05-03 16:32:36 +02:00
if (!is_dir(self::DATADIRECTORY . 'pages/' . $this->getUrl(0) )) {
mkdir (self::DATADIRECTORY . 'pages/' . $this->getUrl(0) , 0777, true);
}
2021-04-07 14:26:12 +02:00
// Check la présence de la feuille de style
2021-05-03 16:32:36 +02:00
if ( !file_exists(self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css')) {
// Générer la feuille de CSS
$style = '.newsContent {height: ' . $this->getData([ 'module', $this->getUrl(0), 'theme', 'itemsHeight' ]) .';}';
$style .= '.newsBlur {background: linear-gradient(#333 ' . $this->getData([ 'module', $this->getUrl(0), 'theme', 'itemsBlur' ]) . ',#FFF );';
$style .= ' background-clip: text;-webkit-background-clip: text;-webkit-text-fill-color: transparent;}';
// Sauver la feuille de style
2021-05-03 16:32:36 +02:00
file_put_contents(self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css' , $style );
// Stocker le nom de la feuille de style
2021-05-03 16:32:36 +02:00
$this->setData(['module', $this->getUrl(0), 'theme', 'style', self::DATADIRECTORY . 'pages/' . $this->getUrl(0) . '/theme.css']);
}
2021-04-07 14:26:12 +02:00
}
2021-02-25 07:53:57 +01:00
}