File: /home/artinside/www/raquel/source/App/Admin/Testimonials.php
<?php
namespace Source\App\Admin;
use Source\Models\Testimonial;
use Source\Support\Pager;
use Source\Support\Thumb;
use Source\Support\Upload;
/**
* Class Testimonials
* @package Source\App\Admin
*/
class Testimonials extends Admin
{
/**
* Testimonials constructor.
*/
public function __construct($router)
{
parent::__construct();
$this->view->addData("router", $router);
}
/**
* @param array|null $data
*/
public function home(?array $data): void
{
$testimonials = (new Testimonial())->find();
$pager = new Pager(url("/admin/testimonials/home/"));
$pager->pager($testimonials->count(), 20, (!empty($data["page"]) ? $data["page"] : 1));
$head = $this->seo->render(
CONF_SITE_NAME . " | Depoimentos",
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/testimonials/home", [
"app" => "testimonials/home",
"head" => $head,
"testimonials" => $testimonials->order("id DESC")->limit($pager->limit())->offset($pager->offset())->fetch(true),
"paginator" => $pager->render()
]);
}
/**
* @param array|null $data
* @throws \Exception
*/
public function testimonial(?array $data): void
{
//create
if (!empty($data["action"]) && $data["action"] == "create") {
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$create = new Testimonial();
$create->author = $data["author"];
$create->content = $data["content"];
$create->about = $data["about"];
//upload cover
if (!empty($_FILES["cover"])) {
$files = $_FILES["cover"];
$upload = new Upload();
$image = $upload->image($files, $create->author.gallery_id(5));
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("Depoimento criado com sucesso...")->flash();
$json["redirect"] = url("/admin/testimonials/home");
echo json_encode($json);
return;
}
//update
if (!empty($data["action"]) && $data["action"] == "update") {
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$edit = (new Testimonial())->findById($data["id"]);
if (!$edit) {
$this->message->error("Você tentou editar um depoimento que não existe ou foi removido")->flash();
echo json_encode(["redirect" => url("/admin/testimonials/home")]);
return;
}
$edit->author = $data["author"];
$edit->content = $data["content"];
$edit->about = $data["about"];
//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->author.gallery_id(5));
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("Depoimento atualizado com sucesso...")->flash();
echo json_encode(["redirect" => url("/admin/testimonials/home")]);
return;
}
//delete
if (!empty($data["action"]) && $data["action"] == "delete") {
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$delete = (new Testimonial())->findById($data["id"]);
if (!$delete) {
$json["message"] = $this->message->error("O Depoimento não existe ou já foi excluído")->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 Depoimento foi excluído com sucesso...")->flash();
echo json_encode(["reload" => true]);
return;
}
$edit = null;
if (!empty($data["id"])) {
$testId = filter_var($data["id"], FILTER_VALIDATE_INT);
$edit = (new Testimonial())->findById($testId);
}
$head = $this->seo->render(
CONF_SITE_NAME . " | Depoimentos",
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/testimonials/testimonial", [
"app" => "testimonials/testimonial",
"head" => $head,
"testimonial" => $edit
]);
}
}