periodical/_public.php

164 lines
6.1 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
*
* @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
*/
2021-08-23 11:24:19 +00:00
if (!defined('DC_RC_PATH')) {
2021-08-23 22:52:29 +00:00
return null;
2021-08-23 11:24:19 +00:00
}
2022-11-14 19:55:22 +00:00
if (!in_array(dcCore::app()->url->type, ['default', 'feed'])) {
2021-08-23 22:52:29 +00:00
return null;
2021-08-23 11:24:19 +00:00
}
2022-11-14 19:55:22 +00:00
dcCore::app()->blog->settings->addNamespace('periodical');
2021-08-23 11:24:19 +00:00
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'publicBeforeDocumentV2',
2021-08-23 22:52:29 +00:00
['publicPeriodical', 'publicBeforeDocument']
2021-08-23 11:24:19 +00:00
);
/**
* @ingroup DC_PLUGIN_PERIODICAL
* @brief Periodical - public methods.
* @since 2.6
*/
class publicPeriodical
{
2021-08-23 22:52:29 +00:00
/**
* Publish periodical
*
*/
2022-11-14 19:55:22 +00:00
public static function publicBeforeDocument()
2021-08-23 22:52:29 +00:00
{
try {
2022-11-14 19:55:22 +00:00
$per = new periodical();
$s = dcCore::app()->blog->settings->periodical;
2021-08-23 22:52:29 +00:00
$per->lockUpdate();
# Get periods
2022-11-14 19:55:22 +00:00
$periods = dcCore::app()->auth->sudo([$per, 'getPeriods']);
2021-08-23 22:52:29 +00:00
# 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';
}
2022-11-14 19:55:22 +00:00
$cur_period = dcCore::app()->con->openCursor(dcCore::app()->prefix . 'periodical');
2021-08-23 22:52:29 +00:00
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);
2021-10-23 21:39:59 +00:00
if ($cur_tz < $now_tz && $now_tz < $end_tz) {
2021-08-23 22:52:29 +00:00
$last_nb = 0;
$last_tz = $cur_tz;
2021-09-02 12:56:02 +00:00
2021-08-23 22:52:29 +00:00
$max_nb = $periods->periodical_pub_nb;
$max_tz = $end_tz < $now_tz ? $end_tz : $now_tz;
2021-09-02 12:56:02 +00:00
2021-08-23 22:52:29 +00:00
# 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;
2021-10-24 07:33:44 +00:00
$posts_params['post_status'] = '-2';
$posts_params['order'] = $posts_order;
$posts_params['limit'] = $limit * $max_nb;
$posts_params['no_content'] = true;
2022-11-14 19:55:22 +00:00
$posts = dcCore::app()->auth->sudo([$per, 'getPosts'], $posts_params);
2021-08-23 22:52:29 +00:00
if (!$posts->isEmpty()) {
2022-11-14 19:55:22 +00:00
$cur_post = dcCore::app()->con->openCursor(dcCore::app()->prefix . 'post');
2021-09-02 12:56:02 +00:00
2021-08-23 22:52:29 +00:00
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) {
2022-11-14 19:55:22 +00:00
$cur_post->post_url = dcCore::app()->blog->getPostURL('', $cur_post->post_dt, $posts->post_title, $posts->post_id);
2021-08-23 22:52:29 +00:00
}
$cur_post->update(
'WHERE post_id = ' . $posts->post_id . ' ' .
2022-11-14 19:55:22 +00:00
"AND blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' "
2021-08-23 22:52:29 +00:00
);
# 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
2022-11-14 19:55:22 +00:00
dcCore::app()->callBehavior('periodicalAfterPublishedPeriodicalEntry', $posts, $periods);
2021-08-23 22:52:29 +00:00
}
2022-11-14 19:55:22 +00:00
dcCore::app()->blog->triggerBlog();
2021-08-23 22:52:29 +00:00
}
}
# 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 . ' ' .
2022-11-14 19:55:22 +00:00
"AND blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' "
2021-08-23 22:52:29 +00:00
);
}
}
$per->unlockUpdate();
} catch (Exception $e) {
$per->unlockUpdate();
return null;
}
}
}