postWidgetText/inc/class.postwidgettext.php

239 lines
6.7 KiB
PHP
Raw Normal View History

2021-09-10 19:18:51 +00:00
<?php
/**
* @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
*/
if (!defined('DC_RC_PATH')) {
return null;
}
class postWidgetText
{
public $core;
public $con;
private $table;
private $blog;
public function __construct($core)
{
$this->core =& $core;
$this->con =& $this->core->con;
2021-10-29 23:12:43 +00:00
$this->table = $this->core->prefix . 'post_option';
2021-09-10 19:18:51 +00:00
$this->blog = $core->con->escape($core->blog->id);
}
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()
{
$this->core->blog->triggerBlog();
}
2021-10-29 23:12:43 +00:00
public function getWidgets($params, $count_only = false)
2021-09-10 19:18:51 +00:00
{
2021-10-29 23:12:43 +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'] = '';
}
2021-10-29 23:12:43 +00:00
$params['from'] .= '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-09-10 19:18:51 +00:00
}
else {
$params['sql'] .= "AND W.option_type = 'postwidgettext' ";
}
unset($params['option_type']);
if (!isset($params['post_type'])) {
$params['post_type'] = '';
}
return $this->core->blog->getPosts($params, $count_only);
}
public function addWidget($cur)
{
if (!$this->core->auth->check('usage,contentadmin', $this->blog)) {
throw new Exception(__('You are not allowed to create an entry text widget'));
}
if ($cur->post_id == '') {
throw new Exception('No such entry ID');
return null;
}
$this->lockTable();
try {
$rs = $this->con->select(
'SELECT MAX(option_id) '.
2021-10-29 23:12:43 +00:00
'FROM ' . $this->table
2021-09-10 19:18:51 +00:00
);
$cur->option_id = (integer) $rs->f(0) + 1;
$cur->option_creadt = date('Y-m-d H:i:s');
$cur->option_upddt = date('Y-m-d H:i:s');
$this->getWidgetContent($cur, $cur->option_id);
$cur->insert();
$this->unlockTable();
}
catch (Exception $e) {
$this->unlockTable();
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
{
if (!$this->core->auth->check('usage,contentadmin', $this->blog)) {
throw new Exception(__('You are not allowed to update entries text widget'));
}
$id = (integer) $id;
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');
if (!$this->core->auth->check('contentadmin', $this->blog)) {
$params['option_id'] = $id;
$params['user_id'] = $this->con->escape($this->core->auth->userID());
$params['no_content'] = true;
$params['limit'] = 1;
$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();
}
2021-10-29 23:12:43 +00:00
public function delWidget($id, $type = 'postwidgettext')
2021-09-10 19:18:51 +00:00
{
if (!$this->core->auth->check('delete,contentadmin', $this->blog)) {
throw new Exception(__('You are not allowed to delete entries text widget'));
}
$id = (integer) $id;
if (empty($id)) {
throw new Exception(__('No such ID'));
}
if (!$this->core->auth->check('contentadmin', $this->blog)) {
$params['option_id'] = $id;
$params['user_id'] = $this->con->escape($this->core->auth->userID());
$params['no_content'] = true;
$params['limit'] = 1;
$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)
{
$option_content = $cur->option_content;
$option_content_xhtml = $cur->option_content_xhtml;
$this->setWidgetContent(
$option_id,$cur->option_format,$cur->option_lang,
$option_content,$option_content_xhtml
);
$cur->option_content = $option_content;
$cur->option_content_xhtml = $option_content_xhtml;
}
public function setWidgetContent($option_id, $format, $lang, &$content, &$content_xhtml)
{
if ($format == 'wiki') {
$this->core->initWikiPost();
2021-10-29 23:12:43 +00:00
$this->core->wiki2xhtml->setOpt('note_prefix','wnote-' . $option_id);
if (strpos($lang, 'fr') === 0) {
2021-09-10 19:18:51 +00:00
$this->core->wiki2xhtml->setOpt('active_fr_syntax', 1);
}
}
if ($content) {
2021-10-29 23:12:43 +00:00
$content_xhtml = $this->core->callFormater($format, $content);
2021-09-10 19:18:51 +00:00
$content_xhtml = $this->core->HTMLfilter($content_xhtml);
}
else {
$content_xhtml = '';
}
$excerpt = $excerpt_xhtml = '';
# --BEHAVIOR-- coreAfterPostContentFormat
2021-10-29 23:12:43 +00:00
$this->core->callBehavior('coreAfterPostContentFormat', [
2021-09-10 19:18:51 +00:00
'excerpt' => &$excerpt,
'content' => &$content,
'excerpt_xhtml' => &$excerpt_xhtml,
'content_xhtml' => &$content_xhtml
2021-10-29 23:12:43 +00:00
]);
2021-09-10 19:18:51 +00:00
}
}