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

View File

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

View File

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

View File

@ -10,12 +10,33 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
// Module specs namespace Dotclear\Plugin\fac;
$mod_conf = [
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
$mod_conf = [
[ [
'active', 'active',
'Enabled fac plugin', 'Enabled fac plugin',
@ -77,45 +98,15 @@ $mod_conf = [
1, 1,
'boolean', 'boolean',
], ],
]; ];
// Nothing to change below // Nothing to change below
try { try {
// Check module version self::growUp();
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) . "' "))
);
}
}
}
// Set module settings // Set module settings
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
foreach ($mod_conf as $v) { foreach ($mod_conf as $v) {
dcCore::app()->blog->settings->get(basename(__DIR__))->put( dcCore::app()->blog->settings->get(My::id())->put(
$v[0], $v[0],
$v[2], $v[2],
$v[3], $v[3],
@ -124,10 +115,36 @@ try {
true true
); );
} }
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
return true; return true;
} catch (Exception $e) { }
dcCore::app()->error->add($e->getMessage());
return false; 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'));
}
}