1
0
mirror of https://koodu.h-i.works/projects/thebadspace synced 2025-05-06 14:41:02 -05:00
thebadspace/src/Service/HandleLocations.php

143 lines
4.1 KiB
PHP
Raw Normal View History

<?php
// src/Controller/ProductController.php
namespace App\Service;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\ORMException;
use PDOException;
use Exception;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Uid\Uuid;
use App\Entity\Location;
//use App\Utils\StringTools;
/**
* Members
*
* Data class for interacting with Member data from the DB
*/
class HandleLocations
{
private $session;
private $entityManager;
private $limit = 4;
public function __construct(
EntityManagerInterface $entityManager,
RequestStack $requestStack
) {
$this->entityManager = $entityManager;
$this->session = $requestStack->getSession();
}
public function getActiveLocations()
{
$listings = $this->entityManager->getRepository(Location::class);
$locations = $listings->findBy(["active" => true]);
return $locations;
}
public function getLocationByUUID(string $uuid)
{
return $this->entityManager->getRepository(Location::class)->findBy(["uuid" => $uuid]);
}
public function getLocationsPage(int $page)
{
$locations = $this->entityManager->getRepository(Location::class);
$list = $locations->findBy(
[],
["id" => "ASC"]
);
$count = ceil(count($list) / $this->limit);
$totalCount = count($list);
$shelf = [];
$range = $page * $this->limit - $this->limit;
for ($i = 0; $i <= $this->limit; $i++) {
try {
array_push($shelf, $list[$i + $range]);
} catch (Exception $error) {
}
}
return [
"locations" => $shelf,
"total" => $count,
"totalLocations" => $totalCount,
];
}
/**
* Add new location to db
*
* @param Request $request object containing posted data
* @return JSON
*/
public function addLocation($request, $memberId)
{
$errorMessage = null;
$location = new Location();
//submitted values
$name = $request->request->get("loc_name");
$location->setName($name);
$url = $request->request->get("loc_url");
$location->setUrl($url);
$desc = $request->request->get("loc_desc");
$location->setDescription($desc);
$tags = $request->request->get("loc_tags");
$location->setTags($tags);
$rating = $request->request->get("rating");
$location->setRating($rating);
//get images
$files = $request->files->get("loc_examples");
if (!empty($files)) {
$examples = [];
for ($i = 0; $i < count($files); $i++) {
$path = $files[$i]->getClientOriginalName();
array_push($examples, ["image_index" => $i, "path" => urlencode($path)]);
}
$location->setImages($examples);
}
//set defaults
$location->setUuid(Uuid::v4());
$location->setActive(false);
$location->setCreatedAt(new \DateTimeImmutable());
$location->setUpdatedAt(new \DateTimeImmutable());
$location->setAddedBy($memberId);
$this->entityManager->persist($location);
try {
$this->entityManager->flush();
} catch (PDOException $error) {
$errorMessage = $error->getMessage();
} catch (DBALException $error) {
$errorMessage = $error->getMessage();
} catch (ORMException $error) {
$errorMessage = $error->getMessage();
} catch (Exception $error) {
$errorMessage = $error->getMessage();
} catch (SyntaxErrorException $e) {
$errorMessage = $error->getMessage();
}
// return result status
if ($errorMessage == null) {
return $response = [
"status" => true,
"message" => "New member added. Woohoo!",
];
} else {
return $response = ["status" => false, "message" => $errorMessage];
}
}
}