use namespace

This commit is contained in:
Jean-Christian Paul Denis 2023-03-26 20:49:41 +02:00
parent e1e6023990
commit 90905e5024
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
6 changed files with 838 additions and 906 deletions

48
src/Backend.php Normal file
View File

@ -0,0 +1,48 @@
<?php
/**
* @brief fac, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\fac;
use dcCore;
use dcNsProcess;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN');
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminBlogPreferencesFormV2' => [BackendBehaviors::class, 'adminBlogPreferencesFormV2'],
'adminBeforeBlogSettingsUpdate' => [BackendBehaviors::class, 'adminBeforeBlogSettingsUpdate'],
'adminPostHeaders' => [BackendBehaviors::class, 'adminPostHeaders'],
'adminPostFormItems' => [BackendBehaviors::class, 'adminPostFormItems'],
'adminAfterPostCreate' => [BackendBehaviors::class, 'adminAfterPostSave'],
'adminAfterPostUpdate' => [BackendBehaviors::class, 'adminAfterPostSave'],
'adminBeforePostDelete' => [BackendBehaviors::class, 'adminBeforePostDelete'],
'adminPostsActions' => [BackendBehaviors::class, 'adminPostsActions'],
]);
return true;
}
}

View File

@ -10,35 +10,47 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
declare(strict_types=1);
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
namespace Dotclear\Plugin\fac;
# Admin behaviors
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', ['facAdmin', 'adminBlogPreferencesFormV2']);
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', ['facAdmin', 'adminBeforeBlogSettingsUpdate']);
dcCore::app()->addBehavior('adminPostHeaders', ['facAdmin', 'adminPostHeaders']);
dcCore::app()->addBehavior('adminPostFormItems', ['facAdmin', 'adminPostFormItems']);
dcCore::app()->addBehavior('adminAfterPostCreate', ['facAdmin', 'adminAfterPostSave']);
dcCore::app()->addBehavior('adminAfterPostUpdate', ['facAdmin', 'adminAfterPostSave']);
dcCore::app()->addBehavior('adminBeforePostDelete', ['facAdmin', 'adminBeforePostDelete']);
dcCore::app()->addBehavior('adminPostsActions', ['facAdmin', 'adminPostsActions']);
use cursor;
use ArrayObject;
use dcAuth;
use dcCore;
use dcPage;
use dcPostsActions;
use dcRecord;
use dcSettings;
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Html\Form\{
Checkbox,
Div,
Form,
Hidden,
Input,
Label,
Note,
Para,
Select,
Submit,
Text
};
use Exception;
/**
* @ingroup DC_PLUGIN_FAC
* @brief Linked feed to entries - admin methods.
* @since 2.6
*/
class facAdmin
class BackendBehaviors
{
/**
* Get combos of types of supported public pages
*
* @return array List of post type and name
*/
public static function getPostsTypes()
public static function getPostsTypes(): array
{
$types = [
__('home page') => 'default',
@ -48,8 +60,8 @@ class facAdmin
__('category pages') => 'category',
__('entries feed') => 'feed',
];
if (dcCore::app()->plugins->moduleExists('muppet')) {
foreach (muppet::getPostTypes() as $k => $v) {
if (dcCore::app()->plugins->moduleExists('muppet') && class_exists('\muppet')) {
foreach (\muppet::getPostTypes() as $k => $v) {
$types[sprintf(
__('"%s" pages from extension muppet'),
$v['name']
@ -65,8 +77,20 @@ class facAdmin
*
* @param dcSettings $blog_settings dcSettings instance
*/
public static function adminBlogPreferencesFormV2(dcSettings $blog_settings)
public static function adminBlogPreferencesFormV2(dcSettings $blog_settings): void
{
$lines = '';
$fac_public_tpltypes = json_decode($blog_settings->get(My::id())->get('public_tpltypes'), true);
if (!is_array($fac_public_tpltypes)) {
$fac_public_tpltypes = [];
}
foreach (self::getPostsTypes() as $k => $v) {
$lines .= (new Para())->items([
(new Checkbox(['fac_public_tpltypes[]', 'fac_public_tpltypes' . $k], in_array($v, $fac_public_tpltypes)))->value($v),
(new Label(__($k), Label::OUTSIDE_LABEL_AFTER))->for('fac_public_tpltypes' . $k)->class('classic'),
])->render();
}
echo
'<div class="fieldset"><h4 id="fac_params">Feed after content</h4>' .
'<p class="form-note">' .
@ -74,49 +98,40 @@ class facAdmin
'</p>';
if (dcCore::app()->auth->isSuperAdmin()) {
echo '<p><a href="' . dcCore::app()->adminurl->get('admin.plugins', [
'module' => basename(__DIR__),
'module' => My::id(),
'conf' => 1,
'redir' => dcCore::app()->adminurl->get('admin.blog.pref') . '#fac_params',
]) . '">' . __('Configure formats') . '</a></p>';
}
echo
'<div class="two-cols">' .
'<div class="col">' .
'<h5>' . __('Activation') . '</h5>' .
'<p><label class="classic">' .
form::checkbox('fac_active', '1', (bool) $blog_settings->get(basename(__DIR__))->get('active')) .
__('Enable "fac" extension') . '</label></p>' .
'<p class="form-note">' .
__('You can manage related feed to display for each post with a predefined format.') .
'</p>' .
'<h5>' . __('Feed') . '</h5>' .
'<p><label for="fac_defaultfeedtitle">' . __('Default title') . '</label>' .
form::field('fac_defaultfeedtitle', 65, 255, (string) $blog_settings->get(basename(__DIR__))->get('defaultfeedtitle')) . '</p>' .
'<p class="form-note">' . __('Use %T to insert title of feed.') . '</p>' .
'<p><label class="classic" for="fac_showfeeddesc">' .
form::checkbox('fac_showfeeddesc', 1, (bool) $blog_settings->get(basename(__DIR__))->get('showfeeddesc')) .
__('Show description of feed') . '</label></p>' .
'</div>' .
'<div class="col">' .
'<h5>' . __('Show feed after content on:') . '</h5>';
$fac_public_tpltypes = json_decode($blog_settings->get(basename(__DIR__))->get('public_tpltypes'), true);
if (!is_array($fac_public_tpltypes)) {
$fac_public_tpltypes = [];
}
foreach (self::getPostsTypes() as $k => $v) {
echo '
<p><label class="classic" for="fac_public_tpltypes' . $k . '">' .
form::checkbox(
['fac_public_tpltypes[]', 'fac_public_tpltypes' . $k],
$v,
in_array($v, $fac_public_tpltypes)
) . __($k) . '</label></p>';
}
echo
'</div>' .
'</div>' .
(new Div())->class('two-cols')->items([
(new Div())->class('col')->items([
(new Text('h5', Html::escapeHTML(__('Activation')))),
// active
(new Para())->items([
(new Checkbox('fac_active', (bool) $blog_settings->get(My::id())->get('active')))->value(1),
(new Label(__('Enable "fac" extension'), Label::OUTSIDE_LABEL_AFTER))->for('fac_active')->class('classic'),
]),
(new Note())->text(__('You can manage related feed to display for each post with a predefined format.'))->class('form-note'),
(new Text('h5', Html::escapeHTML(__('Feed')))),
// defaultfeedtitle
(new Para())->items([
(new Label(__('Default title')))->for('fac_defaultfeedtitle'),
(new Input('fac_defaultfeedtitle'))->size(70)->maxlenght(255)->value((string) $blog_settings->get(My::id())->get('defaultfeedtitle')),
]),
(new Note())->text(__('Use %T to insert title of feed.'))->class('form-note'),
// showfeeddesc
(new Para())->items([
(new Checkbox('fac_showfeeddesc', (bool) $blog_settings->get(My::id())->get('showfeeddesc')))->value(1),
(new Label(__('Show description of feed'), Label::OUTSIDE_LABEL_AFTER))->for('fac_showfeeddesc')->class('classic'),
]),
]),
(new Div())->class('col')->items([
(new Text('h5', Html::escapeHTML(__('Show feed after content on:')))),
(new Text('', $lines)),
]),
])->render() .
'<br class="clear" />' .
'</div>';
}
@ -126,12 +141,12 @@ class facAdmin
*
* @param dcSettings $blog_settings dcSettings instance
*/
public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings)
public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings): void
{
$blog_settings->get(basename(__DIR__))->put('active', !empty($_POST['fac_active']));
$blog_settings->get(basename(__DIR__))->put('public_tpltypes', json_encode($_POST['fac_public_tpltypes']));
$blog_settings->get(basename(__DIR__))->put('defaultfeedtitle', (string) $_POST['fac_defaultfeedtitle']);
$blog_settings->get(basename(__DIR__))->put('showfeeddesc', !empty($_POST['fac_showfeeddesc']));
$blog_settings->get(My::id())->put('active', !empty($_POST['fac_active']));
$blog_settings->get(My::id())->put('public_tpltypes', json_encode($_POST['fac_public_tpltypes']));
$blog_settings->get(My::id())->put('defaultfeedtitle', (string) $_POST['fac_defaultfeedtitle']);
$blog_settings->get(My::id())->put('showfeeddesc', !empty($_POST['fac_showfeeddesc']));
}
/**
@ -139,9 +154,9 @@ class facAdmin
*
* @return string HTML head
*/
public static function adminPostHeaders()
public static function adminPostHeaders(): string
{
return dcPage::jsModuleLoad(basename(__DIR__) . '/js/admin.js');
return dcPage::jsModuleLoad(My::id() . '/js/backend.js');
}
/**
@ -149,12 +164,12 @@ class facAdmin
*
* @param ArrayObject $main_items Main items
* @param ArrayObject $sidebar_items Sidebar items
* @param record $post Post record or null
* @param null|dcRecord $post Post record or null
*/
public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, $post)
public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?dcRecord $post): void
{
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
return null;
if (!dcCore::app()->blog->settings->get(My::id())->get('active')) {
return;
}
# Get existing linked feed
@ -162,17 +177,17 @@ class facAdmin
if ($post) {
$rs = dcCore::app()->meta->getMetadata([
'meta_type' => 'fac',
'post_id' => $post->post_id,
'post_id' => $post->f('post_id'),
'limit' => 1,
]);
$fac_url = $rs->isEmpty() ? '' : $rs->meta_id;
$fac_url = $rs->isEmpty() ? '' : $rs->f('meta_id');
$rs = dcCore::app()->meta->getMetadata([
'meta_type' => 'facformat',
'post_id' => $post->post_id,
'post_id' => $post->f('post_id'),
'limit' => 1,
]);
$fac_format = $rs->isEmpty() ? '' : $rs->meta_id;
$fac_format = $rs->isEmpty() ? '' : $rs->f('meta_id');
}
# Set linked feed form items
@ -185,11 +200,11 @@ class facAdmin
* @param cursor $cur Current post cursor
* @param integer $post_id Post id
*/
public static function adminAfterPostSave(cursor $cur, $post_id)
public static function adminAfterPostSave(cursor $cur, int $post_id): void
{
if (!isset($_POST['fac_url'])
|| !isset($_POST['fac_format'])) {
return null;
return;
}
# Delete old linked feed
@ -204,7 +219,7 @@ class facAdmin
*
* @param integer $post_id Post id
*/
public static function adminBeforePostDelete($post_id)
public static function adminBeforePostDelete(int $post_id): void
{
self::delFeed($post_id);
}
@ -214,26 +229,26 @@ class facAdmin
*
* @param dcPostsActions $pa dcPostsActionsPage instance
*/
public static function adminPostsActions(dcPostsActions $pa)
public static function adminPostsActions(dcPostsActions $pa): void
{
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
return null;
if (!dcCore::app()->blog->settings->get(My::id())->get('active')) {
return;
}
$pa->addAction(
[__('Linked feed') => [__('Add feed') => 'fac_add']],
['facAdmin', 'callbackAdd']
[self::class, 'callbackAdd']
);
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_DELETE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)) {
return null;
return;
}
$pa->addAction(
[__('Linked feed') => [__('Remove feed') => 'fac_remove']],
['facAdmin', 'callbackRemove']
[self::class, 'callbackRemove']
);
}
@ -243,7 +258,7 @@ class facAdmin
* @param dcPostsActions $pa dcPostsActions instance
* @param ArrayObject $post _POST actions
*/
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post)
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post): void
{
# No entry
$posts_ids = $pa->getIDs();
@ -274,7 +289,7 @@ class facAdmin
* @param dcPostsActions $pa dcPostsActions instance
* @param ArrayObject $post _POST actions
*/
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post)
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post): void
{
# No entry
$posts_ids = $pa->getIDs();
@ -297,24 +312,21 @@ class facAdmin
} else {
$pa->beginPage(
dcPage::breadcrumb([
html::escapeHTML(dcCore::app()->blog->name) => '',
Html::escapeHTML(dcCore::app()->blog->name) => '',
$pa->getCallerTitle() => $pa->getRedirection(true),
__('Linked feed to this selection') => '',
])
);
echo
'<form action="' . $pa->getURI() . '" method="post">' .
$pa->getCheckboxes() .
self::formFeed() .
'<p>' .
dcCore::app()->formNonce() .
$pa->getHiddenFields() .
form::hidden(['action'], 'fac_add') .
'<input type="submit" value="' . __('Save') . '" /></p>' .
'</form>';
(new Form('fac_form'))->action($pa->getURI())->method('post')->fields([
(new Text('', $pa->getCheckboxes() . self::formFeed())),
(new Para())->items([
(new Text('', dcCore::app()->formNonce() . $pa->getHiddenFields())),
(new Hidden(['action'], 'fac_add')),
(new Submit(['save']))->value(__('Save')),
]),
])->render();
$pa->endPage();
}
@ -325,36 +337,29 @@ class facAdmin
*
* @param string $url Feed URL
* @param string $format Feed format
* @return null|string Feed form content
* @return string Feed form content
*/
protected static function formFeed($url = '', $format = '')
protected static function formFeed(string $url = '', string $format = ''): string
{
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
return null;
if (!dcCore::app()->blog->settings->get(My::id())->get('active')) {
return '';
}
return
'<div id="fac">' .
'<h5>' . __('Linked feed') . '</h5>' .
'<p><label for="fac_url">' .
__('Feed URL:') . '</label>' .
form::field(
'fac_url',
60,
255,
$url,
'maximal'
) . '</p>' .
'<p><label for="fac_format">' .
__('Format:') . '</label>' .
form::combo(
'fac_format',
self::comboFac(),
$format,
'maximal'
) . '</p>' .
($url ? '<p><a href="' . $url . '" title="' . $url . '">' . __('view feed') . '</a></p>' : '') .
'</div>';
(new Div('fac'))->items([
(new Text('h5', __('Linked feed'))),
// fac_url
(new Para())->items([
(new Label(__('Feed URL:')))->for('fac_url')->class('required'),
(new Input('fac_url'))->size(60)->maxlenght(255)->value($url),
]),
// fac_format
(new Para())->items([
(new Label(__('Format:')))->for('fac_format'),
(new Select('fac_format'))->default($format)->items(self::comboFac()),
]),
(new Text('', $url ? '<p><a href="' . $url . '" title="' . $url . '">' . __('view feed') . '</a></p>' : '')),
])->render();
}
/**
@ -362,9 +367,9 @@ class facAdmin
*
* @return array List of fac formats
*/
protected static function comboFac()
protected static function comboFac(): array
{
$formats = json_decode(dcCore::app()->blog->settings->get(basename(__DIR__))->get('formats'), true);
$formats = json_decode(dcCore::app()->blog->settings->get(My::id())->get('formats'), true);
if (!is_array($formats) || empty($formats)) {
return [];
}
@ -382,7 +387,7 @@ class facAdmin
*
* @param integer $post_id Post id
*/
protected static function delFeed($post_id)
protected static function delFeed(int $post_id): void
{
$post_id = (int) $post_id;
dcCore::app()->meta->delPostMeta($post_id, 'fac');
@ -395,11 +400,11 @@ class facAdmin
* @param integer $post_id Post id
* @param array|ArrayObject $options Feed options
*/
protected static function addFeed($post_id, $options)
protected static function addFeed(int $post_id, array|ArrayObject $options): void
{
if (empty($options['fac_url'])
|| empty($options['fac_format'])) {
return null;
return;
}
$post_id = (int) $post_id;

View File

@ -10,19 +10,47 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_MODULE')) {
return null;
declare(strict_types=1);
namespace Dotclear\Plugin\fac;
use dcCore;
use dcPage;
use dcNsProcess;
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Html\Form\{
Checkbox,
Div,
Input,
Label,
Note,
Number,
Para,
Text
};
use Exception;
class Config extends dcNsProcess
{
public static function init(): bool
{
static::$init == defined('DC_CONTEXT_ADMIN')
&& dcCore::app()->auth?->isSuperAdmin();
return static::$init;
}
if (!dcCore::app()->auth->isSuperAdmin()) {
return null;
public static function process(): bool
{
if (!static::$init) {
return false;
}
$redir = empty($_REQUEST['redir']) ?
dcCore::app()->admin->list->getURL() . '#plugins' : $_REQUEST['redir'];
dcCore::app()->admin->__get('list')->getURL() . '#plugins' : $_REQUEST['redir'];
# -- Get settings --
$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
$s = dcCore::app()->blog->settings->get(My::id());
$fac_formats = json_decode($s->get('formats'), true);
@ -54,425 +82,59 @@ if (!empty($_POST['save'])) {
dcCore::app()->blog->triggerBlog();
dcAdminNotices::addSuccessNotice(
dcPage::addSuccessNotice(
__('Configuration successfully updated.')
);
http::redirect(
dcCore::app()->admin->list->getURL('module=' . basename(__DIR__) . '&conf=1&redir=' . dcCore::app()->admin->list->getRedir())
dcCore::app()->adminurl?->redirect(
'admin.plugins',
['module' => My::id(), 'conf' => 1, 'redir' => dcCore::app()->admin->__get('list')->getRedir()]
);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
# -- Display form --
return true;
}
public static function render(): void
{
if (!static::$init) {
return;
}
$s = dcCore::app()->blog->settings->get(My::id());
$fac_formats = json_decode($s->get('formats'), true);
$i = 1;
foreach ($fac_formats as $uid => $f) {
if (empty($f['name'])) {
foreach ($fac_formats as $uid => $format) {
if (empty($format['name'])) {
continue;
}
echo '
<div class="fieldset">
<h4>' . sprintf(__('Format %s'), $i) . '</h4>
<div class="two-boxes"><h5>' . __('General') . '</h5>
<p><label for="fac_formats_' . $uid . '_name">' .
__('Name:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][name]',
'fac_formats_' . $uid . '_name',
],
20,
255,
empty($f['name']) ? '' : $f['name'],
'maximal'
) . '</p>
<p class="form-note">' .
__('In order to remove a format, leave its name empty.') .
'</p>
<p><label for="fac_formats_' . $uid . '_dateformat">' .
__('Date format:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][dateformat]',
'fac_formats_' . $uid . '_dateformat',
],
20,
255,
empty($f['dateformat']) ? '' : $f['dateformat'],
'maximal'
) . '</p>
<p class="form-note">' .
__('Use date format of Dotclear or leave empty to use default date format of blog.') .
'</p>
<p><label for="fac_formats_' . $uid . '_lineslimit">' .
__('Entries limit:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][lineslimit]',
'fac_formats_' . $uid . '_lineslimit',
],
5,
4,
empty($f['lineslimit']) ? '' : $f['lineslimit'],
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div><div class="two-boxes"><h5>' . __('Title') . '</h5>
<p><label for="fac_formats_' . $uid . '_linestitletext">' .
__('Title format:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linestitletext]',
'fac_formats_' . $uid . '_linestitletext',
],
20,
255,
empty($f['linestitletext']) ? '' : $f['linestitletext'],
'maximal'
) . '</p>
<p class="form-note">' .
__('Format can be:') .
'%D : ' . __('Date') .
', %T : ' . __('Title') .
', %A : ' . __('Author') .
', %E : ' . __('Description') .
', %C : ' . __('Content') .
'</p>
<p><label for="fac_formats_' . $uid . '_linestitleover">' .
__('Over title format:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linestitleover]',
'fac_formats_' . $uid . '_linestitleover',
],
20,
255,
empty($f['linestitleover']) ? '' : $f['linestitleover'],
'maximal'
) . '</p>
<p class="form-note">' .
__('Format can be:') .
'%D : ' . __('Date') .
', %T : ' . __('Title') .
', %A : ' . __('Author') .
', %E : ' . __('Description') .
', %C : ' . __('Content') .
'</p>
<p><label for="fac_formats_' . $uid . '_linestitlelength">' .
__('Maximum length of title:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linestitlelength]',
'fac_formats_' . $uid . '_linestitlelength',
],
5,
4,
empty($f['linestitlelength']) ? '' : $f['linestitlelength'],
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div><div class="two-boxes"><h5>' . __('Description') . '</h5>
<p><label for="fac_formats_' . $uid . '_showlinesdescription">' .
form::checkbox(
[
'fac_formats[' . $uid . '][showlinesdescription]',
'fac_formats_' . $uid . '_showlinesdescription',
],
1,
!empty($f['showlinesdescription'])
) .
__('Show description of entries') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linesdescriptionnohtml">' .
form::checkbox(
[
'fac_formats[' . $uid . '][linesdescriptionnohtml]',
'fac_formats_' . $uid . '_linesdescriptionnohtml',
],
1,
!empty($f['linesdescriptionnohtml'])
) .
__('Remove html of description') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linesdescriptionlength">' .
__('Maximum length of description:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linesdescriptionlength]',
'fac_formats_' . $uid . '_linesdescriptionlength',
],
5,
4,
empty($f['linesdescriptionlength']) ? '' : $f['linesdescriptionlength'],
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div><div class="two-boxes"><h5>' . __('Content') . '</h5>
<p><label for="fac_formats_' . $uid . '_showlinescontent">' .
form::checkbox(
[
'fac_formats[' . $uid . '][showlinescontent]',
'fac_formats_' . $uid . '_showlinescontent',
],
1,
!empty($f['showlinescontent'])
) .
__('Show content of entries') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linescontentnohtml">' .
form::checkbox(
[
'fac_formats[' . $uid . '][linescontentnohtml]',
'fac_formats_' . $uid . '_linescontentnohtml',
],
1,
!empty($f['linescontentnohtml'])
) .
__('Remove html of content') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linescontentlength">' .
__('Maximum length of content:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linescontentlength]',
'fac_formats_' . $uid . '_linescontentlength',
],
5,
4,
empty($f['linescontentlength']) ? '' : $f['linescontentlength'],
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div>
</div>';
self::displayFacFormat(sprintf(__('Format %s'), $i), $uid, $format);
$i++;
}
$uid = uniqid();
$new_format = [
'name' => '',
'dateformat' => '',
'lineslimit' => '5',
'linestitletext' => '%T',
'linestitleover' => '%D',
'linestitlelength' => '150',
'showlinesdescription' => '0',
'linesdescriptionlength' => '350',
'linesdescriptionnohtml' => '1',
'showlinescontent' => '0',
'linescontentlength' => '350',
'linescontentnohtml' => '1',
];
self::displayFacFormat(__('New format'), uniqid(), $new_format);
echo '
<div class="fieldset">
<h4>' . __('New format') . '</h4>
<div class="two-boxes"><h5>' . __('General') . '</h5>
<p><label for="fac_formats_' . $uid . '_name">' .
__('Name:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][name]',
'fac_formats_' . $uid . '_name',
],
20,
255,
'',
'maximal'
) . '</p>
<p class="form-note">' .
__('In order to remove a format, leave its name empty.') .
'</p>
<p><label for="fac_formats_' . $uid . '_dateformat">' .
__('Date format:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][dateformat]',
'fac_formats_' . $uid . '_dateformat',
],
20,
255,
'',
'maximal'
) . '</p>
<p class="form-note">' .
__('Use date format of Dotclear or leave empty to use default date format of blog.') .
'</p>
<p><label for="fac_formats_' . $uid . '_lineslimit">' .
__('Entries limit:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][lineslimit]',
'fac_formats_' . $uid . '_lineslimit',
],
5,
4,
5,
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div><div class="two-boxes"><h5>' . __('Title') . '</h5>
<p><label for="fac_formats_' . $uid . '_linestitletext">' .
__('Title format:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linestitletext]',
'fac_formats_' . $uid . '_linestitletext',
],
20,
255,
'%T',
'maximal'
) . '</p>
<p class="form-note">' .
__('Format can be:') .
'%D : ' . __('Date') .
', %T : ' . __('Title') .
', %A : ' . __('Author') .
', %E : ' . __('Description') .
', %C : ' . __('Content') .
'</p>
<p><label for="fac_formats_' . $uid . '_linestitleover">' .
__('Over title format:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linestitleover]',
'fac_formats_' . $uid . '_linestitleover',
],
20,
255,
'%D',
'maximal'
) . '</p>
<p class="form-note">' .
__('Format can be:') .
'%D : ' . __('Date') .
', %T : ' . __('Title') .
', %A : ' . __('Author') .
', %E : ' . __('Description') .
', %C : ' . __('Content') .
'</p>
<p><label for="fac_formats_' . $uid . '_linestitlelength">' .
__('Maximum length of title:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linestitlelength]',
'fac_formats_' . $uid . '_linestitlelength',
],
5,
4,
150,
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div><div class="two-boxes"><h5>' . __('Description') . '</h5>
<p><label for="fac_formats_' . $uid . '_showlinesdescription">' .
form::checkbox(
[
'fac_formats[' . $uid . '][showlinesdescription]',
'fac_formats_' . $uid . '_showlinesdescription',
],
1,
0
) .
__('Show description of entries') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linesdescriptionnohtml">' .
form::checkbox(
[
'fac_formats[' . $uid . '][linesdescriptionnohtml]',
'fac_formats_' . $uid . '_linesdescriptionnohtml',
],
1,
1
) .
__('Remove html of description') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linesdescriptionlength">' .
__('Maximum length of description:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linesdescriptionlength]',
'fac_formats_' . $uid . '_linesdescriptionlength',
],
5,
4,
350,
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div><div class="two-boxes"><h5>' . __('Content') . '</h5>
<p><label for="fac_formats_' . $uid . '_showlinescontent">' .
form::checkbox(
[
'fac_formats[' . $uid . '][showlinescontent]',
'fac_formats_' . $uid . '_showlinescontent',
],
1,
0
) .
__('Show content of entries') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linescontentnohtml">' .
form::checkbox(
[
'fac_formats[' . $uid . '][linescontentnohtml]',
'fac_formats_' . $uid . '_linescontentnohtml',
],
1,
1
) .
__('Remove html of content') . '</label></p>
<p><label for="fac_formats_' . $uid . '_linescontentlength">' .
__('Maximum length of content:') . '</label>' .
form::field(
[
'fac_formats[' . $uid . '][linescontentlength]',
'fac_formats_' . $uid . '_linescontentlength',
],
5,
4,
350,
'maximal'
) . '</p>
<p class="form-note">' .
__('Leave lengh empty for no limit.') .
'</p>
</div>
</div>
<div class="fieldset">
<h4>' . __('Informations') . '</h4>
@ -487,7 +149,7 @@ __('To add feed to an entry edit this entry and put in sidebar the url of the fe
</div><div class="two-boxes">
<h5>' . __('Structure') . '</h5>
<pre>' . html::escapeHTML('
<pre>' . Html::escapeHTML('
<div class="post-fac">
<h3>' . __('Title of feed') . '</h3>
<p>' . __('Description of feed') . '</p>
@ -503,3 +165,143 @@ __('To add feed to an entry edit this entry and put in sidebar the url of the fe
</div>';
dcPage::helpBlock('fac');
}
private static function displayFacFormat(string $title, string $uid, array $format): void
{
echo
(new Div())->class('fieldset')->separator('')->items([
(new Text('h4', $title)),
(new Div())->class('two-boxes even')->items([
(new Text('h5', __('General'))),
// name
(new Para())->items([
(new Label(__('Name:')))->for('fac_formats_' . $uid . '_name'),
(new Input([
'fac_formats[' . $uid . '][name]',
'fac_formats_' . $uid . '_name',
]))->value(empty($format['name']) ? '' : $format['name'])->size(20)->maxlenght(255)->class('maximal'),
]),
(new Note())->text(__('In order to remove a format, leave its name empty.'))->class('form-note'),
// dateformat
(new Para())->items([
(new Label(__('Date format:')))->for('fac_formats_' . $uid . '_dateformat'),
(new Input([
'fac_formats[' . $uid . '][dateformat]',
'fac_formats_' . $uid . '_dateformat',
]))->value(empty($format['dateformat']) ? '' : $format['dateformat'])->size(20)->maxlenght(255)->class('maximal'),
]),
(new Note())->text(__('Use date format of Dotclear or leave empty to use default date format of blog.'))->class('form-note'),
// dateformat //todo: use Number
(new Para())->items([
(new Label(__('Entries limit:')))->for('fac_formats_' . $uid . '_lineslimit'),
(new Input([
'fac_formats[' . $uid . '][lineslimit]',
'fac_formats_' . $uid . '_lineslimit',
]))->value(empty($format['lineslimit']) ? '' : $format['lineslimit'])->size(4)->maxlenght(5),
]),
(new Note())->text(__('Leave lengh empty for no limit.'))->class('form-note'),
]),
(new Div())->class('two-boxes odd')->items([
(new Text('h5', __('Title'))),
// linestitletext
(new Para())->items([
(new Label(__('Title format:')))->for('fac_formats_' . $uid . '_linestitletext'),
(new Input([
'fac_formats[' . $uid . '][linestitletext]',
'fac_formats_' . $uid . '_linestitletext',
]))->value(empty($format['linestitletext']) ? '' : $format['linestitletext'])->size(20)->maxlenght(255)->class('maximal'),
]),
(new Note())->text(
__('Format can be:') .
'%D : ' . __('Date') .
', %T : ' . __('Title') .
', %A : ' . __('Author') .
', %E : ' . __('Description') .
', %C : ' . __('Content')
)->class('form-note'),
// linestitleover
(new Para())->items([
(new Label(__('Over title format:')))->for('fac_formats_' . $uid . '_linestitleover'),
(new Input([
'fac_formats[' . $uid . '][linestitleover]',
'fac_formats_' . $uid . '_linestitleover',
]))->value(empty($format['linestitleover']) ? '' : $format['linestitleover'])->size(20)->maxlenght(255)->class('maximal'),
]),
(new Note())->text(
__('Format can be:') .
'%D : ' . __('Date') .
', %T : ' . __('Title') .
', %A : ' . __('Author') .
', %E : ' . __('Description') .
', %C : ' . __('Content')
)->class('form-note'),
// linestitlelength //todo: use Number
(new Para())->items([
(new Label(__('Maximum length of title:')))->for('fac_formats_' . $uid . '_linestitlelength'),
(new Input([
'fac_formats[' . $uid . '][linestitlelength]',
'fac_formats_' . $uid . '_linestitlelength',
]))->value(empty($format['linestitlelength']) ? '' : $format['linestitlelength'])->size(4)->maxlenght(5),
]),
(new Note())->text(__('Leave lengh empty for no limit.'))->class('form-note'),
]),
(new Div())->class('two-boxes even')->items([
(new Text('h5', __('Description'))),
// showlinesdescription
(new Para())->items([
(new Checkbox([
'fac_formats[' . $uid . '][showlinesdescription]',
'fac_formats_' . $uid . '_showlinesdescription',
], !empty($format['showlinesdescription'])))->value(1),
(new Label(__('Show description of entries'), Label::OUTSIDE_LABEL_AFTER))->for('fac_formats_' . $uid . '_showlinesdescription')->class('classic'),
]),
// linesdescriptionnohtml
(new Para())->items([
(new Checkbox([
'fac_formats[' . $uid . '][linesdescriptionnohtml]',
'fac_formats_' . $uid . '_linesdescriptionnohtml',
], !empty($format['linesdescriptionnohtml'])))->value(1),
(new Label(__('Remove html of description'), Label::OUTSIDE_LABEL_AFTER))->for('fac_formats_' . $uid . '_linesdescriptionnohtml')->class('classic'),
]),
// linesdescriptionlength //todo: use Number
(new Para())->items([
(new Label(__('Maximum length of description:')))->for('fac_formats_' . $uid . '_linesdescriptionlength'),
(new Input([
'fac_formats[' . $uid . '][linesdescriptionlength]',
'fac_formats_' . $uid . '_linesdescriptionlength',
]))->value(empty($format['linesdescriptionlength']) ? '' : $format['linesdescriptionlength'])->size(4)->maxlenght(5),
]),
(new Note())->text(__('Leave lengh empty for no limit.'))->class('form-note'),
]),
(new Div())->class('two-boxes odd')->items([
(new Text('h5', __('Content'))),
// showlinescontent
(new Para())->items([
(new Checkbox([
'fac_formats[' . $uid . '][showlinescontent]',
'fac_formats_' . $uid . '_showlinescontent',
], !empty($format['showlinescontent'])))->value(1),
(new Label(__('Show content of entries'), Label::OUTSIDE_LABEL_AFTER))->for('fac_formats_' . $uid . '_showlinescontent')->class('classic'),
]),
// linescontentnohtml
(new Para())->items([
(new Checkbox([
'fac_formats[' . $uid . '][linescontentnohtml]',
'fac_formats_' . $uid . '_linescontentnohtml',
], !empty($format['linescontentnohtml'])))->value(1),
(new Label(__('Remove html of content'), Label::OUTSIDE_LABEL_AFTER))->for('fac_formats_' . $uid . '_linescontentnohtml')->class('classic'),
]),
// linescontentlength //todo: use Number
(new Para())->items([
(new Label(__('Maximum length of content:')))->for('fac_formats_' . $uid . '_linescontentlength'),
(new Input([
'fac_formats[' . $uid . '][linescontentlength]',
'fac_formats_' . $uid . '_linescontentlength',
]))->value(empty($format['linescontentlength']) ? '' : $format['linescontentlength'])->size(4)->maxlenght(5),
]),
(new Note())->text(__('Leave lengh empty for no limit.'))->class('form-note'),
]),
])->render();
}
}

View File

@ -10,44 +10,64 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
declare(strict_types=1);
namespace Dotclear\Plugin\fac;
use context;
use dcCore;
use dcNsProcess;
use Dotclear\Helper\Html\Html;
use Exception;
use dt;
use feedReader;
class Frontend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_RC_PATH');
return static::$init;
}
dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, context $_ctx) {
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
public static function process(): bool
{
if (!static::$init || !dcCore::app()->blog->settings->get(My::id())->get('active')) {
return false;
}
# Not active or not a post
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')
|| !dcCore::app()->ctx->exists('posts')) {
return null;
dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, context $_ctx): void {
# Not a post
if (!dcCore::app()->ctx->exists('posts')) {
return;
}
# Not in page to show
$types = json_decode((string) dcCore::app()->blog->settings->get(basename(__DIR__))->get('public_tpltypes'), true);
$types = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get('public_tpltypes'), true);
if (!is_array($types)
|| !in_array(dcCore::app()->url->type, $types)) {
return null;
return;
}
# Get related feed
$fac_url = dcCore::app()->meta->getMetadata([
'meta_type' => 'fac',
'post_id' => dcCore::app()->ctx->posts->post_id,
'post_id' => dcCore::app()->ctx->__get('posts')->f('post_id'),
'limit' => 1,
]);
if ($fac_url->isEmpty()) {
return null;
return;
}
# Get related format
$fac_format = dcCore::app()->meta->getMetadata([
'meta_type' => 'facformat',
'post_id' => dcCore::app()->ctx->posts->post_id,
'post_id' => dcCore::app()->ctx->__get('posts')->f('post_id'),
'limit' => 1,
]);
if ($fac_format->isEmpty()) {
return null;
return;
}
# Get format info
@ -66,15 +86,15 @@ dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, co
'linescontentnohtml' => '1',
];
$formats = json_decode((string) dcCore::app()->blog->settings->get(basename(__DIR__))->get('formats'), true);
$formats = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get('formats'), true);
if (empty($formats)
|| !is_array($formats)
|| !isset($formats[$fac_format->meta_id])) {
|| !isset($formats[$fac_format->f('meta_id')])) {
$format = $default_format;
} else {
$format = array_merge(
$default_format,
$formats[$fac_format->meta_id]
$formats[$fac_format->f('meta_id')]
);
}
@ -82,37 +102,37 @@ dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, co
$cache = is_dir(DC_TPL_CACHE . '/fac') ? DC_TPL_CACHE . '/fac' : null;
try {
$feed = feedReader::quickParse($fac_url->meta_id, $cache);
$feed = feedReader::quickParse($fac_url->f('meta_id'), $cache);
} catch (Exception $e) {
$feed = null;
}
# No entries
if (!$feed) {
return null;
return;
}
# Feed title
$feedtitle = '';
if ('' != dcCore::app()->blog->settings->get(basename(__DIR__))->get('defaultfeedtitle')) {
$feedtitle = '<h3>' . html::escapeHTML(
if ('' != dcCore::app()->blog->settings->get(My::id())->get('defaultfeedtitle')) {
$feedtitle = '<h3>' . Html::escapeHTML(
empty($feed->title) ?
str_replace(
'%T',
__('a related feed'),
dcCore::app()->blog->settings->get(basename(__DIR__))->get('defaultfeedtitle')
dcCore::app()->blog->settings->get(My::id())->get('defaultfeedtitle')
) :
str_replace(
'%T',
$feed->title,
dcCore::app()->blog->settings->get(basename(__DIR__))->get('defaultfeedtitle')
dcCore::app()->blog->settings->get(My::id())->get('defaultfeedtitle')
)
) . '</h3>';
}
# Feed desc
$feeddesc = '';
if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('showfeeddesc')
if (dcCore::app()->blog->settings->get(My::id())->get('showfeeddesc')
&& '' != $feed->description) {
$feeddesc = '<p>' . context::global_filters(
$feed->description,
@ -217,3 +237,7 @@ dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, co
}
echo '</dl></div>';
});
return true;
}
}

View File

@ -10,8 +10,29 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
declare(strict_types=1);
namespace Dotclear\Plugin\fac;
use dcCore;
use dcNamespace;
use dcNsProcess;
use Exception;
class Install extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
// Module specs
@ -81,41 +102,11 @@ $mod_conf = [
// Nothing to change below
try {
// Check module version
if (!dcCore::app()->newVersion(
basename(__DIR__),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
)) {
return null;
}
// version < 1.0 : upgrade settings id and ns and array
$current = dcCore::app()->getVersion(basename(__DIR__));
if ($current && version_compare($current, '1.0', '<')) {
$record = dcCore::app()->con->select(
'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' .
"WHERE setting_ns = 'fac' "
);
while ($record->fetch()) {
if (preg_match('/^fac_(.*?)$/', $record->setting_id, $match)) {
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME);
if (in_array($record->setting_id, ['fac_public_tpltypes', 'fac_formats'])) {
$cur->setting_value = json_encode(@unserialize($record->setting_value));
}
$cur->setting_id = $match[1];
$cur->setting_ns = basename(__DIR__);
$cur->update(
"WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'fac' " .
'AND blog_id ' . (null === $record->blog_id ? 'IS NULL ' : ("= '" . dcCore::app()->con->escape($record->blog_id) . "' "))
);
}
}
}
self::growUp();
// Set module settings
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
foreach ($mod_conf as $v) {
dcCore::app()->blog->settings->get(basename(__DIR__))->put(
dcCore::app()->blog->settings->get(My::id())->put(
$v[0],
$v[2],
$v[3],
@ -124,10 +115,36 @@ try {
true
);
}
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
return false;
}
return true;
}
private static function growUp(): void
{
// version < 1.0 : upgrade settings id and ns and array
$current = dcCore::app()->getVersion(My::id());
if ($current && version_compare($current, '1.0', '<')) {
$record = dcCore::app()->con->select(
'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' .
"WHERE setting_ns = 'fac' "
);
while ($record->fetch()) {
if (preg_match('/^fac_(.*?)$/', $record->f('setting_id'), $match)) {
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME);
if (in_array($record->f('setting_id'), ['fac_public_tpltypes', 'fac_formats'])) {
$cur->setField('setting_value', json_encode(@unserialize($record->f('setting_value'))));
}
$cur->setField('setting_id', $match[1]);
$cur->SetField('setting_ns', My::id());
$cur->update(
"WHERE setting_id = '" . $record->f('setting_id') . "' and setting_ns = 'fac' " .
'AND blog_id ' . (null === $record->f('blog_id') ? 'IS NULL ' : ("= '" . dcCore::app()->con->escapeStr($record->f('blog_id')) . "' "))
);
}
}
}
}
}

36
src/My.php Normal file
View File

@ -0,0 +1,36 @@
<?php
/**
* @brief fac, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\fac;
use dcCore;
class My
{
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
}
}