MOON
Server: Apache
System: Linux server1.studioinfinity.com.br 2.6.32-954.3.5.lve1.4.90.el6.x86_64 #1 SMP Tue Feb 21 12:26:30 UTC 2023 x86_64
User: artinside (517)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //home/artinside/sites.artinside.com.br/mainpro/source/App/Admin/Document.php
<?php

namespace Source\App\Admin;


use Source\Models\Documents;
use Source\Models\Ecommerce\Coupon;

use Source\Models\User;
use Source\Support\Pager;

/**
 * Class Document
 * @package Source\App\Admin
 */
class Document extends Admin
{
    /**
     * Dash constructor.
     */
    public function __construct($router)
    {
        parent::__construct();
        $this->view->addData("router", $router);
    }


    /**
     * @param array|null $data
     * @throws \Exception
     */
    /**
     * @param array|null $data
     */
    public function home(?array $data): void
    {
        //search redirect
        if (!empty($data["s"])) {
            $s = str_search($data["s"]);
            echo json_encode(["redirect" => url("/admin/documents/home/{$s}/1")]);
            return;
        }

        $search = null;
        $documents = (new Documents())->find();

//
//        if (!empty($data["search"]) && str_search($data["search"]) != "all") {
//            $search = str_search($data["search"]);
//            $documents = (new Documents())->find("MATCH(title) AGAINST(:s)", "s={$search}");
//            if (!$coupons->count()) {
//                $this->message->info("Sua pesquisa não retornou resultados")->flash();
//                redirect("/admin/coupon/home");
//            }
//        }

        $all = ($search ?? "all");
        $pager = new Pager(url("/admin/documents/home/{$all}/"));
        $pager->pager($documents->count(), 50, (!empty($data["page"]) ? $data["page"] : 1));

        $head = $this->seo->render(
            CONF_SITE_NAME . " | Documentos",
            CONF_SITE_DESC,
            url("/admin"),
            url("/admin/assets/images/image.jpg"),
            false
        );

        echo $this->view->render("widgets/documents/home", [
            "app" => "coupon",
            "head" => $head,
            "documents" => $documents->limit($pager->limit())->offset($pager->offset())->order("created_at DESC")->fetch(true),
            "paginator" => $pager->render(),
            "search" => $search
        ]);
    }


    /**
     * @param array|null $data
     * @throws \Exception
     */
    public function document(?array $data): void
    {


        //create
        if (!empty($data["action"]) && $data["action"] == "create") {


            $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
            $create = (new Documents())->find();

            $create->title = $data["title"];
            $create->description = $data["description"];


            if (!$create->save()) {
                $json["message"] = $create->message()->render();
                echo json_encode($json);
                return;
            }

            $this->message->success("Documento criado com sucesso...")->flash();
            $json["redirect"] = url("/admin/documents/home");

            echo json_encode($json);
            return;
        }

        //update
        if (!empty($data["action"]) && $data["action"] == "update") {
            $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
            $edit = (new Documents())->findById($data["id"]);

            if (!$edit) {
                $this->message->error("Você tentou editar um documento que não existe ou foi removido")->flash();
                echo json_encode(["redirect" => url("/admin/documents/home")]);
                return;
            }

            $edit->title = $data["title"];
            $edit->description = $data["description"];
            

            if (!$edit->save()) {
                $json["message"] = $edit->message()->render();
                echo json_encode($json);
                return;
            }

            $this->message->success("Documento atualizado com sucesso...")->flash();
            echo json_encode(["redirect" => url("/admin/documents/home")]);
            return;
        }


        //delete
        if (!empty($data["action"]) && $data["action"] == "delete") {
            $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
            $delete = (new Documents())->findById($data["id"]);

            if (!$delete) {
                $json["message"] = $this->message->error("O Documento não existe ou já foi excluído antes")->render();
                echo json_encode($json);
                return;
            }


            $delete->destroy();
            $this->message->success("O Documento foi excluído com sucesso...")->flash();
            echo json_encode(["reload" => true]);

            return;
        }

        $edit = null;
        if (!empty($data["id"])) {
            $itemId = filter_var($data["id"], FILTER_VALIDATE_INT);
            $edit = (new Documents())->findById($itemId);
        }

        $head = $this->seo->render(
            CONF_SITE_NAME . " | Documentos",
            CONF_SITE_DESC,
            url("/admin"),
            url("/admin/assets/images/image.jpg"),
            false
        );

        echo $this->view->render("widgets/documents/document", [
            "app" => "documents",
            "head" => $head,
            "documents" => $edit
        ]);
    }


}