fac/_admin.php

419 lines
13 KiB
PHP
Raw Normal View History

2021-08-20 15:44:19 +00:00
<?php
2021-09-02 19:08:55 +00:00
/**
* @brief fac, a plugin for Dotclear 2
2022-11-20 20:13:56 +00:00
*
2021-09-02 19:08:55 +00:00
* @package Dotclear
* @subpackage Plugin
2022-11-20 20:13:56 +00:00
*
2021-09-02 19:08:55 +00:00
* @author Jean-Christian Denis and Contributors
2022-11-20 20:13:56 +00:00
*
2021-09-02 19:08:55 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-20 15:44:19 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
2021-08-20 16:39:12 +00:00
return null;
2021-08-20 15:44:19 +00:00
}
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
2021-08-20 15:44:19 +00:00
# Admin behaviors
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', ['facAdmin', 'adminBlogPreferencesFormV2']);
2022-11-20 20:13:56 +00:00
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', ['facAdmin', 'adminBeforeBlogSettingsUpdate']);
dcCore::app()->addBehavior('adminPostHeaders', ['facAdmin', 'adminPostHeaders']);
dcCore::app()->addBehavior('adminPostFormItems', ['facAdmin', 'adminPostFormItems']);
dcCore::app()->addBehavior('adminAfterPostCreate', ['facAdmin', 'adminAfterPostSave']);
dcCore::app()->addBehavior('adminAfterPostUpdate', ['facAdmin', 'adminAfterPostSave']);
dcCore::app()->addBehavior('adminBeforePostDelete', ['facAdmin', 'adminBeforePostDelete']);
dcCore::app()->addBehavior('adminPostsActions', ['facAdmin', 'adminPostsActions']);
2021-08-20 15:44:19 +00:00
/**
* @ingroup DC_PLUGIN_FAC
* @brief Linked feed to entries - admin methods.
* @since 2.6
*/
class facAdmin
{
2021-08-23 20:47:08 +00:00
/**
* Get combos of types of supported public pages
2022-11-20 20:13:56 +00:00
*
2021-08-23 20:47:08 +00:00
* @return array List of post type and name
*/
2022-11-20 20:13:56 +00:00
public static function getPostsTypes()
2021-08-23 20:47:08 +00:00
{
$types = [
2022-11-20 20:13:56 +00:00
__('home page') => 'default',
__('post pages') => 'post',
__('tags pages') => 'tag',
__('archives pages') => 'archive',
__('category pages') => 'category',
__('entries feed') => 'feed',
2021-08-23 20:47:08 +00:00
];
2022-11-20 20:13:56 +00:00
if (dcCore::app()->plugins->moduleExists('muppet')) {
foreach (muppet::getPostTypes() as $k => $v) {
2021-08-23 20:47:08 +00:00
$types[sprintf(
__('"%s" pages from extension muppet'),
$v['name']
)] = $k;
}
}
2022-11-20 20:13:56 +00:00
2021-08-23 20:47:08 +00:00
return $types;
}
/**
* Add settings to blog preference
2022-11-20 20:13:56 +00:00
*
2021-08-23 20:47:08 +00:00
* @param dcSettings $blog_settings dcSettings instance
*/
public static function adminBlogPreferencesFormV2(dcSettings $blog_settings)
2021-08-23 20:47:08 +00:00
{
echo
'<div class="fieldset"><h4 id="fac_params">Feed after content</h4>' .
2022-11-20 20:13:56 +00:00
'<p class="form-note">' .
2021-08-23 20:47:08 +00:00
__('To add feed to an entry edit this entry and put in sidebar the url of the feed and select a format.') .
'</p>';
2022-11-20 20:13:56 +00:00
if (dcCore::app()->auth->isSuperAdmin()) {
echo '<p><a href="' . dcCore::app()->adminurl->get('admin.plugins', [
'module' => basename(__DIR__),
2022-11-20 20:13:56 +00:00
'conf' => 1,
'redir' => dcCore::app()->adminurl->get('admin.blog.pref') . '#fac_params',
]) . '">' . __('Configure formats') . '</a></p>';
}
2022-11-20 20:13:56 +00:00
echo
2021-08-23 20:47:08 +00:00
'<div class="two-cols">' .
'<div class="col">' .
'<h5>' . __('Activation') . '</h5>' .
'<p><label class="classic">' .
form::checkbox('fac_active', '1', (bool) $blog_settings->get(basename(__DIR__))->get('active')) .
2021-08-23 20:47:08 +00:00
__('Enable "fac" extension') . '</label></p>' .
'<p class="form-note">' .
2022-11-20 20:13:56 +00:00
__('You can manage related feed to display for each post with a predefined format.') .
2021-08-23 20:47:08 +00:00
'</p>' .
'<h5>' . __('Feed') . '</h5>' .
'<p><label for="fac_defaultfeedtitle">' . __('Default title') . '</label>' .
form::field('fac_defaultfeedtitle', 65, 255, (string) $blog_settings->get(basename(__DIR__))->get('defaultfeedtitle')) . '</p>' .
2021-08-23 20:47:08 +00:00
'<p class="form-note">' . __('Use %T to insert title of feed.') . '</p>' .
'<p><label class="classic" for="fac_showfeeddesc">' .
form::checkbox('fac_showfeeddesc', 1, (bool) $blog_settings->get(basename(__DIR__))->get('showfeeddesc')) .
2021-08-23 20:47:08 +00:00
__('Show description of feed') . '</label></p>' .
'</div>' .
'<div class="col">' .
'<h5>' . __('Show feed after content on:') . '</h5>';
$fac_public_tpltypes = json_decode($blog_settings->get(basename(__DIR__))->get('public_tpltypes'), true);
2021-08-23 20:47:08 +00:00
if (!is_array($fac_public_tpltypes)) {
$fac_public_tpltypes = [];
}
2022-11-20 20:13:56 +00:00
foreach (self::getPostsTypes() as $k => $v) {
2021-08-23 20:47:08 +00:00
echo '
<p><label class="classic" for="fac_public_tpltypes' . $k . '">' .
form::checkbox(
['fac_public_tpltypes[]', 'fac_public_tpltypes' . $k],
$v,
in_array($v, $fac_public_tpltypes)
) . __($k) . '</label></p>';
}
2022-11-20 20:13:56 +00:00
echo
2021-08-23 20:47:08 +00:00
'</div>' .
'</div>' .
2022-11-20 20:13:56 +00:00
'<br class="clear" />' .
2021-08-23 20:47:08 +00:00
'</div>';
}
/**
* Save blog settings
2022-11-20 20:13:56 +00:00
*
2021-08-23 20:47:08 +00:00
* @param dcSettings $blog_settings dcSettings instance
*/
public static function adminBeforeBlogSettingsUpdate(dcSettings $blog_settings)
{
$blog_settings->get(basename(__DIR__))->put('active', !empty($_POST['fac_active']));
$blog_settings->get(basename(__DIR__))->put('public_tpltypes', json_encode($_POST['fac_public_tpltypes']));
$blog_settings->get(basename(__DIR__))->put('defaultfeedtitle', (string) $_POST['fac_defaultfeedtitle']);
$blog_settings->get(basename(__DIR__))->put('showfeeddesc', !empty($_POST['fac_showfeeddesc']));
2021-08-23 20:47:08 +00:00
}
2021-08-20 16:39:12 +00:00
/**
* Add javascript (toggle)
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +00:00
* @return string HTML head
*/
public static function adminPostHeaders()
{
return dcPage::jsModuleLoad(basename(__DIR__) . '/js/admin.js');
2021-08-20 16:39:12 +00:00
}
/**
* Add form to post sidebar
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +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 (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
2021-08-23 20:47:08 +00:00
return null;
}
2021-08-20 16:39:12 +00:00
# Get existing linked feed
$fac_url = $fac_format = '';
if ($post) {
2022-11-20 20:13:56 +00:00
$rs = dcCore::app()->meta->getMetadata([
2021-08-20 16:39:12 +00:00
'meta_type' => 'fac',
2022-11-20 20:13:56 +00:00
'post_id' => $post->post_id,
'limit' => 1,
2021-08-23 20:47:08 +00:00
]);
2021-08-20 16:39:12 +00:00
$fac_url = $rs->isEmpty() ? '' : $rs->meta_id;
2022-11-20 20:13:56 +00:00
$rs = dcCore::app()->meta->getMetadata([
2021-08-20 16:39:12 +00:00
'meta_type' => 'facformat',
2022-11-20 20:13:56 +00:00
'post_id' => $post->post_id,
'limit' => 1,
2021-08-23 20:47:08 +00:00
]);
2021-08-20 16:39:12 +00:00
$fac_format = $rs->isEmpty() ? '' : $rs->meta_id;
}
# Set linked feed form items
2022-11-20 20:13:56 +00:00
$sidebar_items['options-box']['items']['fac'] = self::formFeed($fac_url, $fac_format);
2021-08-20 16:39:12 +00:00
}
/**
* Save linked feed
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +00:00
* @param cursor $cur Current post cursor
* @param integer $post_id Post id
*/
public static function adminAfterPostSave(cursor $cur, $post_id)
{
2022-11-20 20:13:56 +00:00
if (!isset($_POST['fac_url'])
2021-08-20 16:39:12 +00:00
|| !isset($_POST['fac_format'])) {
return null;
}
# Delete old linked feed
2022-11-20 20:13:56 +00:00
self::delFeed($post_id);
2021-08-20 16:39:12 +00:00
# Add new linked feed
2022-11-20 20:13:56 +00:00
self::addFeed($post_id, $_POST);
2021-08-20 16:39:12 +00:00
}
/**
* Delete linked feed on post edition
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +00:00
* @param integer $post_id Post id
*/
public static function adminBeforePostDelete($post_id)
{
2022-11-20 20:13:56 +00:00
self::delFeed($post_id);
2021-08-20 16:39:12 +00:00
}
/**
* Add actions to posts page combo
2022-11-20 20:13:56 +00:00
*
* @param dcPostsActions $pa dcPostsActionsPage instance
2021-08-20 16:39:12 +00:00
*/
public static function adminPostsActions(dcPostsActions $pa)
2021-08-20 16:39:12 +00:00
{
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
2021-08-23 20:47:08 +00:00
return null;
}
2021-08-20 16:39:12 +00:00
$pa->addAction(
2021-08-23 20:47:08 +00:00
[__('Linked feed') => [__('Add feed') => 'fac_add']],
['facAdmin', 'callbackAdd']
2021-08-20 16:39:12 +00:00
);
2022-11-20 20:13:56 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_DELETE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)) {
2021-08-20 16:39:12 +00:00
return null;
}
$pa->addAction(
2021-08-23 20:47:08 +00:00
[__('Linked feed') => [__('Remove feed') => 'fac_remove']],
['facAdmin', 'callbackRemove']
2021-08-20 16:39:12 +00:00
);
}
/**
* Posts actions callback to remove linked feed
2022-11-20 20:13:56 +00:00
*
* @param dcPostsActions $pa dcPostsActions instance
2021-08-20 16:39:12 +00:00
* @param ArrayObject $post _POST actions
*/
2022-11-20 20:13:56 +00:00
public static function callbackRemove(dcPostsActions $pa, ArrayObject $post)
2021-08-20 16:39:12 +00:00
{
# No entry
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
# No right
2022-11-20 20:13:56 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_DELETE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id)) {
2021-08-20 16:39:12 +00:00
throw new Exception(__('No enough right'));
}
2021-08-23 20:47:08 +00:00
# Delete unused feed
2022-11-20 20:13:56 +00:00
foreach ($posts_ids as $post_id) {
self::delFeed($post_id);
2021-08-20 16:39:12 +00:00
}
dcPage::addSuccessNotice(__('Linked feed deleted.'));
$pa->redirect(true);
}
/**
* Posts actions callback to add linked feed
2022-11-20 20:13:56 +00:00
*
* @param dcPostsActions $pa dcPostsActions instance
2021-08-20 16:39:12 +00:00
* @param ArrayObject $post _POST actions
*/
2022-11-20 20:13:56 +00:00
public static function callbackAdd(dcPostsActions $pa, ArrayObject $post)
2021-08-20 16:39:12 +00:00
{
2022-11-20 20:13:56 +00:00
# No entry
2021-08-20 16:39:12 +00:00
$posts_ids = $pa->getIDs();
if (empty($posts_ids)) {
throw new Exception(__('No entry selected'));
}
# Save action
if (!empty($post['fac_url'])
&& !empty($post['fac_format'])) {
2022-11-20 20:13:56 +00:00
foreach ($posts_ids as $post_id) {
self::delFeed($post_id);
self::addFeed($post_id, $post);
2021-08-20 16:39:12 +00:00
}
dcPage::addSuccessNotice(__('Linked feed added.'));
$pa->redirect(true);
# Display form
} else {
$pa->beginPage(
2021-08-23 20:47:08 +00:00
dcPage::breadcrumb([
2022-11-20 20:13:56 +00:00
html::escapeHTML(dcCore::app()->blog->name) => '',
$pa->getCallerTitle() => $pa->getRedirection(true),
__('Linked feed to this selection') => '',
2021-08-23 20:47:08 +00:00
])
2021-08-20 16:39:12 +00:00
);
echo
'<form action="' . $pa->getURI() . '" method="post">' .
$pa->getCheckboxes() .
2022-11-20 20:13:56 +00:00
self::formFeed() .
2021-08-20 16:39:12 +00:00
'<p>' .
2022-11-20 20:13:56 +00:00
dcCore::app()->formNonce() .
2021-08-20 16:39:12 +00:00
$pa->getHiddenFields() .
2021-08-23 20:47:08 +00:00
form::hidden(['action'], 'fac_add') .
2021-08-20 16:39:12 +00:00
'<input type="submit" value="' . __('Save') . '" /></p>' .
'</form>';
$pa->endPage();
}
}
/**
* Linked feed form field
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +00:00
* @param string $url Feed URL
* @param string $format Feed format
2022-11-20 20:13:56 +00:00
* @return null|string Feed form content
2021-08-20 16:39:12 +00:00
*/
2022-11-20 20:13:56 +00:00
protected static function formFeed($url = '', $format = '')
2021-08-20 16:39:12 +00:00
{
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
2021-08-23 20:47:08 +00:00
return null;
}
2022-11-20 20:13:56 +00:00
return
'<div id="fac">' .
2021-08-20 16:39:12 +00:00
'<h5>' . __('Linked feed') . '</h5>' .
'<p><label for="fac_url">' .
__('Feed URL:') . '</label>' .
form::field(
'fac_url',
60,
255,
$url,
'maximal'
) . '</p>' .
'<p><label for="fac_format">' .
__('Format:') . '</label>' .
form::combo(
'fac_format',
2022-11-20 20:13:56 +00:00
self::comboFac(),
2021-08-20 16:39:12 +00:00
$format,
'maximal'
) . '</p>' .
($url ? '<p><a href="' . $url . '" title="' . $url . '">' . __('view feed') . '</a></p>' : '') .
'</div>';
}
/**
* List of fac formats
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +00:00
* @return array List of fac formats
*/
2022-11-20 20:13:56 +00:00
protected static function comboFac()
2021-08-20 16:39:12 +00:00
{
$formats = json_decode(dcCore::app()->blog->settings->get(basename(__DIR__))->get('formats'), true);
2021-08-20 16:39:12 +00:00
if (!is_array($formats) || empty($formats)) {
2021-08-23 20:47:08 +00:00
return [];
2021-08-20 16:39:12 +00:00
}
2021-08-23 20:47:08 +00:00
$res = [];
2022-11-20 20:13:56 +00:00
foreach ($formats as $uid => $f) {
2021-08-20 16:39:12 +00:00
$res[$f['name']] = $uid;
}
return $res;
}
/**
* Delete linked feed
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +00:00
* @param integer $post_id Post id
*/
2022-11-20 20:13:56 +00:00
protected static function delFeed($post_id)
2021-08-20 16:39:12 +00:00
{
2022-11-20 20:13:56 +00:00
$post_id = (int) $post_id;
dcCore::app()->meta->delPostMeta($post_id, 'fac');
dcCore::app()->meta->delPostMeta($post_id, 'facformat');
2021-08-20 16:39:12 +00:00
}
/**
* Add linked feed
2022-11-20 20:13:56 +00:00
*
2021-08-20 16:39:12 +00:00
* @param integer $post_id Post id
2022-11-20 20:13:56 +00:00
* @param array|ArrayObject $options Feed options
2021-08-20 16:39:12 +00:00
*/
2022-11-20 20:13:56 +00:00
protected static function addFeed($post_id, $options)
2021-08-20 16:39:12 +00:00
{
2022-11-20 20:13:56 +00:00
if (empty($options['fac_url'])
2021-08-20 16:39:12 +00:00
|| empty($options['fac_format'])) {
return null;
}
2022-11-20 20:13:56 +00:00
$post_id = (int) $post_id;
2021-08-20 16:39:12 +00:00
2022-11-20 20:13:56 +00:00
dcCore::app()->meta->setPostMeta(
2021-08-20 16:39:12 +00:00
$post_id,
'fac',
$options['fac_url']
);
2022-11-20 20:13:56 +00:00
dcCore::app()->meta->setPostMeta(
2021-08-20 16:39:12 +00:00
$post_id,
'facformat',
$options['fac_format']
);
}
2022-11-20 20:13:56 +00:00
}