postWidgetText/inc/class.postwidgettext.php

15 lines
5.8 KiB
PHP

<?php
# -- BEGIN LICENSE BLOCK ----------------------------------
#
# This file is part of postWidgetText, a plugin for Dotclear 2.
#
# Copyright (c) 2009-2013 Jean-Christian Denis and contributors
# contact@jcdenis.fr http://jcd.lv
#
# Licensed under the GPL version 2.0 license.
# A copy of this license is available in LICENSE file or at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK ------------------------------------
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;
$this->table = $this->core->prefix.'post_option';
$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();
}
public function getWidgets($params,$count_only=false)
{
if (!isset($params['columns'])) $params['columns'] = array();
$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'] = '';
}
$params['from'] .= 'LEFT JOIN '.$this->table.' W ON P.post_id=W.post_id ';
if (!isset($params['sql'])) {
$params['sql'] = '';
}
if (isset($params['option_type'])) {
$params['sql'] .= "AND W.option_type = '".$this->con->escape($params['option_type'])."' ";
}
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) '.
'FROM '.$this->table
);
$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;
}
public function updWidget($id,&$cur)
{
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'));
}
$this->getWidgetContent($cur,$id);
$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'));
}
}
$cur->update('WHERE option_id = '.$id.' ');
$this->triggerBlog();
}
public function delWidget($id,$type='postwidgettext')
{
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(
'DELETE FROM '.$this->table.' '.
'WHERE option_id = '.$id.' '.
"AND option_type = '".$this->con->escape($type)."' "
);
$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();
$this->core->wiki2xhtml->setOpt('note_prefix','wnote-'.$option_id);
if (strpos($lang,'fr') === 0) {
$this->core->wiki2xhtml->setOpt('active_fr_syntax', 1);
}
}
if ($content) {
$content_xhtml = $this->core->callFormater($format,$content);
$content_xhtml = $this->core->HTMLfilter($content_xhtml);
}
else {
$content_xhtml = '';
}
$excerpt = $excerpt_xhtml = '';
# --BEHAVIOR-- coreAfterPostContentFormat
$this->core->callBehavior('coreAfterPostContentFormat',array(
'excerpt' => &$excerpt,
'content' => &$content,
'excerpt_xhtml' => &$excerpt_xhtml,
'content_xhtml' => &$content_xhtml
));
}
}