ZwiiCMS, le gestionnaire de site Web sans base de données. #zwii #cms #nosql #json https://www.zwiicms.fr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
12 KiB

3 years ago
5 months ago
3 years ago
5 months ago
3 years ago
3 years ago
3 years ago
8 months ago
8 months ago
3 years ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. /**
  3. * This file is part of Zwii.
  4. * For full copyright and license information, please see the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @author Rémi Jean <remi.jean@outlook.com>
  8. * @copyright Copyright (C) 2008-2018, Rémi Jean
  9. * @author Frédéric Tempez <frederic.tempez@outlook.com>
  10. * @copyright Copyright (C) 2018-2021, Frédéric Tempez
  11. * @license GNU General Public License, version 3
  12. * @link http://zwiicms.fr/
  13. */
  14. class news extends common {
  15. const VERSION = '2.1';
  16. const REALNAME = 'Actualités';
  17. const DELETE = true;
  18. const UPDATE = '0.0';
  19. const DATADIRECTORY = []; // Contenu localisé inclus par défaut (page.json et module.json)
  20. public static $actions = [
  21. 'add' => self::GROUP_MODERATOR,
  22. 'config' => self::GROUP_MODERATOR,
  23. 'delete' => self::GROUP_MODERATOR,
  24. 'edit' => self::GROUP_MODERATOR,
  25. 'index' => self::GROUP_VISITOR,
  26. 'rss' => self::GROUP_VISITOR
  27. ];
  28. public static $news = [];
  29. public static $comments = [];
  30. public static $pages;
  31. public static $states = [
  32. false => 'Brouillon',
  33. true => 'Publié'
  34. ];
  35. public static $users = [];
  36. /**
  37. * Flux RSS
  38. */
  39. public function rss() {
  40. // Inclure les classes
  41. include_once 'module/news/vendor/FeedWriter/Item.php';
  42. include_once 'module/news/vendor/FeedWriter/Feed.php';
  43. include_once 'module/news/vendor/FeedWriter/RSS2.php';
  44. include_once 'module/news/vendor/FeedWriter/InvalidOperationException.php';
  45. date_default_timezone_set('UTC');
  46. $feeds = new \FeedWriter\RSS2();
  47. // En-tête
  48. $feeds->setTitle($this->getData (['page', $this->getUrl(0),'title']));
  49. $feeds->setLink(helper::baseUrl() . $this->getUrl(0));
  50. $feeds->setDescription($this->getData (['page', $this->getUrl(0), 'metaDescription']));
  51. $feeds->setChannelElement('language', 'fr-FR');
  52. $feeds->setDate(date('r',time()));
  53. $feeds->addGenerator();
  54. // Corps des articles
  55. $newsIdsPublishedOns = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'publishedOn', 'SORT_DESC');
  56. // Articles de la première page uniquement
  57. $newsIdsPublishedOns = array_slice($newsIdsPublishedOns, 0, $this->getData(['config', 'itemsperPage']) );
  58. $newsIdsStates = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'state', 'SORT_DESC');
  59. foreach($newsIdsPublishedOns as $newsId => $newsPublishedOn) {
  60. if($newsPublishedOn <= time() AND $newsIdsStates[$newsId]) {
  61. $newsArticle = $feeds->createNewItem();
  62. $author = $this->signature($this->getData(['module', $this->getUrl(0), 'posts', $newsId, 'userId']));
  63. $newsArticle->addElementArray([
  64. 'title' => $this->getData(['module', $this->getUrl(0),'posts', $newsId, 'title']),
  65. 'link' => helper::baseUrl() . $this->getUrl(0) . '/' . $newsId . '#' . $newsId,
  66. 'description' => $this->getData(['module', $this->getUrl(0),'posts', $newsId, 'content'])
  67. ]);
  68. $newsArticle->setAuthor($author,'no@mail.com');
  69. $newsArticle->setId(helper::baseUrl() .$this->getUrl(0) . '/' . $newsId . '#' . $newsId);
  70. $newsArticle->setDate(date('r', $this->getData(['module', $this->getUrl(0), 'posts', $newsId, 'publishedOn'])));
  71. $feeds->addItem($newsArticle);
  72. }
  73. }
  74. // Valeurs en sortie
  75. $this->addOutput([
  76. 'display' => self::DISPLAY_RSS,
  77. 'content' => $feeds->generateFeed(),
  78. 'view' => 'rss'
  79. ]);
  80. }
  81. /**
  82. * Édition
  83. */
  84. public function add() {
  85. // Soumission du formulaire
  86. if($this->isPost()) {
  87. // Crée la news
  88. $newsId = helper::increment($this->getInput('newsAddTitle', helper::FILTER_ID), (array) $this->getData(['module', $this->getUrl(0)]));
  89. $this->setData(['module', $this->getUrl(0),'posts', $newsId, [
  90. 'content' => $this->getInput('newsAddContent', null),
  91. 'publishedOn' => $this->getInput('newsAddPublishedOn', helper::FILTER_DATETIME, true),
  92. 'state' => $this->getInput('newsAddState', helper::FILTER_BOOLEAN),
  93. 'title' => $this->getInput('newsAddTitle', helper::FILTER_STRING_SHORT, true),
  94. 'userId' => $this->getInput('newsAddUserId', helper::FILTER_ID, true)
  95. ]]);
  96. // Valeurs en sortie
  97. $this->addOutput([
  98. 'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
  99. 'notification' => 'Nouvelle news créée',
  100. 'state' => true
  101. ]);
  102. }
  103. // Liste des utilisateurs
  104. self::$users = helper::arrayCollumn($this->getData(['user']), 'firstname');
  105. ksort(self::$users);
  106. foreach(self::$users as $userId => &$userFirstname) {
  107. $userFirstname = $userFirstname . ' ' . $this->getData(['user', $userId, 'lastname']);
  108. }
  109. unset($userFirstname);
  110. // Valeurs en sortie
  111. $this->addOutput([
  112. 'title' => 'Nouvelle news',
  113. 'vendor' => [
  114. 'flatpickr',
  115. 'tinymce'
  116. ],
  117. 'view' => 'add'
  118. ]);
  119. }
  120. /**
  121. * Configuration
  122. */
  123. public function config() {
  124. // Soumission du formulaire
  125. if($this->isPost()) {
  126. $this->setData(['module', $this->getUrl(0), 'config',[
  127. 'feeds' => $this->getInput('newsConfigShowFeeds',helper::FILTER_BOOLEAN),
  128. 'feedsLabel' => $this->getInput('newsConfigFeedslabel',helper::FILTER_STRING_SHORT)
  129. ]]);
  130. // Valeurs en sortie
  131. $this->addOutput([
  132. 'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
  133. 'notification' => 'Modifications enregistrées',
  134. 'state' => true
  135. ]);
  136. } else {
  137. // Ids des news par ordre de publication
  138. $newsIds = array_keys(helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'publishedOn', 'SORT_DESC'));
  139. // Pagination
  140. $pagination = helper::pagination($newsIds, $this->getUrl(),$this->getData(['config','itemsperPage']));
  141. // Liste des pages
  142. self::$pages = $pagination['pages'];
  143. // News en fonction de la pagination
  144. for($i = $pagination['first']; $i < $pagination['last']; $i++) {
  145. // Met en forme le tableau
  146. $date = mb_detect_encoding(strftime('%d %B %Y', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])), 'UTF-8', true)
  147. ? strftime('%d %B %Y', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn']))
  148. : utf8_encode(strftime('%d %B %Y', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])));
  149. $heure = mb_detect_encoding(strftime('%H:%M', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])), 'UTF-8', true)
  150. ? strftime('%H:%M', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn']))
  151. : utf8_encode(strftime('%H:%M', $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'publishedOn'])));
  152. self::$news[] = [
  153. $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'title']),
  154. $date .' à '. $heure,
  155. self::$states[$this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i], 'state'])],
  156. template::button('newsConfigEdit' . $newsIds[$i], [
  157. 'href' => helper::baseUrl() . $this->getUrl(0) . '/edit/' . $newsIds[$i]. '/' . $_SESSION['csrf'],
  158. 'value' => template::ico('pencil')
  159. ]),
  160. template::button('newsConfigDelete' . $newsIds[$i], [
  161. 'class' => 'newsConfigDelete buttonRed',
  162. 'href' => helper::baseUrl() . $this->getUrl(0) . '/delete/' . $newsIds[$i] . '/' . $_SESSION['csrf'],
  163. 'value' => template::ico('cancel')
  164. ])
  165. ];
  166. }
  167. // Valeurs en sortie
  168. $this->addOutput([
  169. 'title' => 'Configuration du module',
  170. 'view' => 'config'
  171. ]);
  172. }
  173. }
  174. /**
  175. * Suppression
  176. */
  177. public function delete() {
  178. // La news n'existe pas
  179. if($this->getData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]) === null) {
  180. // Valeurs en sortie
  181. $this->addOutput([
  182. 'access' => false
  183. ]);
  184. }
  185. // Jeton incorrect
  186. elseif ($this->getUrl(3) !== $_SESSION['csrf']) {
  187. // Valeurs en sortie
  188. $this->addOutput([
  189. 'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
  190. 'notification' => 'Action non autorisée'
  191. ]);
  192. }
  193. // Suppression
  194. else {
  195. $this->deleteData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]);
  196. // Valeurs en sortie
  197. $this->addOutput([
  198. 'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
  199. 'notification' => 'News supprimée',
  200. 'state' => true
  201. ]);
  202. }
  203. }
  204. /**
  205. * Édition
  206. */
  207. public function edit() {
  208. // Jeton incorrect
  209. if ($this->getUrl(3) !== $_SESSION['csrf']) {
  210. // Valeurs en sortie
  211. $this->addOutput([
  212. 'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
  213. 'notification' => 'Action non autorisée'
  214. ]);
  215. }
  216. // La news n'existe pas
  217. if($this->getData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]) === null) {
  218. // Valeurs en sortie
  219. $this->addOutput([
  220. 'access' => false
  221. ]);
  222. }
  223. // La news existe
  224. else {
  225. // Soumission du formulaire
  226. if($this->isPost()) {
  227. // Si l'id a changée
  228. $newsId = $this->getInput('newsEditTitle', helper::FILTER_ID, true);
  229. if($newsId !== $this->getUrl(2)) {
  230. // Incrémente le nouvel id de la news
  231. $newsId = helper::increment($newsId, $this->getData(['module', $this->getUrl(0)]));
  232. // Supprime l'ancien news
  233. $this->deleteData(['module', $this->getUrl(0),'posts', $this->getUrl(2)]);
  234. }
  235. $this->setData(['module', $this->getUrl(0),'posts', $newsId, [
  236. 'content' => $this->getInput('newsEditContent', null),
  237. 'publishedOn' => $this->getInput('newsEditPublishedOn', helper::FILTER_DATETIME, true),
  238. 'state' => $this->getInput('newsEditState', helper::FILTER_BOOLEAN),
  239. 'title' => $this->getInput('newsEditTitle', helper::FILTER_STRING_SHORT, true),
  240. 'userId' => $this->getInput('newsEditUserId', helper::FILTER_ID, true)
  241. ]]);
  242. // Valeurs en sortie
  243. $this->addOutput([
  244. 'redirect' => helper::baseUrl() . $this->getUrl(0) . '/config',
  245. 'notification' => 'Modifications enregistrées',
  246. 'state' => true
  247. ]);
  248. }
  249. // Liste des utilisateurs
  250. self::$users = helper::arrayCollumn($this->getData(['user']), 'firstname');
  251. ksort(self::$users);
  252. foreach(self::$users as $userId => &$userFirstname) {
  253. $userFirstname = $userFirstname . ' ' . $this->getData(['user', $userId, 'lastname']);
  254. }
  255. unset($userFirstname);
  256. // Valeurs en sortie
  257. $this->addOutput([
  258. 'title' => $this->getData(['module', $this->getUrl(0),'posts', $this->getUrl(2), 'title']),
  259. 'vendor' => [
  260. 'flatpickr',
  261. 'tinymce'
  262. ],
  263. 'view' => 'edit'
  264. ]);
  265. }
  266. }
  267. /**
  268. * Accueil
  269. */
  270. public function index() {
  271. // Ids des news par ordre de publication
  272. $newsIdsPublishedOns = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'publishedOn', 'SORT_DESC');
  273. $newsIdsStates = helper::arrayCollumn($this->getData(['module', $this->getUrl(0), 'posts']), 'state', 'SORT_DESC');
  274. $newsIds = [];
  275. foreach($newsIdsPublishedOns as $newsId => $newsPublishedOn) {
  276. if($newsPublishedOn <= time() AND $newsIdsStates[$newsId]) {
  277. $newsIds[] = $newsId;
  278. }
  279. }
  280. // Pagination
  281. $pagination = helper::pagination($newsIds, $this->getUrl(),$this->getData(['config','itemsperPage']));
  282. // Liste des pages
  283. self::$pages = $pagination['pages'];
  284. // News en fonction de la pagination
  285. for($i = $pagination['first']; $i < $pagination['last']; $i++) {
  286. self::$news[$newsIds[$i]] = $this->getData(['module', $this->getUrl(0),'posts', $newsIds[$i]]);
  287. }
  288. // Valeurs en sortie
  289. $this->addOutput([
  290. 'showBarEditButton' => true,
  291. 'showPageContent' => true,
  292. 'view' => 'index'
  293. ]);
  294. }
  295. /**
  296. * Retourne la signature d'un utilisateur
  297. */
  298. private function signature($userId) {
  299. switch ($this->getData(['user', $userId, 'signature'])){
  300. case 1:
  301. return $userId;
  302. break;
  303. case 2:
  304. return $this->getData(['user', $userId, 'pseudo']);
  305. break;
  306. case 3:
  307. return $this->getData(['user', $userId, 'firstname']) . ' ' . $this->getData(['user', $userId, 'lastname']);
  308. break;
  309. case 4:
  310. return $this->getData(['user', $userId, 'lastname']) . ' ' . $this->getData(['user', $userId, 'firstname']);
  311. break;
  312. default:
  313. return $this->getData(['user', $userId, 'firstname']);
  314. }
  315. }
  316. }