use namespace

master
Jean-Christian Paul Denis 2023-03-24 23:12:11 +01:00
parent 820ee61f17
commit 8fc262a089
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
13 changed files with 1359 additions and 1065 deletions

View File

@ -1,20 +0,0 @@
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
Clearbricks::lib()->autoload([
'periodical' => __DIR__ . '/inc/class.periodical.php',
'adminPeriodicalList' => __DIR__ . '/inc/lib.index.pager.php',
]);

View File

@ -1,23 +0,0 @@
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
$part = !empty($_REQUEST['part']) ? $_REQUEST['part'] : 'periods';
if ($part == 'period') {
include __DIR__ . '/inc/index.period.php';
} else {
include __DIR__ . '/inc/index.periods.php';
}

77
src/Backend.php 100644
View File

@ -0,0 +1,77 @@
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\periodical;
use dcAdmin;
use dcAuth;
use dcCore;
use dcNsProcess;
use dcPage;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init == defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id);
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminBlogPreferencesFormV2' => [BackendBehaviors::class, 'adminBlogPreferencesForm'],
'adminBeforeBlogSettingsUpdate' => [BackendBehaviors::class, 'adminBeforeBlogSettingsUpdate'],
'adminFiltersListsV2' => [BackendBehaviors::class, 'adminFiltersLists'],
'adminColumnsListsV2' => [BackendBehaviors::class, 'adminColumnsLists'],
'adminPostListHeaderV2' => [BackendBehaviors::class, 'adminPostListHeader'],
'adminPostListValueV2' => [BackendBehaviors::class, 'adminPostListValue'],
'adminBeforePostDelete' => [BackendBehaviors::class, 'adminBeforePostDelete'],
]);
if (dcCore::app()->blog->settings->get(My::id())->get('periodical_active')) {
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
My::name(),
dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
dcPage::getPF(My::id() . '/icon.svg'),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)
);
dcCore::app()->addBehaviors([
'adminDashboardFavoritesV2' => [BackendBehaviors::class, 'adminDashboardFavoritesV2'],
'adminPostHeaders' => [BackendBehaviors::class, 'adminPostHeaders'],
'adminPostsActions' => [BackendBehaviors::class, 'adminPostsActions'],
'adminPostFormItems' => [BackendBehaviors::class, 'adminPostFormItems'],
'adminAfterPostUpdate' => [BackendBehaviors::class, 'adminAfterPostSave'],
'adminAfterPostCreate' => [BackendBehaviors::class, 'adminAfterPostSave'],
]);
}
return true;
}
}

View File

@ -10,133 +10,55 @@
* @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('periodical');
namespace Dotclear\Plugin\periodical;
dcCore::app()->addBehavior(
'adminBlogPreferencesFormV2',
['adminPeriodical', 'adminBlogPreferencesForm']
);
dcCore::app()->addBehavior(
'adminBeforeBlogSettingsUpdate',
['adminPeriodical', 'adminBeforeBlogSettingsUpdate']
);
dcCore::app()->addBehavior(
'adminFiltersListsV2',
['adminPeriodical', 'adminFiltersLists']
);
dcCore::app()->addBehavior(
'adminColumnsListsV2',
['adminPeriodical', 'adminColumnsLists']
);
dcCore::app()->addBehavior(
'adminPostListHeaderV2',
['adminPeriodical', 'adminPostListHeader']
);
dcCore::app()->addBehavior(
'adminPostListValueV2',
['adminPeriodical', 'adminPostListValue']
);
if (dcCore::app()->blog->settings->periodical->periodical_active) {
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
__('Periodical'),
dcCore::app()->adminurl->get('admin.plugin.periodical'),
dcPage::getPF('periodical/icon.svg'),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.periodical')) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)
);
dcCore::app()->addBehavior(
'adminDashboardFavoritesV2',
['adminPeriodical', 'adminDashboardFavoritesV2']
);
dcCore::app()->addBehavior(
'adminPostHeaders',
['adminPeriodical', 'adminPostHeaders']
);
dcCore::app()->addBehavior(
'adminPostsActions',
['adminPeriodical', 'adminPostsActions']
);
dcCore::app()->addBehavior(
'adminPostFormItems',
['adminPeriodical', 'adminPostFormItems']
);
dcCore::app()->addBehavior(
'adminAfterPostUpdate',
['adminPeriodical', 'adminAfterPostSave']
);
dcCore::app()->addBehavior(
'adminAfterPostCreate',
['adminPeriodical', 'adminAfterPostSave']
);
}
dcCore::app()->addBehavior(
'adminBeforePostDelete',
['adminPeriodical', 'adminBeforePostDelete']
);
use ArrayObject;
use cursor;
use dcAuth;
use dcCore;
use dcFavorites;
use dcPage;
use dcPostsActions;
use dcRecord;
use dcSettings;
use Exception;
use form;
use html;
/**
* @ingroup DC_PLUGIN_PERIODICAL
* @brief Periodical - admin methods.
* @since 2.6
*/
class adminPeriodical
class BackendBehaviors
{
public static $combo_period = null;
protected static $per = null;
public static function sortbyCombo()
{
return [
__('Next update') => 'periodical_curdt',
__('End date') => 'periodical_enddt',
__('Frequence') => 'periodical_pub_int',
];
}
protected static function period()
{
if (self::$per === null) {
self::$per = new periodical();
}
return self::$per;
}
private static array $combo_period = [];
/**
* Add settings to blog preference
*
* @param dcSettings $blog_settings dcSettings instance
*/
public static function adminBlogPreferencesForm(dcSettings $blog_settings)
public static function adminBlogPreferencesForm(dcSettings $blog_settings): void
{
$s_active = (bool) $blog_settings->periodical->periodical_active;
$s_upddate = (bool) $blog_settings->periodical->periodical_upddate;
$s_updurl = (bool) $blog_settings->periodical->periodical_updurl;
$s = $blog_settings->get('periodical');
echo
'<div class="fieldset"><h4 id="periodical_params">' . __('Periodical') . '</h4>' .
'<div class="two-cols">' .
'<div class="col">' .
'<p><label class="classic" for="periodical_active">' .
form::checkbox('periodical_active', 1, $s_active) .
form::checkbox('periodical_active', 1, (bool) $s->get('periodical_active')) .
__('Enable periodical on this blog') . '</label></p>' .
'</div>' .
'<div class="col">' .
'<p><label for="periodical_upddate">' .
form::checkbox('periodical_upddate', 1, $s_upddate) .
form::checkbox('periodical_upddate', 1, (bool) $s->get('periodical_upddate')) .
__('Update post date when publishing it') . '</label></p>' .
'<p><label for="periodical_updurl">' .
form::checkbox('periodical_updurl', 1, $s_updurl) .
form::checkbox('periodical_updurl', 1, (bool) $s->get('periodical_updurl')) .
__('Update post url when publishing it') . '</label></p>' .
'</div>' .
'</div>' .
@ -149,11 +71,11 @@ class adminPeriodical
*
* @param dcSettings $blog_settings dcSettings instance
*/
public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings)
public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings): void
{
$blog_settings->periodical->put('periodical_active', !empty($_POST['periodical_active']));
$blog_settings->periodical->put('periodical_upddate', !empty($_POST['periodical_upddate']));
$blog_settings->periodical->put('periodical_updurl', !empty($_POST['periodical_updurl']));
$blog_settings->get('periodical')->put('periodical_active', !empty($_POST['periodical_active']));
$blog_settings->get('periodical')->put('periodical_upddate', !empty($_POST['periodical_upddate']));
$blog_settings->get('periodical')->put('periodical_updurl', !empty($_POST['periodical_updurl']));
}
/**
@ -161,10 +83,10 @@ class adminPeriodical
*
* @param arrayObject $cols Columns
*/
public static function adminColumnsLists($cols)
public static function adminColumnsLists(ArrayObject $cols): void
{
$cols['periodical'] = [
__('Periodical'),
$cols[My::id()] = [
My::name(),
[
'curdt' => [true, __('Next update')],
'pub_int' => [true, __('Frequency')],
@ -182,11 +104,11 @@ class adminPeriodical
*
* @param arrayObject $sorts Sort options
*/
public static function adminFiltersLists($sorts)
public static function adminFiltersLists(ArrayObject $sorts): void
{
$sorts['periodical'] = [
__('Periodical'),
self::sortbyCombo(),
$sorts[My::id()] = [
My::name(),
My::sortbyCombo(),
'periodical_curdt',
'desc',
[__('periods per page'), 10],
@ -196,12 +118,12 @@ class adminPeriodical
/**
* Add columns period to posts list header.
*
* @param record $rs record instance
* @param arrayObject $cols Columns
* @param dcRecord $rs record instance
* @param ArrayObject $cols Columns
*/
public static function adminPostListHeader($rs, $cols)
public static function adminPostListHeader(dcRecord $rs, ArrayObject $cols): void
{
if (dcCore::app()->blog->settings->periodical->periodical_active) {
if (dcCore::app()->blog->settings->get('periodical')->get('periodical_active')) {
$cols['period'] = '<th scope="col">' . __('Period') . '</th>';
}
}
@ -209,37 +131,37 @@ class adminPeriodical
/**
* Add columns period to posts list values.
*
* @param record $rs record instance
* @param arrayObject $cols Columns
* @param dcRecord $rs record instance
* @param ArrayObject $cols Columns
*/
public static function adminPostListValue($rs, $cols)
public static function adminPostListValue(dcRecord $rs, ArrayObject $cols): void
{
if (!dcCore::app()->blog->settings->periodical->periodical_active) {
return null;
if (!dcCore::app()->blog->settings->get('periodical')->get('periodical_active')) {
return;
}
$r = self::period()->getPosts(['post_id' => $rs->post_id]);
$r = Utils::getPosts(['post_id' => $rs->f('post_id')]);
if ($r->isEmpty()) {
$name = '-';
} else {
$url = dcCore::app()->adminurl->get('admin.plugin.periodical', ['part' => 'period', 'period_id' => $r->periodical_id]);
$name = '<a href="' . $url . '#period" title="' . __('edit period') . '">' . html::escapeHTML($r->periodical_title) . '</a>';
$url = dcCore::app()->adminurl->get('admin.plugin.periodical', ['part' => 'period', 'period_id' => $r->f('periodical_id')]);
$name = '<a href="' . $url . '#period" title="' . __('edit period') . '">' . html::escapeHTML($r->f('periodical_title')) . '</a>';
}
$cols['period'] = '<td class="nowrap">' . $name . '</td>';
}
/**
* Favorites.
* Dashboard Favorites.
*
* @param dcFavorites $favs Array of favorites
*/
public static function adminDashboardFavoritesV2(dcFavorites $favs)
public static function adminDashboardFavoritesV2(dcFavorites $favs): void
{
$favs->register('periodical', [
'title' => __('Periodical'),
'url' => dcCore::app()->adminurl->get('admin.plugin.periodical'),
'small-icon' => dcPage::getPF('periodical/icon.svg'),
'large-icon' => dcPage::getPF('periodical/icon.svg'),
$favs->register(My::id(), [
'title' => My::name(),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
'small-icon' => dcPage::getPF(My::id() . '/icon.svg'),
'large-icon' => dcPage::getPF(My::id() . '/icon.svg'),
'permissions' => dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
@ -252,9 +174,9 @@ class adminPeriodical
*
* @return string HTML head
*/
public static function adminPostHeaders()
public static function adminPostHeaders(): string
{
return dcPage::jsLoad(dcPage::getPF('periodical/js/toggle.js'));
return dcPage::jsModuleLoad(My::id() . '/js/toggle.js');
}
/**
@ -262,7 +184,7 @@ class adminPeriodical
*
* @param integer $post_id Post id
*/
public static function adminBeforePostDelete($post_id)
public static function adminBeforePostDelete(int $post_id): void
{
self::delPeriod($post_id);
}
@ -272,23 +194,22 @@ class adminPeriodical
*
* @param dcPostsActions $pa dcPostsActions instance
*/
public static function adminPostsActions(dcPostsActions $pa)
public static function adminPostsActions(dcPostsActions $pa): void
{
$pa->addAction(
[__('Periodical') => [__('Add to periodical') => 'periodical_add']],
['adminPeriodical', 'callbackAdd']
[My::name() => [__('Add to periodical') => 'periodical_add']],
[self::class, 'callbackAdd']
);
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_DELETE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)) {
return null;
$pa->addAction(
[My::name() => [__('Remove from periodical') => 'periodical_remove']],
[self::class, 'callbackRemove']
);
}
$pa->addAction(
[__('Periodical') => [__('Remove from periodical') => 'periodical_remove']],
['adminPeriodical', 'callbackRemove']
);
}
/**
@ -297,7 +218,7 @@ class adminPeriodical
* @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();
@ -318,7 +239,7 @@ class adminPeriodical
self::delPeriod($post_id);
}
dcAdminNotices::addSuccessNotice(__('Posts have been removed from periodical.'));
dcPage::addSuccessNotice(__('Posts have been removed from periodical.'));
$pa->redirect(true);
}
@ -328,7 +249,7 @@ class adminPeriodical
* @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();
@ -342,10 +263,10 @@ class adminPeriodical
if (!empty($post['periodical'])) {
foreach ($posts_ids as $post_id) {
self::delPeriod($post_id);
self::addPeriod($post_id, $post['periodical']);
self::addPeriod($post_id, (int) $post['periodical']);
}
dcAdminNotices::addSuccessNotice(__('Posts have been added to periodical.'));
dcPage::addSuccessNotice(__('Posts have been added to periodical.'));
$pa->redirect(true);
}
@ -381,52 +302,52 @@ class adminPeriodical
*
* @param ArrayObject $main_items Main items
* @param ArrayObject $sidebar_items Sidebar items
* @param record $post Post record or null
* @param 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
{
# Get existing linked period
$period = '';
if ($post) {
$rs = self::period()->getPosts(['post_id' => $post->post_id]);
$period = $rs->isEmpty() ? '' : $rs->periodical_id;
if ($post !== null) {
$rs = Utils::getPosts(['post_id' => $post->f('post_id')]);
$period = $rs->isEmpty() ? '' : $rs->f('periodical_id');
}
# Set linked period form items
$sidebar_items['options-box']['items']['period'] = self::formPeriod($period);
$sidebar_items['options-box']['items']['period'] = self::formPeriod((int) $period);
}
/**
* Save linked period
*
* @param cursor $cur Current post cursor
* @param integer $post_id Post id
* @param cursor $cur Current post cursor
* @param null|int $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['periodical'])) {
return null;
if (!isset($_POST['periodical']) || $post_id === null) {
return;
}
# Delete old linked period
self::delPeriod($post_id);
# Add new linked period
self::addPeriod($post_id, $_POST['periodical']);
self::addPeriod($post_id, (int) $_POST['periodical']);
}
/**
* Posts period form field
*
* @param string $period Period
* @return null|string Period form content
* @param int $period Period
* @return string Period form content
*/
protected static function formPeriod($period = '')
private static function formPeriod(int $period = 0): string
{
$combo = self::comboPeriod();
if (empty($combo)) {
return null;
return '';
}
return
@ -441,63 +362,50 @@ class adminPeriodical
*
* @return array List of period
*/
protected static function comboPeriod()
private static function comboPeriod(): array
{
if (adminPeriodical::$combo_period === null) {
$periods = self::period()->getPeriods();
adminPeriodical::$combo_period = [];
if (empty(self::$combo_period)) {
$periods = Utils::getPeriods();
if (!$periods->isEmpty()) {
$combo = ['-' => ''];
while ($periods->fetch()) {
$combo[html::escapeHTML($periods->periodical_title)] = $periods->periodical_id;
$combo[html::escapeHTML($periods->f('periodical_title'))] = $periods->f('periodical_id');
}
adminPeriodical::$combo_period = $combo;
self::$combo_period = $combo;
}
}
return adminPeriodical::$combo_period;
return self::$combo_period;
}
/**
* Remove period from posts.
*
* @param integer $post_id Post id
* @param int $post_id Post id
*/
protected static function delPeriod($post_id)
private static function delPeriod(int $post_id): void
{
if ($post_id === null) {
return null;
}
$post_id = (int) $post_id;
self::period()->delPost($post_id);
Utils::delPost((int) $post_id);
}
/**
* Add period to posts
*
* @param integer $post_id Post id
* @param array $period Period
* @param int $post_id Post id
* @param int $period_id Period
*/
protected static function addPeriod($post_id, $period)
private static function addPeriod(int $post_id, int $period_id): void
{
# Not saved
if ($post_id === null || empty($period)) {
return null;
}
# Get periods
$period = self::period()->getPeriods(['periodical_id' => $period]);
$period = Utils::getPeriods(['periodical_id' => $period_id]);
# No period
if ($period->isEmpty()) {
return null;
return;
}
$post_id = (int) $post_id;
# Add relation
self::period()->addPost($period->periodical_id, $post_id);
Utils::addPost($period_id, $post_id);
}
}

102
src/Dater.php 100644
View File

@ -0,0 +1,102 @@
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\periodical;
use DateTimeZone;
use dcCore;
use Exception;
/**
* Tools to manupilate period date
*/
class Dater
{
/**
* Format a date from UTC to user TZ
*/
public static function fromUser(string $date, string $format = 'Y-m-d H:i:00'): string
{
$d = date_create($date, new DateTimeZone(dcCore::app()->auth->getInfo('user_tz')));
return $d ? date_format($d->setTimezone(new DateTimeZone('UTC')), $format) : '';
}
/**
* Format a date from user TZ to UTC
*/
public static function toUser(string $date, string $format = 'Y-m-d\TH:i'): string
{
$d = date_create($date, new DateTimeZone('UTC'));
return $d ? date_format($d->setTimezone(new DateTimeZone(dcCore::app()->auth->getInfo('user_tz'))), $format) : '';
}
/**
* Format a date to specific TZ (UTC by default) from another format
*/
public static function toDate(int|string $date = 'now', string $format = 'Y-m-d H:i:00', string $to_tz = 'UTC'): string
{
$d = is_int($date) ?
date_create_from_format('U', (string) $date, new DateTimeZone('UTC')) :
date_create($date, new DateTimeZone('UTC'));
return $d ? date_format($d->setTimeZone(new DateTimeZone($to_tz)), $format) : '';
}
/**
* Get next timestamp from a period
*/
public static function getNextTime(int $ts, string $period): int
{
$dt = date_create_from_format('U', (string) $ts);
if ($dt === false) {
return $ts;
}
switch($period) {
case 'hour':
$dt->modify('+1 hour');
break;
case 'halfday':
$dt->modify('+12 hours');
break;
case 'day':
$dt->modify('+1 day');
break;
case 'week':
$dt->modify('+1 week');
break;
case 'month':
$dt->modify('+1 month');
break;
default:
throw new Exception(__('Unknow frequence'));
}
return (int) $dt->format('U');
}
}

View File

@ -10,155 +10,153 @@
* @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);
if (!in_array(dcCore::app()->url->type, ['default', 'feed'])) {
return null;
}
namespace Dotclear\Plugin\periodical;
dcCore::app()->blog->settings->addNamespace('periodical');
dcCore::app()->addBehavior(
'publicBeforeDocumentV2',
['publicPeriodical', 'publicBeforeDocument']
);
use dcBlog;
use dcCore;
use dcNsProcess;
use Exception;
/**
* @ingroup DC_PLUGIN_PERIODICAL
* @brief Periodical - public methods.
* @since 2.6
* Update posts from periods on frontend
*/
class publicPeriodical
class Frontend extends dcNsProcess
{
/**
* Publish periodical
*/
public static function publicBeforeDocument()
public static function init(): bool
{
try {
$per = new periodical();
$s = dcCore::app()->blog->settings->periodical;
static::$init = defined('DC_RC_PATH')
&& in_array(dcCore::app()->url->type, ['default', 'feed']);
$per->lockUpdate();
return static::$init;
}
# Get periods
$periods = dcCore::app()->auth->sudo([$per, 'getPeriods']);
# No period
if ($periods->isEmpty()) {
$per->unlockUpdate();
return null;
}
$now = dt::toUTC(time());
$posts_order = $s->periodical_pub_order;
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 . initPeriodical::PERIOD_TABLE_NAME);
while ($periods->fetch()) {
# Check if period is ongoing
$cur_tz = strtotime($periods->periodical_curdt);
$end_tz = strtotime($periods->periodical_enddt);
$now_tz = $now + dt::getTimeOffset($periods->periodical_tz, $now);
if ($cur_tz < $now_tz && $now_tz < $end_tz) {
$last_nb = 0;
$last_tz = $cur_tz;
$max_nb = $periods->periodical_pub_nb;
$max_tz = $end_tz < $now_tz ? $end_tz : $now_tz;
# Calculate nb of posts to get
$loop_tz = $cur_tz;
$limit = 0;
try {
while (1) {
if ($loop_tz > $max_tz) {
break;
}
$loop_tz = $per->getNextTime($loop_tz, $periods->periodical_pub_int);
$limit += 1;
}
} catch (Exception $e) {
}
# If period need update
if ($limit > 0) {
# Get posts to publish related to this period
$posts_params = [];
$posts_params['periodical_id'] = $periods->periodical_id;
$posts_params['post_status'] = '-2';
$posts_params['order'] = $posts_order;
$posts_params['limit'] = $limit * $max_nb;
$posts_params['no_content'] = true;
$posts = dcCore::app()->auth->sudo([$per, 'getPosts'], $posts_params);
if (!$posts->isEmpty()) {
$cur_post = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME);
while ($posts->fetch()) {
# Publish post with right date
$cur_post->clean();
$cur_post->post_status = 1;
# Update post date with right date
if ($s->periodical_upddate) {
$cur_post->post_dt = date('Y-m-d H:i:s', $last_tz);
$cur_post->post_tz = $periods->periodical_tz;
} else {
$cur_post->post_dt = $posts->post_dt;
}
# Also update post url with right date
if ($s->periodical_updurl) {
$cur_post->post_url = dcCore::app()->blog->getPostURL('', $cur_post->post_dt, $posts->post_title, $posts->post_id);
}
$cur_post->update(
'WHERE post_id = ' . $posts->post_id . ' ' .
"AND blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' "
);
# Delete post relation to this period
$per->delPost($posts->post_id);
$last_nb++;
# Increment upddt if nb of publishing is to the max
if ($last_nb == $max_nb) {
$last_tz = $per->getNextTime($last_tz, $periods->periodical_pub_int);
$last_nb = 0;
}
# --BEHAVIOR-- periodicalAfterPublishedPeriodicalEntry
dcCore::app()->callBehavior('periodicalAfterPublishedPeriodicalEntry', $posts, $periods);
}
dcCore::app()->blog->triggerBlog();
}
}
# Update last published date of this period even if there's no post to publish
$cur_period->clean();
$cur_period->periodical_curdt = date('Y-m-d H:i:s', $loop_tz);
$cur_period->update(
'WHERE periodical_id = ' . $periods->periodical_id . ' ' .
"AND blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' "
);
}
}
$per->unlockUpdate();
} catch (Exception $e) {
if (isset($per)) {
$per->unlockUpdate();
}
return null;
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehavior('publicBeforeDocumentV2', function (): void {
try {
$s = dcCore::app()->blog->settings->get(My::id());
Utils::lockUpdate();
# Get periods
$periods = dcCore::app()->auth->sudo([Utils::class, 'getPeriods']);
# No period
if ($periods->isEmpty()) {
Utils::unlockUpdate();
return;
}
$now_ts = (int) Dater::toDate('now', 'U');
$posts_order = $s->get('periodical_pub_order');
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);
while ($periods->fetch()) {
# Check if period is ongoing
$cur_ts = (int) Dater::toDate($periods->f('periodical_curdt'), 'U');
$end_ts = (int) Dater::toDate($periods->f('periodical_enddt'), 'U');
if ($cur_ts < $now_ts && $now_ts < $end_ts) {
$max_nb = (int) $periods->f('periodical_pub_nb');
$last_nb = 0;
$last_ts = $loop_ts = $cur_ts;
$limit = 0;
try {
while (1) {
if ($loop_ts > $now_ts) {
break;
}
$loop_ts = Dater::getNextTime($loop_ts, $periods->f('periodical_pub_int'));
$limit += 1;
}
} catch (Exception $e) {
}
# If period need update
if ($limit > 0) {
# Get posts to publish related to this period
$posts_params = [];
$posts_params['periodical_id'] = $periods->f('periodical_id');
$posts_params['post_status'] = dcBlog::POST_PENDING;
$posts_params['order'] = $posts_order;
$posts_params['limit'] = $limit * $max_nb;
$posts_params['no_content'] = true;
$posts = dcCore::app()->auth->sudo([Utils::class, 'getPosts'], $posts_params);
if (!$posts->isEmpty()) {
$cur_post = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME);
while ($posts->fetch()) {
# Publish post with right date
$cur_post->clean();
$cur_post->setField('post_status', dcBlog::POST_PUBLISHED);
# Update post date with right date
if ($s->get('periodical_upddate')) {
$cur_post->setField('post_dt', Dater::toDate($last_ts, 'Y-m-d H:i:00', $posts->post_tz));
} else {
$cur_post->setField('post_dt', $posts->f('post_dt'));
}
# Also update post url with right date
if ($s->get('periodical_updurl')) {
$cur_post->setField('post_url', dcCore::app()->blog->getPostURL(
'',
$cur_post->getField('post_dt'),
$posts->f('post_title'),
$posts->f('post_id')
));
}
$cur_post->update(
'WHERE post_id = ' . $posts->f('post_id') . ' ' .
"AND blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' "
);
# Delete post relation to this period
Utils::delPost((int) $posts->f('post_id'));
$last_nb++;
# Increment upddt if nb of publishing is to the max
if ($last_nb == $max_nb) {
$last_ts = Dater::getNextTime($last_ts, $periods->f('periodical_pub_int'));
$last_nb = 0;
}
# --BEHAVIOR-- periodicalAfterPublishedPeriodicalEntry
dcCore::app()->callBehavior('periodicalAfterPublishedPeriodicalEntry', $posts, $periods);
}
dcCore::app()->blog->triggerBlog();
}
}
# Update last published date of this period even if there's no post to publish
$cur_period->clean();
$cur_period->setField('periodical_curdt', Dater::toDate($loop_ts, 'Y-m-d H:i:00'));
$cur_period->update(
'WHERE periodical_id = ' . $periods->f('periodical_id') . ' ' .
"AND blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' "
);
}
}
Utils::unlockUpdate();
} catch (Exception $e) {
Utils::unlockUpdate();
}
});
return true;
}
}

View File

@ -10,51 +10,64 @@
* @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);
try {
# Check installed version
if (!dcCore::app()->newVersion(
basename(__DIR__),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
)) {
return null;
namespace Dotclear\Plugin\periodical;
use dbStruct;
use dcCore;
use dcNsProcess;
use Exception;
class Install extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
return static::$init;
}
# Tables
$t = new dbStruct(dcCore::app()->con, dcCore::app()->prefix);
public static function process(): bool
{
if (!static::$init) {
return false;
}
# Table principale des sondages
$t->{initPeriodical::PERIOD_TABLE_NAME}
->periodical_id('bigint', 0, false)
->blog_id('varchar', 32, false)
->periodical_type('varchar', 32, false, "'post'")
->periodical_title('varchar', 255, false, "''")
->periodical_tz('varchar', 128, false, "'UTC'")
->periodical_curdt('timestamp', 0, false, ' now()')
->periodical_enddt('timestamp', 0, false, 'now()')
->periodical_pub_int('varchar', 32, false, "'day'")
->periodical_pub_nb('smallint', 0, false, 1)
try {
# Tables
$t = new dbStruct(dcCore::app()->con, dcCore::app()->prefix);
->primary('pk_periodical', 'periodical_id')
->index('idx_periodical_type', 'btree', 'periodical_type');
# Table principale des sondages
$t->{My::TABLE_NAME} // @phpstan-ignore-line
->periodical_id('bigint', 0, false)
->blog_id('varchar', 32, false)
->periodical_type('varchar', 32, false, "'post'")
->periodical_title('varchar', 255, false, "''")
->periodical_curdt('timestamp', 0, false, ' now()')
->periodical_enddt('timestamp', 0, false, 'now()')
->periodical_pub_int('varchar', 32, false, "'day'")
->periodical_pub_nb('smallint', 0, false, 1)
$ti = new dbStruct(dcCore::app()->con, dcCore::app()->prefix);
$changes = $ti->synchronize($t);
->primary('pk_periodical', 'periodical_id')
->index('idx_periodical_type', 'btree', 'periodical_type');
# Settings
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
$s = dcCore::app()->blog->settings->__get(basename(__DIR__));
$s->put('periodical_active', false, 'boolean', 'Enable extension', false, true);
$s->put('periodical_upddate', true, 'boolean', 'Update post date', false, true);
$s->put('periodical_updurl', false, 'boolean', 'Update post url', false, true);
$s->put('periodical_pub_order', 'post_dt asc', 'string', 'Order of publication', false, true);
(new dbStruct(dcCore::app()->con, dcCore::app()->prefix))->synchronize($t);
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
# Settings
$s = dcCore::app()->blog->settings->get(My::id());
$s->put('periodical_active', false, 'boolean', 'Enable extension', false, true);
$s->put('periodical_upddate', true, 'boolean', 'Update post date', false, true);
$s->put('periodical_updurl', false, 'boolean', 'Update post url', false, true);
$s->put('periodical_pub_order', 'post_dt asc', 'string', 'Order of publication', false, true);
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
return false;
}
}
}
return false;

View File

@ -10,123 +10,172 @@
* @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);
dcPage::check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_USAGE, dcAuth::PERMISSION_CONTENT_ADMIN]));
namespace Dotclear\Plugin\periodical;
# Objects
$per = new periodical();
use adminGenericFilter;
use dcAuth;
use dcCore;
use dcNsProcess;
use dcPage;
use Exception;
use form;
use http;
# Default values
$action = $_POST['action'] ?? '';
/**
* Admin page for periods
*/
class Manage extends dcNsProcess
{
public static function init(): bool
{
static::$init == defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id);
# Delete periods and related posts links
if ($action == 'deleteperiods' && !empty($_POST['periods'])) {
try {
foreach ($_POST['periods'] as $id) {
$id = (int) $id;
$per->delPeriodPosts($id);
$per->delPeriod($id);
// call period manage page
if (($_REQUEST['part'] ?? 'periods') === 'period') {
static::$init = ManagePeriod::init();
}
dcAdminNotices::addSuccessNotice(
__('Periods removed.')
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
if (($_REQUEST['part'] ?? 'periods') === 'period') {
return ManagePeriod::process();
}
# Default values
$vars = ManageVars::init();
# Delete periods and related posts links
if ($vars->action == 'deleteperiods' && !empty($vars->periods)) {
try {
foreach ($vars->periods as $id) {
Utils::delPeriodPosts($id);
Utils::delPeriod($id);
}
dcPage::addSuccessNotice(
__('Periods removed.')
);
if (!empty($vars->redir)) {
http::redirect($vars->redir);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id(), ['part' => 'periods']);
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
# Delete periods related posts links (without delete periods)
if ($vars->action == 'emptyperiods' && !empty($vars->periods)) {
try {
foreach ($vars->periods as $id) {
Utils::delPeriodPosts($id);
}
dcPage::addSuccessNotice(
__('Periods emptied.')
);
if (!empty($vars->redir)) {
http::redirect($vars->redir);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id(), ['part' => 'periods']);
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
return true;
}
/**
* Renders the page.
*/
public static function render(): void
{
if (!static::$init) {
return;
}
if (($_REQUEST['part'] ?? 'periods') === 'period') {
ManagePeriod::render();
return;
}
# Filters
$p_filter = new adminGenericFilter(dcCore::app(), My::id());
$p_filter->add('part', 'periods');
$params = $p_filter->params();
# Get periods
try {
$periods = Utils::getPeriods($params);
$counter = Utils::getPeriods($params, true);
$period_list = new ManageList(dcCore::app(), $periods, $counter->f(0));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
# Display
dcPage::openModule(
My::name(),
dcPage::jsModuleLoad(My::id() . '/js/checkbox.js') .
$p_filter->js(dcCore::app()->adminurl->get('admin.plugin.' . My::id(), ['part' => 'periods']))
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'periods']);
echo dcPage::breadcrumb([
__('Plugins') => '',
My::name() => '',
]) .
dcPage::notices() .
'<p class="top-add">
<a class="button add" href="' . dcCore::app()->admin->getPageURL() . '&amp;part=period">' . __('New period') . '</a>
</p>';
if (isset($period_list)) {
# Filters
$p_filter->display('admin.plugin.' . My::id(), form::hidden('p', My::id()) . form::hidden('part', 'periods'));
# Periods list
$period_list->periodDisplay(
$p_filter,
'<form action="' . dcCore::app()->admin->getPageURL() . '" method="post" id="form-periods">' .
'%s' .
'<div class="two-cols">' .
'<p class="col checkboxes-helpers"></p>' .
'<p class="col right">' . __('Selected periods action:') . ' ' .
form::combo('action', My::periodsActionCombo()) .
'<input type="submit" value="' . __('ok') . '" /></p>' .
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.' . My::id(), array_merge(['p' => My::id()], $p_filter->values(true))) .
dcCore::app()->formNonce() .
'</div>' .
'</form>'
);
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
dcPage::helpBlock('periodical');
dcPage::closeModule();
}
}
# Delete periods related posts links (without delete periods)
if ($action == 'emptyperiods' && !empty($_POST['periods'])) {
try {
foreach ($_POST['periods'] as $id) {
$id = (int) $id;
$per->delPeriodPosts($id);
}
dcAdminNotices::addSuccessNotice(
__('Periods emptied.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'periods']);
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
$combo_action = [
__('empty periods') => 'emptyperiods',
__('delete periods') => 'deleteperiods',
];
# Filters
$p_filter = new adminGenericFilter(dcCore::app(), 'periodical');
$p_filter->add('part', 'periods');
$params = $p_filter->params();
# Get periods
try {
$periods = $per->getPeriods($params);
$counter = $per->getPeriods($params, true);
$period_list = new adminPeriodicalList(dcCore::app(), $periods, $counter->f(0));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
# Display
echo
'<html><head><title>' . __('Periodical') . '</title>' .
dcPage::jsLoad(dcPage::getPF('periodical/js/checkbox.js')) .
$p_filter->js(dcCore::app()->adminurl->get('admin.plugin.periodical', ['part' => 'periods'])) .
'</head>' .
'<body>' .
dcPage::breadcrumb([
__('Plugins') => '',
__('Periodical') => '',
]) .
dcPage::notices() .
'<p class="top-add">
<a class="button add" href="' . dcCore::app()->admin->getPageURL() . '&amp;part=period">' . __('New period') . '</a>
</p>';
if (isset($period_list)) {
# Filters
$p_filter->display('admin.plugin.periodical', form::hidden('p', 'periodical') . form::hidden('part', 'periods'));
# Periods list
$period_list->periodDisplay(
$p_filter,
'<form action="' . dcCore::app()->admin->getPageURL() . '" method="post" id="form-periods">' .
'%s' .
'<div class="two-cols">' .
'<p class="col checkboxes-helpers"></p>' .
'<p class="col right">' . __('Selected periods action:') . ' ' .
form::combo('action', $combo_action) .
'<input type="submit" value="' . __('ok') . '" /></p>' .
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.periodical', array_merge(['p' => 'periodical'], $p_filter->values(true))) .
dcCore::app()->formNonce() .
'</div>' .
'</form>'
);
}
dcPage::helpBlock('periodical');
echo '</body></html>';

View File

@ -10,20 +10,30 @@
* @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);
namespace Dotclear\Plugin\periodical;
use ArrayObject;
use adminGenericFilter;
use adminGenericList;
use adminPostFilter;
use dcAuth;
use dcBlog;
use dcCore;
use dcPager;
use dt;
use html;
use form;
/**
* @ingroup DC_PLUGIN_PERIODICAL
* @brief Periodical - admin pager methods.
* @since 2.6
*/
class adminPeriodicalList extends adminGenericList
class ManageList extends adminGenericList
{
private $periodical = null;
public function periodDisplay($filter, $enclose_block = '')
public function periodDisplay(adminGenericFilter $filter, string $enclose_block = ''): void
{
if ($this->rs->isEmpty()) {
if ($filter->show()) {
@ -32,9 +42,8 @@ class adminPeriodicalList extends adminGenericList
echo '<p><strong>' . __('No period') . '</strong></p>';
}
} else {
$this->periodical = new periodical();
$pager = new dcPager((int) $filter->page, $this->rs_count, $filter->nb, 10);
$pager->var_page = 'page';
$pager = new dcPager((int) $filter->value('page'), (int) $this->rs_count, (int) $filter->value('nb'), 10);
$pager->var_page = 'page';
$periods = [];
if (isset($_REQUEST['periods'])) {
@ -58,7 +67,7 @@ class adminPeriodicalList extends adminGenericList
'enddt' => '<th scope="col" class="nowrap">' . __('End date') . '</th>',
]);
$this->userColumns('periodical', $cols);
$this->userColumns(My::id(), $cols);
$html_block .= '<tr>' . implode(iterator_to_array($cols)) . '</tr>%s</table>%s</div>';
if ($enclose_block) {
@ -69,17 +78,17 @@ class adminPeriodicalList extends adminGenericList
echo $pager->getLinks() . $blocks[0];
while ($this->rs->fetch()) {
echo $this->periodLine(isset($periods[$this->rs->periodical_id]));
echo $this->periodLine(isset($periods[(int) $this->rs->f('periodical_id')]));
}
echo $blocks[1] . $blocks[2] . $pager->getLinks();
}
}
private function periodLine($checked)
private function periodLine(bool $checked): string
{
$nb_posts = $this->periodical->getPosts(['periodical_id' => $this->rs->periodical_id], true)->f(0);
$url = dcCore::app()->adminurl->get('admin.plugin.periodical', ['part' => 'period', 'period_id' => $this->rs->periodical_id]);
$nb_posts = Utils::getPosts(['periodical_id' => $this->rs->f('periodical_id')], true)->f(0);
$url = dcCore::app()->adminurl->get('admin.plugin.periodical', ['part' => 'period', 'period_id' => $this->rs->f('periodical_id')]);
$name = '<a href="' . $url . '#period" title="' . __('edit period') . '">' . html::escapeHTML($this->rs->periodical_title) . '</a>';
@ -87,28 +96,28 @@ class adminPeriodicalList extends adminGenericList
'<a href="' . $url . '#posts" title="' . __('view related entries') . '">' . $nb_posts . '</a>' :
'0';
$interval = in_array($this->rs->periodical_pub_int, $this->periodical->getTimesCombo()) ?
__(array_search($this->rs->periodical_pub_int, $this->periodical->getTimesCombo())) : __('Unknow frequence');
$interval = in_array($this->rs->f('periodical_pub_int'), My::periodCombo()) ?
__((string) array_search($this->rs->f('periodical_pub_int'), My::periodCombo())) : __('Unknow frequence');
$cols = new ArrayObject([
'check' => '<td class="nowrap">' . form::checkbox(['periods[]'], $this->rs->periodical_id, ['checked' => $checked]) . '</td>',
'check' => '<td class="nowrap">' . form::checkbox(['periods[]'], $this->rs->f('periodical_id'), ['checked' => $checked]) . '</td>',
'name' => '<td class="maximal">' . $name . '</td>',
'curdt' => '<td class="nowrap count">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->periodical_curdt) . '</td>',
'curdt' => '<td class="nowrap count">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->f('periodical_curdt'), dcCore::app()->auth->getInfo('user_tz')) . '</td>',
'pub_int' => '<td class="nowrap">' . $interval . '</td>',
'pub_nb' => '<td class="nowrap count">' . $this->rs->periodical_pub_nb . '</td>',
'pub_nb' => '<td class="nowrap count">' . $this->rs->f('periodical_pub_nb') . '</td>',
'nbposts' => '<td class="nowrap count">' . $posts . '</td>',
'enddt' => '<td class="nowrap count">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->periodical_enddt) . '</td>',
'enddt' => '<td class="nowrap count">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->f('periodical_enddt'), dcCore::app()->auth->getInfo('user_tz')) . '</td>',
]);
$this->userColumns('periodical', $cols);
$this->userColumns(My::id(), $cols);
return
'<tr class="line ' . ($nb_posts ? '' : ' offline') . '" id="p' . $this->rs->periodical_id . '">' .
'<tr class="line ' . ($nb_posts ? '' : ' offline') . '" id="p' . $this->rs->f('periodical_id') . '">' .
implode(iterator_to_array($cols)) .
'</tr>';
}
public function postDisplay($filter, $base_url, $enclose_block = '')
public function postDisplay(adminPostFilter $filter, string $base_url, string $enclose_block = ''): void
{
$echo = '';
if ($this->rs->isEmpty()) {
@ -118,7 +127,7 @@ class adminPeriodicalList extends adminGenericList
echo '<p><strong>' . __('No entry') . '</strong></p>';
}
} else {
$pager = new dcPager($filter->page, $this->rs_count, $filter->nb, 10);
$pager = new dcPager((int) $filter->value('page'), (int) $this->rs_count, (int) $filter->value('nb'), 10);
$pager->base_url = $base_url;
$pager->var_page = 'page';
@ -153,7 +162,7 @@ class adminPeriodicalList extends adminGenericList
echo $pager->getLinks() . $blocks[0];
while ($this->rs->fetch()) {
echo $this->postLine(isset($periodical_entries[$this->rs->post_id]));
echo $this->postLine(isset($periodical_entries[(int) $this->rs->f('post_id')]));
}
$img = '<img alt="%1$s" title="%1$s" src="images/%2$s" /> %1$s';
@ -170,7 +179,7 @@ class adminPeriodicalList extends adminGenericList
}
}
private function postLine($checked)
private function postLine(bool $checked): string
{
if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CATEGORIES]), dcCore::app()->blog->id)) {
$cat_link = '<a href="category.php?id=%s">%s</a>';
@ -178,11 +187,11 @@ class adminPeriodicalList extends adminGenericList
$cat_link = '%2$s';
}
if ($this->rs->cat_title) {
if ($this->rs->f('cat_title')) {
$cat_title = sprintf(
$cat_link,
$this->rs->cat_id,
html::escapeHTML($this->rs->cat_title)
$this->rs->f('cat_id'),
html::escapeHTML($this->rs->f('cat_title'))
);
} else {
$cat_title = __('None');
@ -190,35 +199,35 @@ class adminPeriodicalList extends adminGenericList
$img_status = '';
$img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
switch ($this->rs->post_status) {
case 1:
switch ((int) $this->rs->f('post_status')) {
case dcBlog::POST_PUBLISHED:
$img_status = sprintf($img, __('published'), 'check-on.png');
break;
case 0:
case dcBlog::POST_UNPUBLISHED:
$img_status = sprintf($img, __('unpublished'), 'check-off.png');
break;
case -1:
case dcBlog::POST_SCHEDULED:
$img_status = sprintf($img, __('scheduled'), 'scheduled.png');
break;
case -2:
case dcBlog::POST_PENDING:
$img_status = sprintf($img, __('pending'), 'check-wrn.png');
break;
}
$protected = '';
if ($this->rs->post_password) {
if ($this->rs->f('post_password')) {
$protected = sprintf($img, __('protected'), 'locker.png');
}
$selected = '';
if ($this->rs->post_selected) {
if ($this->rs->f('post_selected')) {
$selected = sprintf($img, __('selected'), 'selected.png');
}
@ -230,14 +239,14 @@ class adminPeriodicalList extends adminGenericList
}
$cols = [
'check' => '<td class="minimal">' . form::checkbox(['periodical_entries[]'], $this->rs->post_id, ['checked' => $checked]) . '</td>',
'title' => '<td class="maximal"><a href="' . dcCore::app()->getPostAdminURL($this->rs->post_type, $this->rs->post_id) . '" ' .
'check' => '<td class="minimal">' . form::checkbox(['periodical_entries[]'], $this->rs->f('post_id'), ['checked' => $checked]) . '</td>',
'title' => '<td class="maximal"><a href="' . dcCore::app()->getPostAdminURL($this->rs->f('post_type'), $this->rs->f('post_id')) . '" ' .
'title="' . html::escapeHTML($this->rs->getURL()) . '">' . html::escapeHTML($this->rs->post_title) . '</a></td>',
'date' => '<td class="nowrap">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->post_dt) . '</td>',
'date' => '<td class="nowrap">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->f('post_dt')) . '</td>',
'category' => '<td class="nowrap">' . $cat_title . '</td>',
'author' => '<td class="nowrap">' . $this->rs->user_id . '</td>',
'author' => '<td class="nowrap">' . $this->rs->f('user_id') . '</td>',
'status' => '<td class="nowrap status">' . $img_status . ' ' . $selected . ' ' . $protected . ' ' . $attach . '</td>',
'create' => '<td class="nowrap">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->post_creadt, dcCore::app()->auth->getInfo('user_tz')) . '</td>',
'create' => '<td class="nowrap">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->f('post_creadt'), dcCore::app()->auth->getInfo('user_tz')) . '</td>',
];
return '<tr class="line">' . implode($cols) . '</tr>';

View File

@ -10,357 +10,312 @@
* @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);
dcPage::check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_USAGE, dcAuth::PERMISSION_CONTENT_ADMIN]));
namespace Dotclear\Plugin\periodical;
# Objects
$per = new periodical();
use adminPostFilter;
use dcAuth;
use dcCore;
use dcNsProcess;
use dcPage;
use Exception;
use form;
use html;
use http;
# Default values
$action = $_POST['action'] ?? '';
/**
* Admin page for a period
*/
class ManagePeriod extends dcNsProcess
{
public static function init(): bool
{
static::$init == defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)
&& ($_REQUEST['part'] ?? 'periods') === 'period';
$starting_script = '';
# Default value for period
$period_id = null;
$period_title = __('One post per day');
$period_pub_nb = 1;
$period_pub_int = 'day';
$period_curdt = date('Y-m-d H:i', time());
$period_enddt = date('Y-m-d H:i', time() + 31536000); //one year
$bad_period_curdt = false;
$bad_period_enddt = false;
# Get period
if (!empty($_REQUEST['period_id'])) {
$rs = $per->getPeriods([
'periodical_id' => $_REQUEST['period_id'],
]);
if ($rs->isEmpty()) {
dcCore::app()->error->add(__('This period does not exist.'));
$period_id = null;
} else {
$period_id = $rs->periodical_id;
$period_title = $rs->periodical_title;
$period_pub_nb = $rs->periodical_pub_nb;
$period_pub_int = $rs->periodical_pub_int;
$period_curdt = date('Y-m-d H:i', strtotime($rs->periodical_curdt));
$period_enddt = date('Y-m-d H:i', strtotime($rs->periodical_enddt));
return static::$init;
}
}
# Set period
if ($action == 'setperiod') {
# Get POST values
if (!empty($_POST['period_title'])) {
$period_title = $_POST['period_title'];
}
if (!empty($_POST['period_pub_nb'])) {
$period_pub_nb = abs((int) $_POST['period_pub_nb']);
}
if (!empty($_POST['period_pub_int'])
&& in_array($_POST['period_pub_int'], $per->getTimesCombo())
) {
$period_pub_int = $_POST['period_pub_int'];
}
if (!empty($_POST['period_curdt'])) {
try {
$period_curdt = strtotime($_POST['period_curdt']);
if ($period_curdt == false || $period_curdt == -1) {
$bad_period_curdt = true;
throw new Exception(__('Invalid date'));
}
$period_curdt = date('Y-m-d H:i', $period_curdt);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
public static function process(): bool
{
if (!static::$init) {
return false;
}
}
if (!empty($_POST['period_enddt'])) {
try {
$period_enddt = strtotime($_POST['period_enddt']);
if ($period_enddt == false || $period_enddt == -1) {
$bad_period_enddt = true;
throw new Exception(__('Invalid date'));
}
$period_enddt = date('Y-m-d H:i', $period_enddt);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
# Default values
$vars = ManageVars::init();
# Get period
if ($vars->bad_period_id) {
dcCore::app()->error->add(__('This period does not exist.'));
}
}
# Check period title and dates
$old_titles = $per->getPeriods([
'periodical_title' => $period_title,
]);
if (!$old_titles->isEmpty()) {
while ($old_titles->fetch()) {
if (!$period_id || $old_titles->periodical_id != $period_id) {
dcCore::app()->error->add(__('Period title is already taken'));
# Set period
if ($vars->action == 'setperiod') {
if ($vars->bad_period_curdt || $vars->bad_period_enddt) {
dcCore::app()->error->add(__('Invalid date'));
}
# Check period title and dates
$old_titles = Utils::getPeriods([
'periodical_title' => $vars->period_title,
]);
if (!$old_titles->isEmpty()) {
while ($old_titles->fetch()) {
if (!$vars->period_id || $old_titles->f('periodical_id') != $vars->period_id) {
dcCore::app()->error->add(__('Period title is already taken'));
}
}
}
if (empty($vars->period_title)) {
dcCore::app()->error->add(__('Period title is required'));
}
if (strtotime($vars->period_curdt) > strtotime($vars->period_enddt)) {
dcCore::app()->error->add(__('Start date must be older than end date'));
}
# If no error, set period
if (!dcCore::app()->error->flag()) {
$cur = Utils::openCursor();
$cur->setField('periodical_title', $vars->period_title);
$cur->setField('periodical_curdt', $vars->period_curdt);
$cur->setField('periodical_enddt', $vars->period_enddt);
$cur->setField('periodical_pub_int', $vars->period_pub_int);
$cur->setField('periodical_pub_nb', $vars->period_pub_nb);
# Update period
if ($vars->period_id) {
Utils::updPeriod($vars->period_id, $cur);
self::redirect($vars->redir, $vars->period_id, '#period', __('Period successfully updated.'));
# Create period
} else {
$period_id = Utils::addPeriod($cur);
self::redirect($vars->redir, $period_id, '#period', __('Period successfully created.'));
}
}
}
}
if (empty($period_title)) {
dcCore::app()->error->add(__('Period title is required'));
}
if (strtotime($period_curdt) > strtotime($period_enddt)) {
dcCore::app()->error->add(__('Start date must be older than end date'));
# Actions on related posts
if (!dcCore::app()->error->flag() && $vars->period_id && $vars->action && !empty($vars->entries)) {
# Publish posts
if ($vars->action == 'publish') {
try {
foreach ($vars->entries as $id) {
dcCore::app()->blog->updPostStatus($id, 1);
Utils::delPost($id);
}
self::redirect($vars->redir, $vars->period_id, '#posts', __('Entries successfully published.'));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
# Unpublish posts
if ($vars->action == 'unpublish') {
try {
foreach ($vars->entries as $id) {
dcCore::app()->blog->updPostStatus($id, 0);
Utils::delPost($id);
}
self::redirect($vars->redir, $vars->period_id, '#posts', __('Entries successfully unpublished.'));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
# Remove posts from periodical
if ($vars->action == 'remove_post_periodical') {
try {
foreach ($vars->entries as $id) {
Utils::delPost($id);
}
self::redirect($vars->redir, $vars->period_id, '#posts', __('Entries successfully removed.'));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
}
return true;
}
# If no error, set period
if (!dcCore::app()->error->flag()) {
$cur = $per->openCursor();
$cur->periodical_title = $period_title;
$cur->periodical_curdt = $period_curdt;
$cur->periodical_enddt = $period_enddt;
$cur->periodical_pub_int = $period_pub_int;
$cur->periodical_pub_nb = $period_pub_nb;
/**
* Renders the page.
*/
public static function render(): void
{
if (!static::$init) {
return;
}
# Update period
if ($period_id) {
$per->updPeriod($period_id, $cur);
# Default values
$vars = ManageVars::init();
dcAdminNotices::addSuccessNotice(
__('Period successfully updated.')
$starting_script = '';
# Prepare combos for posts list
if ($vars->period_id > 0) {
# Filters
$post_filter = new adminPostFilter();
$post_filter->add('part', 'period');
$params = $post_filter->params();
$params['periodical_id'] = $vars->period_id;
$params['no_content'] = true;
# Get posts
try {
$posts = Utils::getPosts($params);
$counter = Utils::getPosts($params, true);
$post_list = new ManageList(dcCore::app(), $posts, $counter->f(0));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
$starting_script = dcPage::jsModuleLoad(My::id() . '/js/checkbox.js') .
$post_filter->js(dcCore::app()->adminurl->get('admin.plugin.' . My::id(), ['part' => 'period', 'period_id' => $vars->period_id], '&') . '#posts');
}
# Display
dcPage::openModule(
My::name(),
dcPage::jsModuleLoad(My::id() . '/js/dates.js') .
$starting_script .
dcPage::jsDatePicker() .
dcPage::jsPageTabs()
);
echo
dcPage::breadcrumb([
__('Plugins') => '',
My::name() => dcCore::app()->admin->getPageURL() . '&amp;part=periods',
(null === $vars->period_id ? __('New period') : __('Edit period')) => '',
]) .
dcPage::notices();
# Period form
echo '
<div id="period"><h3>' . (null === $vars->period_id ? __('New period') : __('Edit period')) . '</h3>
<form method="post" action="' . dcCore::app()->admin->getPageURL() . '">
<p><label for="period_title">' . __('Title:') . '</label>' .
form::field('period_title', 60, 255, html::escapeHTML($vars->period_title), 'maximal') . '</p>
<div class="two-boxes">
<p><label for="period_curdt">' . __('Next update:') . '</label>' .
form::datetime('period_curdt', [
'default' => html::escapeHTML(Dater::toUser($vars->period_curdt)),
'class' => ($vars->bad_period_curdt ? 'invalid' : ''),
]) . '</p>
<p><label for="period_enddt">' . __('End date:') . '</label>' .
form::datetime('period_enddt', [
'default' => html::escapeHTML(Dater::toUser($vars->period_enddt)),
'class' => ($vars->bad_period_enddt ? 'invalid' : ''),
]) . '</p>
</div><div class="two-boxes">
<p><label for="period_pub_int">' . __('Publication frequency:') . '</label>' .
form::combo('period_pub_int', My::periodCombo(), $vars->period_pub_int) . '</p>
<p><label for="period_pub_nb">' . __('Number of entries to publish every time:') . '</label>' .
form::number('period_pub_nb', ['min' => 1, 'max' => 20, 'default' => $vars->period_pub_nb]) . '</p>
</div>
<div class="clear">
<p><input type="submit" name="save" value="' . __('Save') . '" />' .
dcCore::app()->formNonce() .
form::hidden(['action'], 'setperiod') .
form::hidden(['period_id'], $vars->period_id) .
form::hidden(['part'], 'period') . '
</p>
</div>
</form>
</div>';
if ($vars->period_id && isset($post_filter) && isset($post_list) && !dcCore::app()->error->flag()) {
$base_url = dcCore::app()->admin->getPageURL() .
'&amp;period_id=' . $vars->period_id .
'&amp;part=period' .
'&amp;user_id=' . $post_filter->value('user_id', '') .
'&amp;cat_id=' . $post_filter->value('cat_id', '') .
'&amp;status=' . $post_filter->value('status', '') .
'&amp;selected=' . $post_filter->value('selected', '') .
'&amp;attachment=' . $post_filter->value('attachment', '') .
'&amp;month=' . $post_filter->value('month', '') .
'&amp;lang=' . $post_filter->value('lang', '') .
'&amp;sortby=' . $post_filter->value('sortby', '') .
'&amp;order=' . $post_filter->value('order', '') .
'&amp;nb=' . $post_filter->value('nb', '') .
'&amp;page=%s' .
'#posts';
echo '
<div id="posts"><h3>' . __('Entries linked to this period') . '</h3>';
# Filters
$post_filter->display(
['admin.plugin.periodical', '#posts'],
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.periodical', [
'period_id' => $vars->period_id,
'part' => 'period',
])
);
# Create period
# Posts list
$post_list->postDisplay(
$post_filter,
$base_url,
'<form action="' . dcCore::app()->admin->getPageURL() . '" method="post" id="form-entries">' .
'%s' .
'<div class="two-cols">' .
'<p class="col checkboxes-helpers"></p>' .
'<p class="col right">' . __('Selected entries action:') . ' ' .
form::combo('action', My::entriesActionsCombo()) .
'<input type="submit" value="' . __('ok') . '" /></p>' .
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.periodical', array_merge($post_filter->values(), [
'period_id' => $vars->period_id,
'redir' => sprintf($base_url, $post_filter->value('page', '')),
])) .
dcCore::app()->formNonce() .
'</div>' .
'</form>'
);
echo
'</div>';
}
dcPage::helpBlock('periodical');
dcPage::closeModule();
}
private static function redirect(string $redir, int $id, string $tab, string $msg): void
{
dcPage::addSuccessNotice($msg);
if (!empty($redir)) {
http::redirect($redir);
} else {
$period_id = $per->addPeriod($cur);
dcAdminNotices::addSuccessNotice(
__('Period successfully created.')
);
}
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#period');
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id(), ['part' => 'period', 'period_id' => $id], $tab);
}
}
}
# Actions on related posts
if (!dcCore::app()->error->flag() && $period_id && $action && !empty($_POST['periodical_entries'])) {
# Publish posts
if ($action == 'publish') {
try {
foreach ($_POST['periodical_entries'] as $id) {
$id = (int) $id;
dcCore::app()->blog->updPostStatus($id, 1);
$per->delPost($id);
}
dcAdminNotices::addSuccessNotice(
__('Entries successfully published.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
# Unpublish posts
if ($action == 'unpublish') {
try {
foreach ($_POST['periodical_entries'] as $id) {
$id = (int) $id;
dcCore::app()->blog->updPostStatus($id, 0);
$per->delPost($id);
}
dcAdminNotices::addSuccessNotice(
__('Entries successfully unpublished.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
# Remove posts from periodical
if ($action == 'remove_post_periodical') {
try {
foreach ($_POST['periodical_entries'] as $id) {
$id = (int) $id;
$per->delPost($id);
}
dcAdminNotices::addSuccessNotice(
__('Entries successfully removed.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
}
# Prepare combos for posts list
if ($period_id) {
# Filters
$post_filter = new adminPostFilter();
$post_filter->add('part', 'period');
$params = $post_filter->params();
$params['periodical_id'] = $period_id;
$params['no_content'] = true;
# Get posts
try {
$posts = $per->getPosts($params);
$counter = $per->getPosts($params, true);
$post_list = new adminPeriodicalList(dcCore::app(), $posts, $counter->f(0));
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
$starting_script = dcPage::jsLoad(dcPage::getPF('periodical/js/checkbox.js')) .
$post_filter->js(dcCore::app()->adminurl->get('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '&') . '#posts');
}
# Display
echo '
<html><head><title>' . __('Periodical') . '</title>' .
dcPage::jsLoad(dcPage::getPF('periodical/js/dates.js')) .
$starting_script .
dcPage::jsDatePicker() .
dcPage::jsPageTabs() .
'</head>
<body>';
echo
dcPage::breadcrumb([
__('Plugins') => '',
__('Periodical') => dcCore::app()->admin->getPageURL() . '&amp;part=periods',
(null === $period_id ? __('New period') : __('Edit period')) => '',
]) .
dcPage::notices();
# Period form
echo '
<div id="period"><h3>' . (null === $period_id ? __('New period') : __('Edit period')) . '</h3>
<form method="post" action="' . dcCore::app()->admin->getPageURL() . '">
<p><label for="period_title">' . __('Title:') . '</label>' .
form::field('period_title', 60, 255, html::escapeHTML($period_title), 'maximal') . '</p>
<div class="two-boxes">
<p><label for="period_curdt">' . __('Next update:') . '</label>' .
form::datetime('period_curdt', [
'default' => html::escapeHTML(dt::str('%Y-%m-%dT%H:%M', strtotime($period_curdt))),
'class' => ($bad_period_curdt ? 'invalid' : ''),
]) . '</p>
<p><label for="period_enddt">' . __('End date:') . '</label>' .
form::datetime('period_enddt', [
'default' => html::escapeHTML(dt::str('%Y-%m-%dT%H:%M', strtotime($period_enddt))),
'class' => ($bad_period_enddt ? 'invalid' : ''),
]) . '</p>
</div><div class="two-boxes">
<p><label for="period_pub_int">' . __('Publication frequency:') . '</label>' .
form::combo('period_pub_int', $per->getTimesCombo(), $period_pub_int) . '</p>
<p><label for="period_pub_nb">' . __('Number of entries to publish every time:') . '</label>' .
form::number('period_pub_nb', ['min' => 1, 'max' => 20, 'default' => $period_pub_nb]) . '</p>
</div>
<div class="clear">
<p><input type="submit" name="save" value="' . __('Save') . '" />' .
dcCore::app()->formNonce() .
form::hidden(['action'], 'setperiod') .
form::hidden(['period_id'], $period_id) .
form::hidden(['part'], 'period') . '
</p>
</div>
</form>
</div>';
if ($period_id && isset($post_filter) && isset($post_list) && !dcCore::app()->error->flag()) {
# Actions combo box
$combo_action = [];
$combo_action[__('Entries')][__('Publish')] = 'publish';
$combo_action[__('Entries')][__('Unpublish')] = 'unpublish';
$combo_action[__('Periodical')][__('Remove from periodical')] = 'remove_post_periodical';
$base_url = dcCore::app()->admin->getPageURL() .
'&amp;period_id=' . $period_id .
'&amp;part=period' .
'&amp;user_id=' . $post_filter->user_id .
'&amp;cat_id=' . $post_filter->cat_id .
'&amp;status=' . $post_filter->status .
'&amp;selected=' . $post_filter->selected .
'&amp;attachment=' . $post_filter->attachment .
'&amp;month=' . $post_filter->month .
'&amp;lang=' . $post_filter->lang .
'&amp;sortby=' . $post_filter->sortby .
'&amp;order=' . $post_filter->order .
'&amp;nb=' . $post_filter->nb .
'&amp;page=%s' .
'#posts';
echo '
<div id="posts"><h3>' . __('Entries linked to this period') . '</h3>';
# Filters
$post_filter->display(
['admin.plugin.periodical', '#posts'],
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.periodical', [
'period_id' => $period_id,
'part' => 'period',
])
);
# Posts list
$post_list->postDisplay(
$post_filter,
$base_url,
'<form action="' . dcCore::app()->admin->getPageURL() . '" method="post" id="form-entries">' .
'%s' .
'<div class="two-cols">' .
'<p class="col checkboxes-helpers"></p>' .
'<p class="col right">' . __('Selected entries action:') . ' ' .
form::combo('action', $combo_action) .
'<input type="submit" value="' . __('ok') . '" /></p>' .
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.periodical', array_merge($post_filter->values(), [
'period_id' => $period_id,
'redir' => sprintf($base_url, $post_filter->page),
])) .
dcCore::app()->formNonce() .
'</div>' .
'</form>'
);
echo
'</div>';
}
dcPage::helpBlock('periodical');
echo '</body></html>';

140
src/ManageVars.php 100644
View File

@ -0,0 +1,140 @@
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\periodical;
/**
* Admin page vars
*/
class ManageVars
{
/**
* @var ManageVars self instance
*/
private static $container;
public readonly string $action;
public readonly string $redir;
public readonly array $periods;
public readonly array $entries;
public readonly ?int $period_id;
public readonly string $period_title;
public readonly int $period_pub_nb;
public readonly string $period_pub_int;
public readonly string $period_curdt;
public readonly string $period_enddt;
public readonly bool $bad_period_id;
public readonly bool $bad_period_curdt;
public readonly bool $bad_period_enddt;
protected function __construct()
{
$this->action = $_POST['action'] ?? '';
$this->redir = $_POST['redir'] ?? '';
// periods
$periods = $_POST['periods'] ?? [];
$periods = is_array($periods) ? $periods : [];
array_walk($periods, function (&$v) { if ($v !== null) { $v = (int) $v; } });
$this->periods = $periods;
// entries
$entries = $_POST['periodical_entries'] ?? [];
$entries = is_array($entries) ? $entries : [];
array_walk($entries, function (&$v) {
if ($v !== null) {
$v = (int) $v;
}
});
$this->entries = $entries;
// period values from default
$period_id = null;
$period_title = __('One post per day');
$period_pub_nb = 1;
$period_pub_int = 'day';
$period_curdt = Dater::toDate('now', 'Y-m-d H:i:00');
$period_enddt = Dater::toDate('+1 year', 'Y-m-d H:i:00');
$bad_period_id = false;
$bad_period_curdt = false;
$bad_period_enddt = false;
# period values from record
if (!empty($_REQUEST['period_id'])) {
$rs = Utils::getPeriods([
'periodical_id' => $_REQUEST['period_id'],
]);
if (!$rs->isEmpty()) {
$period_id = (int) $rs->f('periodical_id');
$period_title = $rs->f('periodical_title');
$period_pub_nb = (int) $rs->f('periodical_pub_nb');
$period_pub_int = $rs->f('periodical_pub_int');
$period_curdt = Dater::toDate($rs->f('periodical_curdt'), 'Y-m-d H:i:00');
$period_enddt = Dater::toDate($rs->f('periodical_enddt'), 'Y-m-d H:i:00');
} else {
$bad_period_id = true;
}
}
# period values from POST
if (!empty($_POST['period_title'])) {
$period_title = $_POST['period_title'];
}
if (!empty($_POST['period_pub_nb'])) {
$period_pub_nb = abs((int) $_POST['period_pub_nb']);
}
if (!empty($_POST['period_pub_int'])
&& in_array($_POST['period_pub_int'], My::periodCombo())
) {
$period_pub_int = $_POST['period_pub_int'];
}
if (!empty($_POST['period_curdt'])) {
$tmp_period_curdt = Dater::fromUser($_POST['period_curdt'], 'Y-m-d H:i:00');
if (empty($tmp_period_curdt)) {
$bad_period_curdt = true;
} else {
$period_curdt = $tmp_period_curdt;
}
}
if (!empty($_POST['period_enddt'])) {
$tmp_period_enddt = Dater::fromUser($_POST['period_enddt'], 'Y-m-d H:i:00');
if (empty($tmp_period_enddt)) {
$bad_period_enddt = true;
} else {
$period_enddt = $tmp_period_enddt;
}
}
// set period values
$this->period_id = $period_id;
$this->period_title = $period_title;
$this->period_pub_nb = $period_pub_nb;
$this->period_pub_int = $period_pub_int;
$this->period_curdt = $period_curdt;
$this->period_enddt = $period_enddt;
$this->bad_period_id = $bad_period_id;
$this->bad_period_curdt = $bad_period_curdt;
$this->bad_period_enddt = $bad_period_enddt;
}
public static function init(): ManageVars
{
if (!(self::$container instanceof self)) {
self::$container = new self();
}
return self::$container;
}
}

109
src/My.php 100644
View File

@ -0,0 +1,109 @@
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\periodical;
use dcCore;
/**
* Plugin def
*/
class My
{
/** @var string Required php version */
public const PHP_MIN = '8.1';
/** @var string This plugin table name */
public const TABLE_NAME = 'periodical';
/** @var string This plugin meta type */
public const META_TYPE = 'periodical';
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
}
/**
* Check php version
*/
public static function phpCompliant(): bool
{
return version_compare(phpversion(), self::PHP_MIN, '>=');
}
/**
* Periods action combo
*/
public static function periodsActionCombo(): array
{
return [
__('empty periods') => 'emptyperiods',
__('delete periods') => 'deleteperiods',
];
}
/**
* Period entries action combo
*/
public static function entriesActionsCombo(): array
{
return [
__('Entries') => [
__('Publish') => 'publish',
__('Unpublish') => 'unpublish',
],
__('Periodical') => [
__('Remove from periodical') => 'remove_post_periodical',
],
];
}
/**
* Periods sortby combo
*/
public static function sortbyCombo(): array
{
return [
__('Next update') => 'periodical_curdt',
__('End date') => 'periodical_enddt',
__('Frequence') => 'periodical_pub_int',
];
}
/**
* Period combo
*/
public static function periodCombo(): array
{
return [
__('Hourly') => 'hour',
__('twice a day') => 'halfday',
__('Daily') => 'day',
__('Weekly') => 'week',
__('Monthly') => 'month',
];
}
}

View File

@ -10,32 +10,56 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return;
}
declare(strict_types=1);
class periodical
namespace Dotclear\Plugin\periodical;
use cursor;
use dcAuth;
use dcBlog;
use dcCore;
use dcMeta;
use dcRecord;
use Exception;
use files;
use path;
/**
* Manage records
*/
class Utils
{
public $con;
/** @var null|resource Lock update process */
private static $lock = null;
protected $table;
protected $blog;
private $lock = null;
public function __construct()
/**
* Get escaped blog id
*/
private static function blog(): string
{
$this->con = dcCore::app()->con;
$this->table = dcCore::app()->con->escape(dcCore::app()->prefix . initPeriodical::PERIOD_TABLE_NAME);
$this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id);
return dcCore::app()->con->escapeStr(dcCore::app()->blog->id);
}
public function openCursor()
/**
* Get escaped periodical full table name
*/
private static function table(): string
{
return $this->con->openCursor($this->table);
return dcCore::app()->con->escapeStr(dcCore::app()->prefix . My::TABLE_NAME);
}
# Get periods
public function getPeriods($params = [], $count_only = false)
/**
* Get periodical table cursor
*/
public static function openCursor(): cursor
{
return dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME);
}
/**
* Get periods
*/
public static function getPeriods(array $params = [], bool $count_only = false): dcRecord
{
if ($count_only) {
$q = 'SELECT count(T.periodical_id) ';
@ -45,125 +69,123 @@ class periodical
if (!empty($params['columns']) && is_array($params['columns'])) {
$q .= implode(', ', $params['columns']) . ', ';
}
$q .= 'T.periodical_title, T.periodical_tz, ' .
$q .= 'T.periodical_title, ' .
'T.periodical_curdt, T.periodical_enddt, ' .
'T.periodical_pub_int, T.periodical_pub_nb ';
}
$q .= 'FROM ' . $this->table . ' T ';
$q .= 'FROM ' . self::table() . ' T ';
if (!empty($params['from'])) {
$q .= $params['from'] . ' ';
}
$q .= "WHERE T.blog_id = '" . $this->blog . "' ";
$q .= "WHERE T.blog_id = '" . self::blog() . "' ";
if (isset($params['periodical_type'])) {
if (is_array($params['periodical_type']) && !empty($params['periodical_type'])) {
$q .= 'AND T.periodical_type ' . $this->con->in($params['periodical_type']);
$q .= 'AND T.periodical_type ' . dcCore::app()->con->in($params['periodical_type']);
} elseif ($params['periodical_type'] != '') {
$q .= "AND T.periodical_type = '" . $this->con->escape($params['periodical_type']) . "' ";
$q .= "AND T.periodical_type = '" . dcCore::app()->con->escapeStr($params['periodical_type']) . "' ";
}
} else {
$q .= "AND T.periodical_type = 'post' ";
}
if (!empty($params['periodical_id'])) {
if (is_array($params['periodical_id'])) {
array_walk($params['periodical_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}'));
array_walk($params['periodical_id'], function ($v) { if ($v !== null) { $v = (int) $v; } });
} else {
$params['periodical_id'] = [(int) $params['periodical_id']];
}
$q .= 'AND T.periodical_id ' . $this->con->in($params['periodical_id']);
$q .= 'AND T.periodical_id ' . dcCore::app()->con->in($params['periodical_id']);
}
if (!empty($params['periodical_title'])) {
$q .= "AND T.periodical_title = '" . $this->con->escape($params['periodical_title']) . "' ";
$q .= "AND T.periodical_title = '" . dcCore::app()->con->escapeStr($params['periodical_title']) . "' ";
}
if (!empty($params['sql'])) {
$q .= $params['sql'] . ' ';
}
if (!$count_only) {
if (!empty($params['order'])) {
$q .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
$q .= 'ORDER BY ' . dcCore::app()->con->escapeStr($params['order']) . ' ';
} else {
$q .= 'ORDER BY T.periodical_id ASC ';
}
}
if (!$count_only && !empty($params['limit'])) {
$q .= $this->con->limit($params['limit']);
$q .= dcCore::app()->con->limit($params['limit']);
}
$rs = $this->con->select($q);
//$rs->core = dcCore::app();
//$rs->periodical = $this;
return $rs;
return new dcRecord(dcCore::app()->con->select($q));
}
public function addPeriod($cur)
/**
* Add a period
*/
public static function addPeriod(cursor $cur): int
{
$this->con->writeLock($this->table);
dcCore::app()->con->writeLock(self::table());
try {
$id = $this->con->select(
'SELECT MAX(periodical_id) FROM ' . $this->table
$id = dcCore::app()->con->select(
'SELECT MAX(periodical_id) FROM ' . self::table()
)->f(0) + 1;
$cur->periodical_id = $id;
$cur->blog_id = $this->blog;
$cur->periodical_type = 'post';
$cur->periodical_tz = dcCore::app()->auth->getInfo('user_tz');
$cur->setField('periodical_id', $id);
$cur->setField('blog_id', self::blog());
$cur->setField('periodical_type', 'post');
$cur->insert();
$this->con->unlock();
dcCore::app()->con->unlock();
} catch (Exception $e) {
$this->con->unlock();
dcCore::app()->con->unlock();
throw $e;
}
return $cur->periodical_id;
return (int) $cur->getField('periodical_id');
}
public function updPeriod($period_id, $cur)
/**
* Update a period
*/
public static function updPeriod(int $period_id, cursor $cur): void
{
$period_id = (int) $period_id;
if ($cur->periodical_tz == ''
&& ($cur->periodical_curdt != '' || $cur->periodical_enddt != '')) {
$cur->periodical_tz = dcCore::app()->auth->getInfo('user_tz');
}
$cur->update(
"WHERE blog_id = '" . $this->blog . "' " .
"WHERE blog_id = '" . self::blog() . "' " .
'AND periodical_id = ' . $period_id . ' '
);
}
# Delete a period
public function delPeriod($period_id)
/**
* Delete a period
*/
public static function delPeriod(int $period_id): void
{
$period_id = (int) $period_id;
$params = [];
$params['periodical_id'] = $period_id;
$params['post_status'] = '';
$rs = $this->getPosts($params);
$rs = self::getPosts($params);
if (!$rs->isEmpty()) {
throw new Exception('Periodical is not empty');
}
$this->con->execute(
'DELETE FROM ' . $this->table . ' ' .
"WHERE blog_id = '" . $this->blog . "' " .
dcCore::app()->con->execute(
'DELETE FROM ' . self::table() . ' ' .
"WHERE blog_id = '" . self::blog() . "' " .
'AND periodical_id = ' . $period_id . ' '
);
}
# Remove all posts related to a period
public function delPeriodPosts($period_id)
/**
* Remove all posts related to a period
*/
public static function delPeriodPosts(int $period_id): void
{
$params = [];
$params['post_status'] = '';
$params['periodical_id'] = (int) $period_id;
$params['periodical_id'] = $period_id;
$rs = $this->getPosts($params);
$rs = self::getPosts($params);
if ($rs->isEmpty()) {
return;
@ -171,22 +193,24 @@ class periodical
$ids = [];
while ($rs->fetch()) {
$ids[] = $rs->post_id;
$ids[] = $rs->f('post_id');
}
if (empty($ids)) {
return;
}
$this->con->execute(
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . dcMeta::META_TABLE_NAME . ' ' .
"WHERE meta_type = 'periodical' " .
'AND post_id ' . $this->con->in($ids)
"WHERE meta_type = '" . My::META_TYPE . "' " .
'AND post_id ' . dcCore::app()->con->in($ids)
);
}
# Get posts related to periods
public function getPosts($params = [], $count_only = false)
/**
* Get posts related to periods
*/
public static function getPosts(array $params = [], bool $count_only = false): dcRecord
{
if (!isset($params['columns'])) {
$params['columns'] = [];
@ -204,25 +228,28 @@ class periodical
$params['columns'][] = 'T.periodical_id';
$params['columns'][] = 'T.periodical_title';
$params['columns'][] = 'T.periodical_type';
$params['columns'][] = 'T.periodical_tz';
$params['columns'][] = 'T.periodical_curdt';
$params['columns'][] = 'T.periodical_enddt';
$params['columns'][] = 'T.periodical_pub_int';
$params['columns'][] = 'T.periodical_pub_nb';
$params['join'] .= 'LEFT JOIN ' . dcCore::app()->prefix . dcMeta::META_TABLE_NAME . ' R ON P.post_id = R.post_id ';
$params['join'] .= 'LEFT JOIN ' . $this->table . ' T ON CAST(T.periodical_id as char) = CAST(R.meta_id as char) ';
$params['join'] .= 'LEFT JOIN ' . self::table() . ' T ON CAST(T.periodical_id as char) = CAST(R.meta_id as char) ';
$params['sql'] .= "AND R.meta_type = 'periodical' ";
$params['sql'] .= "AND R.meta_type = '" . My::META_TYPE . "' ";
$params['sql'] .= "AND T.periodical_type = 'post' ";
if (!empty($params['periodical_id'])) {
if (is_array($params['periodical_id'])) {
array_walk($params['periodical_id'], function ($v) { if ($v !== null) { $v = (int) $v; } });
array_walk($params['periodical_id'], function ($v) {
if ($v !== null) {
$v = (int) $v;
}
});
} else {
$params['periodical_id'] = [(int) $params['periodical_id']];
}
$params['sql'] .= 'AND T.periodical_id ' . $this->con->in($params['periodical_id']);
$params['sql'] .= 'AND T.periodical_id ' . dcCore::app()->con->in($params['periodical_id']);
unset($params['periodical_id']);
}
if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) {
@ -233,67 +260,63 @@ class periodical
unset($params['post_status']);
}
} else {
$params['sql'] .= 'AND P.post_status = -2 ';
$params['sql'] .= 'AND P.post_status = ' . dcBlog::POST_PENDING . ' ';
}
$rs = dcCore::app()->blog->getPosts($params, $count_only);
$rs->periodical = $this;
return $rs;
return dcCore::app()->blog->getPosts($params, $count_only);
}
# Add post to periodical
public function addPost($period_id, $post_id)
/**
* Add post to a period
*/
public static function addPost(int $period_id, int $post_id): void
{
$period_id = (int) $period_id;
$post_id = (int) $post_id;
# Check if exists
$rs = $this->getPosts(['post_id' => $post_id, 'periodical_id' => $period_id]);
$rs = self::getPosts(['post_id' => $post_id, 'periodical_id' => $period_id]);
if (!$rs->isEmpty()) {
return;
}
$cur = $this->con->openCursor(dcCore::app()->prefix . dcMeta::META_TABLE_NAME);
$this->con->writeLock(dcCore::app()->prefix . dcMeta::META_TABLE_NAME);
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcMeta::META_TABLE_NAME);
dcCore::app()->con->writeLock(dcCore::app()->prefix . dcMeta::META_TABLE_NAME);
try {
$cur->post_id = $post_id;
$cur->meta_id = $period_id;
$cur->meta_type = 'periodical';
$cur->setField('post_id', $post_id);
$cur->setField('meta_id', $period_id);
$cur->setField('meta_type', My::META_TYPE);
$cur->insert();
$this->con->unlock();
dcCore::app()->con->unlock();
} catch (Exception $e) {
$this->con->unlock();
dcCore::app()->con->unlock();
throw $e;
}
}
# Delete post from periodical
public function delPost($post_id)
/**
* Remove a post from periods
*/
public static function delPost(int $post_id): void
{
$post_id = (int) $post_id;
$this->con->execute(
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . dcMeta::META_TABLE_NAME . ' ' .
"WHERE meta_type = 'periodical' " .
"WHERE meta_type = '" . My::META_TYPE . "' " .
"AND post_id = '" . $post_id . "' "
);
return true;
}
# Remove all posts without pending status from periodical
public function cleanPosts($period_id = null)
/**
* Remove all posts without pending status from periodical
*/
public static function cleanPosts(?int $period_id = null): void
{
$params = [];
$params['post_status'] = '';
$params['sql'] = 'AND post_status != -2 ';
$params['sql'] = 'AND post_status != ' . dcBlog::POST_PENDING . ' ';
if ($period_id !== null) {
$params['periodical_id'] = (int) $period_id;
$params['periodical_id'] = $period_id;
}
$rs = $this->getPosts($params);
$rs = self::getPosts($params);
if ($rs->isEmpty()) {
return;
@ -301,74 +324,24 @@ class periodical
$ids = [];
while ($rs->fetch()) {
$ids[] = $rs->post_id;
$ids[] = (int) $rs->f('post_id');
}
if (empty($ids)) {
return;
}
$this->con->execute(
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . dcMeta::META_TABLE_NAME . ' ' .
"WHERE meta_type = 'periodical' " .
'AND post_id ' . $this->con->in($ids)
"WHERE meta_type = '" . My::META_TYPE . "' " .
'AND post_id ' . dcCore::app()->con->in($ids)
);
}
public static function getTimesCombo()
{
return [
__('Hourly') => 'hour',
__('twice a day') => 'halfday',
__('Daily') => 'day',
__('Weekly') => 'week',
__('Monthly') => 'month',
];
}
public static function getNextTime($ts, $period)
{
$ts = (int) $ts;
$e = explode(',', date('H,i,s,n,j,Y', $ts));
switch($period) {
case 'hour':
$new_ts = mktime($e[0] + 1, $e[1], $e[2], $e[3], $e[4], $e[5]);
break;
case 'halfday':
$new_ts = mktime($e[0] + 12, $e[1], $e[2], $e[3], $e[4], $e[5]);
break;
case 'day':
$new_ts = mktime($e[0], $e[1], $e[2], $e[3], $e[4] + 1, $e[5]);
break;
case 'week':
$new_ts = mktime($e[0], $e[1], $e[2], $e[3], $e[4] + 7, $e[5]);
break;
case 'month':
$new_ts = mktime($e[0], $e[1], $e[2], $e[3] + 1, $e[4], $e[5]);
break;
default:
$new_ts = 0;
throw new Exception(__('Unknow frequence'));
break;
}
return $new_ts;
}
# Lock a file to see if an update is ongoing
public function lockUpdate()
/**
* Lock a file to see if an update is ongoing
*/
public static function lockUpdate(): bool
{
try {
# Need flock function
@ -380,7 +353,7 @@ class periodical
throw new Exception("Can't write in cache fodler");
}
# Set file path
$f_md5 = md5($this->blog);
$f_md5 = md5(self::blog());
$cached_file = sprintf(
'%s/%s/%s/%s/%s.txt',
DC_TPL_CACHE,
@ -391,6 +364,9 @@ class periodical
);
# Real path
$cached_file = path::real($cached_file, false);
if (is_bool($cached_file)) {
throw new Exception("Can't write in cache fodler");
}
# Make dir
if (!is_dir(dirname($cached_file))) {
files::makeDir(dirname($cached_file), true);
@ -412,19 +388,20 @@ class periodical
if (!flock($fp, LOCK_EX)) {
throw new Exception("Can't lock file");
}
$this->lock = $fp;
self::$lock = $fp;
return true;
} catch (Exception $e) {
throw $e;
}
return false;
}
public function unlockUpdate()
/**
* Unlock update process
*/
public static function unlockUpdate(): void
{
@fclose($this->lock);
$this->lock = null;
@fclose(self::$lock);
self::$lock = null;
}
}