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/sourcebackup/App/Admin/Batches.php
<?php

namespace Source\App\Admin;
use Source\Models\Batch;
use Source\Support\Pager;

/**
 * Class Products
 * @package Source\App\Admin
 */
class Batches extends Admin
{

    /**
     * product constructor.
     */
    public function __construct($router)
    {
        parent::__construct();
        $this->view->addData("router", $router);
    }

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

        $search = null;
        $batches = (new Batch())->find();

        if (!empty($data["search"]) && str_search($data["search"]) != "all") {
            $search = str_search($data["search"]);
            $batches = (new Batch())->find("title LIKE '%{$search}%' OR batch_number LIKE '%{$search}%'");
            if (!$batches->count()) {
                $this->message->info("Sua pesquisa não retornou resultados")->flash();
                redirect("/admin/batch/home");
            }
        }

        $all = ($search ?? "all");
        $pager = new Pager(url("/admin/batch/home/{$all}/"));
        $pager->pager($batches->count(), 20, (!empty($data["page"]) ? $data["page"] : 1));
        
        
        $head = $this->seo->render(
            CONF_SITE_NAME . " | Lote de Produtos",
            CONF_SITE_DESC,
            url("/admin"),
            url("/admin/assets/images/image.jpg"),
            false
        );

        echo $this->view->render("widgets/batch/home", [
            "app" => "batch/home",
            "head" => $head,
            "batches" => $batches->order("batch_number")->limit($pager->limit())->offset($pager->offset())->fetch(true),
            "paginator" => $pager->render()
        ]);
    }

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


        //create
        if (!empty($data["action"]) && $data["action"] == "create") {
            $data = filter_var_array($data, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
            $create = new Batch();
            $create->title = $data["title"];
            $create->manufacturing = $data["manufacturing"];
            $create->valid = $data["valid"];
            $create->batch_number = $data["batch_number"];
            $create->quantity = $data["quantity"];


            if (!$create->save()) {
                $json["message"] = $create->message()->render();
                echo json_encode($json);
                return;
            }
            $this->message->success("Lote criado com sucesso...")->flash();
            $json["redirect"] = url("/admin/batch/home");
            echo json_encode($json);
            return;
        }
        //update
        if (!empty($data["action"]) && $data["action"] == "update") {
            $data = filter_var_array($data, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

            $edit = (new Batch())->findById($data["data_id"]);
            if (!$edit) {
                $this->message->error("Você tentou editar um Lote que não existe ou foi removido")->flash();
                echo json_encode(["redirect" => url("/admin/batch/home")]);
                return;
            }
            $edit->title = $data["title"];
            $edit->manufacturing = $data["manufacturing"];
            $edit->valid = $data["valid"];
            $edit->batch_number = $data["batch_number"];
            $edit->quantity = $data["quantity"];

            if (!$edit->save()) {
                $json["message"] = $edit->message()->render();
                echo json_encode($json);
                return;
            }
            $this->message->success("Lote atualizado com sucesso...")->flash();
            echo json_encode(["redirect" => url("/admin/batch/home")]);
            return;
        }

        //delete
        if (!empty($data["action"]) && $data["action"] == "delete") {
            $data = filter_var_array($data, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
            $delete = (new Batch())->findById($data["data_id"]);

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

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

            return;
        }

        $edit = null;
        if (!empty($data["data_id"])) {
            $typeId = filter_var($data["data_id"], FILTER_VALIDATE_INT);
            $edit = (new Batch())->findById($typeId);
        }

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

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


}