2018-04-02 08:29:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is part of Zwii.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* @license GNU General Public License, version 3
|
2020-09-01 20:48:40 +02:00
|
|
|
* @link http://zwiicms.fr/
|
2018-04-02 08:29:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
class news extends common {
|
|
|
|
|
|
|
|
public static $actions = [
|
2020-10-11 05:03:47 +02:00
|
|
|
'add' => self::GROUP_EDITOR,
|
|
|
|
'config' => self::GROUP_EDITOR,
|
2020-10-11 05:16:20 +02:00
|
|
|
'delete' => self::GROUP_MODERATOR,
|
2020-10-11 05:03:47 +02:00
|
|
|
'edit' => self::GROUP_EDITOR,
|
2018-04-02 08:29:19 +02:00
|
|
|
'index' => self::GROUP_VISITOR
|
|
|
|
];
|
|
|
|
|
|
|
|
public static $news = [];
|
|
|
|
|
|
|
|
public static $comments = [];
|
|
|
|
|
|
|
|
public static $pages;
|
|
|
|
|
|
|
|
public static $states = [
|
|
|
|
false => 'Brouillon',
|
|
|
|
true => 'Publié'
|
|
|
|
];
|
2019-02-22 22:38:06 +01:00
|
|
|
const NEWS_VERSION = '1.2';
|
2018-04-02 08:29:19 +02:00
|
|
|
|
|
|
|
public static $users = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Édition
|
|
|
|
*/
|
|
|
|
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)]));
|
|
|
|
$this->setData(['module', $this->getUrl(0), $newsId, [
|
|
|
|
'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() {
|
|
|
|
// Ids des news par ordre de publication
|
|
|
|
$newsIds = array_keys(helper::arrayCollumn($this->getData(['module', $this->getUrl(0)]), 'publishedOn', 'SORT_DESC'));
|
|
|
|
// Pagination
|
2018-12-09 00:52:05 +01:00
|
|
|
$pagination = helper::pagination($newsIds, $this->getUrl(),$this->getData(['config','itemsperPage']));
|
2018-04-02 08:29:19 +02: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
|
|
|
|
self::$news[] = [
|
|
|
|
$this->getData(['module', $this->getUrl(0), $newsIds[$i], 'title']),
|
2020-10-02 17:07:14 +02:00
|
|
|
strftime('%d %B %Y', $this->getData(['module', $this->getUrl(0), $newsIds[$i], 'publishedOn']))
|
2018-11-24 14:25:51 +01:00
|
|
|
.' à '.
|
2020-10-02 17:07:14 +02:00
|
|
|
strftime('%H:%M', $this->getData(['module', $this->getUrl(0), $newsIds[$i], 'publishedOn'])),
|
2018-04-02 08:29:19 +02:00
|
|
|
self::$states[$this->getData(['module', $this->getUrl(0), $newsIds[$i], 'state'])],
|
|
|
|
template::button('newsConfigEdit' . $newsIds[$i], [
|
2019-01-16 19:25:09 +01:00
|
|
|
'href' => helper::baseUrl() . $this->getUrl(0) . '/edit/' . $newsIds[$i]. '/' . $_SESSION['csrf'],
|
2018-04-02 08:29:19 +02:00
|
|
|
'value' => template::ico('pencil')
|
|
|
|
]),
|
|
|
|
template::button('newsConfigDelete' . $newsIds[$i], [
|
|
|
|
'class' => 'newsConfigDelete buttonRed',
|
2019-01-16 19:25:09 +01:00
|
|
|
'href' => helper::baseUrl() . $this->getUrl(0) . '/delete/' . $newsIds[$i] . '/' . $_SESSION['csrf'],
|
2018-04-02 08:29:19 +02:00
|
|
|
'value' => template::ico('cancel')
|
|
|
|
])
|
|
|
|
];
|
|
|
|
}
|
|
|
|
// Valeurs en sortie
|
|
|
|
$this->addOutput([
|
|
|
|
'title' => 'Configuration du module',
|
|
|
|
'view' => 'config'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Suppression
|
|
|
|
*/
|
2020-06-03 09:07:00 +02:00
|
|
|
public function delete() {
|
2020-10-11 05:16:20 +02:00
|
|
|
// Contrôle d'accès
|
|
|
|
if ( self::$actions[__FUNCTION__] >= $this->getUser('group')) {
|
2018-04-02 08:29:19 +02:00
|
|
|
// Valeurs en sortie
|
|
|
|
$this->addOutput([
|
|
|
|
'access' => false
|
|
|
|
]);
|
2020-10-11 05:16:20 +02:00
|
|
|
} else {
|
|
|
|
// La news n'existe pas
|
|
|
|
if($this->getData(['module', $this->getUrl(0), $this->getUrl(2)]) === null) {
|
|
|
|
// Valeurs en sortie
|
|
|
|
$this->addOutput([
|
|
|
|
'access' => false
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
// Jeton incorrect
|
|
|
|
elseif ($this->getUrl(3) !== $_SESSION['csrf']) {
|
|
|
|
// Valeurs en sortie
|
|
|
|
$this->addOutput([
|
|
|
|
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
|
|
|
|
'notification' => 'Action non autorisée'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
// Suppression
|
|
|
|
else {
|
|
|
|
$this->deleteData(['module', $this->getUrl(0), $this->getUrl(2)]);
|
|
|
|
// Valeurs en sortie
|
|
|
|
$this->addOutput([
|
|
|
|
'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
|
|
|
|
'notification' => 'News supprimée',
|
|
|
|
'state' => true
|
|
|
|
]);
|
|
|
|
}
|
2018-04-02 08:29:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* É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
|
|
|
|
if($this->getData(['module', $this->getUrl(0), $this->getUrl(2)]) === null) {
|
|
|
|
// 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
|
|
|
|
$this->deleteData(['module', $this->getUrl(0), $this->getUrl(2)]);
|
|
|
|
}
|
|
|
|
$this->setData(['module', $this->getUrl(0), $newsId, [
|
|
|
|
'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([
|
|
|
|
'title' => $this->getData(['module', $this->getUrl(0), $this->getUrl(2), 'title']),
|
|
|
|
'vendor' => [
|
|
|
|
'flatpickr',
|
|
|
|
'tinymce'
|
|
|
|
],
|
|
|
|
'view' => 'edit'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accueil
|
|
|
|
*/
|
|
|
|
public function index() {
|
|
|
|
// Ids des news par ordre de publication
|
|
|
|
$newsIdsPublishedOns = helper::arrayCollumn($this->getData(['module', $this->getUrl(0)]), 'publishedOn', 'SORT_DESC');
|
|
|
|
$newsIdsStates = helper::arrayCollumn($this->getData(['module', $this->getUrl(0)]), 'state', 'SORT_DESC');
|
|
|
|
$newsIds = [];
|
|
|
|
foreach($newsIdsPublishedOns as $newsId => $newsPublishedOn) {
|
|
|
|
if($newsPublishedOn <= time() AND $newsIdsStates[$newsId]) {
|
|
|
|
$newsIds[] = $newsId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Pagination
|
2018-12-09 00:52:05 +01:00
|
|
|
$pagination = helper::pagination($newsIds, $this->getUrl(),$this->getData(['config','itemsperPage']));
|
2018-04-02 08:29:19 +02:00
|
|
|
// 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), $newsIds[$i]]);
|
|
|
|
}
|
|
|
|
// Valeurs en sortie
|
|
|
|
$this->addOutput([
|
|
|
|
'showBarEditButton' => true,
|
|
|
|
'showPageContent' => true,
|
|
|
|
'view' => 'index'
|
|
|
|
]);
|
|
|
|
}
|
2019-03-08 11:22:19 +01:00
|
|
|
}
|