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,19 +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_MODULE')) { declare(strict_types=1);
return null;
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()) { public static function process(): bool
return null; {
if (!static::$init) {
return false;
} }
$redir = empty($_REQUEST['redir']) ? $redir = empty($_REQUEST['redir']) ?
dcCore::app()->admin->list->getURL() . '#plugins' : $_REQUEST['redir']; dcCore::app()->admin->__get('list')->getURL() . '#plugins' : $_REQUEST['redir'];
# -- Get settings -- # -- 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); $fac_formats = json_decode($s->get('formats'), true);
@ -54,425 +82,59 @@ 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;
}
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; $i = 1;
foreach ($fac_formats as $uid => $f) { foreach ($fac_formats as $uid => $format) {
if (empty($f['name'])) { if (empty($format['name'])) {
continue; continue;
} }
echo ' self::displayFacFormat(sprintf(__('Format %s'), $i), $uid, $format);
<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>';
$i++; $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 ' 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"> <div class="fieldset">
<h4>' . __('Informations') . '</h4> <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"> </div><div class="two-boxes">
<h5>' . __('Structure') . '</h5> <h5>' . __('Structure') . '</h5>
<pre>' . html::escapeHTML(' <pre>' . Html::escapeHTML('
<div class="post-fac"> <div class="post-fac">
<h3>' . __('Title of feed') . '</h3> <h3>' . __('Title of feed') . '</h3>
<p>' . __('Description of feed') . '</p> <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>'; </div>';
dcPage::helpBlock('fac'); 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 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;
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) { public static function process(): bool
dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); {
if (!static::$init || !dcCore::app()->blog->settings->get(My::id())->get('active')) {
return false;
}
# Not active or not a post dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, context $_ctx): void {
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active') # Not a post
|| !dcCore::app()->ctx->exists('posts')) { if (!dcCore::app()->ctx->exists('posts')) {
return null; 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,
@ -217,3 +237,7 @@ dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, co
} }
echo '</dl></div>'; echo '</dl></div>';
}); });
return true;
}
}

View File

@ -10,8 +10,29 @@
* @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;
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 // Module specs
@ -81,41 +102,11 @@ $mod_conf = [
// 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
); );
} }
return true;
} catch (Exception $e) { } catch (Exception $e) {
dcCore::app()->error->add($e->getMessage()); 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'));
}
}