periodical/inc/index.period.php

367 lines
12 KiB
PHP
Raw Normal View History

2021-10-24 07:43:26 +00:00
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
2021-10-24 07:43:26 +00:00
* @package Dotclear
* @subpackage Plugin
*
2021-10-24 07:43:26 +00:00
* @author Jean-Christian Denis and contributors
*
2021-10-24 07:43:26 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
2022-11-14 19:55:22 +00:00
dcPage::check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_USAGE, dcAuth::PERMISSION_CONTENT_ADMIN]));
2021-10-24 07:43:26 +00:00
# Objects
2022-11-14 19:55:22 +00:00
$per = new periodical();
2021-10-24 07:43:26 +00:00
# Default values
$action = $_POST['action'] ?? '';
2021-10-24 07:43:26 +00:00
$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
2022-05-16 21:05:40 +00:00
$bad_period_curdt = false;
$bad_period_enddt = false;
2021-10-24 07:43:26 +00:00
# Get period
if (!empty($_REQUEST['period_id'])) {
$rs = $per->getPeriods([
'periodical_id' => $_REQUEST['period_id'],
2021-10-24 07:43:26 +00:00
]);
if ($rs->isEmpty()) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add(__('This period does not exist.'));
2021-10-24 07:43:26 +00:00
$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;
2022-05-16 21:05:40 +00:00
$period_curdt = date('Y-m-d H:i', strtotime($rs->periodical_curdt));
$period_enddt = date('Y-m-d H:i', strtotime($rs->periodical_enddt));
2021-10-24 07:43:26 +00:00
}
}
# 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']);
2021-10-24 07:43:26 +00:00
}
if (!empty($_POST['period_pub_int'])
2021-10-24 07:43:26 +00:00
&& in_array($_POST['period_pub_int'], $per->getTimesCombo())
) {
$period_pub_int = $_POST['period_pub_int'];
}
if (!empty($_POST['period_curdt'])) {
2022-05-16 21:05:40 +00:00
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) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add($e->getMessage());
2022-05-16 21:05:40 +00:00
}
2021-10-24 07:43:26 +00:00
}
if (!empty($_POST['period_enddt'])) {
2022-05-16 21:05:40 +00:00
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) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add($e->getMessage());
2022-05-16 21:05:40 +00:00
}
2021-10-24 07:43:26 +00:00
}
# Check period title and dates
$old_titles = $per->getPeriods([
'periodical_title' => $period_title,
2021-10-24 07:43:26 +00:00
]);
if (!$old_titles->isEmpty()) {
while ($old_titles->fetch()) {
2021-10-24 07:43:26 +00:00
if (!$period_id || $old_titles->periodical_id != $period_id) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add(__('Period title is already taken'));
2021-10-24 07:43:26 +00:00
}
}
}
if (empty($period_title)) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add(__('Period title is required'));
2021-10-24 07:43:26 +00:00
}
if (strtotime($period_curdt) > strtotime($period_enddt)) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add(__('Start date must be older than end date'));
2021-10-24 07:43:26 +00:00
}
# If no error, set period
2022-11-14 19:55:22 +00:00
if (!dcCore::app()->error->flag()) {
$cur = $per->openCursor();
2021-10-24 07:43:26 +00:00
$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;
# Update period
if ($period_id) {
$per->updPeriod($period_id, $cur);
2022-11-14 19:55:22 +00:00
dcAdminNotices::addSuccessNotice(
2021-10-24 07:43:26 +00:00
__('Period successfully updated.')
);
# Create period
} else {
$period_id = $per->addPeriod($cur);
2022-11-14 19:55:22 +00:00
dcAdminNotices::addSuccessNotice(
2021-10-24 07:43:26 +00:00
__('Period successfully created.')
);
}
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-14 19:55:22 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#period');
2021-10-24 07:43:26 +00:00
}
}
}
# Actions on related posts
2022-11-14 19:55:22 +00:00
if (!dcCore::app()->error->flag() && $period_id && $action && !empty($_POST['periodical_entries'])) {
2021-10-24 07:43:26 +00:00
# Publish posts
if ($action == 'publish') {
try {
foreach ($_POST['periodical_entries'] as $id) {
$id = (int) $id;
2022-11-14 19:55:22 +00:00
dcCore::app()->blog->updPostStatus($id, 1);
2021-10-24 07:43:26 +00:00
$per->delPost($id);
}
2022-11-14 19:55:22 +00:00
dcAdminNotices::addSuccessNotice(
2021-10-24 07:43:26 +00:00
__('Entries successfully published.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-14 19:55:22 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
2021-10-24 07:43:26 +00:00
}
} catch (Exception $e) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add($e->getMessage());
2021-10-24 07:43:26 +00:00
}
}
# Unpublish posts
if ($action == 'unpublish') {
try {
foreach ($_POST['periodical_entries'] as $id) {
$id = (int) $id;
dcCore::app()->blog->updPostStatus($id, 0);
2021-10-24 07:43:26 +00:00
$per->delPost($id);
}
2022-11-14 19:55:22 +00:00
dcAdminNotices::addSuccessNotice(
2021-10-24 07:43:26 +00:00
__('Entries successfully unpublished.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-14 19:55:22 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
2021-10-24 07:43:26 +00:00
}
} catch (Exception $e) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add($e->getMessage());
2021-10-24 07:43:26 +00:00
}
}
# Remove posts from periodical
if ($action == 'remove_post_periodical') {
try {
foreach ($_POST['periodical_entries'] as $id) {
$id = (int) $id;
2021-10-24 07:43:26 +00:00
$per->delPost($id);
}
2022-11-14 19:55:22 +00:00
dcAdminNotices::addSuccessNotice(
2021-10-24 07:43:26 +00:00
__('Entries successfully removed.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-14 19:55:22 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
2021-10-24 07:43:26 +00:00
}
} catch (Exception $e) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add($e->getMessage());
2021-10-24 07:43:26 +00:00
}
}
}
# Prepare combos for posts list
if ($period_id) {
# Filters
2022-11-14 19:55:22 +00:00
$post_filter = new adminPostFilter();
2021-10-24 07:43:26 +00:00
$post_filter->add('part', 'period');
$params = $post_filter->params();
2021-10-24 07:43:26 +00:00
$params['periodical_id'] = $period_id;
$params['no_content'] = true;
# Get posts
try {
$posts = $per->getPosts($params);
$counter = $per->getPosts($params, true);
2022-11-14 19:55:22 +00:00
$post_list = new adminPeriodicalList(dcCore::app(), $posts, $counter->f(0));
2021-10-24 07:43:26 +00:00
} catch (Exception $e) {
2022-11-14 19:55:22 +00:00
dcCore::app()->error->add($e->getMessage());
2021-10-24 07:43:26 +00:00
}
$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');
2021-10-24 07:43:26 +00:00
}
# 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')) => '',
2021-10-24 07:43:26 +00:00
]) .
dcPage::notices();
# Period form
echo '
<div id="period"><h3>' . (null === $period_id ? __('New period') : __('Edit period')) . '</h3>
2022-11-14 19:55:22 +00:00
<form method="post" action="' . dcCore::app()->admin->getPageURL() . '">
2021-10-24 07:43:26 +00:00
<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))),
2022-05-16 21:05:40 +00:00
'class' => ($bad_period_curdt ? 'invalid' : ''),
]) . '</p>
2021-10-24 07:43:26 +00:00
<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))),
2022-05-16 21:05:40 +00:00
'class' => ($bad_period_enddt ? 'invalid' : ''),
]) . '</p>
2021-10-24 07:43:26 +00:00
</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>
2021-10-24 07:43:26 +00:00
<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') . '" />' .
2022-11-14 19:55:22 +00:00
dcCore::app()->formNonce() .
2021-10-24 07:43:26 +00:00
form::hidden(['action'], 'setperiod') .
form::hidden(['period_id'], $period_id) .
form::hidden(['part'], 'period') . '
2021-10-24 07:43:26 +00:00
</p>
</div>
</form>
</div>';
if ($period_id && isset($post_filter) && isset($post_list) && !dcCore::app()->error->flag()) {
2021-10-24 07:43:26 +00:00
# Actions combo box
$combo_action = [];
$combo_action[__('Entries')][__('Publish')] = 'publish';
$combo_action[__('Entries')][__('Unpublish')] = 'unpublish';
2021-10-24 07:43:26 +00:00
$combo_action[__('Periodical')][__('Remove from periodical')] = 'remove_post_periodical';
2022-11-14 19:55:22 +00:00
$base_url = dcCore::app()->admin->getPageURL() .
'&amp;period_id=' . $period_id .
2021-10-24 07:43:26 +00:00
'&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'],
2022-11-14 19:55:22 +00:00
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.periodical', [
2021-10-24 07:43:26 +00:00
'period_id' => $period_id,
'part' => 'period',
2021-10-24 07:43:26 +00:00
])
);
# Posts list
$post_list->postDisplay(
$post_filter,
$base_url,
2022-11-14 19:55:22 +00:00
'<form action="' . dcCore::app()->admin->getPageURL() . '" method="post" id="form-entries">' .
2021-10-24 07:43:26 +00:00
'%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>' .
2022-11-14 19:55:22 +00:00
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.periodical', array_merge($post_filter->values(), [
2021-10-24 07:43:26 +00:00
'period_id' => $period_id,
'redir' => sprintf($base_url, $post_filter->page),
2021-10-24 07:43:26 +00:00
])) .
2022-11-14 19:55:22 +00:00
dcCore::app()->formNonce() .
2021-10-24 07:43:26 +00:00
'</div>' .
'</form>'
);
echo
'</div>';
}
dcPage::helpBlock('periodical');
echo '</body></html>';