use Database helper

master
Jean-Christian Paul Denis 2023-04-23 13:47:24 +02:00
parent 642ea68a41
commit 1eeb654116
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
3 changed files with 102 additions and 66 deletions

View File

@ -16,11 +16,13 @@ namespace Dotclear\Plugin\postExpired;
use ArrayObject; use ArrayObject;
use DateTimeZone; use DateTimeZone;
use cursor;
use dcCore; use dcCore;
use dcPostsActions; use dcPostsActions;
use dcPage; use dcPage;
use dcRecord; use Dotclear\Database\{
Cursor,
MetaRecord
};
use Dotclear\Helper\Html\Form\{ use Dotclear\Helper\Html\Form\{
Datetime, Datetime,
Form, Form,
@ -43,9 +45,9 @@ use Exception;
class BackendBehaviors class BackendBehaviors
{ {
/** /**
* Add actions to posts page combo * Add actions to posts page combo.
* *
* @param dcPostsActions $pa dcPostsActions instance * @param dcPostsActions $pa dcPostsActions instance
*/ */
public static function adminPostsActions(dcPostsActions $pa): void public static function adminPostsActions(dcPostsActions $pa): void
{ {
@ -69,9 +71,9 @@ class BackendBehaviors
} }
/** /**
* Add javascript for date field and toggle * Add javascript for date field and toggle.
* *
* @return string HTML head * @return string HTML head
*/ */
public static function adminPostHeaders(): string public static function adminPostHeaders(): string
{ {
@ -79,13 +81,13 @@ class BackendBehaviors
} }
/** /**
* Add form to post sidebar * Add form to post sidebar.
* *
* @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 ?dcRecord $post Post record or null * @param ?MetaRecord $post Post record or null
*/ */
public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?dcRecord $post): void public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?MetaRecord $post): void
{ {
if ($post === null) { if ($post === null) {
return; return;
@ -102,9 +104,9 @@ class BackendBehaviors
} }
/** /**
* Delete expired date on post edition * Delete expired date on post edition.
* *
* @param integer $post_id Post id * @param int $post_id Post id
*/ */
public static function adminBeforePostDelete(int $post_id): void public static function adminBeforePostDelete(int $post_id): void
{ {
@ -112,12 +114,12 @@ class BackendBehaviors
} }
/** /**
* Add expired date on post edition * Add expired date on post edition.
* *
* @param cursor $cur Current post cursor * @param Cursor $cur Current post Cursor
* @param integer $post_id Post id * @param int $post_id Post id
*/ */
public static function adminAfterPostSave(cursor $cur, int $post_id): void public static function adminAfterPostSave(Cursor $cur, int $post_id): void
{ {
self::delPostExpired($post_id); self::delPostExpired($post_id);
@ -132,20 +134,25 @@ class BackendBehaviors
} }
/** /**
* Posts actions callback to add expired date * Posts actions callback to add expired date.
* *
* @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): void public static function callbackAdd(dcPostsActions $pa, ArrayObject $post): void
{ {
# No entry // nullsafe
if (is_null(dcCore::app()->blog)) {
return;
}
// No entry
$posts_ids = $pa->getIDs(); $posts_ids = $pa->getIDs();
if (empty($posts_ids)) { if (empty($posts_ids)) {
throw new Exception(__('No entry selected')); throw new Exception(__('No entry selected'));
} }
# Add epired date // Add epired date
if (!empty($post['post_expired_date']) if (!empty($post['post_expired_date'])
&& (!empty($post['post_expired_status']) && (!empty($post['post_expired_status'])
|| !empty($post['post_expired_category']) || !empty($post['post_expired_category'])
@ -160,9 +167,9 @@ class BackendBehaviors
dcPage::addSuccessNotice(__('Expired date added.')); dcPage::addSuccessNotice(__('Expired date added.'));
$pa->redirect(true); $pa->redirect(true);
# Display form // Display form
} else { } else {
# Get records to know post type // Get records to know post type
$posts = $pa->getRS(); $posts = $pa->getRS();
$pa->beginPage( $pa->beginPage(
@ -194,20 +201,20 @@ class BackendBehaviors
} }
/** /**
* Posts actions callback to add expired date * Posts actions callback to add expired date.
* *
* @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): void public static function callbackRemove(dcPostsActions $pa, ArrayObject $post): void
{ {
# No entry // No entry
$posts_ids = $pa->getIDs(); $posts_ids = $pa->getIDs();
if (empty($posts_ids)) { if (empty($posts_ids)) {
throw new Exception(__('No entry selected')); throw new Exception(__('No entry selected'));
} }
# Delete expired date // Delete expired date
foreach ($posts_ids as $post_id) { foreach ($posts_ids as $post_id) {
self::delPostExpired($post_id); self::delPostExpired($post_id);
} }
@ -217,9 +224,9 @@ class BackendBehaviors
} }
/** /**
* Delete expired date * Delete expired date.
* *
* @param integer $post_id Post id * @param int $post_id Post id
*/ */
private static function delPostExpired(int $post_id): void private static function delPostExpired(int $post_id): void
{ {
@ -227,10 +234,10 @@ class BackendBehaviors
} }
/** /**
* Save expired date * Save expired date.
* *
* @param integer $post_id Post id * @param int $post_id Post id
* @param ArrayObject $post _POST fields * @param ArrayObject $post _POST fields
*/ */
private static function setPostExpired(int $post_id, ArrayObject $post): void private static function setPostExpired(int $post_id, ArrayObject $post): void
{ {
@ -267,15 +274,20 @@ class BackendBehaviors
} }
/** /**
* Expired date form fields * Expired date form fields.
* *
* @param string $post_type Posts type * @param string $post_type Posts type
* @param null|int $post_id Post ID * @param null|int $post_id Post ID
* @param bool $render Render fileds to HTML * @param bool $render Render fileds to HTML
* @return array Array of object form fields * @return array Array of object form fields
*/ */
private static function fieldsPostExpired(string $post_type, ?int $post_id = null, bool $render = true): array private static function fieldsPostExpired(string $post_type, ?int $post_id = null, bool $render = true): array
{ {
// nullsafe
if (is_null(dcCore::app()->blog)) {
return [];
}
$fields = $post_expired = []; $fields = $post_expired = [];
if ($post_id) { if ($post_id) {
@ -338,14 +350,20 @@ class BackendBehaviors
} }
/** /**
* Custom categories combo * Custom categories combo.
* *
* @param dcRecord $categories Categories recordset * @param MetaRecord $categories Categories recordset
* @return array Categorires combo *
* @return array Categorires combo
*/ */
private static function categoriesCombo(dcRecord $categories): array private static function categoriesCombo(MetaRecord $categories): array
{ {
# Getting categories // nullsafe
if (is_null(dcCore::app()->blog)) {
return [];
}
// Getting categories
$categories_combo = [ $categories_combo = [
__('Not changed') => '', __('Not changed') => '',
__('Uncategorized') => '!', __('Uncategorized') => '!',
@ -369,9 +387,9 @@ class BackendBehaviors
} }
/** /**
* Custom status combo * Custom status combo.
* *
* @return array Status combo * @return array Status combo
*/ */
private static function statusCombo(): array private static function statusCombo(): array
{ {
@ -384,9 +402,9 @@ class BackendBehaviors
} }
/** /**
* Custom selection combo * Custom selection combo.
* *
* @return array Selection combo * @return array Selection combo
*/ */
private static function selectedCombo(): array private static function selectedCombo(): array
{ {
@ -398,9 +416,9 @@ class BackendBehaviors
} }
/** /**
* Custom comment status combo * Custom comment status combo.
* *
* @return array Comment status combo * @return array Comment status combo
*/ */
private static function commentCombo(): array private static function commentCombo(): array
{ {
@ -412,9 +430,9 @@ class BackendBehaviors
} }
/** /**
* Custom trackback status combo * Custom trackback status combo.
* *
* @return array Trackback status combo * @return array Trackback status combo
*/ */
private static function trackbackCombo(): array private static function trackbackCombo(): array
{ {
@ -425,17 +443,33 @@ class BackendBehaviors
]; ];
} }
/**
* Change a date from user timezone to UTC.
*
* @param string $date The date
*
* @return string The UTC date
*/
private static function dateFromUser(string $date): string private static function dateFromUser(string $date): string
{ {
$d = date_create($date, new DateTimeZone(dcCore::app()->auth->getInfo('user_tz'))); $u = is_null(dcCore::app()->auth) ? 'UTC' : dcCore::app()->auth->getInfo('user_tz');
$d = date_create($date, new DateTimeZone($u));
return $d ? date_format($d->setTimezone(new DateTimeZone('UTC')), 'Y-m-d H:i:00') : ''; return $d ? date_format($d->setTimezone(new DateTimeZone('UTC')), 'Y-m-d H:i:00') : '';
} }
/**
* Change a date from UTC to user timezone.
*
* @param string $date The UTC date
*
* @return string The date
*/
private static function dateToUser(string $date): string private static function dateToUser(string $date): string
{ {
$u = is_null(dcCore::app()->auth) ? 'UTC' : dcCore::app()->auth->getInfo('user_tz');
$d = date_create($date, new DateTimeZone('UTC')); $d = date_create($date, new DateTimeZone('UTC'));
return $d ? date_format($d->setTimezone(new DateTimeZone(dcCore::app()->auth->getInfo('user_tz'))), 'Y-m-d\TH:i') : ''; return $d ? date_format($d->setTimezone(new DateTimeZone($u)), 'Y-m-d\TH:i') : '';
} }
} }

View File

@ -18,7 +18,7 @@ use DateTimeZone;
use dcBlog; use dcBlog;
use dcCore; use dcCore;
use dcMeta; use dcMeta;
use dcRecord; use Dotclear\Database\MetaRecord;
/** /**
* @ingroup DC_PLUGIN_POSTEXPIRED * @ingroup DC_PLUGIN_POSTEXPIRED
@ -53,7 +53,7 @@ class FrontendBehaviors
$utc = new DateTimeZone('UTC'); $utc = new DateTimeZone('UTC');
$now_tz = (int) date_format(date_create('now', $utc), 'U'); $now_tz = (int) date_format(date_create('now', $utc), 'U');
# Prepared post cursor # Prepared post Cursor
$post_cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME); $post_cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME);
# Loop through marked posts # Loop through marked posts
@ -74,7 +74,7 @@ class FrontendBehaviors
My::META_TYPE My::META_TYPE
); );
# Prepare post cursor # Prepare post Cursor
$post_cur->clean(); $post_cur->clean();
$post_cur->setField('post_upddt', date('Y-m-d H:i:s', $now_tz)); $post_cur->setField('post_upddt', date('Y-m-d H:i:s', $now_tz));
@ -87,7 +87,7 @@ class FrontendBehaviors
# values are prefixed by "!" # values are prefixed by "!"
$v = (int) substr($v, 1); $v = (int) substr($v, 1);
# Put value in post cursor # Put value in post Cursor
switch($k) { switch($k) {
case 'status': case 'status':
$post_cur->setField('post_status', $v); $post_cur->setField('post_status', $v);
@ -135,9 +135,9 @@ class FrontendBehaviors
/** /**
* Extends posts record with expired date * Extends posts record with expired date
* *
* @param dcRecord $rs Post recordset * @param MetaRecord $rs Post recordset
*/ */
public static function coreBlogGetPosts(dcRecord $rs): void public static function coreBlogGetPosts(MetaRecord $rs): void
{ {
$rs->extend('rsExtPostExpired'); $rs->extend('rsExtPostExpired');
} }

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace Dotclear\Plugin\postExpired; namespace Dotclear\Plugin\postExpired;
use dcCore; use dcCore;
use dcRecord; use Dotclear\Database\MetaRecord;
use rsExtPost; use rsExtPost;
/** /**
@ -25,17 +25,19 @@ use rsExtPost;
*/ */
class rsExtPostExpired extends rsExtPost class rsExtPostExpired extends rsExtPost
{ {
/** @var array $memory Memory to prevent redondant call */
protected static array $memory = []; protected static array $memory = [];
/** /**
* Retrieve expired date of a post * Retrieve expired date of a post.
* *
* @param dcRecord $rs Post recordset * @param MetaRecord $rs Post recordset
* @return string Expired date or null *
* @return string Expired date or null
*/ */
public static function postExpiredDate(dcRecord $rs): string public static function postExpiredDate(MetaRecord $rs): string
{ {
if (!self::$memory[$rs->f('post_id')]) { //memory if (!static::$memory[$rs->f('post_id')]) {
$rs_date = dcCore::app()->meta->getMetadata([ $rs_date = dcCore::app()->meta->getMetadata([
'meta_type' => My::META_TYPE, 'meta_type' => My::META_TYPE,
'post_id' => $rs->f('post_id'), 'post_id' => $rs->f('post_id'),
@ -46,10 +48,10 @@ class rsExtPostExpired extends rsExtPost
return ''; return '';
} }
$v = My::decode($rs_date->f('meta_id')); $v = My::decode($rs_date->f('meta_id'));
self::$memory[$rs->f('post_id')] = $v['date']; static::$memory[$rs->f('post_id')] = $v['date'];
} }
return self::$memory[$rs->f('post_id')]; return static::$memory[$rs->f('post_id')];
} }
} }