koblog/bl-plugins/last-articles/plugin.php
Kazhnuz 849b15ed59 ♻️ Separation plugin navigation et derniers articles
Ils ont des rôles différents
2025-08-22 18:24:38 +02:00

78 lines
1.9 KiB
PHP

<?php
class pluginLastArticles extends Plugin
{
public function init()
{
global $L;
// Fields and default values for the database of this plugin
$this->dbFields = array(
'label' => $L->get('Lastest articles'),
'numberOfItems' => 5
);
}
// Method called on the settings of the plugin on the admin area
public function form()
{
global $L;
$html = '<div>';
$html .= '<label>' . $L->get('Label') . '</label>';
$html .= '<input id="jslabel" name="label" type="text" dir="auto" value="' . $this->getValue('label') . '">';
$html .= '<span class="tip">' . $L->get('This title is almost always used in the sidebar of the site') . '</span>';
$html .= '</div>';
$html .= '<div>';
$html .= '<label>' . $L->get('Amount of items') . '</label>';
$html .= '<input id="jsnumberOfItems" name="numberOfItems" type="text" dir="auto" value="' . $this->getValue('numberOfItems') . '">';
$html .= '</div>';
return $html;
}
// Method called on the sidebar of the website
public function siteSidebar()
{
global $L;
global $url;
global $site;
global $pages;
// HTML for sidebar
$html = '<div class="plugin plugin-lastarticles">';
// Print the label if not empty
$label = $this->getValue('label');
if (!empty($label)) {
$html .= '<h2 class="plugin-label">' . $label . '</h2>';
}
$html .= '<div class="plugin-content">';
$html .= '<ul>';
// List of published pages
$onlyPublished = true;
$pageNumber = 1;
$numberOfItems = $this->getValue('numberOfItems');
$publishedPages = $pages->getList($pageNumber, $numberOfItems, $onlyPublished);
foreach ($publishedPages as $pageKey) {
try {
$page = new Page($pageKey);
$html .= '<li>';
$html .= '<a href="' . $page->permalink() . '">' . $page->title() . '</a>';
$html .= '</li>';
} catch (Exception $e) {
// Continue
}
}
$html .= '</ul>';
$html .= '</div>';
$html .= '</div>';
return $html;
}
}