From 8684011f3e3588ee926f3c282616c7fb27046a45 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Tue, 15 Aug 2023 13:43:30 +0200 Subject: [PATCH] use plugin ID as table name and meta ID --- _init.php | 1 + src/Frontend.php | 2 +- src/Install.php | 2 +- src/ManagePeriod.php | 2 +- src/My.php | 6 ------ src/Uninstall.php | 4 ++-- src/Utils.php | 22 +++++++++++----------- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/_init.php b/_init.php index 261705d..0868be2 100644 --- a/_init.php +++ b/_init.php @@ -16,5 +16,6 @@ if (!defined('DC_RC_PATH')) { class initPeriodical { + // now use My::id() public const PERIOD_TABLE_NAME = 'periodical'; } diff --git a/src/Frontend.php b/src/Frontend.php index 51da4dd..e67e339 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -60,7 +60,7 @@ class Frontend extends Process if (!preg_match('/^(post_dt|post_creadt|post_id) (asc|desc)$/', $posts_order)) { $posts_order = 'post_dt asc'; } - $cur_period = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME); + $cur_period = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::id()); while ($periods->fetch()) { // Check if period is ongoing diff --git a/src/Install.php b/src/Install.php index bc3970c..dec31c6 100644 --- a/src/Install.php +++ b/src/Install.php @@ -36,7 +36,7 @@ class Install extends Process $t = new Structure(dcCore::app()->con, dcCore::app()->prefix); // create database table - $t->__get(My::TABLE_NAME) + $t->__get(My::id()) ->field('periodical_id', 'bigint', 0, false) ->field('blog_id', 'varchar', 32, false) ->field('periodical_type', 'varchar', 32, false, "'post'") diff --git a/src/ManagePeriod.php b/src/ManagePeriod.php index 4cbfb0e..51f9f31 100644 --- a/src/ManagePeriod.php +++ b/src/ManagePeriod.php @@ -104,8 +104,8 @@ class ManagePeriod extends Process Utils::updPeriod($vars->period_id, $cur); self::redirect($vars->redir, $vars->period_id, '#period', __('Period successfully updated.')); - // Create period } else { + // Create period $period_id = Utils::addPeriod($cur); self::redirect($vars->redir, $period_id, '#period', __('Period successfully created.')); diff --git a/src/My.php b/src/My.php index a06846a..0c4fb2f 100644 --- a/src/My.php +++ b/src/My.php @@ -22,12 +22,6 @@ use Dotclear\Module\MyPlugin; */ class My extends MyPlugin { - /** @var string This module table name */ - public const TABLE_NAME = 'periodical'; - - /** @var string This module meta type */ - public const META_TYPE = 'periodical'; - public static function checkCustomContext(int $context): ?bool { return in_array($context, [My::MANAGE, My::MENU]) ? diff --git a/src/Uninstall.php b/src/Uninstall.php index 444564b..373ac05 100644 --- a/src/Uninstall.php +++ b/src/Uninstall.php @@ -40,7 +40,7 @@ class Uninstall extends Process ->addUserAction( 'tables', 'delete', - My::TABLE_NAME + My::id() ) ->addUserAction( 'plugins', @@ -60,7 +60,7 @@ class Uninstall extends Process ->addDirectAction( 'tables', 'delete', - My::TABLE_NAME + My::id() ) ->addDirectAction( 'plugins', diff --git a/src/Utils.php b/src/Utils.php index dc88e20..262f20f 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -48,7 +48,7 @@ class Utils */ public static function openCursor(): Cursor { - return dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME); + return dcCore::app()->con->openCursor(dcCore::app()->prefix . My::id()); } /** @@ -82,7 +82,7 @@ class Utils ]); } - $sql->from($sql->as(dcCore::app()->prefix . My::TABLE_NAME, 'T'), false, true); + $sql->from($sql->as(dcCore::app()->prefix . My::id(), 'T'), false, true); if (!empty($params['join'])) { $sql->join($params['join']); @@ -150,12 +150,12 @@ class Utils */ public static function addPeriod(Cursor $cur): int { - dcCore::app()->con->writeLock(dcCore::app()->prefix . My::TABLE_NAME); + dcCore::app()->con->writeLock(dcCore::app()->prefix . My::id()); try { // get next id $sql = new SelectStatement(); - $rs = $sql->from(dcCore::app()->prefix . My::TABLE_NAME) + $rs = $sql->from(dcCore::app()->prefix . My::id()) ->column($sql->max('periodical_id')) ->select(); @@ -207,7 +207,7 @@ class Utils } $sql = new DeleteStatement(); - $sql->from(dcCore::app()->prefix . My::TABLE_NAME) + $sql->from(dcCore::app()->prefix . My::id()) ->where('blog_id = ' . $sql->quote((string) dcCore::app()->blog?->id)) ->and('periodical_id = ' . $period_id) ->delete(); @@ -236,7 +236,7 @@ class Utils $sql = new DeleteStatement(); $sql->from(dcCore::app()->prefix . dcMeta::META_TABLE_NAME) - ->where('meta_type = ' . $sql->quote(My::META_TYPE)) + ->where('meta_type = ' . $sql->quote(My::id())) ->and('post_id ' . $sql->in($ids)) ->delete(); } @@ -282,11 +282,11 @@ class Utils ->join( (new JoinStatement()) ->left() - ->from($sql->as(dcCore::app()->prefix . My::TABLE_NAME, 'T')) + ->from($sql->as(dcCore::app()->prefix . My::id(), 'T')) ->on('CAST(T.periodical_id as char) = CAST(R.meta_id as char)') ->statement() ) - ->and('R.meta_type = ' . $sql->quote(My::META_TYPE)) + ->and('R.meta_type = ' . $sql->quote(My::id())) ->and("T.periodical_type = 'post' ") ; @@ -343,7 +343,7 @@ class Utils try { $cur->setField('post_id', $post_id); $cur->setField('meta_id', $period_id); - $cur->setField('meta_type', My::META_TYPE); + $cur->setField('meta_type', My::id()); $cur->insert(); dcCore::app()->con->unlock(); } catch (Exception $e) { @@ -362,7 +362,7 @@ class Utils { $sql = new DeleteStatement(); $sql->from(dcCore::app()->prefix . dcMeta::META_TABLE_NAME) - ->where('meta_type = ' . $sql->quote(My::META_TYPE)) + ->where('meta_type = ' . $sql->quote(My::id())) ->and('post_id = ' . $post_id) ->delete(); } @@ -395,7 +395,7 @@ class Utils $sql = new DeleteStatement(); $sql->from(dcCore::app()->prefix . dcMeta::META_TABLE_NAME) - ->where('meta_type = ' . $sql->quote(My::META_TYPE)) + ->where('meta_type = ' . $sql->quote(My::id())) ->and('post_id ' . $sql->in($ids)) ->delete(); }