jonnybarnes.uk/app/Place.php

161 lines
3.7 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;
2017-05-28 22:45:14 +01:00
use Illuminate\Database\Eloquent\Builder;
2017-06-13 19:17:55 +01:00
use Cviebrock\EloquentSluggable\Sluggable;
2017-06-17 12:56:10 +00:00
use Phaza\LaravelPostgis\Geometries\Point;
2016-05-19 15:01:28 +01:00
use Phaza\LaravelPostgis\Eloquent\PostgisTrait;
class Place extends Model
{
use Sluggable;
2017-06-13 19:17:55 +01:00
use PostgisTrait;
2016-05-19 15:01:28 +01:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'slug'];
/**
* The attributes that are Postgis geometry objects.
*
* @var array
*/
2016-06-27 10:43:47 +01:00
protected $postgisFields = [
'location',
'polygon',
2016-06-27 10:43:47 +01:00
];
2016-05-19 15:01:28 +01:00
2017-06-13 19:17:55 +01:00
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable()
{
return [
'slug' => [
2017-06-17 12:56:10 +00:00
'source' => 'name',
2017-06-17 14:12:48 +01:00
'onUpdate' => true,
2017-06-17 12:56:10 +00:00
],
2017-06-13 19:17:55 +01:00
];
}
2016-05-19 15:01:28 +01:00
/**
* Define the relationship with Notes.
*
* @var array
*/
public function notes()
{
return $this->hasMany('App\Note');
}
/**
2017-05-28 22:45:14 +01:00
* Select places near a given location.
2016-05-19 15:01:28 +01:00
*
2017-05-28 22:45:14 +01:00
* @param \Illuminate\Database\Eloquent\Builder $query
* @param Point $point
* @param int Distance
* @return \Illuminate\Database\Eloquent\Builder
2016-05-19 15:01:28 +01:00
*/
2017-05-30 20:07:40 +01:00
public function scopeNear(Builder $query, Point $point, $distance = 1000)
2016-05-19 15:01:28 +01:00
{
2017-05-28 22:45:14 +01:00
$field = DB::raw(
2017-09-14 09:49:18 +01:00
sprintf(
"ST_Distance(%s.location, ST_GeogFromText('%s'))",
2017-05-28 22:45:14 +01:00
$this->getTable(),
$point->toWKT()
)
);
2016-05-19 15:01:28 +01:00
2017-05-28 22:45:14 +01:00
return $query->where($field, '<=', $distance)->orderBy($field);
2016-05-19 15:01:28 +01:00
}
public function scopeWhereExternalURL(Builder $query, string $url)
{
$type = $this->getType($url);
if ($type === null) {
// we havent set a type, therefore result must be empty set
// id cant be null, so this will return empty set
return $query->whereNull('id');
}
return $query->where('external_urls', '@>', json_encode([
$type => $url,
]));
}
2016-05-19 15:01:28 +01:00
/**
* Get the latitude from the `location` property.
*
* @return string latitude
*/
public function getLatitudeAttribute()
{
2016-06-27 10:43:47 +01:00
return explode(' ', $this->location)[1];
2016-05-19 15:01:28 +01:00
}
/**
* Get the longitude from the `location` property.
*
* @return string longitude
*/
public function getLongitudeAttribute()
{
2016-06-27 10:43:47 +01:00
return explode(' ', $this->location)[0];
2016-05-19 15:01:28 +01:00
}
/**
* The Long URL for a place.
*
* @return string
*/
public function getLongurlAttribute()
{
return config('app.url') . '/places/' . $this->slug;
}
/**
* The Short URL for a place.
*
* @return string
*/
public function getShorturlAttribute()
{
return config('app.shorturl') . '/places/' . $this->slug;
}
2017-06-30 14:01:26 +01:00
public function setExternalUrlsAttribute($url)
{
2017-06-30 14:01:26 +01:00
$type = $this->getType($url);
if ($type === null) {
2017-06-30 14:01:26 +01:00
throw new \Exception('Unkown external url type ' . $url);
}
$already = [];
if (array_key_exists('external_urls', $this->attributes)) {
$already = json_decode($this->attributes['external_urls'], true);
}
2017-06-30 14:01:26 +01:00
$already[$type] = $url;
$this->attributes['external_urls'] = json_encode($already);
}
private function getType(string $url): ?string
{
$host = parse_url($url, PHP_URL_HOST);
if (ends_with($host, 'foursquare.com') === true) {
return 'foursquare';
}
if (ends_with($host, 'openstreetmap.org') === true) {
return 'osm';
}
return null;
}
2016-05-19 15:01:28 +01:00
}