postWidgetText/inc/lib.pwt.admin.php

143 lines
3.8 KiB
PHP
Raw Normal View History

2021-09-10 19:06:49 +00:00
<?php
2021-09-10 19:18:51 +00:00
/**
* @brief postWidgetText, 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-09-10 19:06:49 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
2021-09-10 19:18:51 +00:00
return null;
2021-09-10 19:06:49 +00:00
}
/**
* @ingroup DC_PLUGIN_POSTWIDGETTEXT
* @brief postWidgetText - admin post methods.
* @since 2.6
*/
class postWidgetTextAdmin
{
2021-10-29 22:36:04 +00:00
public static function sortbyCombo()
{
return [
__('Post title') => 'post_title',
__('Post date') => 'post_dt',
__('Widget title') => 'option_title',
__('Widget date') => 'option_upddt',
];
}
public static function adminFiltersLists(dcCore $core, $sorts)
{
$sorts['pwt'] = [
__('Post widget text'),
self::sortbyCombo(),
'post_dt',
'desc',
[__('entries per page'), 20]
];
}
2021-09-10 19:18:51 +00:00
public static function headers()
{
2021-10-29 22:36:04 +00:00
return dcPage::jsLoad(dcPage::getPF('postWidgetText/js/post.js'));
2021-09-10 19:18:51 +00:00
}
public static function form($main, $sidebar, $post)
{
# _POST fields
2021-10-29 23:12:43 +00:00
$title = $_POST['post_wtitle'] ?? '';
$content = $_POST['post_wtext'] ?? '';
2021-09-10 19:18:51 +00:00
# Existing post
if ($post) {
$post_id = (integer) $post->post_id;
$pwt = new postWidgetText($GLOBALS['core']);
2021-10-29 23:12:43 +00:00
$w = $pwt->getWidgets(['post_id' => $post_id]);
2021-09-10 19:18:51 +00:00
# Existing widget
if (!$w->isEmpty()) {
$title = $w->option_title;
$content = $w->option_content;
}
}
$main['post_widget'] =
2021-10-29 23:12:43 +00:00
'<div id="post-wtext-form">' .
'<h4>' . __('Additional widget') . '</h4>' .
2021-09-10 19:18:51 +00:00
2021-10-29 23:12:43 +00:00
'<p class="col">'
'<label class="bold" for="post_wtitle">' . __('Widget title:') . '</label>' .
form::field('post_wtitle', 20, 255, html::escapeHTML($title), 'maximal') .
'</p>' .
2021-09-10 19:18:51 +00:00
2021-10-29 23:12:43 +00:00
'<p class="area" id="post-wtext">' .
'<label class="bold" for="post_wtext">' .__('Wigdet text:') . '</label>' .
form::textarea('post_wtext', 50, 5, html::escapeHTML($content)) .
'</p>' .
2021-09-10 19:18:51 +00:00
'</div>';
}
public static function save($cur, $post_id)
{
$post_id = (integer) $post_id;
# _POST fields
2021-10-29 23:12:43 +00:00
$title = $_POST['post_wtitle'] ?? '';
$content = $_POST['post_wtext'] ?? '';
2021-09-10 19:18:51 +00:00
# Object
$pwt = new postWidgetText($GLOBALS['core']);
# Get existing widget
2021-10-29 23:12:43 +00:00
$w = $pwt->getWidgets(['post_id' => $post_id]);
2021-09-10 19:18:51 +00:00
# If new content is empty, delete old existing widget
if (empty($title) && empty($content) && !$w->isEmpty()) {
$pwt->delWidget($w->option_id);
}
# If new content is not empty
if (!empty($title) || !empty($content)) {
$wcur = $pwt->openCursor();
$wcur->post_id = $post_id;
2021-10-29 23:12:43 +00:00
$wcur->option_type = 'postwidgettext';
$wcur->option_lang = $cur->post_lang;
$wcur->option_format = $cur->post_format;
$wcur->option_title = $title;
$wcur->option_content = $content;
2021-09-10 19:18:51 +00:00
# Create widget
if ($w->isEmpty()) {
$id = $pwt->addWidget($wcur);
}
# Upddate widget
else {
2021-10-29 23:12:43 +00:00
$pwt->updWidget($w->option_id, $wcur);
2021-09-10 19:18:51 +00:00
}
}
}
public static function delete($post_id)
{
$post_id = (integer) $post_id;
# Object
$pwt = new postWidgetText($GLOBALS['core']);
# Get existing widget
2021-10-29 23:12:43 +00:00
$w = $pwt->getWidgets(['post_id' => $post_id]);
2021-09-10 19:18:51 +00:00
# If new content is empty, delete old existing widget
if (!$w->isEmpty()) {
$pwt->delWidget($w->option_id);
}
}
}