<?php
namespace lgxNotepad\Pub\Controller;
use XF\Mvc\ParameterBag;
use XF\Api\Controller\AbstractController;
class Notepad extends AbstractController
{
public function actionIndex(ParameterBag $params)
{
$db = \XF::db();
$visitor = \XF::visitor();
if(!$visitor->hasPermission('general', 'lgxNotepadPermission')){
return $this->noPermission();
}
if($params['notepad_id'])
{
$notepad_id = $params['notepad_id'];
$note = $db->fetchRow("SELECT * FROM lgx_notepad WHERE notepad_id= '$notepad_id' AND user_id = '$visitor->user_id' ");
if(!$note){
return $this->redirect( $this->buildLink('notepad') );
}
$params = [
'note' => $note,
];
return $this->view('', 'lgx_notepadSingle', $params);
}
$entries = $db->fetchAll("SELECT * FROM lgx_notepad WHERE user_id = '$visitor->user_id' ORDER BY edit_at DESC");
$params = [
'notes' => $entries,
];
return $this->view('', 'lgx_notepad', $params);
}
public function actionAdd()
{
$db = \XF::db();
$visitor = \XF::visitor();
$time = \XF::$time;
if(!$visitor->hasPermission('general', 'lgxNotepadPermission')){
return $this->noPermission();
}
if($this->isPost())
{
$input = $this->filter([
'subject' => 'str',
'description_html' => 'str',
]);
$subject = $input['subject'];
$html = $input['description_html'];
$bbCode = \XF\Html\Renderer\BbCode::renderFromHtml($html);
$description = \XF::cleanString($bbCode);
$db->query("INSERT INTO `lgx_notepad` (`notepad_id`, `user_id`, `subject`, `description`, `created_at`, `edit_at`) VALUES (? ,? ,? ,? ,? ,?) ", [NULL, $visitor->user_id, new \XF\PreEscaped($subject), new \XF\PreEscaped($description), $time, $time]);
$last_insert = $db->lastInsertId();
$note = $db->fetchRow("SELECT * FROM lgx_notepad WHERE notepad_id= '$last_insert'");
return $this->redirect( $this->buildLink('notepad', $note) );
}
return $this->view('', 'lgx_notepadAddEdit');
}
public function actionEdit(ParameterBag $params)
{
$db = \XF::db();
$visitor = \XF::visitor();
$time = \XF::$time;
if(!$visitor->hasPermission('general', 'lgxNotepadPermission')){
return $this->noPermission();
}
$notepad_id = $this->filter('id', 'uint');
if(!$notepad_id){
$notepad_id = $params['notepad_id'];
}
$note = $db->fetchRow("SELECT * FROM lgx_notepad WHERE notepad_id= '$notepad_id' AND user_id = '$visitor->user_id' ");
if(!$note)
{
return $this->redirect( $this->buildLink('notepad') );
}
if($this->isPost())
{
$input = $this->filter([
'subject' => 'str',
'description_html' => 'str',
]);
$subject = $input['subject'];
$html = $input['description_html'];
$bbCode = \XF\Html\Renderer\BbCode::renderFromHtml($html);
$description = \XF::cleanString($bbCode);
$db->query(" UPDATE `lgx_notepad` SET
`subject`= ?, `description`= ?, `edit_at` = ? , `user_id` = ? WHERE `notepad_id` = ? ",
[new \XF\PreEscaped($subject), new \XF\PreEscaped($description), $time, $visitor->user_id, $notepad_id] );
return $this->redirect( $this->buildLink('notepad', $note) );
}
$params = [
'note' => $note,
];
return $this->view('', 'lgx_notepadAddEdit', $params);
}
public function actionDelete(ParameterBag $params)
{
$db = \XF::db();
$visitor = \XF::visitor();
if(!$visitor->hasPermission('general', 'lgxNotepadPermission')){
return $this->noPermission();
}
$notepad_id = $params['notepad_id'];
$db->query("DELETE FROM lgx_notepad WHERE notepad_id= '$notepad_id' AND user_id='$visitor->user_id'");
return $this->redirect( $this->buildLink('notepad') );
}
}