postWidgetText/inc/class.postwidgettext.php

254 lines
7.4 KiB
PHP
Raw Normal View History

2021-09-10 19:18:51 +00:00
<?php
/**
* @brief postWidgetText, a plugin for Dotclear 2
2021-11-06 15:11:19 +00:00
*
2021-09-10 19:18:51 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:11:19 +00:00
*
2021-09-10 19:18:51 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-06 15:11:19 +00:00
*
2021-09-10 19:18:51 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
2021-10-30 13:49:37 +00:00
/**
* @ingroup DC_PLUGIN_POSTWIDGETTEXT
* @brief postWidgetText - admin and public methods.
* @since 2.6
*/
2021-09-10 19:18:51 +00:00
class postWidgetText
{
public $con;
private $table;
private $blog;
2022-11-15 21:05:23 +00:00
public function __construct()
2021-09-10 19:18:51 +00:00
{
2022-11-15 21:05:23 +00:00
$this->con = dcCore::app()->con;
2022-12-03 15:57:39 +00:00
$this->table = dcCore::app()->prefix . initPostWidgetText::PWT_TABLE_NAME;
2022-11-15 21:05:23 +00:00
$this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id);
2021-09-10 19:18:51 +00:00
}
public function tableName()
{
return $this->table;
}
public function openCursor()
{
return $this->con->openCursor($this->table);
}
public function lockTable()
{
$this->con->writeLock($this->table);
}
public function unlockTable()
{
$this->con->unlock();
}
public function triggerBlog()
{
2022-11-15 21:05:23 +00:00
dcCore::app()->blog->triggerBlog();
2021-09-10 19:18:51 +00:00
}
2021-10-29 23:12:43 +00:00
public function getWidgets($params, $count_only = false)
2021-09-10 19:18:51 +00:00
{
2021-11-06 15:11:19 +00:00
if (!isset($params['columns'])) {
$params['columns'] = [];
}
2021-09-10 19:18:51 +00:00
$params['columns'][] = 'option_id';
$params['columns'][] = 'option_creadt';
$params['columns'][] = 'option_upddt';
$params['columns'][] = 'option_type';
$params['columns'][] = 'option_format';
$params['columns'][] = 'option_lang';
$params['columns'][] = 'option_title';
$params['columns'][] = 'option_content';
$params['columns'][] = 'option_content_xhtml';
if (!isset($params['from'])) {
$params['from'] = '';
}
2022-11-15 21:05:23 +00:00
$params['join'] = 'LEFT JOIN ' . $this->table . ' W ON P.post_id=W.post_id ';
2021-09-10 19:18:51 +00:00
if (!isset($params['sql'])) {
$params['sql'] = '';
}
if (isset($params['option_type'])) {
2021-10-29 23:12:43 +00:00
$params['sql'] .= "AND W.option_type = '" . $this->con->escape($params['option_type']) . "' ";
2021-11-06 15:11:19 +00:00
} else {
$params['sql'] .= "AND W.option_type = '" . $this->con->escape(basename(__DIR__)) . "' ";
2021-09-10 19:18:51 +00:00
}
unset($params['option_type']);
if (!isset($params['post_type'])) {
$params['post_type'] = '';
}
2022-11-15 21:05:23 +00:00
return dcCore::app()->blog->getPosts($params, $count_only);
2021-09-10 19:18:51 +00:00
}
public function addWidget($cur)
{
2022-11-15 21:05:23 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), $this->blog)) {
2021-09-10 19:18:51 +00:00
throw new Exception(__('You are not allowed to create an entry text widget'));
}
if ($cur->post_id == '') {
throw new Exception('No such entry ID');
}
$this->lockTable();
2021-11-06 15:11:19 +00:00
2021-09-10 19:18:51 +00:00
try {
$rs = $this->con->select(
2021-11-06 15:11:19 +00:00
'SELECT MAX(option_id) ' .
'FROM ' . $this->table
2021-09-10 19:18:51 +00:00
);
2021-11-06 15:11:19 +00:00
$cur->option_id = (int) $rs->f(0) + 1;
2021-09-10 19:18:51 +00:00
$cur->option_creadt = date('Y-m-d H:i:s');
2021-11-06 15:11:19 +00:00
$cur->option_upddt = date('Y-m-d H:i:s');
2021-09-10 19:18:51 +00:00
$this->getWidgetContent($cur, $cur->option_id);
$cur->insert();
$this->unlockTable();
2021-11-06 15:11:19 +00:00
} catch (Exception $e) {
2021-09-10 19:18:51 +00:00
$this->unlockTable();
2021-11-06 15:11:19 +00:00
2021-09-10 19:18:51 +00:00
throw $e;
}
$this->triggerBlog();
return $cur->option_id;
}
2021-10-29 23:12:43 +00:00
public function updWidget($id, &$cur)
2021-09-10 19:18:51 +00:00
{
2022-11-15 21:05:23 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), $this->blog)) {
2021-09-10 19:18:51 +00:00
throw new Exception(__('You are not allowed to update entries text widget'));
}
2021-11-06 15:11:19 +00:00
$id = (int) $id;
2021-09-10 19:18:51 +00:00
if (empty($id)) {
throw new Exception(__('No such ID'));
}
2021-10-29 23:12:43 +00:00
$this->getWidgetContent($cur, $id);
2021-09-10 19:18:51 +00:00
$cur->option_upddt = date('Y-m-d H:i:s');
2022-12-03 15:49:44 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), $this->blog)) {
2021-11-06 15:11:19 +00:00
$params['option_id'] = $id;
2022-11-15 21:05:23 +00:00
$params['user_id'] = $this->con->escape(dcCore::app()->auth->userID());
2021-09-10 19:18:51 +00:00
$params['no_content'] = true;
2021-11-06 15:11:19 +00:00
$params['limit'] = 1;
2021-09-10 19:18:51 +00:00
$rs = $this->getWidgets($params);
if ($rs->isEmpty()) {
throw new Exception(__('You are not allowed to delete this entry text widget'));
}
}
2021-10-29 23:12:43 +00:00
$cur->update('WHERE option_id = ' . $id . ' ');
2021-09-10 19:18:51 +00:00
$this->triggerBlog();
}
public function delWidget($id, $type = '')
2021-09-10 19:18:51 +00:00
{
2022-11-15 21:05:23 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_DELETE,
dcAuth::PERMISSION_CONTENT_ADMIN,
]), $this->blog)) {
2021-09-10 19:18:51 +00:00
throw new Exception(__('You are not allowed to delete entries text widget'));
}
2022-12-23 13:10:54 +00:00
$id = (int) $id;
$type ??= basename(__DIR__);
2021-09-10 19:18:51 +00:00
if (empty($id)) {
throw new Exception(__('No such ID'));
}
2022-12-03 15:49:44 +00:00
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), $this->blog)) {
2021-11-06 15:11:19 +00:00
$params['option_id'] = $id;
2022-11-15 21:05:23 +00:00
$params['user_id'] = $this->con->escape(dcCore::app()->auth->userID());
2021-09-10 19:18:51 +00:00
$params['no_content'] = true;
2021-11-06 15:11:19 +00:00
$params['limit'] = 1;
2021-09-10 19:18:51 +00:00
$rs = $this->getWidgets($params);
if ($rs->isEmpty()) {
throw new Exception(__('You are not allowed to delete this entry text widget'));
}
}
$this->con->execute(
2021-10-29 23:12:43 +00:00
'DELETE FROM ' . $this->table . ' ' .
'WHERE option_id = ' . $id . ' ' .
"AND option_type = '" . $this->con->escape($type) . "' "
2021-09-10 19:18:51 +00:00
);
$this->triggerBlog();
}
private function getWidgetContent(&$cur, $option_id)
{
2021-11-06 15:11:19 +00:00
$option_content = $cur->option_content;
2021-09-10 19:18:51 +00:00
$option_content_xhtml = $cur->option_content_xhtml;
$this->setWidgetContent(
2021-11-06 15:11:19 +00:00
$option_id,
$cur->option_format,
$cur->option_lang,
$option_content,
$option_content_xhtml
2021-09-10 19:18:51 +00:00
);
2021-11-06 15:11:19 +00:00
$cur->option_content = $option_content;
2021-09-10 19:18:51 +00:00
$cur->option_content_xhtml = $option_content_xhtml;
}
public function setWidgetContent($option_id, $format, $lang, &$content, &$content_xhtml)
{
if ($format == 'wiki') {
2022-11-15 21:05:23 +00:00
dcCore::app()->initWikiPost();
dcCore::app()->wiki2xhtml->setOpt('note_prefix', 'wnote-' . $option_id);
2021-10-29 23:12:43 +00:00
if (strpos($lang, 'fr') === 0) {
2022-11-15 21:05:23 +00:00
dcCore::app()->wiki2xhtml->setOpt('active_fr_syntax', 1);
2021-09-10 19:18:51 +00:00
}
}
if ($content) {
2022-11-15 21:05:23 +00:00
$content_xhtml = dcCore::app()->callFormater($format, $content);
$content_xhtml = dcCore::app()->HTMLfilter($content_xhtml);
2021-11-06 15:11:19 +00:00
} else {
2021-09-10 19:18:51 +00:00
$content_xhtml = '';
}
$excerpt = $excerpt_xhtml = '';
# --BEHAVIOR-- coreAfterPostContentFormat
2022-11-15 21:05:23 +00:00
dcCore::app()->callBehavior('coreAfterPostContentFormat', [
2021-11-06 15:11:19 +00:00
'excerpt' => &$excerpt,
'content' => &$content,
2021-09-10 19:18:51 +00:00
'excerpt_xhtml' => &$excerpt_xhtml,
2022-11-15 21:07:37 +00:00
'content_xhtml' => &$content_xhtml,
2021-10-29 23:12:43 +00:00
]);
2021-09-10 19:18:51 +00:00
}
2021-11-06 15:11:19 +00:00
}