mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
wiki
This commit is contained in:
@ -3,12 +3,111 @@
|
||||
namespace App\Controllers\Wiki;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\Wiki\WikiContentModel;
|
||||
use App\Models\Wiki\WikiFileModel;
|
||||
use App\Models\Wiki\WikiPageModel;
|
||||
use App\Models\Wiki\WikiSectionModel;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class WikiController extends BaseController
|
||||
{
|
||||
protected WikiSectionModel $wikiSectionModel;
|
||||
protected WikiContentModel $wikiContentModel;
|
||||
protected WikiPageModel $wikiPageModel;
|
||||
protected WikiFileModel $wikiFileModel;
|
||||
|
||||
protected string $locale;
|
||||
protected array $viewData;
|
||||
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
$this->wikiSectionModel = model(WikiSectionModel::class);
|
||||
$this->wikiPageModel = model(WikiPageModel::class);
|
||||
$this->wikiContentModel = model(WikiContentModel::class);
|
||||
$this->wikiFileModel = model(WikiFileModel::class);
|
||||
|
||||
$sections = $this->wikiSectionModel->sections();
|
||||
$this->locale = session()->get('lang');
|
||||
|
||||
$this->viewData['wiki_sections'] = $sections;
|
||||
parent::initController($request, $response, $logger);
|
||||
}
|
||||
public function index()
|
||||
{
|
||||
return view('themes/vuexy/wiki/pages/home');
|
||||
return view('themes/vuexy/wiki/pages/render', $this->viewData);
|
||||
}
|
||||
public function show_page(string $slug)
|
||||
{
|
||||
$section = $this->wikiSectionModel->where('slug', $slug)->first();
|
||||
$this->viewData['slug'] = $slug;
|
||||
$this->viewData['section'] = $section->withAll($this->locale);
|
||||
|
||||
return view('themes/vuexy/wiki/pages/render', $this->viewData);
|
||||
}
|
||||
public function get_section(int $section_id)
|
||||
{
|
||||
$section = $this->wikiSectionModel->find($section_id)->withAll($this->locale);
|
||||
return $this->response->setJSON(["data" => $section, "message" => lang("App.global_alert_fetch_success")]);
|
||||
}
|
||||
public function store_save_page(int $section_id)
|
||||
{
|
||||
$bodyData = $this->request->getPost();
|
||||
$wikiSectionPage = $this->wikiSectionModel->find($section_id)->page();
|
||||
if ($wikiSectionPage) {
|
||||
$wikiPageId = $wikiSectionPage->id;
|
||||
} else {
|
||||
$wikiPageId = $this->wikiPageModel->insert(['section_id' => $section_id]);
|
||||
}
|
||||
$content = $this->wikiContentModel->where('locale', $this->locale)->where('page_id', $wikiPageId)->countAllResults();
|
||||
if ($content > 0) {
|
||||
$this->wikiContentModel->where('locale', $this->locale)->where('page_id', $wikiPageId)->delete();
|
||||
}
|
||||
|
||||
$this->wikiContentModel->insert([
|
||||
"locale" => $this->locale,
|
||||
"page_id" => $wikiPageId,
|
||||
"editor_data" => json_encode($bodyData)
|
||||
]);
|
||||
return $this->response->setJSON(["data" => [], "message" => lang("App.global_alert_save_success")]);
|
||||
}
|
||||
public function wiki_file_upload(int $section_id)
|
||||
{
|
||||
try {
|
||||
$file = $this->request->getFile('image');
|
||||
$section = $this->wikiSectionModel->find($section_id);
|
||||
$content = $section->content();
|
||||
$r = null;
|
||||
$fullpath = null;
|
||||
if ($file->isValid() && !$file->hasMoved()) {
|
||||
$fullpath = $file->store('wiki_images/' . $section->slug);
|
||||
$r = $this->wikiFileModel->insert(["content_id" => $content->id, "path" => $fullpath]);
|
||||
return $this->response->setJSON(["success" => 1, "file" => [
|
||||
"url" => '/wiki/file/'.$r
|
||||
]]);
|
||||
}else{
|
||||
return $this->response->setJSON(["success" => 0, "file" => [
|
||||
"url" => null
|
||||
]]);
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
|
||||
return $this->response->setJSON(["success" => 0, "error" => $th->getMessage()])->setStatusCode($th->getCode());
|
||||
}
|
||||
}
|
||||
public function get_wiki_file(int $wiki_file_id)
|
||||
{
|
||||
$wikiFile = $this->wikiFileModel->find($wiki_file_id);
|
||||
if ($wikiFile->path) {
|
||||
$filePath = WRITEPATH . 'uploads/' . $wikiFile->path;
|
||||
$mimeType = mime_content_type($filePath);
|
||||
return $this->response
|
||||
->setHeader('Content-Type', $mimeType)
|
||||
->setHeader('Content-Length', filesize($filePath))
|
||||
->setBody(file_get_contents($filePath));
|
||||
} else {
|
||||
return $this->response->setJSON(["message" => "Portada error", "error" => "No hay portada"])->setStatusCode(400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user