From 90905e502404a533692e16dbf4af800a802fc5d7 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sun, 26 Mar 2023 20:49:41 +0200 Subject: [PATCH] use namespace --- src/Backend.php | 48 +++ src/BackendBehaviors.php | 251 +++++++------- src/Config.php | 732 ++++++++++++++------------------------- src/Frontend.php | 436 ++++++++++++----------- src/Install.php | 241 +++++++------ src/My.php | 36 ++ 6 files changed, 838 insertions(+), 906 deletions(-) create mode 100644 src/Backend.php create mode 100644 src/My.php diff --git a/src/Backend.php b/src/Backend.php new file mode 100644 index 0000000..57bdc4d --- /dev/null +++ b/src/Backend.php @@ -0,0 +1,48 @@ +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; + } +} diff --git a/src/BackendBehaviors.php b/src/BackendBehaviors.php index aac7174..e12df5b 100644 --- a/src/BackendBehaviors.php +++ b/src/BackendBehaviors.php @@ -10,35 +10,47 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); +namespace Dotclear\Plugin\fac; -# Admin behaviors -dcCore::app()->addBehavior('adminBlogPreferencesFormV2', ['facAdmin', 'adminBlogPreferencesFormV2']); -dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', ['facAdmin', 'adminBeforeBlogSettingsUpdate']); -dcCore::app()->addBehavior('adminPostHeaders', ['facAdmin', 'adminPostHeaders']); -dcCore::app()->addBehavior('adminPostFormItems', ['facAdmin', 'adminPostFormItems']); -dcCore::app()->addBehavior('adminAfterPostCreate', ['facAdmin', 'adminAfterPostSave']); -dcCore::app()->addBehavior('adminAfterPostUpdate', ['facAdmin', 'adminAfterPostSave']); -dcCore::app()->addBehavior('adminBeforePostDelete', ['facAdmin', 'adminBeforePostDelete']); -dcCore::app()->addBehavior('adminPostsActions', ['facAdmin', 'adminPostsActions']); +use cursor; +use ArrayObject; +use dcAuth; +use dcCore; +use dcPage; +use dcPostsActions; +use dcRecord; +use dcSettings; +use Dotclear\Helper\Html\Html; +use Dotclear\Helper\Html\Form\{ + Checkbox, + Div, + Form, + Hidden, + Input, + Label, + Note, + Para, + Select, + Submit, + Text +}; +use Exception; /** * @ingroup DC_PLUGIN_FAC * @brief Linked feed to entries - admin methods. * @since 2.6 */ -class facAdmin +class BackendBehaviors { /** * Get combos of types of supported public pages * * @return array List of post type and name */ - public static function getPostsTypes() + public static function getPostsTypes(): array { $types = [ __('home page') => 'default', @@ -48,8 +60,8 @@ class facAdmin __('category pages') => 'category', __('entries feed') => 'feed', ]; - if (dcCore::app()->plugins->moduleExists('muppet')) { - foreach (muppet::getPostTypes() as $k => $v) { + if (dcCore::app()->plugins->moduleExists('muppet') && class_exists('\muppet')) { + foreach (\muppet::getPostTypes() as $k => $v) { $types[sprintf( __('"%s" pages from extension muppet'), $v['name'] @@ -65,8 +77,20 @@ class facAdmin * * @param dcSettings $blog_settings dcSettings instance */ - public static function adminBlogPreferencesFormV2(dcSettings $blog_settings) + public static function adminBlogPreferencesFormV2(dcSettings $blog_settings): void { + $lines = ''; + $fac_public_tpltypes = json_decode($blog_settings->get(My::id())->get('public_tpltypes'), true); + if (!is_array($fac_public_tpltypes)) { + $fac_public_tpltypes = []; + } + foreach (self::getPostsTypes() as $k => $v) { + $lines .= (new Para())->items([ + (new Checkbox(['fac_public_tpltypes[]', 'fac_public_tpltypes' . $k], in_array($v, $fac_public_tpltypes)))->value($v), + (new Label(__($k), Label::OUTSIDE_LABEL_AFTER))->for('fac_public_tpltypes' . $k)->class('classic'), + ])->render(); + } + echo '

Feed after content

' . '

' . @@ -74,49 +98,40 @@ class facAdmin '

'; if (dcCore::app()->auth->isSuperAdmin()) { echo '

' . __('Configure formats') . '

'; } - echo - '
' . - '
' . - '
' . __('Activation') . '
' . - '

' . - '

' . - __('You can manage related feed to display for each post with a predefined format.') . - '

' . - '
' . __('Feed') . '
' . - '

' . - form::field('fac_defaultfeedtitle', 65, 255, (string) $blog_settings->get(basename(__DIR__))->get('defaultfeedtitle')) . '

' . - '

' . __('Use %T to insert title of feed.') . '

' . - '

' . - '
' . - '
' . - '
' . __('Show feed after content on:') . '
'; - - $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 ' -

'; - } echo - '
' . - '
' . + (new Div())->class('two-cols')->items([ + (new Div())->class('col')->items([ + (new Text('h5', Html::escapeHTML(__('Activation')))), + // active + (new Para())->items([ + (new Checkbox('fac_active', (bool) $blog_settings->get(My::id())->get('active')))->value(1), + (new Label(__('Enable "fac" extension'), Label::OUTSIDE_LABEL_AFTER))->for('fac_active')->class('classic'), + ]), + (new Note())->text(__('You can manage related feed to display for each post with a predefined format.'))->class('form-note'), + (new Text('h5', Html::escapeHTML(__('Feed')))), + // defaultfeedtitle + (new Para())->items([ + (new Label(__('Default title')))->for('fac_defaultfeedtitle'), + (new Input('fac_defaultfeedtitle'))->size(70)->maxlenght(255)->value((string) $blog_settings->get(My::id())->get('defaultfeedtitle')), + ]), + (new Note())->text(__('Use %T to insert title of feed.'))->class('form-note'), + // showfeeddesc + (new Para())->items([ + (new Checkbox('fac_showfeeddesc', (bool) $blog_settings->get(My::id())->get('showfeeddesc')))->value(1), + (new Label(__('Show description of feed'), Label::OUTSIDE_LABEL_AFTER))->for('fac_showfeeddesc')->class('classic'), + ]), + ]), + (new Div())->class('col')->items([ + (new Text('h5', Html::escapeHTML(__('Show feed after content on:')))), + (new Text('', $lines)), + ]), + ])->render() . '
' . '
'; } @@ -126,12 +141,12 @@ class facAdmin * * @param dcSettings $blog_settings dcSettings instance */ - public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings) + public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings): void { - $blog_settings->get(basename(__DIR__))->put('active', !empty($_POST['fac_active'])); - $blog_settings->get(basename(__DIR__))->put('public_tpltypes', json_encode($_POST['fac_public_tpltypes'])); - $blog_settings->get(basename(__DIR__))->put('defaultfeedtitle', (string) $_POST['fac_defaultfeedtitle']); - $blog_settings->get(basename(__DIR__))->put('showfeeddesc', !empty($_POST['fac_showfeeddesc'])); + $blog_settings->get(My::id())->put('active', !empty($_POST['fac_active'])); + $blog_settings->get(My::id())->put('public_tpltypes', json_encode($_POST['fac_public_tpltypes'])); + $blog_settings->get(My::id())->put('defaultfeedtitle', (string) $_POST['fac_defaultfeedtitle']); + $blog_settings->get(My::id())->put('showfeeddesc', !empty($_POST['fac_showfeeddesc'])); } /** @@ -139,9 +154,9 @@ class facAdmin * * @return string HTML head */ - public static function adminPostHeaders() + public static function adminPostHeaders(): string { - return dcPage::jsModuleLoad(basename(__DIR__) . '/js/admin.js'); + return dcPage::jsModuleLoad(My::id() . '/js/backend.js'); } /** @@ -149,12 +164,12 @@ class facAdmin * * @param ArrayObject $main_items Main items * @param ArrayObject $sidebar_items Sidebar items - * @param record $post Post record or null + * @param null|dcRecord $post Post record or null */ - public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, $post) + public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?dcRecord $post): void { - if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) { - return null; + if (!dcCore::app()->blog->settings->get(My::id())->get('active')) { + return; } # Get existing linked feed @@ -162,17 +177,17 @@ class facAdmin if ($post) { $rs = dcCore::app()->meta->getMetadata([ 'meta_type' => 'fac', - 'post_id' => $post->post_id, + 'post_id' => $post->f('post_id'), 'limit' => 1, ]); - $fac_url = $rs->isEmpty() ? '' : $rs->meta_id; + $fac_url = $rs->isEmpty() ? '' : $rs->f('meta_id'); $rs = dcCore::app()->meta->getMetadata([ 'meta_type' => 'facformat', - 'post_id' => $post->post_id, + 'post_id' => $post->f('post_id'), 'limit' => 1, ]); - $fac_format = $rs->isEmpty() ? '' : $rs->meta_id; + $fac_format = $rs->isEmpty() ? '' : $rs->f('meta_id'); } # Set linked feed form items @@ -185,11 +200,11 @@ class facAdmin * @param cursor $cur Current post cursor * @param integer $post_id Post id */ - public static function adminAfterPostSave(cursor $cur, $post_id) + public static function adminAfterPostSave(cursor $cur, int $post_id): void { if (!isset($_POST['fac_url']) || !isset($_POST['fac_format'])) { - return null; + return; } # Delete old linked feed @@ -204,7 +219,7 @@ class facAdmin * * @param integer $post_id Post id */ - public static function adminBeforePostDelete($post_id) + public static function adminBeforePostDelete(int $post_id): void { self::delFeed($post_id); } @@ -214,26 +229,26 @@ class facAdmin * * @param dcPostsActions $pa dcPostsActionsPage instance */ - public static function adminPostsActions(dcPostsActions $pa) + public static function adminPostsActions(dcPostsActions $pa): void { - if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) { - return null; + if (!dcCore::app()->blog->settings->get(My::id())->get('active')) { + return; } $pa->addAction( [__('Linked feed') => [__('Add feed') => 'fac_add']], - ['facAdmin', 'callbackAdd'] + [self::class, 'callbackAdd'] ); if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ dcAuth::PERMISSION_DELETE, dcAuth::PERMISSION_CONTENT_ADMIN, ]), dcCore::app()->blog->id)) { - return null; + return; } $pa->addAction( [__('Linked feed') => [__('Remove feed') => 'fac_remove']], - ['facAdmin', 'callbackRemove'] + [self::class, 'callbackRemove'] ); } @@ -243,7 +258,7 @@ class facAdmin * @param dcPostsActions $pa dcPostsActions instance * @param ArrayObject $post _POST actions */ - public static function callbackRemove(dcPostsActions $pa, ArrayObject $post) + public static function callbackRemove(dcPostsActions $pa, ArrayObject $post): void { # No entry $posts_ids = $pa->getIDs(); @@ -274,7 +289,7 @@ class facAdmin * @param dcPostsActions $pa dcPostsActions instance * @param ArrayObject $post _POST actions */ - public static function callbackAdd(dcPostsActions $pa, ArrayObject $post) + public static function callbackAdd(dcPostsActions $pa, ArrayObject $post): void { # No entry $posts_ids = $pa->getIDs(); @@ -297,24 +312,21 @@ class facAdmin } else { $pa->beginPage( dcPage::breadcrumb([ - html::escapeHTML(dcCore::app()->blog->name) => '', + Html::escapeHTML(dcCore::app()->blog->name) => '', $pa->getCallerTitle() => $pa->getRedirection(true), __('Linked feed to this selection') => '', ]) ); echo - '
' . - $pa->getCheckboxes() . - - self::formFeed() . - - '

' . - dcCore::app()->formNonce() . - $pa->getHiddenFields() . - form::hidden(['action'], 'fac_add') . - '

' . - '
'; + (new Form('fac_form'))->action($pa->getURI())->method('post')->fields([ + (new Text('', $pa->getCheckboxes() . self::formFeed())), + (new Para())->items([ + (new Text('', dcCore::app()->formNonce() . $pa->getHiddenFields())), + (new Hidden(['action'], 'fac_add')), + (new Submit(['save']))->value(__('Save')), + ]), + ])->render(); $pa->endPage(); } @@ -325,36 +337,29 @@ class facAdmin * * @param string $url Feed URL * @param string $format Feed format - * @return null|string Feed form content + * @return string Feed form content */ - protected static function formFeed($url = '', $format = '') + protected static function formFeed(string $url = '', string $format = ''): string { - if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) { - return null; + if (!dcCore::app()->blog->settings->get(My::id())->get('active')) { + return ''; } return - '
' . - '
' . __('Linked feed') . '
' . - '

' . - form::field( - 'fac_url', - 60, - 255, - $url, - 'maximal' - ) . '

' . - '

' . - form::combo( - 'fac_format', - self::comboFac(), - $format, - 'maximal' - ) . '

' . - ($url ? '

' . __('view feed') . '

' : '') . - '
'; + (new Div('fac'))->items([ + (new Text('h5', __('Linked feed'))), + // fac_url + (new Para())->items([ + (new Label(__('Feed URL:')))->for('fac_url')->class('required'), + (new Input('fac_url'))->size(60)->maxlenght(255)->value($url), + ]), + // fac_format + (new Para())->items([ + (new Label(__('Format:')))->for('fac_format'), + (new Select('fac_format'))->default($format)->items(self::comboFac()), + ]), + (new Text('', $url ? '

' . __('view feed') . '

' : '')), + ])->render(); } /** @@ -362,9 +367,9 @@ class facAdmin * * @return array List of fac formats */ - protected static function comboFac() + protected static function comboFac(): array { - $formats = json_decode(dcCore::app()->blog->settings->get(basename(__DIR__))->get('formats'), true); + $formats = json_decode(dcCore::app()->blog->settings->get(My::id())->get('formats'), true); if (!is_array($formats) || empty($formats)) { return []; } @@ -382,7 +387,7 @@ class facAdmin * * @param integer $post_id Post id */ - protected static function delFeed($post_id) + protected static function delFeed(int $post_id): void { $post_id = (int) $post_id; dcCore::app()->meta->delPostMeta($post_id, 'fac'); @@ -395,11 +400,11 @@ class facAdmin * @param integer $post_id Post id * @param array|ArrayObject $options Feed options */ - protected static function addFeed($post_id, $options) + protected static function addFeed(int $post_id, array|ArrayObject $options): void { if (empty($options['fac_url']) || empty($options['fac_format'])) { - return null; + return; } $post_id = (int) $post_id; diff --git a/src/Config.php b/src/Config.php index ce3e0d7..c6e8f8f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -10,496 +10,298 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_MODULE')) { - return null; -} +declare(strict_types=1); -if (!dcCore::app()->auth->isSuperAdmin()) { - return null; -} +namespace Dotclear\Plugin\fac; -$redir = empty($_REQUEST['redir']) ? - dcCore::app()->admin->list->getURL() . '#plugins' : $_REQUEST['redir']; +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; -# -- Get settings -- -$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); +class Config extends dcNsProcess +{ + 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)) { - $fac_formats = []; -} + public static function process(): bool + { + if (!static::$init) { + return false; + } -# -- Set settings -- -if (!empty($_POST['save'])) { - try { - $fac_formats = []; + $redir = empty($_REQUEST['redir']) ? + dcCore::app()->admin->__get('list')->getURL() . '#plugins' : $_REQUEST['redir']; - foreach ($_POST['fac_formats'] as $uid => $f) { - if (!empty($f['name'])) { - $fac_formats[$uid] = $f; + # -- 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 = []; + } + + # -- Set settings -- + if (!empty($_POST['save'])) { + try { + $fac_formats = []; + + foreach ($_POST['fac_formats'] as $uid => $f) { + if (!empty($f['name'])) { + $fac_formats[$uid] = $f; + } + } + + // fix 2021.08.21 : formats are now global + $s->drop('formats'); + $s->put( + 'formats', + json_encode($fac_formats), + 'string', + 'Formats of feeds contents', + true, + true + ); + + dcCore::app()->blog->triggerBlog(); + + dcPage::addSuccessNotice( + __('Configuration successfully updated.') + ); + dcCore::app()->adminurl?->redirect( + 'admin.plugins', + ['module' => My::id(), 'conf' => 1, 'redir' => dcCore::app()->admin->__get('list')->getRedir()] + ); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); } } - // fix 2021.08.21 : formats are now global - $s->drop('formats'); - $s->put( - 'formats', - json_encode($fac_formats), - 'string', - 'Formats of feeds contents', - true, - true - ); - - dcCore::app()->blog->triggerBlog(); - - dcAdminNotices::addSuccessNotice( - __('Configuration successfully updated.') - ); - http::redirect( - dcCore::app()->admin->list->getURL('module=' . basename(__DIR__) . '&conf=1&redir=' . dcCore::app()->admin->list->getRedir()) - ); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - } -} - -# -- Display form -- - -$i = 1; -foreach ($fac_formats as $uid => $f) { - if (empty($f['name'])) { - continue; + return true; } - echo ' -
-

' . sprintf(__('Format %s'), $i) . '

+ public static function render(): void + { + if (!static::$init) { + return; + } + $s = dcCore::app()->blog->settings->get(My::id()); -
' . __('General') . '
+ $fac_formats = json_decode($s->get('formats'), true); -

' . - form::field( - [ - 'fac_formats[' . $uid . '][name]', - 'fac_formats_' . $uid . '_name', - ], - 20, - 255, - empty($f['name']) ? '' : $f['name'], - 'maximal' - ) . '

-

' . - __('In order to remove a format, leave its name empty.') . - '

+ $i = 1; + foreach ($fac_formats as $uid => $format) { + if (empty($format['name'])) { + continue; + } -

' . - form::field( - [ - 'fac_formats[' . $uid . '][dateformat]', - 'fac_formats_' . $uid . '_dateformat', - ], - 20, - 255, - empty($f['dateformat']) ? '' : $f['dateformat'], - 'maximal' - ) . '

-

' . - __('Use date format of Dotclear or leave empty to use default date format of blog.') . - '

+ self::displayFacFormat(sprintf(__('Format %s'), $i), $uid, $format); -

' . - form::field( - [ - 'fac_formats[' . $uid . '][lineslimit]', - 'fac_formats_' . $uid . '_lineslimit', - ], - 5, - 4, - empty($f['lineslimit']) ? '' : $f['lineslimit'], - 'maximal' - ) . '

-

' . - __('Leave lengh empty for no limit.') . - '

+ $i++; + } -
' . __('Title') . '
+ $new_format = [ + 'name' => '', + 'dateformat' => '', + 'lineslimit' => '5', + 'linestitletext' => '%T', + 'linestitleover' => '%D', + 'linestitlelength' => '150', + 'showlinesdescription' => '0', + 'linesdescriptionlength' => '350', + 'linesdescriptionnohtml' => '1', + 'showlinescontent' => '0', + 'linescontentlength' => '350', + 'linescontentnohtml' => '1', + ]; -

' . - form::field( - [ - 'fac_formats[' . $uid . '][linestitletext]', - 'fac_formats_' . $uid . '_linestitletext', - ], - 20, - 255, - empty($f['linestitletext']) ? '' : $f['linestitletext'], - 'maximal' - ) . '

-

' . - __('Format can be:') . - '%D : ' . __('Date') . - ', %T : ' . __('Title') . - ', %A : ' . __('Author') . - ', %E : ' . __('Description') . - ', %C : ' . __('Content') . - '

+ self::displayFacFormat(__('New format'), uniqid(), $new_format); -

' . - form::field( - [ - 'fac_formats[' . $uid . '][linestitleover]', - 'fac_formats_' . $uid . '_linestitleover', - ], - 20, - 255, - empty($f['linestitleover']) ? '' : $f['linestitleover'], - 'maximal' - ) . '

-

' . - __('Format can be:') . - '%D : ' . __('Date') . - ', %T : ' . __('Title') . - ', %A : ' . __('Author') . - ', %E : ' . __('Description') . - ', %C : ' . __('Content') . - '

+ echo ' +
+

' . __('Informations') . '

-

' . - form::field( - [ - 'fac_formats[' . $uid . '][linestitlelength]', - 'fac_formats_' . $uid . '_linestitlelength', - ], - 5, - 4, - empty($f['linestitlelength']) ? '' : $f['linestitlelength'], - 'maximal' - ) . '

-

' . - __('Leave lengh empty for no limit.') . - '

+
-
' . __('Description') . '
+
' . __('Theme') . '
+

' . + __('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.') . + '

-

+
-

+
' . __('Structure') . '
+
' . Html::escapeHTML('
+            
+

' . __('Title of feed') . '

+

' . __('Description of feed') . '

+
+
' . __('Title of entry') . '
+
' . __('Description of entry') . '
+
+
+ ') . '
-

' . - form::field( - [ - 'fac_formats[' . $uid . '][linesdescriptionlength]', - 'fac_formats_' . $uid . '_linesdescriptionlength', - ], - 5, - 4, - empty($f['linesdescriptionlength']) ? '' : $f['linesdescriptionlength'], - 'maximal' - ) . '

-

' . - __('Leave lengh empty for no limit.') . - '

+
-
' . __('Content') . '
+
'; -

+ dcPage::helpBlock('fac'); + } -

- -

' . - form::field( - [ - 'fac_formats[' . $uid . '][linescontentlength]', - 'fac_formats_' . $uid . '_linescontentlength', - ], - 5, - 4, - empty($f['linescontentlength']) ? '' : $f['linescontentlength'], - 'maximal' - ) . '

-

' . - __('Leave lengh empty for no limit.') . - '

- -
- -
'; - - $i++; + 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(); + } } - -$uid = uniqid(); -echo ' -
-

' . __('New format') . '

- -
' . __('General') . '
- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][name]', - 'fac_formats_' . $uid . '_name', - ], - 20, - 255, - '', - 'maximal' -) . '

-

' . -__('In order to remove a format, leave its name empty.') . -'

- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][dateformat]', - 'fac_formats_' . $uid . '_dateformat', - ], - 20, - 255, - '', - 'maximal' -) . '

-

' . -__('Use date format of Dotclear or leave empty to use default date format of blog.') . -'

- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][lineslimit]', - 'fac_formats_' . $uid . '_lineslimit', - ], - 5, - 4, - 5, - 'maximal' -) . '

-

' . -__('Leave lengh empty for no limit.') . -'

- -
' . __('Title') . '
- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][linestitletext]', - 'fac_formats_' . $uid . '_linestitletext', - ], - 20, - 255, - '%T', - 'maximal' -) . '

-

' . -__('Format can be:') . -'%D : ' . __('Date') . -', %T : ' . __('Title') . -', %A : ' . __('Author') . -', %E : ' . __('Description') . -', %C : ' . __('Content') . -'

- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][linestitleover]', - 'fac_formats_' . $uid . '_linestitleover', - ], - 20, - 255, - '%D', - 'maximal' -) . '

-

' . -__('Format can be:') . -'%D : ' . __('Date') . -', %T : ' . __('Title') . -', %A : ' . __('Author') . -', %E : ' . __('Description') . -', %C : ' . __('Content') . -'

- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][linestitlelength]', - 'fac_formats_' . $uid . '_linestitlelength', - ], - 5, - 4, - 150, - 'maximal' -) . '

-

' . -__('Leave lengh empty for no limit.') . -'

- -
' . __('Description') . '
- -

- -

- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][linesdescriptionlength]', - 'fac_formats_' . $uid . '_linesdescriptionlength', - ], - 5, - 4, - 350, - 'maximal' -) . '

-

' . -__('Leave lengh empty for no limit.') . -'

- -
' . __('Content') . '
- -

- -

- -

' . -form::field( - [ - 'fac_formats[' . $uid . '][linescontentlength]', - 'fac_formats_' . $uid . '_linescontentlength', - ], - 5, - 4, - 350, - 'maximal' -) . '

-

' . -__('Leave lengh empty for no limit.') . -'

- -
- -
- -
-

' . __('Informations') . '

- -
- -
' . __('Theme') . '
-

' . -__('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.') . -'

- -
- -
' . __('Structure') . '
-
' . html::escapeHTML('
-
-

' . __('Title of feed') . '

-

' . __('Description of feed') . '

-
-
' . __('Title of entry') . '
-
' . __('Description of entry') . '
-
-
-') . '
- -
- -
'; - -dcPage::helpBlock('fac'); diff --git a/src/Frontend.php b/src/Frontend.php index 091b216..00af701 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -10,210 +10,234 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; +declare(strict_types=1); + +namespace Dotclear\Plugin\fac; + +use context; +use dcCore; +use dcNsProcess; +use Dotclear\Helper\Html\Html; +use Exception; +use dt; +use feedReader; + +class Frontend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_RC_PATH'); + + return static::$init; + } + + 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 + $types = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get('public_tpltypes'), true); + if (!is_array($types) + || !in_array(dcCore::app()->url->type, $types)) { + return; + } + + # Get related feed + $fac_url = dcCore::app()->meta->getMetadata([ + 'meta_type' => 'fac', + 'post_id' => dcCore::app()->ctx->__get('posts')->f('post_id'), + 'limit' => 1, + ]); + if ($fac_url->isEmpty()) { + return; + } + + # Get related format + $fac_format = dcCore::app()->meta->getMetadata([ + 'meta_type' => 'facformat', + 'post_id' => dcCore::app()->ctx->__get('posts')->f('post_id'), + 'limit' => 1, + ]); + if ($fac_format->isEmpty()) { + return; + } + + # Get format info + $default_format = [ + 'name' => 'default', + 'dateformat' => '', + 'lineslimit' => '5', + 'linestitletext' => '%T', + 'linestitleover' => '%D', + 'linestitlelength' => '150', + 'showlinesdescription' => '0', + 'linesdescriptionlength' => '350', + 'linesdescriptionnohtml' => '1', + 'showlinescontent' => '0', + 'linescontentlength' => '350', + 'linescontentnohtml' => '1', + ]; + + $formats = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get('formats'), true); + if (empty($formats) + || !is_array($formats) + || !isset($formats[$fac_format->f('meta_id')])) { + $format = $default_format; + } else { + $format = array_merge( + $default_format, + $formats[$fac_format->f('meta_id')] + ); + } + + # Read feed url + $cache = is_dir(DC_TPL_CACHE . '/fac') ? DC_TPL_CACHE . '/fac' : null; + + try { + $feed = feedReader::quickParse($fac_url->f('meta_id'), $cache); + } catch (Exception $e) { + $feed = null; + } + + # No entries + if (!$feed) { + return; + } + + # Feed title + $feedtitle = ''; + if ('' != dcCore::app()->blog->settings->get(My::id())->get('defaultfeedtitle')) { + $feedtitle = '

' . Html::escapeHTML( + empty($feed->title) ? + str_replace( + '%T', + __('a related feed'), + dcCore::app()->blog->settings->get(My::id())->get('defaultfeedtitle') + ) : + str_replace( + '%T', + $feed->title, + dcCore::app()->blog->settings->get(My::id())->get('defaultfeedtitle') + ) + ) . '

'; + } + + # Feed desc + $feeddesc = ''; + if (dcCore::app()->blog->settings->get(My::id())->get('showfeeddesc') + && '' != $feed->description) { + $feeddesc = '

' . context::global_filters( + $feed->description, + ['encode_xml', 'remove_html'] + ) . '

'; + } + + # Date format + $dateformat = '' != $format['dateformat'] ? + $format['dateformat'] : + dcCore::app()->blog->settings->get('system')->get('date_format') . ',' . dcCore::app()->blog->settings->get('system')->get('time_format'); + + # Enrties limit + $entrieslimit = abs((int) $format['lineslimit']); + $uselimit = $entrieslimit > 0 ? true : false; + + echo + '
' . + $feedtitle . $feeddesc . + '
'; + + $i = 0; + foreach ($feed->items as $item) { + # Format date + $date = dt::dt2str($dateformat, $item->pubdate); + + # Entries title + $title = context::global_filters( + str_replace( + [ + '%D', + '%T', + '%A', + '%E', + '%C', + ], + [ + $date, + $item->title, + $item->creator, + $item->description, + $item->content, + ], + $format['linestitletext'] + ), + ['remove_html', 'cut_string' => abs((int) $format['linestitlelength'])], + ); + + # Entries over title + $overtitle = context::global_filters( + str_replace( + [ + '%D', + '%T', + '%A', + '%E', + '%C', + ], + [ + $date, + $item->title, + $item->creator, + $item->description, + $item->content, + ], + $format['linestitleover'] + ), + ['remove_html', 'cut_string' => 350], + ); + + # Entries description + $description = ''; + if ($format['showlinesdescription'] + && '' != $item->description) { + $description = '
' . + context::global_filters( + $item->description, + ['remove_html' => (int) $format['linesdescriptionnohtml'], 'cut_string' => abs((int) $format['linesdescriptionlength'])] + ) . '
'; + } + + # Entries content + $content = ''; + if ($format['showlinescontent'] + && '' != $item->content) { + $content = '
' . + context::global_filters( + $item->content, + ['remove_html' => (int) $format['linescontentnohtml'], 'cut_string' => abs((int) $format['linescontentlength'])] + ) . '
'; + } + + echo + '
' . $title . '
' . + $description . $content; + + $i++; + if ($uselimit && $i == $entrieslimit) { + break; + } + } + echo '
'; + }); + + return true; + } } - -dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, context $_ctx) { - dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - - # Not active or not a post - if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active') - || !dcCore::app()->ctx->exists('posts')) { - return null; - } - - # Not in page to show - $types = json_decode((string) dcCore::app()->blog->settings->get(basename(__DIR__))->get('public_tpltypes'), true); - if (!is_array($types) - || !in_array(dcCore::app()->url->type, $types)) { - return null; - } - - # Get related feed - $fac_url = dcCore::app()->meta->getMetadata([ - 'meta_type' => 'fac', - 'post_id' => dcCore::app()->ctx->posts->post_id, - 'limit' => 1, - ]); - if ($fac_url->isEmpty()) { - return null; - } - - # Get related format - $fac_format = dcCore::app()->meta->getMetadata([ - 'meta_type' => 'facformat', - 'post_id' => dcCore::app()->ctx->posts->post_id, - 'limit' => 1, - ]); - if ($fac_format->isEmpty()) { - return null; - } - - # Get format info - $default_format = [ - 'name' => 'default', - 'dateformat' => '', - 'lineslimit' => '5', - 'linestitletext' => '%T', - 'linestitleover' => '%D', - 'linestitlelength' => '150', - 'showlinesdescription' => '0', - 'linesdescriptionlength' => '350', - 'linesdescriptionnohtml' => '1', - 'showlinescontent' => '0', - 'linescontentlength' => '350', - 'linescontentnohtml' => '1', - ]; - - $formats = json_decode((string) dcCore::app()->blog->settings->get(basename(__DIR__))->get('formats'), true); - if (empty($formats) - || !is_array($formats) - || !isset($formats[$fac_format->meta_id])) { - $format = $default_format; - } else { - $format = array_merge( - $default_format, - $formats[$fac_format->meta_id] - ); - } - - # Read feed url - $cache = is_dir(DC_TPL_CACHE . '/fac') ? DC_TPL_CACHE . '/fac' : null; - - try { - $feed = feedReader::quickParse($fac_url->meta_id, $cache); - } catch (Exception $e) { - $feed = null; - } - - # No entries - if (!$feed) { - return null; - } - - # Feed title - $feedtitle = ''; - if ('' != dcCore::app()->blog->settings->get(basename(__DIR__))->get('defaultfeedtitle')) { - $feedtitle = '

' . html::escapeHTML( - empty($feed->title) ? - str_replace( - '%T', - __('a related feed'), - dcCore::app()->blog->settings->get(basename(__DIR__))->get('defaultfeedtitle') - ) : - str_replace( - '%T', - $feed->title, - dcCore::app()->blog->settings->get(basename(__DIR__))->get('defaultfeedtitle') - ) - ) . '

'; - } - - # Feed desc - $feeddesc = ''; - if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('showfeeddesc') - && '' != $feed->description) { - $feeddesc = '

' . context::global_filters( - $feed->description, - ['encode_xml', 'remove_html'] - ) . '

'; - } - - # Date format - $dateformat = '' != $format['dateformat'] ? - $format['dateformat'] : - dcCore::app()->blog->settings->get('system')->get('date_format') . ',' . dcCore::app()->blog->settings->get('system')->get('time_format'); - - # Enrties limit - $entrieslimit = abs((int) $format['lineslimit']); - $uselimit = $entrieslimit > 0 ? true : false; - - echo - '
' . - $feedtitle . $feeddesc . - '
'; - - $i = 0; - foreach ($feed->items as $item) { - # Format date - $date = dt::dt2str($dateformat, $item->pubdate); - - # Entries title - $title = context::global_filters( - str_replace( - [ - '%D', - '%T', - '%A', - '%E', - '%C', - ], - [ - $date, - $item->title, - $item->creator, - $item->description, - $item->content, - ], - $format['linestitletext'] - ), - ['remove_html', 'cut_string' => abs((int) $format['linestitlelength'])], - ); - - # Entries over title - $overtitle = context::global_filters( - str_replace( - [ - '%D', - '%T', - '%A', - '%E', - '%C', - ], - [ - $date, - $item->title, - $item->creator, - $item->description, - $item->content, - ], - $format['linestitleover'] - ), - ['remove_html', 'cut_string' => 350], - ); - - # Entries description - $description = ''; - if ($format['showlinesdescription'] - && '' != $item->description) { - $description = '
' . - context::global_filters( - $item->description, - ['remove_html' => (int) $format['linesdescriptionnohtml'], 'cut_string' => abs((int) $format['linesdescriptionlength'])] - ) . '
'; - } - - # Entries content - $content = ''; - if ($format['showlinescontent'] - && '' != $item->content) { - $content = '
' . - context::global_filters( - $item->content, - ['remove_html' => (int) $format['linescontentnohtml'], 'cut_string' => abs((int) $format['linescontentlength'])] - ) . '
'; - } - - echo - '
' . $title . '
' . - $description . $content; - - $i++; - if ($uselimit && $i == $entrieslimit) { - break; - } - } - echo '
'; -}); diff --git a/src/Install.php b/src/Install.php index 74fe4ba..fa2ff15 100644 --- a/src/Install.php +++ b/src/Install.php @@ -10,124 +10,141 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -// Module specs -$mod_conf = [ - [ - 'active', - 'Enabled fac plugin', - false, - 'boolean', - ], - [ - 'public_tpltypes', - 'List of templates types which used fac', - json_encode(['post', 'tag', 'archive']), - 'string', - ], - [ - 'formats', - 'Formats of feeds contents', - json_encode([ - uniqid() => [ - 'name' => 'default', - 'dateformat' => '', - 'lineslimit' => '5', - 'linestitletext' => '%T', - 'linestitleover' => '%D', - 'linestitlelength' => '150', - 'showlinesdescription' => '0', - 'linesdescriptionlength' => '350', - 'linesdescriptionnohtml' => '1', - 'showlinescontent' => '0', - 'linescontentlength' => '350', - 'linescontentnohtml' => '1', - ], - uniqid() => [ - 'name' => 'full', - 'dateformat' => '', - 'lineslimit' => '20', - 'linestitletext' => '%T', - 'linestitleover' => '%D - %E', - 'linestitlelength' => '', - 'showlinesdescription' => '1', - 'linesdescriptionlength' => '', - 'linesdescriptionnohtml' => '1', - 'showlinescontent' => '1', - 'linescontentlength' => '', - 'linescontentnohtml' => '1', - ], - ]), - 'string', - false, - true, - ], - [ - 'defaultfeedtitle', - 'Default title of feed', - '%T', - 'string', - ], - [ - 'showfeeddesc', - 'Show description of feed', - 1, - 'boolean', - ], -]; +namespace Dotclear\Plugin\fac; -// Nothing to change below -try { - // Check module version - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; +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; } - // 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) . "' ")) + public static function process(): bool + { + if (!static::$init) { + return false; + } + + // Module specs + $mod_conf = [ + [ + 'active', + 'Enabled fac plugin', + false, + 'boolean', + ], + [ + 'public_tpltypes', + 'List of templates types which used fac', + json_encode(['post', 'tag', 'archive']), + 'string', + ], + [ + 'formats', + 'Formats of feeds contents', + json_encode([ + uniqid() => [ + 'name' => 'default', + 'dateformat' => '', + 'lineslimit' => '5', + 'linestitletext' => '%T', + 'linestitleover' => '%D', + 'linestitlelength' => '150', + 'showlinesdescription' => '0', + 'linesdescriptionlength' => '350', + 'linesdescriptionnohtml' => '1', + 'showlinescontent' => '0', + 'linescontentlength' => '350', + 'linescontentnohtml' => '1', + ], + uniqid() => [ + 'name' => 'full', + 'dateformat' => '', + 'lineslimit' => '20', + 'linestitletext' => '%T', + 'linestitleover' => '%D - %E', + 'linestitlelength' => '', + 'showlinesdescription' => '1', + 'linesdescriptionlength' => '', + 'linesdescriptionnohtml' => '1', + 'showlinescontent' => '1', + 'linescontentlength' => '', + 'linescontentnohtml' => '1', + ], + ]), + 'string', + false, + true, + ], + [ + 'defaultfeedtitle', + 'Default title of feed', + '%T', + 'string', + ], + [ + 'showfeeddesc', + 'Show description of feed', + 1, + 'boolean', + ], + ]; + + // Nothing to change below + try { + self::growUp(); + + // Set module settings + foreach ($mod_conf as $v) { + dcCore::app()->blog->settings->get(My::id())->put( + $v[0], + $v[2], + $v[3], + $v[1], + false, + true ); } + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + 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')) . "' ")) + ); + } + } } } - - // Set module settings - dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - foreach ($mod_conf as $v) { - dcCore::app()->blog->settings->get(basename(__DIR__))->put( - $v[0], - $v[2], - $v[3], - $v[1], - false, - true - ); - } - - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - - return false; } diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..72981fb --- /dev/null +++ b/src/My.php @@ -0,0 +1,36 @@ +plugins->moduleInfo(self::id(), 'name')); + } +}