ForRo/app/Repositories/LocationRepository.php
ro da0ddb3ef0 Plugged in repository class for location data
Seperated data logic for locations and put it into its own repository
class for better organization and readability of controller classes.

Data methods are still simple so no need for an interface class just
yet, but will probably implement at a later date
2024-02-09 14:53:08 -06:00

103 lines
2.6 KiB
PHP

<?php
namespace App\Repositories;
use App\Models\Location;
use Illuminate\Support\Facades\DB;
class LocationRepository
{
protected $model;
private $limit = 15;
public function __construct(Location $model)
{
$this->model = $model;
}
public function search($request)
{
// this grabs the search results from the db
$terms = $request->index_search;
$rawSearch = $terms;
$terms = str_replace(",", "", $terms);
$terms = str_replace(" ", "|", $terms);
$raw = DB::select("SELECT * FROM searchlocations(?)", [$terms]);
$results = [];
foreach ($raw as $item) {
if (($item->block_count + $item->silence_count) >= 2) {
array_push($results, $item);
}
}
return $results;
}
public function getLocation($uuid)
{
return $this->model::where("uuid", $uuid)->first();
}
public function getRecent()
{
$locations = $this->model::where("active", true)->orderByDesc('updated_at')->get();
$list = [];
foreach ($locations as $location) {
if (($location->block_count + $location->silence_count) >= 2) {
array_push($list, $location);
}
}
return $list;
}
public function getPage($pageNum)
{
$locations = $this->model::where("active", true)->get();
$active = [];
foreach ($locations as $location) {
if (($location->block_count + $location->silence_count) >= 2) {
array_push($active, $location);
}
}
$pages = [];
if (count($active) != 0) {
if (count($active) < $this->limit) {
$this->limit = count($active) - 1;
}
$range = $pageNum * $this->limit - $this->limit;
if ($range != 0) {
$range = $range + 1;
}
for ($i = 0; $i <= $this->limit; ++$i) {
if (isset($active[$i + $range])) {
array_push($pages, $active[$i + $range]);
} else {
// chill out
}
}
}
$pageCount = ceil(count($active) / $this->limit);
if ($range != 0) {
$range = $range + 1;
}
$next = $pageNum + 1;
if ($next > $pageCount) {
$next = 1;
}
$prev = $pageNum - 1;
if ($prev <= 0) {
$prev = $pageCount;
}
return $result = [$pages, $pageCount, $prev, $next];
}
}