From 3344f71dfd981cd3db230e61d6cfddf219affcc3 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Wed, 7 Dec 2022 21:58:18 +0100 Subject: [PATCH] code review --- _admin.php | 35 +++++--- _define.php | 14 ++- _init.php | 12 ++- _prepend.php | 12 ++- _public.php | 12 ++- inc/class.templator.media.php | 30 ++++--- inc/class.templator.pager.php | 94 ++++++++++---------- inc/class.templator.php | 30 ++++--- index.php | 158 ++++++++++++++++++---------------- 9 files changed, 235 insertions(+), 162 deletions(-) diff --git a/_admin.php b/_admin.php index 10e8ede..16af95b 100644 --- a/_admin.php +++ b/_admin.php @@ -1,5 +1,15 @@ auth->check(dcCore::app()->auth->makePermissions([ dcCore::app()->addBehavior('adminAfterPageCreate', ['templatorBehaviors','adminBeforePostUpdate']); dcCore::app()->addBehavior('adminBeforePageUpdate', ['templatorBehaviors','adminBeforePostUpdate']); - dcCore::app()->addBehavior('adminPostsActions', ['templatorBehaviors','adminPostsActions']); - dcCore::app()->addBehavior('adminPagesActions', ['templatorBehaviors','adminPostsActions']); + dcCore::app()->addBehavior('adminPostsActionsV2', ['templatorBehaviors','adminPostsActionsV2']); + dcCore::app()->addBehavior('adminPagesActionsV2', ['templatorBehaviors','adminPostsActionsV2']); dcCore::app()->addBehavior('adminFiltersListsV2', function (ArrayObject $sorts) { $sorts['templator'] = [ __('Templates engine'), [ - __('Date') => 'post_upddt', - __('Title') => 'post_title', - __('Category') => 'cat_id', + __('Date') => 'post_upddt', + __('Title') => 'post_title', + __('Category') => 'cat_id', ], 'post_upddt', 'desc', @@ -65,10 +75,9 @@ class templatorBehaviors $selected = $post_meta->isEmpty() ? '' : $post_meta->meta_id; } - $sidebar_items['options-box']['items']['templator'] = - '
' . + $sidebar_items['options-box']['items']['templator'] = '
' . '
' . __('Template') . '
' . - '

' . + '

' . form::combo('post_tpl', self::getTemplateCombo(), $selected) . '

' . '
'; } @@ -85,7 +94,7 @@ class templatorBehaviors } } - public static function adminPostsActions(dcPostsActions $pa) + public static function adminPostsActionsV2(dcPostsActions $pa) { $pa->addAction( [ @@ -114,8 +123,8 @@ class templatorBehaviors } } - dcAdminNotices::addSuccessNotice(__('Entries template updated.')); - $pa->redirect(true); + dcAdminNotices::addSuccessNotice(__('Entries template updated.')); + $pa->redirect(true); } catch (Exception $e) { dcCore::app()->error->add($e->getMessage()); } @@ -125,7 +134,7 @@ class templatorBehaviors dcPage::breadcrumb([ html::escapeHTML(dcCore::app()->blog->name) => '', $pa->getCallerTitle() => $pa->getRedirection(true), - __('Entry template') => '', + __('Entry template') => '', ]) ); diff --git a/_define.php b/_define.php index 60d1f7c..a6ad75c 100644 --- a/_define.php +++ b/_define.php @@ -1,11 +1,21 @@ registerModule( - 'Templator', + 'Templates engine', 'Create and select more templates for your posts', 'Osku and contributors', '1.4-dev', diff --git a/_init.php b/_init.php index 2e55c21..ad94697 100644 --- a/_init.php +++ b/_init.php @@ -1,5 +1,15 @@ ♦'; - $part = 'copy'; + $part = 'copy'; - if (preg_match('/^category-(.+)$/', $f->basename)) { - // That is ugly. - $cat_id = str_replace('category-', '', $f->basename); - $cat_id = str_replace('.html', '', $cat_id); - $cat_parents = dcCore::app()->blog->getCategoryParents($cat_id); + if (preg_match('/^category-(.+).html$/', $f->basename, $cat_id)) { + $cat_id = (int) $cat_id[1]; + $category = dcCore::app()->blog->getCategory($cat_id); $full_name = ''; + $cat_parents = dcCore::app()->blog->getCategoryParents($cat_id); while ($cat_parents->fetch()) { $full_name = $cat_parents->cat_title . ' › '; }; @@ -43,21 +42,20 @@ class templatorPager $params['cat_id'] = $cat_id; $params['post_type'] = ''; $icon = dcPage::getPF('templator/img/template-alt.png'); - $part = 'copycat';; + $part = 'copycat'; try { $counter = dcCore::app()->blog->getPosts($params, true); + if ($counter->f(0) == 0) { + $count = __('No entry'); + } elseif ($counter->f(0) == 1) { + $count = '' . $counter->f(0) . ' ' . __('entry') . ''; + } else { + $count = '' . $counter->f(0) . ' ' . __('entries') . ''; + } } catch (Exception $e) { dcCore::app()->error->add($e->getMessage()); } - - if ($counter->f(0) == 0) { - $count = __('No entry'); - } elseif ($counter->f(0) == 1) { - $count = '' . $counter->f(0) . ' ' . __('entry') . ''; - } else { - $count = '' . $counter->f(0) . ' ' . __('entries') . ''; - } } elseif (preg_match('/^widget-(.+)$/', $f->basename)) { $count = ' '; $icon = dcPage::getPF('templator/img/template-widget.png'); @@ -69,35 +67,35 @@ class templatorPager try { $counter = dcCore::app()->meta->getPostsByMeta($params, true); + $url = dcCore::app()->adminurl->get('admin.plugin.templator', [ + 'part' => 'posts', + 'file' => $fname, + 'redir' => dcCore::app()->adminurl->get('admin.plugin.templator', ['part' => 'files']), + ]); + if ($counter->f(0) == 0) { + $count = __('No entry'); + } elseif ($counter->f(0) == 1) { + $count = '' . $counter->f(0) . ' ' . __('entry') . ''; + } else { + $count = '' . $counter->f(0) . ' ' . __('entries') . ''; + } } catch (Exception $e) { dcCore::app()->error->add($e->getMessage()); } - $url = dcCore::app()->adminurl->get('admin.plugin.templator', [ - 'part' => 'posts', - 'file' => $fname, - 'redir' => dcCore::app()->adminurl->get('admin.plugin.templator', ['part' => 'files']), - ]); - if ($counter->f(0) == 0) { - $count = __('No entry'); - } elseif ($counter->f(0) == 1) { - $count = '' . $counter->f(0) . ' ' . __('entry') . ''; - } else { - $count = '' . $counter->f(0) . ' ' . __('entries') . ''; - } } $res = '
' . '' . '
    ' . '
  • ' . $fname . ' ' . $special . '
  • '; -/* - if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ - dcAuth::PERMISSION_CONTENT_ADMIN, - initTemplator::PERMISSION_TEMPLATOR, - ]), dcCore::app()->blog->id)) { - $details = ' - ' . __('details') . ''; - } -*/ + /* + if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + initTemplator::PERMISSION_TEMPLATOR, + ]), dcCore::app()->blog->id)) { + $details = ' - ' . __('details') . ''; + } + */ if (!$f->d) { $res .= '
  • ' . $count . '
  • ' . '
  • ' . @@ -109,12 +107,12 @@ class templatorPager $res .= '
  •  '; - $res .= '' . '' . __('copy') . ' '; if ($f->del) { - $res .= '' . '' . __('delete') . ''; } diff --git a/inc/class.templator.php b/inc/class.templator.php index 42fd5a8..d381968 100644 --- a/inc/class.templator.php +++ b/inc/class.templator.php @@ -1,5 +1,15 @@ file_get_contents($base), 'w' => $this->getDestinationFile($name) !== false, - 'f' => $f, ]; + ]; if (!$source['w']) { - throw new Exception(sprintf(__('File %s is not readable'), $source)); + throw new Exception(sprintf(__('File %s is not readable'), $name)); } if ($type == 'empty') { @@ -185,24 +195,20 @@ class dcTemplator public function copypasteTpl($name, $source) { if ($name == $source) { - throw new Exception(__('Why copy file content in the same file?')); + throw new Exception(__('File already exists.')); } $file = $this->getSourceContent($source); - $source = [ + $data = [ 'c' => $file['c'], 'w' => $this->getDestinationFile($name) !== false, - 'f' => $f, ]; + ]; - if (!$source['w']) { + if (!$data['w']) { throw new Exception(sprintf(__('File %s is not readable'), $source)); } - if ($type == 'empty') { - $source['c'] = ''; - } - try { $dest = $this->getDestinationFile($name); @@ -210,7 +216,7 @@ class dcTemplator throw new Exception(); } - $content = $source['c']; + $content = $data['c']; if (!is_dir(dirname($dest))) { files::makeDir(dirname($dest)); diff --git a/index.php b/index.php index 1d162f8..c327300 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,15 @@ blog->getCategories(['post_type' => 'post']); @@ -63,7 +75,7 @@ if (in_array($part, ['new', 'copycat'])) { ### Action ### -/** +/* * Duplicate dc template */ if ('new' == $part && !empty($_POST['filesource'])) { @@ -86,13 +98,13 @@ if ('new' == $part && !empty($_POST['filesource'])) { } } -/** +/* * Copy tempaltor template */ if ('copy' == $part && !empty($_POST['filename'])) { try { dcCore::app()->templator->copypasteTpl( - rawurldecode($_POST['filename']) . '.html', + rawurldecode($_POST['filename']) . '.html', rawurldecode($_POST['file']) ); @@ -105,13 +117,13 @@ if ('copy' == $part && !empty($_POST['filename'])) { } } -/** +/* * Copy templator category template */ if ('copycat' == $part && !empty($_POST['filecat'])) { try { dcCore::app()->templator->copypasteTpl( - 'category-' . rawurldecode($_POST['filecat']) . '.html', + 'category-' . rawurldecode($_POST['filecat']) . '.html', rawurldecode($_POST['file']) ); @@ -124,7 +136,7 @@ if ('copycat' == $part && !empty($_POST['filecat'])) { } } -/** +/* * Delete tempaltor template */ if ('delete' == $part && !empty($_POST['file'])) { @@ -144,7 +156,7 @@ if ('delete' == $part && !empty($_POST['file'])) { ### Display ### -/** +/* * Check */ @@ -154,23 +166,23 @@ if (!dcCore::app()->templator->canUseRessources(true)) { '' . __('Templator') . '' . '' . dcPage::breadcrumb([ - __('Plugins') => '', + __('Plugins') => '', __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), - __('New template') => '', + __('New template') => '', ]) . dcPage::notices(); -/** - * Duplicate dotclear template - */ + /* + * Duplicate dotclear template + */ } elseif ('new' == $part) { echo '' . __('Templator') . '' . '' . dcPage::breadcrumb([ - __('Plugins') => '', + __('Plugins') => '', __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), - __('New template') => '', + __('New template') => '', ]) . dcPage::notices() . @@ -183,28 +195,28 @@ if (!dcCore::app()->templator->canUseRessources(true)) { '

    ' . __('Extension .html is automatically added to filename.') . '

    '; if ($has_categories) { - echo - '

    ' . + echo + '

    ' . form::combo('filecat', $categories_combo, '') . '

    ' . '

    ' . __('Required only for category template.') . '

    '; } echo - '

    ' . + '

    ' . dcCore::app()->formNonce() . '

    ' . ''; -/** - * Copy templator template - */ + /* + * Copy templator template + */ } elseif ('copy' == $part && !empty($_REQUEST['file'])) { echo '' . __('Templator') . '' . '' . dcPage::breadcrumb([ - __('Plugins') => '', - __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), + __('Plugins') => '', + __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), __('Copy available template') => '', ]) . dcPage::notices() . @@ -224,11 +236,11 @@ if (!dcCore::app()->templator->canUseRessources(true)) { form::hidden('file', html::escapeHTML($_REQUEST['file'])) . '

    ' . ''; -/** - * Copy templator category template - */ + /* + * Copy templator category template + */ } elseif ('copycat' == $part && !empty($_REQUEST['file'])) { - $category_id = str_replace(['category-','.html'], '', $_REQUEST['file']); + $category_id = (int) str_replace(['category-','.html'], '', $_REQUEST['file']); $cat_parents = dcCore::app()->blog->getCategoryParents($category_id); $full_name = ''; while ($cat_parents->fetch()) { @@ -240,8 +252,8 @@ if (!dcCore::app()->templator->canUseRessources(true)) { '' . __('Templator') . '' . '' . dcPage::breadcrumb([ - __('Plugins') => '', - __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), + __('Plugins') => '', + __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), __('Copy available template') => '', ]) . dcPage::notices() . @@ -261,16 +273,16 @@ if (!dcCore::app()->templator->canUseRessources(true)) { form::hidden('file', html::escapeHTML($_REQUEST['file'])) . '

    ' . ''; -/** - * Delete templator template - */ + /* + * Delete templator template + */ } elseif ('delete' == $part && !empty($_REQUEST['file'])) { echo '' . __('Templator') . '' . '' . dcPage::breadcrumb([ - __('Plugins') => '', - __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), + __('Plugins') => '', + __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), __('Delete available template') => '', ]) . dcPage::notices() . @@ -287,16 +299,16 @@ if (!dcCore::app()->templator->canUseRessources(true)) { form::hidden('file', html::escapeHTML($_GET['file'])) . '

    ' . ''; -/** - * List templator templates - */ + /* + * List templator templates + */ } elseif ('files' == $part) { echo '' . __('Templator') . '' . '' . dcPage::breadcrumb([ - __('Plugins') => '', - __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), + __('Plugins') => '', + __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), __('Available templates') => '', ]) . dcPage::notices() . @@ -309,9 +321,7 @@ if (!dcCore::app()->templator->canUseRessources(true)) { $filter = new adminGenericFilterV2('templator'); $filter->add(dcAdminFilters::getPageFilter()); - $pager = new dcPager($filter->page, count($items), $filter->nb, 10); - $pager->html_prev = __('«prev.'); - $pager->html_next = __('next»'); + $pager = new dcPager($filter->page, count($items), $filter->nb, 10); echo '
    ' . @@ -326,9 +336,9 @@ if (!dcCore::app()->templator->canUseRessources(true)) { '
    '; } -/** - * List Used templator template - */ + /* + * List Used templator template + */ } elseif ('used' == $part) { $tags = dcCore::app()->meta->getMetadata(['meta_type' => 'template']); $tags = dcCore::app()->meta->computeMetaStats($tags); @@ -356,12 +366,12 @@ if (!dcCore::app()->templator->canUseRessources(true)) { } $cols[$col] .= '' . - '' . $tags->meta_id . ' ' . $img_status . '' . + 'redir' => dcCore::app()->adminurl->get('admin.plugin.templator', ['part' => 'used']), + ]) . '">' . $tags->meta_id . ' ' . $img_status . '' . '' . $tags->count . ' ' . (($tags->count == 1) ? __('entry') : __('entries')) . '' . ''; @@ -376,9 +386,9 @@ if (!dcCore::app()->templator->canUseRessources(true)) { dcPage::cssLoad(dcPage::getPF('tags/style.css')) . '' . dcPage::breadcrumb([ - __('Plugins') => '', + __('Plugins') => '', __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), - __('Used templates') => '', + __('Used templates') => '', ]) . dcPage::notices() . '

    ' . __('Used templates') . '

    '; @@ -394,9 +404,9 @@ if (!dcCore::app()->templator->canUseRessources(true)) { echo '

    ' . __('No specific templates on this blog.') . '

    '; } -/** - * Edit emplator template - */ + /* + * Edit emplator template + */ } elseif ('edit' == $part && !empty($_REQUEST['file'])) { try { try { @@ -405,9 +415,9 @@ if (!dcCore::app()->templator->canUseRessources(true)) { $name = $file['f']; if (preg_match('/^category-(.+).html$/', $name, $cat_id)) { - $category = dcCore::app()->blog->getCategory($cat_id[1]); + $category = dcCore::app()->blog->getCategory((int) $cat_id[1]); $full_name = ''; - $cat_parents = dcCore::app()->blog->getCategoryParents($cat_id[1]); + $cat_parents = dcCore::app()->blog->getCategoryParents((int) $cat_id[1]); while ($cat_parents->fetch()) { $full_name = $cat_parents->cat_title . ' › '; }; @@ -453,9 +463,9 @@ if (!dcCore::app()->templator->canUseRessources(true)) { dcPage::cssModuleLoad('themeEditor/style.css') . '' . dcPage::breadcrumb([ - __('Plugins') => '', + __('Plugins') => '', __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), - __('Edit template') => '', + __('Edit template') => '', ]) . dcPage::notices(); @@ -490,11 +500,11 @@ if (!dcCore::app()->templator->canUseRessources(true)) { } } -/** - * Edit posts options linked to a template - */ + /* + * Edit posts options linked to a template + */ } elseif ('posts' == $part && (!empty($_REQUEST['file']) || $_REQUEST['file'] == '0')) { - $file = $_REQUEST['file']; + $file = $_REQUEST['file']; $redir = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.templator', ['part' => 'used']); # Unselect the template @@ -516,10 +526,10 @@ if (!dcCore::app()->templator->canUseRessources(true)) { $filter->add('file', $file); $filter->add('post_type', ''); - $params = $filter->params(); + $params = $filter->params(); $params['no_content'] = true; - $params['meta_id'] = $file; - $params['meta_type'] = 'template'; + $params['meta_id'] = $file; + $params['meta_type'] = 'template'; # Get posts try { @@ -537,13 +547,13 @@ if (!dcCore::app()->templator->canUseRessources(true)) { $filter->js(dcCore::app()->adminurl->get('admin.plugin.templator', ['part' => 'posts', 'file' => $file])) . '' . dcPage::breadcrumb([ - __('Plugins') => '', - __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), + __('Plugins') => '', + __('Templates engine') => dcCore::app()->adminurl->get('admin.plugin.templator'), __('Unselect template') => '', ]) . dcPage::notices() . - '

    ' . __('Unselect template') . '

    ' . + '

    ' . sprintf(__('Unselect template "%s"'), '' . $file . '') . '

    ' . '

    ' . __('Back') . '

    '; if (!dcCore::app()->error->flag()) { @@ -565,8 +575,8 @@ if (!dcCore::app()->templator->canUseRessources(true)) { '
    ' . '

    ' . - '

    ' . - '

    ' . + '

    ' . + '

    ' . form::hidden('action', 'unselecttpl') . dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.templator', $filter->values()) . form::hidden('redir', $redir) . @@ -579,15 +589,15 @@ if (!dcCore::app()->templator->canUseRessources(true)) { } } -/** - * Default page - */ + /* + * Default page + */ } else { echo '' . __('Templator') . '' . '' . dcPage::breadcrumb([ - __('Plugins') => '', + __('Plugins') => '', __('Templates engine') => '', ]) . dcPage::notices(); @@ -617,4 +627,4 @@ if (!dcCore::app()->templator->canUseRessources(true)) { dcPage::helpBlock('templator'); -echo ''; \ No newline at end of file +echo '';