Files
safekat/ci4/app/Controllers/Wiki/WikiController.php
2025-02-25 18:41:59 +01:00

139 lines
5.7 KiB
PHP

<?php
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 CodeIgniter\I18n\Time;
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/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);
$this->viewData['breadcrumb'] = [
['title' => lang("Wiki.".$section->slug), 'route' => route_to('showWikiPage',$slug), 'active' => true],
];
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,
"last_edit_by" => auth()->user()->id,
"editor_data" => json_encode($bodyData)
]);
return $this->response->setJSON(["data" => [], "message" => lang("App.global_alert_save_success")]);
}
public function store_publish_page(int $section_id)
{
$bodyData = $this->request->getPost();
$wikiSectionPage = $this->wikiSectionModel->find($section_id)->page();
if ($wikiSectionPage) {
$wikiPageId = $wikiSectionPage->id;
$wikiContentId = $this->wikiContentModel->where("page_id",$wikiPageId)->where('locale',$this->locale)->first()->id;
$this->wikiContentModel->update($wikiContentId,[
"published_by" => auth()->user()->id,
"last_edit_by" => auth()->user()->id,
"editor_data" => json_encode($bodyData),
"published_data" => json_encode($bodyData),
"published_at" => Time::now()->format('Y-m-d H:i:s'),
]);
$response = $this->response->setJSON(["data" => [], "message" => lang("App.global_alert_save_success")]);
} else {
$response = $this->response->setStatusCode(400)->setJSON(["data" => [], "error" => lang('Wiki.errors.publish_before_save')]);
}
return $response;
}
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);
}
}
}