File: /home/artinside/www/raquel/source/App/Admin/Services.php
<?php
namespace Source\App\Admin;
use Source\Models\Category;
use Source\Models\Service;
use Source\Support\Pager;
use Source\Support\Thumb;
use Source\Support\Upload;
/**
* Class Services
* @package Source\App\Admin
*/
class Services extends Admin
{
/**
* Services 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/services/home/{$s}/1")]);
return;
}
$search = null;
$services = (new Service())->find("category = 2");
if (!empty($data["search"]) && str_search($data["search"]) != "all") {
$search = str_search($data["search"]);
$services = (new Service())->find("(title LIKE '%{$search}%') AND category = 2");
if (!$services->count()) {
$this->message->info("Sua pesquisa não retornou resultados")->flash();
redirect("/admin/services/home");
}
}
$all = ($search ?? "all");
$pager = new Pager(url("/admin/services/home/{$all}/"));
$pager->pager($services->count(), 12, (!empty($data["page"]) ? $data["page"] : 1));
$head = $this->seo->render(
CONF_SITE_NAME . " | Serviços",
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/services/home", [
"app" => "services",
"head" => $head,
"services" => $services->limit($pager->limit())->offset($pager->offset())->order("post_at DESC")->fetch(true),
"paginator" => $pager->render(),
"search" => $search
]);
}
/**
* @param array|null $data
*/
public function homeAulas(?array $data): void
{
//search redirect
if (!empty($data["s"])) {
$s = str_search($data["s"]);
echo json_encode(["redirect" => url("/admin/services/home-aulas/{$s}/1")]);
return;
}
$search = null;
$services = (new Service())->find("category = 1");
if (!empty($data["search"]) && str_search($data["search"]) != "all") {
$search = str_search($data["search"]);
$services = (new Service())->find("(title LIKE '%{$search}%') AND category = 1");
if (!$services->count()) {
$this->message->info("Sua pesquisa não retornou resultados")->flash();
redirect("/admin/services/home-aulas");
}
}
$all = ($search ?? "all");
$pager = new Pager(url("/admin/services/home-aulas/{$all}/"));
$pager->pager($services->count(), 12, (!empty($data["page"]) ? $data["page"] : 1));
$head = $this->seo->render(
CONF_SITE_NAME . " | Serviços",
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/services/home-aulas", [
"app" => "lessons",
"head" => $head,
"services" => $services->limit($pager->limit())->offset($pager->offset())->order("post_at DESC")->fetch(true),
"paginator" => $pager->render(),
"search" => $search
]);
}
/**
* @param array|null $data
* @throws \Exception
*/
public function service(?array $data): void
{
//MCE Upload
if (!empty($data["upload"]) && !empty($_FILES["image"])) {
$files = $_FILES["image"];
$upload = new Upload();
$image = $upload->image($files, "services-" . time());
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$json["mce_image"] = '<img style="width: 100%;" src="' . url("/storage/{$image}") . '" alt="{title}" title="{title}">';
echo json_encode($json);
return;
}
//create
if (!empty($data["action"]) && $data["action"] == "create") {
$content = $data["content"];
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$serviceCreate = new Service();
$serviceCreate->title = $data["title"];
$serviceCreate->uri = str_slug($serviceCreate->title);
$serviceCreate->subtitle = $data["subtitle"];
$serviceCreate->content = str_replace(["{title}"], [$serviceCreate->title], $content);
$serviceCreate->post_at = date_fmt_back($data["post_at"]);
$serviceCreate->category = $data["category"];
//upload cover
if (!empty($_FILES["cover"])) {
$files = $_FILES["cover"];
$upload = new Upload();
$image = $upload->image($files, $serviceCreate->title);
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$serviceCreate->cover = $image;
}
if (!$serviceCreate->save()) {
$json["message"] = $serviceCreate->message()->render();
echo json_encode($json);
return;
}
$this->message->success("Serviço publicado com sucesso...")->flash();
$json["redirect"] = url("/admin/services/service/{$serviceCreate->id}");
echo json_encode($json);
return;
}
//update
if (!empty($data["action"]) && $data["action"] == "update") {
$content = $data["content"];
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$serviceEdit = (new Service())->findById($data["service_id"]);
if (!$serviceEdit) {
$this->message->error("Você tentou atualizar um Serviço que não existe ou foi removido")->flash();
echo json_encode(["redirect" => url("/admin/services/home")]);
return;
}
$serviceEdit->title = $data["title"];
$serviceEdit->uri = str_slug($serviceEdit->title);
$serviceEdit->subtitle = $data["subtitle"];
$serviceEdit->content = str_replace(["{title}"], [$serviceEdit->title], $content);
$serviceEdit->post_at = date_fmt_back($data["post_at"]);
$serviceEdit->category = $data["category"];
//upload cover
if (!empty($_FILES["cover"])) {
if ($serviceEdit->cover && file_exists(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$serviceEdit->cover}")) {
unlink(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$serviceEdit->cover}");
(new Thumb())->flush($serviceEdit->cover);
}
$files = $_FILES["cover"];
$upload = new Upload();
$image = $upload->image($files, $serviceEdit->title);
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$serviceEdit->cover = $image;
}
if (!$serviceEdit->save()) {
$json["message"] = $serviceEdit->message()->render();
echo json_encode($json);
return;
}
$this->message->success("Serviço atualizado com sucesso...")->flash();
echo json_encode(["redirect" => url("/admin/services/home")]);
return;
}
//delete
if (!empty($data["action"]) && $data["action"] == "delete") {
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$serviceDelete = (new Service())->findById($data["service_id"]);
if (!$serviceDelete) {
$this->message->error("Você tentou excluir um Serviço que não existe ou já foi removido")->flash();
echo json_encode(["reload" => true]);
return;
}
$serviceDelete->destroy();
$this->message->success("O Serviço foi excluído com sucesso...")->flash();
echo json_encode(["reload" => true]);
return;
}
$serviceEdit = null;
if (!empty($data["service_id"])) {
$serviceId = filter_var($data["service_id"], FILTER_VALIDATE_INT);
$serviceEdit = (new Service())->findById($serviceId);
}
$head = $this->seo->render(
CONF_SITE_NAME . " | " . ($serviceEdit->title ?? "Novo Artigo"),
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/services/service", [
"app" => "services",
"categories" => (new Category())->find("type = :type", "type=services")->order("title")->fetch(true),
"head" => $head,
"services" => $serviceEdit,
]);
}
/**
* @param array|null $data
* @throws \Exception
*/
public function lessons(?array $data): void
{
//MCE Upload
if (!empty($data["upload"]) && !empty($_FILES["image"])) {
$files = $_FILES["image"];
$upload = new Upload();
$image = $upload->image($files, "services-" . time());
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$json["mce_image"] = '<img style="width: 100%;" src="' . url("/storage/{$image}") . '" alt="{title}" title="{title}">';
echo json_encode($json);
return;
}
//create
if (!empty($data["action"]) && $data["action"] == "create") {
$content = $data["content"];
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$serviceCreate = new Service();
$serviceCreate->title = $data["title"];
$serviceCreate->uri = str_slug($serviceCreate->title);
$serviceCreate->subtitle = $data["subtitle"];
$serviceCreate->content = $data["title"];
$serviceCreate->post_at = date_fmt_back($data["post_at"]);
$serviceCreate->category = 1;
//upload cover
if (!empty($_FILES["cover"])) {
$files = $_FILES["cover"];
$upload = new Upload();
$image = $upload->image($files, $serviceCreate->title);
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$serviceCreate->cover = $image;
}
if (!$serviceCreate->save()) {
$json["message"] = $serviceCreate->message()->render();
echo json_encode($json);
return;
}
$this->message->success("Serviço publicado com sucesso...")->flash();
$json["redirect"] = url("/admin/services/lessons/{$serviceCreate->id}");
echo json_encode($json);
return;
}
//update
if (!empty($data["action"]) && $data["action"] == "update") {
$content = $data["content"];
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$serviceEdit = (new Service())->findById($data["service_id"]);
if (!$serviceEdit) {
$this->message->error("Você tentou atualizar uma aula que não existe ou foi removida")->flash();
echo json_encode(["redirect" => url("/admin/services/home-aulas")]);
return;
}
$serviceEdit->title = $data["title"];
$serviceEdit->uri = str_slug($serviceEdit->title);
$serviceEdit->subtitle = $data["subtitle"];
$serviceEdit->content = $data["title"];
$serviceEdit->post_at = date_fmt_back($data["post_at"]);
$serviceEdit->category = 1;
//upload cover
if (!empty($_FILES["cover"])) {
if ($serviceEdit->cover && file_exists(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$serviceEdit->cover}")) {
unlink(__DIR__ . "/../../../" . CONF_UPLOAD_DIR . "/{$serviceEdit->cover}");
(new Thumb())->flush($serviceEdit->cover);
}
$files = $_FILES["cover"];
$upload = new Upload();
$image = $upload->image($files, $serviceEdit->title);
if (!$image) {
$json["message"] = $upload->message()->render();
echo json_encode($json);
return;
}
$serviceEdit->cover = $image;
}
if (!$serviceEdit->save()) {
$json["message"] = $serviceEdit->message()->render();
echo json_encode($json);
return;
}
$this->message->success("Serviço atualizado com sucesso...")->flash();
echo json_encode(["redirect" => url("/admin/services/home-aulas")]);
return;
}
//delete
if (!empty($data["action"]) && $data["action"] == "delete") {
$data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
$serviceDelete = (new Service())->findById($data["service_id"]);
if (!$serviceDelete) {
$this->message->error("Você tentou excluir um Serviço que não existe ou já foi removido")->flash();
echo json_encode(["reload" => true]);
return;
}
$serviceDelete->destroy();
$this->message->success("O Serviço foi excluído com sucesso...")->flash();
echo json_encode(["reload" => true]);
return;
}
$serviceEdit = null;
if (!empty($data["service_id"])) {
$serviceId = filter_var($data["service_id"], FILTER_VALIDATE_INT);
$serviceEdit = (new Service())->findById($serviceId);
}
$head = $this->seo->render(
CONF_SITE_NAME . " | " . ($serviceEdit->title ?? "Novo Artigo"),
CONF_SITE_DESC,
url("/admin"),
url("/admin/assets/images/image.jpg"),
false
);
echo $this->view->render("widgets/services/lessons", [
"app" => "lessons",
"head" => $head,
"services" => $serviceEdit,
]);
}
}