periodical/src/Frontend.php

163 lines
6.8 KiB
PHP
Raw Normal View History

2021-08-23 11:24:19 +00:00
<?php
2021-09-02 12:56:02 +00:00
/**
* @brief periodical, a plugin for Dotclear 2
*
2021-09-02 12:56:02 +00:00
* @package Dotclear
* @subpackage Plugin
*
2021-09-02 12:56:02 +00:00
* @author Jean-Christian Denis and contributors
*
2021-09-02 12:56:02 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-03-24 22:12:11 +00:00
declare(strict_types=1);
2021-08-23 11:24:19 +00:00
2023-03-24 22:12:11 +00:00
namespace Dotclear\Plugin\periodical;
2021-08-23 11:24:19 +00:00
2023-03-24 22:12:11 +00:00
use dcBlog;
use dcCore;
use dcNsProcess;
use Exception;
2021-08-23 11:24:19 +00:00
/**
2023-03-24 22:12:11 +00:00
* Update posts from periods on frontend
2021-08-23 11:24:19 +00:00
*/
2023-03-24 22:12:11 +00:00
class Frontend extends dcNsProcess
2021-08-23 11:24:19 +00:00
{
2023-03-24 22:12:11 +00:00
public static function init(): bool
2021-08-23 22:52:29 +00:00
{
2023-03-24 22:12:11 +00:00
static::$init = defined('DC_RC_PATH')
&& in_array(dcCore::app()->url->type, ['default', 'feed']);
2021-08-23 22:52:29 +00:00
2023-03-24 22:12:11 +00:00
return static::$init;
}
2021-08-23 22:52:29 +00:00
2023-03-24 22:12:11 +00:00
public static function process(): bool
{
if (!static::$init) {
return false;
}
2021-08-23 22:52:29 +00:00
2023-03-24 22:12:11 +00:00
dcCore::app()->addBehavior('publicBeforeDocumentV2', function (): void {
try {
$s = dcCore::app()->blog->settings->get(My::id());
2021-08-23 22:52:29 +00:00
2023-03-24 22:12:11 +00:00
Utils::lockUpdate();
2021-08-23 22:52:29 +00:00
2023-03-24 22:12:11 +00:00
# Get periods
$periods = dcCore::app()->auth->sudo([Utils::class, 'getPeriods']);
2021-09-02 12:56:02 +00:00
2023-03-24 22:12:11 +00:00
# No period
if ($periods->isEmpty()) {
Utils::unlockUpdate();
2021-09-02 12:56:02 +00:00
2023-03-24 22:12:11 +00:00
return;
}
2023-03-24 22:12:11 +00:00
$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;
2021-08-23 22:52:29 +00:00
}
2023-03-24 22:12:11 +00:00
} catch (Exception $e) {
2021-08-23 22:52:29 +00:00
}
2023-03-24 22:12:11 +00:00
# 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);
2021-08-23 22:52:29 +00:00
}
2023-03-24 22:12:11 +00:00
dcCore::app()->blog->triggerBlog();
2021-08-23 22:52:29 +00:00
}
}
2023-03-24 22:12:11 +00:00
# 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) . "' "
);
}
2021-08-23 22:52:29 +00:00
}
2023-03-24 22:12:11 +00:00
Utils::unlockUpdate();
} catch (Exception $e) {
Utils::unlockUpdate();
2021-08-23 22:52:29 +00:00
}
2023-03-24 22:12:11 +00:00
});
2021-08-23 22:52:29 +00:00
2023-03-24 22:12:11 +00:00
return true;
2021-08-23 22:52:29 +00:00
}
}