mirror of
https://koodu.h-i.works/projects/thebadspace
synced 2025-06-25 16:04:37 -05:00
Location Editing Part. 1
Now that full-text searching is set up in the DB, the next step is data population. The adding and editing templates were added as long as routes and base functionality to add single locations. Adding works and editing is almost there but both still need to cleaned up. The basic plumbing will be completed and then the tweaking to account for roles and login status for the sake of security. Part 2 will include clean up and and bulk uploads through the use of CSV files.
This commit is contained in:
142
src/Service/HandleLocations.php
Normal file
142
src/Service/HandleLocations.php
Normal file
@ -0,0 +1,142 @@
|
||||
<?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];
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user