periodical/_admin.php

524 lines
15 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
*/
2021-08-23 11:24:19 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
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 23:26:11 +00:00
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'adminBlogPreferencesFormV2',
2021-08-23 23:26:11 +00:00
['adminPeriodical', 'adminBlogPreferencesForm']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
2021-08-23 23:26:11 +00:00
'adminBeforeBlogSettingsUpdate',
['adminPeriodical', 'adminBeforeBlogSettingsUpdate']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'adminFiltersListsV2',
2021-10-22 22:03:15 +00:00
['adminPeriodical', 'adminFiltersLists']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'adminColumnsListsV2',
2021-10-23 12:44:54 +00:00
['adminPeriodical', 'adminColumnsLists']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'adminPostListHeaderV2',
['adminPeriodical', 'adminPostListHeader']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'adminPostListValueV2',
['adminPeriodical', 'adminPostListValue']
);
2021-08-23 11:24:19 +00:00
2022-11-14 19:55:22 +00:00
if (dcCore::app()->blog->settings->periodical->periodical_active) {
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
2021-08-23 22:52:29 +00:00
__('Periodical'),
2022-11-14 19:55:22 +00:00
dcCore::app()->adminurl->get('admin.plugin.periodical'),
2021-10-23 21:39:59 +00:00
dcPage::getPF('periodical/icon.png'),
2022-11-14 19:55:22 +00:00
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)
2021-08-23 22:52:29 +00:00
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'adminDashboardFavoritesV2',
2021-08-23 22:52:29 +00:00
['adminPeriodical', 'adminDashboardFavorites']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
2021-08-23 22:52:29 +00:00
'adminPostHeaders',
['adminPeriodical', 'adminPostHeaders']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
'adminPostsActions',
['adminPeriodical', 'adminPostsActions']
2021-08-23 22:52:29 +00:00
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
2021-08-23 22:52:29 +00:00
'adminPostFormItems',
['adminPeriodical', 'adminPostFormItems']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
2021-08-23 22:52:29 +00:00
'adminAfterPostUpdate',
['adminPeriodical', 'adminAfterPostSave']
);
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
2021-08-23 22:52:29 +00:00
'adminAfterPostCreate',
['adminPeriodical', 'adminAfterPostSave']
);
2021-08-23 11:24:19 +00:00
}
2022-11-14 19:55:22 +00:00
dcCore::app()->addBehavior(
2021-08-23 22:52:29 +00:00
'adminBeforePostDelete',
['adminPeriodical', 'adminBeforePostDelete']
2021-08-23 11:24:19 +00:00
);
/**
* @ingroup DC_PLUGIN_PERIODICAL
* @brief Periodical - admin methods.
* @since 2.6
*/
class adminPeriodical
{
2021-08-23 22:52:29 +00:00
public static $combo_period = null;
protected static $per = null;
2021-08-23 22:52:29 +00:00
2021-10-22 22:03:15 +00:00
public static function sortbyCombo()
{
return [
__('Next update') => 'periodical_curdt',
__('End date') => 'periodical_enddt',
__('Frequence') => 'periodical_pub_int',
2021-10-22 22:03:15 +00:00
];
}
2022-11-14 19:55:22 +00:00
protected static function period()
{
if (self::$per === null) {
2022-11-14 19:55:22 +00:00
self::$per = new periodical();
}
return self::$per;
}
2021-08-23 23:26:11 +00:00
/**
* Add settings to blog preference
*
2021-08-23 23:26:11 +00:00
* @param dcSettings $blog_settings dcSettings instance
*/
2022-11-14 19:55:22 +00:00
public static function adminBlogPreferencesForm(dcSettings $blog_settings)
2021-08-23 23:26:11 +00:00
{
$s_active = (bool) $blog_settings->periodical->periodical_active;
$s_upddate = (bool) $blog_settings->periodical->periodical_upddate;
$s_updurl = (bool) $blog_settings->periodical->periodical_updurl;
2021-08-23 23:26:11 +00:00
echo
2021-10-22 22:03:15 +00:00
'<div class="fieldset"><h4 id="periodical_params">' . __('Periodical') . '</h4>' .
2021-08-23 23:26:11 +00:00
'<div class="two-cols">' .
'<div class="col">' .
'<p><label class="classic" for="periodical_active">' .
form::checkbox('periodical_active', 1, $s_active) .
2021-10-30 19:06:32 +00:00
__('Enable periodical on this blog') . '</label></p>' .
2021-10-22 22:03:15 +00:00
'</div>' .
'<div class="col">' .
2021-08-23 23:26:11 +00:00
'<p><label for="periodical_upddate">' .
form::checkbox('periodical_upddate', 1, $s_upddate) .
2021-10-30 19:06:32 +00:00
__('Update post date when publishing it') . '</label></p>' .
2021-08-23 23:26:11 +00:00
'<p><label for="periodical_updurl">' .
form::checkbox('periodical_updurl', 1, $s_updurl) .
2021-10-30 19:06:32 +00:00
__('Update post url when publishing it') . '</label></p>' .
2021-08-23 23:26:11 +00:00
'</div>' .
'</div>' .
'<br class="clear" />' .
'</div>';
}
/**
* Save blog settings
*
2021-08-23 23:26:11 +00:00
* @param dcSettings $blog_settings dcSettings instance
*/
public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings)
{
$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']));
2021-10-22 22:03:15 +00:00
}
/**
* User pref for periods columns lists.
*
* @param arrayObject $cols Columns
*/
2022-11-14 19:55:22 +00:00
public static function adminColumnsLists($cols)
2021-10-23 12:44:54 +00:00
{
$cols['periodical'] = [
__('Periodical'),
[
'curdt' => [true, __('Next update')],
'pub_int' => [true, __('Frequency')],
2021-10-23 21:55:11 +00:00
'pub_nb' => [true, __('Entries per update')],
2021-10-23 12:44:54 +00:00
'nbposts' => [true, __('Entries')],
'enddt' => [true, __('End date')],
],
2021-10-23 12:44:54 +00:00
];
$cols['posts'][1]['period'] = [true, __('Period')];
2021-10-23 12:44:54 +00:00
}
/**
* User pref periods filters options.
*
* @param arrayObject $sorts Sort options
*/
2022-11-14 19:55:22 +00:00
public static function adminFiltersLists($sorts)
2021-10-22 22:03:15 +00:00
{
$sorts['periodical'] = [
__('Periodical'),
self::sortbyCombo(),
'periodical_curdt',
'desc',
[__('periods per page'), 10],
2021-10-22 22:03:15 +00:00
];
2021-08-23 23:26:11 +00:00
}
/**
* Add columns period to posts list header.
*
* @param record $rs record instance
* @param arrayObject $cols Columns
*/
2022-11-14 19:55:22 +00:00
public static function adminPostListHeader($rs, $cols)
{
2022-11-14 19:55:22 +00:00
if (dcCore::app()->blog->settings->periodical->periodical_active) {
$cols['period'] = '<th scope="col">' . __('Period') . '</th>';
}
}
/**
* Add columns period to posts list values.
*
* @param record $rs record instance
* @param arrayObject $cols Columns
*/
2022-11-14 19:55:22 +00:00
public static function adminPostListValue($rs, $cols)
{
2022-11-14 19:55:22 +00:00
if (!dcCore::app()->blog->settings->periodical->periodical_active) {
return null;
}
2022-11-14 19:55:22 +00:00
$r = self::period()->getPosts(['post_id' => $rs->post_id]);
if ($r->isEmpty()) {
$name = '-';
} else {
2022-11-14 19:55:22 +00:00
$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>';
}
$cols['period'] = '<td class="nowrap">' . $name . '</td>';
}
2021-08-23 22:52:29 +00:00
/**
* Favorites.
*
* @param dcFavorites $favs Array of favorites
2021-08-23 22:52:29 +00:00
*/
2022-11-14 19:55:22 +00:00
public static function adminDashboardFavorites(dcFavorites $favs)
2021-08-23 22:52:29 +00:00
{
$favs->register('periodical', [
'title' => __('Periodical'),
'url' => 'plugin.php?p=periodical',
'small-icon' => 'index.php?pf=periodical/icon.png',
'large-icon' => 'index.php?pf=periodical/icon-big.png',
'permissions' => dcCore::app()->auth->check(
dcCore::app()->auth->makePermissions([
2022-11-14 19:55:22 +00:00
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]),
dcCore::app()->blog->id
2021-08-23 22:52:29 +00:00
),
'active_cb' => [
'adminPeriodical',
'adminDashboardFavoritesActive',
],
2021-08-23 22:52:29 +00:00
]);
}
/**
* Favorites selection.
*
* @param string $request Requested page
* @param array $params Requested parameters
*/
public static function adminDashboardFavoritesActive($request, $params)
{
return $request == 'plugin.php'
&& isset($params['p'])
2021-08-23 22:52:29 +00:00
&& $params['p'] == 'periodical';
}
/**
* Add javascript for toggle
*
2021-08-23 22:52:29 +00:00
* @return string HTML head
*/
public static function adminPostHeaders()
{
return dcPage::jsLoad('index.php?pf=periodical/js/toggle.js');
}
/**
* Delete relation between post and period
*
2021-08-23 22:52:29 +00:00
* @param integer $post_id Post id
*/
public static function adminBeforePostDelete($post_id)
{
2022-11-14 19:55:22 +00:00
self::delPeriod($post_id);
2021-08-23 22:52:29 +00:00
}
/**
* Add actions to posts page combo
*
* @param dcPostsActions $pa dcPostsActions instance
2021-08-23 22:52:29 +00:00
*/
2022-11-14 19:55:22 +00:00
public static function adminPostsActions(dcPostsActions $pa)
2021-08-23 22:52:29 +00:00
{
$pa->addAction(
[__('Periodical') => [__('Add to periodical') => 'periodical_add']],
['adminPeriodical', 'callbackAdd']
);
2022-11-14 19:55:22 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_DELETE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)) {
2021-08-23 22:52:29 +00:00
return null;
}
$pa->addAction(
[__('Periodical') => [__('Remove from periodical') => 'periodical_remove']],
['adminPeriodical', 'callbackRemove']
);
}
/**
* Posts actions callback to remove period
*
2022-11-14 19:55:22 +00:00
* @param dcPostsActions $pa dcPostsActions instance
2021-08-23 22:52:29 +00:00
* @param ArrayObject $post _POST actions
*/
2022-11-14 19:55:22 +00:00
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post)
2021-08-23 22:52:29 +00:00
{
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
# No right
2022-11-14 19:55:22 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_DELETE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)) {
2021-08-23 22:52:29 +00:00
throw new Exception(__('No enough right'));
}
# Remove linked period
foreach ($posts_ids as $post_id) {
2022-11-14 19:55:22 +00:00
self::delPeriod($post_id);
2021-08-23 22:52:29 +00:00
}
2022-11-14 20:47:55 +00:00
dcAdminNotices::addSuccessNotice(__('Posts have been removed from periodical.'));
2021-08-23 22:52:29 +00:00
$pa->redirect(true);
}
/**
* Posts actions callback to add period
*
2022-11-14 19:55:22 +00:00
* @param dcPostsActions $pa dcPostsActions instance
2021-08-23 22:52:29 +00:00
* @param ArrayObject $post _POST actions
*/
2022-11-14 19:55:22 +00:00
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post)
2021-08-23 22:52:29 +00:00
{
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
//todo: check if selected posts is unpublished
# Save action
if (!empty($post['periodical'])) {
foreach ($posts_ids as $post_id) {
2022-11-14 19:55:22 +00:00
self::delPeriod($post_id);
self::addPeriod($post_id, $post['periodical']);
2021-08-23 22:52:29 +00:00
}
2022-11-14 20:47:55 +00:00
dcAdminNotices::addSuccessNotice(__('Posts have been added to periodical.'));
2021-08-23 22:52:29 +00:00
$pa->redirect(true);
}
# Display form
else {
$pa->beginPage(
dcPage::breadcrumb([
2022-11-14 19:55:22 +00:00
html::escapeHTML(dcCore::app()->blog->name) => '',
$pa->getCallerTitle() => $pa->getRedirection(true),
__('Add a period to this selection') => '',
])
2021-08-23 22:52:29 +00:00
);
echo
'<form action="' . $pa->getURI() . '" method="post">' .
$pa->getCheckboxes() .
2022-11-14 19:55:22 +00:00
self::formPeriod() .
2021-08-23 22:52:29 +00:00
'<p>' .
2022-11-14 19:55:22 +00:00
dcCore::app()->formNonce() .
2021-08-23 22:52:29 +00:00
$pa->getHiddenFields() .
form::hidden(['action'], 'periodical_add') .
'<input type="submit" value="' . __('Save') . '" /></p>' .
'</form>';
$pa->endPage();
}
}
/**
* Add form to post sidebar
*
2021-08-23 22:52:29 +00:00
* @param ArrayObject $main_items Main items
* @param ArrayObject $sidebar_items Sidebar items
* @param record $post Post record or null
*/
public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, $post)
{
# Get existing linked period
$period = '';
if ($post) {
$rs = self::period()->getPosts(['post_id' => $post->post_id]);
2021-08-23 22:52:29 +00:00
$period = $rs->isEmpty() ? '' : $rs->periodical_id;
}
# Set linked period form items
$sidebar_items['options-box']['items']['period'] = self::formPeriod($period);
2021-08-23 22:52:29 +00:00
}
/**
* Save linked period
*
2021-08-23 22:52:29 +00:00
* @param cursor $cur Current post cursor
* @param integer $post_id Post id
*/
public static function adminAfterPostSave(cursor $cur, $post_id)
{
if (!isset($_POST['periodical'])) {
return null;
}
# Delete old linked period
2022-11-14 19:55:22 +00:00
self::delPeriod($post_id);
2021-08-23 22:52:29 +00:00
# Add new linked period
2022-11-14 19:55:22 +00:00
self::addPeriod($post_id, $_POST['periodical']);
2021-08-23 22:52:29 +00:00
}
/**
* Posts period form field
*
2021-08-23 22:52:29 +00:00
* @param string $period Period
* @return null|string Period form content
2021-08-23 22:52:29 +00:00
*/
protected static function formPeriod($period = '')
2021-08-23 22:52:29 +00:00
{
2022-11-14 19:55:22 +00:00
$combo = self::comboPeriod();
2021-08-23 22:52:29 +00:00
if (empty($combo)) {
return null;
}
return
2021-08-23 22:52:29 +00:00
'<p><label for="periodical">' .
__('Periodical') . '</label>' .
form::combo('periodical', $combo, $period) .
'</p>';
}
/**
* Combo of available periods
*
2021-08-23 22:52:29 +00:00
* @return array List of period
*/
2022-11-14 19:55:22 +00:00
protected static function comboPeriod()
2021-08-23 22:52:29 +00:00
{
if (adminPeriodical::$combo_period === null) {
$periods = self::period()->getPeriods();
adminPeriodical::$combo_period = [];
2021-08-23 22:52:29 +00:00
if (!$periods->isEmpty()) {
2021-08-23 22:52:29 +00:00
$combo = ['-' => ''];
while ($periods->fetch()) {
$combo[html::escapeHTML($periods->periodical_title)] = $periods->periodical_id;
}
adminPeriodical::$combo_period = $combo;
2021-08-23 22:52:29 +00:00
}
}
return adminPeriodical::$combo_period;
}
/**
* Remove period from posts.
*
2021-08-23 22:52:29 +00:00
* @param integer $post_id Post id
*/
2022-11-14 19:55:22 +00:00
protected static function delPeriod($post_id)
2021-08-23 22:52:29 +00:00
{
if ($post_id === null) {
return null;
}
$post_id = (int) $post_id;
2022-11-14 19:55:22 +00:00
self::period()->delPost($post_id);
2021-08-23 22:52:29 +00:00
}
/**
* Add period to posts
*
2021-08-23 22:52:29 +00:00
* @param integer $post_id Post id
* @param array $period Period
*/
2022-11-14 19:55:22 +00:00
protected static function addPeriod($post_id, $period)
2021-08-23 22:52:29 +00:00
{
# Not saved
if ($post_id === null || empty($period)) {
return null;
}
# Get periods
2022-11-14 19:55:22 +00:00
$period = self::period()->getPeriods(['periodical_id' => $period]);
2021-08-23 22:52:29 +00:00
# No period
if ($period->isEmpty()) {
return null;
}
$post_id = (int) $post_id;
2021-08-23 22:52:29 +00:00
# Add relation
2022-11-14 19:55:22 +00:00
self::period()->addPost($period->periodical_id, $post_id);
2021-08-23 22:52:29 +00:00
}
}