feat wiki/ayuda section

This commit is contained in:
amazuecos
2025-03-02 10:22:01 +01:00
parent 3406fb3005
commit 3140e527e8
24 changed files with 735 additions and 168 deletions

View File

@ -7,10 +7,12 @@ use App\Models\Wiki\WikiContentModel;
use App\Models\Wiki\WikiFileModel;
use App\Models\Wiki\WikiPageModel;
use App\Models\Wiki\WikiSectionModel;
use App\Models\Wiki\WikiSectionRoleModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\I18n\Time;
use Psr\Log\LoggerInterface;
use Throwable;
class WikiController extends BaseController
{
@ -18,6 +20,8 @@ class WikiController extends BaseController
protected WikiContentModel $wikiContentModel;
protected WikiPageModel $wikiPageModel;
protected WikiFileModel $wikiFileModel;
protected WikiSectionRoleModel $wikiSectionRoleModel;
protected string $locale;
protected array $viewData;
@ -28,6 +32,7 @@ class WikiController extends BaseController
$this->wikiPageModel = model(WikiPageModel::class);
$this->wikiContentModel = model(WikiContentModel::class);
$this->wikiFileModel = model(WikiFileModel::class);
$this->wikiSectionRoleModel = model(WikiSectionRoleModel::class);
$sections = $this->wikiSectionModel->sections();
$this->locale = session()->get('lang');
@ -41,18 +46,32 @@ class WikiController extends BaseController
}
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);
if($section){
if(auth()->user()->inGroup(...$section->roles_array()) || auth()->user()->inGroup('admin')){
$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);
}else{
return $this->response->setStatusCode(403);
}
}else{
return redirect_to('/');
}
}
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")]);
if(auth()->user()->inGroup(...$section->roles_array()) || auth()->user()->inGroup('admin')){
return $this->response->setJSON(["data" => $section, "message" => lang("App.global_alert_fetch_success")]);
}else{
return $this->response->setStatusCode(403);
}
}
public function store_save_page(int $section_id)
{
@ -82,8 +101,8 @@ class WikiController extends BaseController
$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,[
$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),
@ -109,16 +128,16 @@ class WikiController extends BaseController
$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
"url" => '/wiki/file/' . $r
]]);
}else{
} 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());
return $this->response->setJSON(["success" => 0, "error" => $th->getMessage()])->setStatusCode(403);
}
}
public function get_wiki_file(int $wiki_file_id)
@ -132,7 +151,69 @@ class WikiController extends BaseController
->setHeader('Content-Length', filesize($filePath))
->setBody(file_get_contents($filePath));
} else {
return $this->response->setJSON(["message" => "Portada error", "error" => "No hay portada"])->setStatusCode(400);
return $this->response->setJSON(["success" => 0, "error" => lang('Wiki.file_dont_exist')])->setStatusCode(400);
}
}
public function wiki_section_update_order()
{
try {
$bodyData = $this->request->getPost();
$orders = $bodyData['orders'];
foreach ($orders as $key => $section_id) {
$this->wikiSectionModel->update($section_id, ['order' => $key]);
}
return $this->response->setJSON(["status" => 1, "message" => lang('Wiki.order_success')]);
} catch (\Throwable $th) {
return $this->response->setJSON(["status" => 0, "message" => lang('Wiki.order_error')])->setStatusCode(400);
}
}
public function store_section()
{
try {
$bodyData = $this->request->getPost();
$sectionName = $bodyData['name'];
$roles = $bodyData['roles'];
$bodyData["slug"] = implode('-', array_map(fn($e) => strtolower($e), explode(' ', $sectionName)));
$bodyData["order"] = $this->wikiSectionModel->selectMax('order')->first()->order + 1;
$wikiSectionId = $this->wikiSectionModel->insert($bodyData);
if(count($roles) > 0){
foreach ($roles as $key => $role) {
$this->wikiSectionRoleModel->insert(['wiki_section_id' => $wikiSectionId , 'role' => $role]);
}
}
return $this->response->setJSON(["status" => 1, "message" => lang('Wiki.section_new_success')]);
} catch (\Throwable $th) {
return $this->response->setJSON(["status" => 0, "message" => $th->getMessage()])->setStatusCode(400);
}
}
public function update_section()
{
try {
$bodyData = $this->request->getPost();
$wikiSectionId = $bodyData['wiki_section_id'];
$roles = $bodyData['roles'];
$this->wikiSectionModel->update($wikiSectionId, [
"name" => $bodyData['name'],
"icon" => $bodyData['icon']
]);
$this->wikiSectionRoleModel->where('wiki_section_id',$wikiSectionId)->delete();
if(count($roles) > 0){
foreach ($roles as $key => $role) {
$this->wikiSectionRoleModel->insert(['wiki_section_id' => $wikiSectionId , 'role' => $role]);
}
}
return $this->response->setJSON(["status" => 1, "message" => lang('Wiki.section_edit_success')]);
} catch (\Throwable $th) {
return $this->response->setJSON(["status" => 0, "message" => $th->getMessage()])->setStatusCode(400);
}
}
public function delete_section(int $section_id)
{
try{
$this->wikiSectionModel->delete($section_id);
return $this->response->setJSON(["status" => 1, "message" => lang('Wiki.section_delete_success')]);
}catch(Throwable $th){
return $this->response->setJSON(["status" => 0, "message" => $th->getMessage()])->setStatusCode(400);
}
}
}