code review

master v1.7.1
Jean-Christian Paul Denis 2023-11-04 21:51:11 +01:00
parent d3bf04228d
commit a77741d890
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
10 changed files with 87 additions and 46 deletions

View File

@ -1,3 +1,9 @@
templator 1.7.1 - 2023.11.04
===========================================================
* Require Dotclear 2.28
* Require PHP 8.1
* Code review (phpstan)
templator 1.7 - 2023.10.20
===========================================================
* Require Dotclear 2.28

View File

@ -1,7 +1,7 @@
# README
[![Release](https://img.shields.io/badge/release-1.7-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/templator/releases)
![Date](https://img.shields.io/badge/date-2023.10.20-c44d58.svg)
[![Release](https://img.shields.io/badge/release-1.7.1-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/templator/releases)
![Date](https://img.shields.io/badge/date-2023.11.04-c44d58.svg)
[![Dotclear](https://img.shields.io/badge/dotclear-v2.28-137bbb.svg)](https://fr.dotclear.org/download)
[![Dotaddict](https://img.shields.io/badge/dotaddict-official-9ac123.svg)](https://plugins.dotaddict.org/dc2/details/templator)
[![License](https://img.shields.io/badge/license-GPL--2.0-ececec.svg)](https://git.dotclear.watch/JcDenis/templator/src/branch/master/LICENSE)

View File

@ -18,7 +18,7 @@ $this->registerModule(
'Templates engine',
'Create and select more templates for your posts',
'Osku and contributors',
'1.7',
'1.7.1',
[
'requires' => [['core', '2.28']],
'permissions' => 'My',

View File

@ -2,10 +2,10 @@
<modules xmlns:da="http://dotaddict.org/da/">
<module id="templator">
<name>Templates engine</name>
<version>1.7</version>
<version>1.7.1</version>
<author>Osku and contributors</author>
<desc>Create and select more templates for your posts</desc>
<file>https://git.dotclear.watch/JcDenis/templator/releases/download/v1.7/plugin-templator.zip</file>
<file>https://git.dotclear.watch/JcDenis/templator/releases/download/v1.7.1/plugin-templator.zip</file>
<da:dcmin>2.28</da:dcmin>
<da:details>https://git.dotclear.watch/JcDenis/templator/src/branch/master/README.md</da:details>
<da:support>https://git.dotclear.watch/JcDenis/templator/issues</da:support>

View File

@ -35,6 +35,10 @@ class BackendBehaviors
return My::jsLoad('admin');
}
/**
* @param ArrayObject<string, mixed> $main_items
* @param ArrayObject<string, mixed> $sidebar_items
*/
public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?MetaRecord $post): void
{
$selected = '';
@ -75,6 +79,9 @@ class BackendBehaviors
);
}
/**
* @param ArrayObject<string, mixed> $post
*/
public static function adminPostsActionsCallback(ActionsPosts $pa, ArrayObject $post): void
{
# No entry
@ -126,6 +133,9 @@ class BackendBehaviors
$pa->endPage();
}
/**
* @param ArrayObject<string, mixed> $sorts
*/
public static function adminFiltersListsV2(ArrayObject $sorts): void
{
$sorts[My::id()] = [
@ -142,7 +152,7 @@ class BackendBehaviors
}
/**
* @return array<string,string>
* @return array<string, string>
*/
private static function getTemplateCombo(): array
{

View File

@ -29,10 +29,7 @@ class Frontend extends Process
return false;
}
App::frontend()->template()->setPath(
App::frontend()->template()->getPath(),
Templator::instance()->getPath()
);
App::frontend()->template()->appendPath(Templator::instance()->getPath());
App::behavior()->addBehaviors([
'urlHandlerBeforeGetData' => function ($_): void {

View File

@ -117,7 +117,7 @@ class Manage extends Process
$v->media->removeItem($file);
App::meta()->delMeta($file, 'template');
if (!dcCore::app()->error->flag()) {
if (!App::error()->flag()) {
Notices::addSuccessNotice(__('The template has been successfully removed.'));
My::redirect(['part' => 'files']);
}
@ -515,11 +515,8 @@ class Manage extends Process
# Get posts
try {
$posts = App::meta()->getPostsByMeta($params);
if (is_null($posts)) {
throw new Exception(__('Failed to get posts meta'));
}
$counter = App::meta()->getPostsByMeta($params, true)?->f(0);
$posts = App::meta()->getPostsByMeta($params);
$counter = App::meta()->getPostsByMeta($params, true)->f(0);
$counter = is_numeric($counter) ? (int) $counter : 0;
$post_list = new ListingPosts($posts, $counter);
} catch (Exception $e) {
@ -544,7 +541,7 @@ class Manage extends Process
'<h3>' . sprintf(__('Unselect template "%s"'), '<strong>' . $file . '</strong>') . '</h3>' .
'<p><a class ="back" href="' . $redir . '">' . __('Back') . '</a></p>';
if (!App::error()->flag() && isset($posts)) {
if (!App::error()->flag() && isset($posts) && isset($post_list)) {
if ($posts->isEmpty() && !$filter->show()) {
echo '<p>' . __('There is no entries') . '</p>';
} else {

View File

@ -21,32 +21,64 @@ use Exception;
*/
class ManageVars
{
/** @var ManageVars $container Self instance */
/**
* Self instance.
*
* @var ManageVars $container
*/
private static $container;
/** @var string $name The requested manage part name*/
/**
* The requested manage part name.
*
* @var string $name
*/
public readonly string $name;
/** @var string $part The requested manage part */
/**
* The requested manage part.
*
* @var string $part
*/
public readonly string $part;
/** @var MediaInterface $media The limited media instance */
/**
* The limited media instance.
*
* @var MediaInterface $media
*/
public readonly MediaInterface $media;
/** @var array<int,File> $items The media items */
/**
* The media items.
*
* @var array<int, File> $items
*/
public readonly array $items;
/** @var array<string,int> $categories The blog categories list */
/**
* The blog categories list.
*
* @var array<string, int> $categories
*/
public readonly array $categories;
/** @var bool $has_categories Blog has categories */
/**
* Blog has categories.
*
* @var bool $has_categories
*/
public readonly bool $has_categories;
/** @var array<string,string> $sources The templates list */
/**
* The templates list.
*
* @var array<string, string> $sources
*/
public readonly array $sources;
/**
* Constructo sets properties.
* Constructor sets properties.
*/
public function __construct()
{
@ -61,8 +93,7 @@ class ManageVars
$this->media->chdir(Templator::MY_TPL_DIR);
// For users with only templator permission, we use sudo.
App::auth()->sudo($this->media->getDir(...));
$dir = $this->media->dir;
$this->items = array_values($dir['files']);
$this->items = array_values($this->media->getFiles());
// categories
$categories_combo = [];
@ -70,28 +101,27 @@ class ManageVars
try {
$categories = App::blog()->getCategories(['post_type' => 'post']);
if (!is_null($categories)) {
$l = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1;
$full_name = [is_string($categories->f('cat_title')) ? $categories->f('cat_title') : ''];
while ($categories->fetch()) {
$id = is_numeric($categories->f('cat_id')) ? (int) $categories->f('cat_id') : 1;
$level = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1;
$title = is_string($categories->f('cat_title')) ? $categories->f('cat_title') : '';
$l = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1;
$full_name = [is_string($categories->f('cat_title')) ? $categories->f('cat_title') : ''];
if ($level < $l) {
$full_name = [];
} elseif ($level == $l) {
array_pop($full_name);
}
$full_name[] = Html::escapeHTML($title);
while ($categories->fetch()) {
$id = is_numeric($categories->f('cat_id')) ? (int) $categories->f('cat_id') : 1;
$level = is_numeric($categories->f('level')) ? (int) $categories->f('level') : 1;
$title = is_string($categories->f('cat_title')) ? $categories->f('cat_title') : '';
$categories_combo[implode(' &rsaquo; ', $full_name)] = $id;
$l = $level;
if ($level < $l) {
$full_name = [];
} elseif ($level == $l) {
array_pop($full_name);
}
$has_categories = !$categories->isEmpty();
$full_name[] = Html::escapeHTML($title);
$categories_combo[implode(' &rsaquo; ', $full_name)] = $id;
$l = $level;
}
$has_categories = !$categories->isEmpty();
} catch (Exception $e) {
}
$this->categories = $categories_combo;

View File

@ -27,6 +27,7 @@ class My extends MyPlugin
public static function checkCustomContext(int $context): ?bool
{
return match ($context) {
// Add templator perm to backend
self::BACKEND, self::MENU, self::MANAGE => App::task()->checkContext('BACKEND')
&& App::auth()->check(App::auth()->makePermissions([
self::PERMISSION_TEMPLATOR,

View File

@ -73,7 +73,7 @@ class Pager
$params['post_type'] = '';
try {
$counter = App::meta()->getPostsByMeta($params, true)?->f(0);
$counter = App::meta()->getPostsByMeta($params, true)->f(0);
$counter = is_numeric($counter) ? (int) $counter : 0;
$url = My::manageUrl([
'part' => 'posts',