postExpired/_admin.php

484 lines
14 KiB
PHP
Raw Normal View History

2021-08-20 19:07:26 +00:00
<?php
2021-09-02 20:43:05 +00:00
/**
* @brief postExpired, a plugin for Dotclear 2
2022-11-20 20:43:32 +00:00
*
2021-09-02 20:43:05 +00:00
* @package Dotclear
* @subpackage Plugin
2022-11-20 20:43:32 +00:00
*
2021-09-02 20:43:05 +00:00
* @author Jean-Christian Denis and Contributors
2022-11-20 20:43:32 +00:00
*
2021-09-02 20:43:05 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-20 19:07:26 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
2021-08-20 19:07:26 +00:00
}
# Check plugin version
2022-11-14 20:47:24 +00:00
if (dcCore::app()->getVersion('postExpired') != dcCore::app()->plugins->moduleInfo('postExpired', 'version')) {
return null;
2021-08-20 19:07:26 +00:00
}
# Check user right
2022-11-14 20:47:24 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
2022-11-20 20:43:32 +00:00
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)
2022-11-14 20:47:24 +00:00
) {
return null;
2021-08-20 19:07:26 +00:00
}
# Admin behaviors
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminPostsActions',
['adminBehaviorPostExpired', 'adminPostsActions']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminPagesActions',
['adminBehaviorPostExpired', 'adminPostsActions']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminPostHeaders',
['adminBehaviorPostExpired', 'adminPostHeaders']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminPageHeaders',
['adminBehaviorPostExpired', 'adminPostHeaders']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminPostFormItems',
['adminBehaviorPostExpired', 'adminPostFormItems']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminPageFormItems',
['adminBehaviorPostExpired', 'adminPostFormItems']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminBeforePostDelete',
['adminBehaviorPostExpired', 'adminBeforePostDelete']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminBeforePageDelete',
['adminBehaviorPostExpired', 'adminBeforePostDelete']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminAfterPostUpdate',
['adminBehaviorPostExpired', 'adminAfterPostSave']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminAfterPageUpdate',
['adminBehaviorPostExpired', 'adminAfterPostSave']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminAfterPostCreate',
['adminBehaviorPostExpired', 'adminAfterPostSave']
2021-08-20 19:07:26 +00:00
);
2022-11-14 20:47:24 +00:00
dcCore::app()->addBehavior(
'adminAfterPageCreate',
['adminBehaviorPostExpired', 'adminAfterPostSave']
2021-08-20 19:07:26 +00:00
);
/**
* @ingroup DC_PLUGIN_POSTEXPIRED
* @brief Scheduled post change - admin methods.
* @since 2.6
*/
class adminBehaviorPostExpired
{
/**
* Add actions to posts page combo
2022-11-20 20:43:32 +00:00
*
* @param dcPostsActionsPage $ap dcPostsActionsPage instance
*/
2022-11-14 20:47:24 +00:00
public static function adminPostsActions(dcPostsActions $pa)
{
$pa->addAction(
2022-11-20 20:43:32 +00:00
[
__('Expired entries') => [
__('Add expired date') => 'post_expired_add',
],
],
['adminBehaviorPostExpired', 'callbackAdd']
);
$pa->addAction(
2022-11-20 20:43:32 +00:00
[
__('Expired entries') => [
__('Remove expired date') => 'post_expired_remove',
],
],
['adminBehaviorPostExpired', 'callbackRemove']
);
}
/**
* Add javascript for date field and toggle
2022-11-20 20:43:32 +00:00
*
* @return string HTML head
*/
public static function adminPostHeaders()
{
2022-11-26 14:00:34 +00:00
return dcPage::jsLoad(dcPage::getPF('postExpired/js/postexpired.js'));
}
/**
* Add form to post sidebar
2022-11-20 20:43:32 +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)
{
if ($post === null) {
return null;
}
2022-11-20 20:43:32 +00:00
$sidebar_items['post_expired'] = [
'title' => __('Expired date'),
'items' => self::fieldsPostExpired(
$post->post_type,
$post->post_id
2022-11-20 20:43:32 +00:00
),
];
}
/**
* Delete expired date on post edition
2022-11-20 20:43:32 +00:00
*
* @param integer $post_id Post id
*/
public static function adminBeforePostDelete($post_id)
{
2022-11-14 20:47:24 +00:00
self::delPostExpired($post_id);
}
/**
* Add expired date on post edition
2022-11-20 20:43:32 +00:00
*
* @param cursor $cur Current post cursor
* @param integer $post_id Post id
*/
public static function adminAfterPostSave(cursor $cur, $post_id)
{
2022-11-14 20:47:24 +00:00
self::delPostExpired($post_id);
if (!empty($_POST['post_expired_date'])
&& (!empty($_POST['post_expired_status'])
|| !empty($_POST['post_expired_category'])
|| !empty($_POST['post_expired_selected'])
|| !empty($_POST['post_expired_comment'])
|| !empty($_POST['post_expired_trackback']))) {
2022-11-14 20:47:24 +00:00
self::setPostExpired($post_id, $_POST);
}
}
/**
* Posts actions callback to add expired date
2022-11-20 20:43:32 +00:00
*
2022-11-14 20:47:24 +00:00
* @param dcPostsActions $pa dcPostsActions instance
* @param ArrayObject $post _POST actions
*/
2022-11-14 20:47:24 +00:00
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post)
{
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
# Add epired date
if (!empty($post['post_expired_date'])
&& (!empty($post['post_expired_status'])
|| !empty($post['post_expired_category'])
|| !empty($post['post_expired_selected'])
|| !empty($post['post_expired_comment'])
|| !empty($post['post_expired_trackback']))) {
2022-11-20 20:43:32 +00:00
foreach ($posts_ids as $post_id) {
2022-11-14 20:47:24 +00:00
self::delPostExpired($post_id);
self::setPostExpired($post_id, $post);
}
2022-11-14 20:47:24 +00:00
dcAdminNotices::addSuccessNotice(__('Expired date added.'));
$pa->redirect(true);
# Display form
} else {
# Get records to know post type
$posts = $pa->getRS();
$pa->beginPage(
2022-11-20 20:43:32 +00:00
dcPage::breadcrumb([
2022-11-14 20:47:24 +00:00
html::escapeHTML(dcCore::app()->blog->name) => '',
2022-11-20 20:43:32 +00:00
$pa->getCallerTitle() => $pa->getRedirection(true),
__('Add expired date to this selection') => '',
]),
2022-04-27 10:04:29 +00:00
//dcPage::jsDatePicker() .
self::adminPostHeaders()
);
echo
'<form action="' . $pa->getURI() . '" method="post">' .
$pa->getCheckboxes() .
2022-11-14 20:47:24 +00:00
implode('', self::fieldsPostExpired($posts->post_type)) .
2022-11-14 20:47:24 +00:00
dcCore::app()->formNonce() .
$pa->getHiddenFields() .
2022-11-20 20:43:32 +00:00
form::hidden(['action'], 'post_expired_add') .
'<input type="submit" value="' . __('Save') . '" /></p>' .
'</form>';
$pa->endPage();
}
}
/**
* Posts actions callback to add expired date
2022-11-20 20:43:32 +00:00
*
2022-11-14 20:47:24 +00:00
* @param dcPostsActions $pa dcPostsActions instance
* @param ArrayObject $post _POST actions
*/
2022-11-14 20:47:24 +00:00
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post)
{
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
# Delete expired date
2022-11-20 20:43:32 +00:00
foreach ($posts_ids as $post_id) {
2022-11-14 20:47:24 +00:00
self::delPostExpired($post_id);
}
2022-11-14 20:47:24 +00:00
dcAdminNotices::addSuccessNotice(__('Expired date deleted.'));
$pa->redirect(true);
}
/**
* Delete expired date
2022-11-20 20:43:32 +00:00
*
* @param integer $post_id Post id
*/
2022-11-14 20:47:24 +00:00
protected static function delPostExpired($post_id)
{
2022-11-14 20:47:24 +00:00
dcCore::app()->meta->delPostMeta($post_id, 'post_expired');
}
/**
* Save expired date
2022-11-20 20:43:32 +00:00
*
* @param integer $post_id Post id
* @param array $post _POST fields
*/
2022-11-14 20:47:24 +00:00
protected static function setPostExpired($post_id, $post)
{
2022-11-20 20:43:32 +00:00
$post_expired = [
'status' => '',
'category' => '',
'selected' => '',
'comment' => '',
'trackback' => '',
2022-11-20 20:43:32 +00:00
'date' => date(
'Y-m-d H:i:00',
strtotime($post['post_expired_date'])
2022-11-20 20:43:32 +00:00
),
];
if (!empty($post['post_expired_status'])) {
2022-11-20 20:43:32 +00:00
$post_expired['status'] = (string) $post['post_expired_status'];
}
if (!empty($post['post_expired_category'])) {
2022-11-20 20:43:32 +00:00
$post_expired['category'] = (string) $post['post_expired_category'];
}
if (!empty($post['post_expired_selected'])) {
2022-11-20 20:43:32 +00:00
$post_expired['selected'] = (string) $post['post_expired_selected'];
}
if (!empty($post['post_expired_comment'])) {
2022-11-20 20:43:32 +00:00
$post_expired['comment'] = (string) $post['post_expired_comment'];
}
if (!empty($post['post_expired_trackback'])) {
2022-11-20 20:43:32 +00:00
$post_expired['trackback'] = (string) $post['post_expired_trackback'];
}
2022-11-14 20:47:24 +00:00
dcCore::app()->meta->setPostMeta(
$post_id,
'post_expired',
encodePostExpired($post_expired)
);
}
/**
* Expired date form fields
2022-11-20 20:43:32 +00:00
*
* @param string $post_type Posts type
* @return array Array of HTML form fields
*/
2022-11-14 20:47:24 +00:00
protected static function fieldsPostExpired($post_type, $post_id = null)
{
2022-11-20 20:43:32 +00:00
$fields = $post_expired = [];
if ($post_id) {
2022-11-14 20:47:24 +00:00
$rs = dcCore::app()->meta->getMetadata([
2021-08-20 21:35:03 +00:00
'meta_type' => 'post_expired',
2022-11-20 20:43:32 +00:00
'post_id' => $post_id,
'limit' => 1,
2021-08-20 21:35:03 +00:00
]);
if (!$rs->isEmpty()) {
$post_expired = decodePostExpired($rs->meta_id);
}
}
2022-11-20 20:43:32 +00:00
$fields['post_expired_date'] = '<p><label for="post_expired_date">' .
__('Date:') . '</label>' .
2022-04-27 10:04:29 +00:00
form::datetime('post_expired_date', [
'default' => html::escapeHTML(dt::str('%Y-%m-%dT%H:%M', strtotime($post_expired['date'] ?? 0))),
'class' => (empty($post_expired['date']) ? 'invalid' : ''),
])
. '</p>';
2022-11-20 20:43:32 +00:00
$fields['post_expired_status'] = '<h5>' . __('On this date, change:') . '</h5>' .
'<p><label for="post_expired_status">' .
__('Status:') . '</label>' .
form::combo(
'post_expired_status',
self::statusCombo(),
2022-11-20 20:43:32 +00:00
empty($post_expired['status']) ?
'' : $post_expired['status']
) . '</p>';
if ($post_type == 'post') {
2022-11-20 20:43:32 +00:00
$fields['post_expired_category'] = '<p><label for="post_expired_category">' .
__('Category:') . '</label>' .
form::combo(
'post_expired_category',
self::categoriesCombo(
2022-11-14 20:47:24 +00:00
dcCore::app()->blog->getCategories(
2022-11-20 20:43:32 +00:00
['post_type' => 'post']
)
),
2022-11-20 20:43:32 +00:00
empty($post_expired['category']) ?
'' : $post_expired['category']
) . '</p>';
2022-11-20 20:43:32 +00:00
$fields['post_expired_selected'] = '<p><label for="post_expired_selected">' .
__('Selection:') . '</label>' .
form::combo(
'post_expired_selected',
self::selectedCombo(),
2022-11-20 20:43:32 +00:00
empty($post_expired['selected']) ?
'' : $post_expired['selected']
) . '</p>';
}
2022-11-20 20:43:32 +00:00
$fields['post_expired_comment'] = '<p><label for="post_expired_comment">' .
__('Comments status:') . '</label>' .
form::combo(
'post_expired_comment',
self::commentCombo(),
2022-11-20 20:43:32 +00:00
empty($post_expired['comment']) ?
'' : $post_expired['comment']
) . '</p>';
2022-11-20 20:43:32 +00:00
$fields['post_expired_trackback'] = '<p><label for="post_expired_trackback">' .
__('Trackbacks status:') . '</label>' .
form::combo(
'post_expired_trackback',
self::trackbackCombo(),
2022-11-20 20:43:32 +00:00
empty($post_expired['trackback']) ?
'' : $post_expired['trackback']
) . '</p>';
return $fields;
}
/**
* Custom categories combo
2022-11-20 20:43:32 +00:00
*
2022-11-14 20:47:24 +00:00
* @param dcRecord $categories Categories recordset
* @return array Categorires combo
*/
2022-11-14 20:47:24 +00:00
protected static function categoriesCombo(dcRecord $categories)
{
# Getting categories
2022-11-20 20:43:32 +00:00
$categories_combo = [
__('Not changed') => '',
__('Uncategorized') => '!',
];
try {
2022-11-14 20:47:24 +00:00
$categories = dcCore::app()->blog->getCategories(
2022-11-20 20:43:32 +00:00
['post_type' => 'post']
);
while ($categories->fetch()) {
$categories_combo[] = new formSelectOption(
2022-11-20 20:43:32 +00:00
str_repeat('&nbsp;&nbsp;', $categories->level - 1) . '&bull; ' . html::escapeHTML($categories->cat_title),
'!' . $categories->cat_id
);
}
} catch (Exception $e) {
2022-11-20 20:43:32 +00:00
return [];
}
return $categories_combo;
}
/**
* Custom status combo
2022-11-20 20:43:32 +00:00
*
* @return array Status combo
*/
protected static function statusCombo()
{
2022-11-20 20:43:32 +00:00
return [
__('Not changed') => '',
2022-11-20 20:43:32 +00:00
__('Published') => '!1',
__('Pending') => '!-2',
__('Unpublished') => '!0',
];
}
/**
* Custom selection combo
2022-11-20 20:43:32 +00:00
*
* @return array Selection combo
*/
protected static function selectedCombo()
{
2022-11-20 20:43:32 +00:00
return [
__('Not changed') => '',
__('Selected') => '!1',
__('Not selected') => '!0',
];
}
/**
* Custom comment status combo
2022-11-20 20:43:32 +00:00
*
* @return array Comment status combo
*/
protected static function commentCombo()
{
2022-11-20 20:43:32 +00:00
return [
__('Not changed') => '',
2022-11-20 20:43:32 +00:00
__('Opened') => '!1',
__('Closed') => '!0',
];
}
/**
* Custom trackback status combo
2022-11-20 20:43:32 +00:00
*
* @return array Trackback status combo
*/
protected static function trackbackCombo()
{
2022-11-20 20:43:32 +00:00
return [
__('Not changed') => '',
2022-11-20 20:43:32 +00:00
__('Opened') => '!1',
__('Closed') => '!0',
];
}
2022-11-20 20:43:32 +00:00
}