File: //home/artinside/sites.artinside.com.br/mainpro/sourcebackup/App/Admin/Brands.php
<?php
namespace Source\App\Admin;
use Source\Models\Brand;
use Source\Models\Category;
use Source\Models\Gallery;
use Source\Models\Post;
use Source\Models\User;
use Source\Support\Pager;
use Source\Support\Thumb;
use Source\Support\Upload;
/**
* Class Brand
* @package Source\App\Admin
*/
class Brands extends Admin
{
/**
* Blog constructor.
*/
public function __construct($router)
{
parent::__construct();
$this->view->addData("router", $router);
}
/**
* @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/brands/home/{$s}/1")]);
return;
}
$search = null;
$brand = (new Brand())->find();
if (!empty($data["search"]) && str_search($data["search"]) != "all") {
$search = str_search($data["search"]);
$brand = (new Brand())->find("MATCH(title) AGAINST(:s)", "s={$search}");
if (!$brand->count()) {
$this->message->info("Sua pesquisa não retornou resultados")->flash();
redirect("/admin/brands/home");
}
}
$all = ($search ?? "all");
$pager = new Pager(url("/admin/brands/home/{$all}/"));
$pager->pager($brand->count(), 12, (!empty($data["page"]) ? $data["page"] : 1));
$head = $this->seo->render(
CONF_SITE_NAME . " | Parceiros",
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/brands/home", [
"app" => "brands/brand",
"head" => $head,
"brands" => $brand->limit($pager->limit())->offset($pager->offset())->order("id DESC")->fetch(true),
"paginator" => $pager->render(),
"search" => $search
]);
}
/**
* @param array|null $data
* @throws \Exception
*/
public function brand(?array $data): void
{
//create
if (!empty($data["action"]) && $data["action"] == "create") {
$data = filter_var_array($data, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$create = new Brand();
$create->title = $data["title"];
//upload cover
if (!empty($_FILES["cover"])) {
$files = $_FILES["cover"];
$upload = new Upload();
$image = $upload->image($files, $create->title);
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$create->cover = $image;
}
if (!$create->save()) {
$json["message"] = $create->message()->render();
echo json_encode($json);
return;
}
$this->message->success("parceiro criado com sucesso...")->flash();
$json["redirect"] = url("/admin/brands/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 Brand())->findById($data["data_id"]);
if (!$edit) {
$this->message->error("Você tentou editar um parceiro que não existe ou foi removido")->flash();
echo json_encode(["redirect" => url("/admin/brands/home")]);
return;
}
$edit->title = $data["title"];
//upload cover
if (!empty($_FILES["cover"])) {
if ($edit->cover && file_exists(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$edit->cover}")) {
unlink(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$edit->cover}");
(new Thumb())->flush($edit->cover);
}
$files = $_FILES["cover"];
$upload = new Upload();
$image = $upload->image($files, $edit->title);
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$edit->cover = $image;
}
if (!$edit->save()) {
$json["message"] = $edit->message()->render();
echo json_encode($json);
return;
}
$this->message->success("parceiro atualizado com sucesso...")->flash();
echo json_encode(["redirect" => url("/admin/brands/home")]);
return;
}
//delete
if (!empty($data["action"]) && $data["action"] == "delete") {
$data = filter_var_array($data, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$delete = (new Brand())->findById($data["data_id"]);
if (!$delete) {
$json["message"] = $this->message->error("O parceiro não existe ou já foi excluído antes")->render();
echo json_encode($json);
return;
}
if ($delete->cover && file_exists(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$delete->cover}")) {
unlink(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$delete->cover}");
(new Thumb())->flush($delete->cover);
}
$delete->destroy();
$this->message->success("O parceiro foi excluído com sucesso...")->flash();
echo json_encode(["reload" => true]);
return;
}
$edit = null;
if (!empty($data["data_id"])) {
$brandId = filter_var($data["data_id"], FILTER_VALIDATE_INT);
$edit = (new Brand())->findById($brandId);
}
$head = $this->seo->render(
CONF_SITE_NAME . " | Parceiros",
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/brands/brand", [
"app" => "brands/brand",
"head" => $head,
"brand" => $edit
]);
}
}