tethys/app/Models/Dataset.php

283 lines
6.9 KiB
PHP
Raw Normal View History

2018-08-06 12:30:51 +00:00
<?php
2018-09-10 13:09:10 +00:00
namespace App\Models;
2018-08-06 12:30:51 +00:00
use App\Library\Xml\DatasetExtension;
2018-09-10 13:09:10 +00:00
use App\Models\Collection;
use App\Models\License;
use App\Models\User;
2018-09-10 13:09:10 +00:00
use App\Models\Project;
2019-01-22 17:24:18 +00:00
use App\Models\Description;
2019-01-24 15:56:39 +00:00
use App\Models\Title;
2018-09-10 13:09:10 +00:00
use App\Models\Person;
use App\Models\XmlCache;
2018-11-05 15:44:25 +00:00
use App\Models\File;
2018-09-10 13:09:10 +00:00
use Illuminate\Database\Eloquent\Model;
2018-11-27 17:06:11 +00:00
use Carbon\Carbon;
2018-12-17 16:10:17 +00:00
use App\Models\GeolocationBox;
2019-03-20 17:40:14 +00:00
use App\Models\Coverage;
2018-08-06 12:30:51 +00:00
class Dataset extends Model
{
use DatasetExtension;
protected $table = 'documents';
2018-08-29 15:18:15 +00:00
2018-08-06 12:30:51 +00:00
//public $timestamps = false; //default true
// customize the names of the columns used to store the timestamps:
2018-08-29 15:18:15 +00:00
const CREATED_AT = 'created_at';
2018-08-06 12:30:51 +00:00
const UPDATED_AT = 'server_date_modified';
const PUBLISHED_AT = 'server_date_published';
2019-02-19 17:52:52 +00:00
protected $fillable = [
'type',
'language',
'server_state',
'creating_corporation',
'project_id',
'embargo_date',
'belongs_to_bibliography',
];
//protected $guarded = [];
2019-02-14 14:09:11 +00:00
/**
2018-08-06 12:30:51 +00:00
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'server_date_created',
'server_date_modified',
2018-08-29 15:18:15 +00:00
'server_date_published',
2018-11-27 17:06:11 +00:00
'embargo_date',
2018-08-06 12:30:51 +00:00
];
//protected $dateFormat = 'Y-m-d';
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
// $this->_init();
}
2019-02-14 14:09:11 +00:00
// public function setUpdatedAt($value)
// {
// $this->{static::UPDATED_AT} = $value;
// }
2018-12-17 16:10:17 +00:00
/**
* Get the geolocation that owns the dataset.
2018-12-17 16:10:17 +00:00
*/
2019-03-29 17:29:20 +00:00
// public function geolocation()
// {
// return $this->hasOne(GeolocationBox::class, 'dataset_id', 'id');
// }
2018-12-17 16:10:17 +00:00
2019-03-20 17:40:14 +00:00
/**
* Get the coverage that owns the dataset.
*/
public function coverage()
{
return $this->hasOne(Coverage::class, 'dataset_id', 'id');
}
2019-02-14 14:09:11 +00:00
2018-08-29 15:18:15 +00:00
/**
2018-09-10 13:09:10 +00:00
* Get the project that the dataset belongs to.
2018-08-29 15:18:15 +00:00
*/
2018-08-06 12:30:51 +00:00
public function project()
{
2018-09-10 13:09:10 +00:00
return $this->belongsTo(Project::class, 'project_id', 'id');
2018-08-06 12:30:51 +00:00
}
/**
* Get the account that the dataset belongs to
*/
public function user()
{
return $this->belongsTo(User::class, 'account_id', 'id');
}
2018-08-06 12:30:51 +00:00
public function collections()
{
return $this
2018-09-10 13:09:10 +00:00
->belongsToMany(Collection::class, 'link_documents_collections', 'document_id', 'collection_id');
2018-08-06 12:30:51 +00:00
}
2018-10-04 14:41:29 +00:00
// public function collectionRoles()
// {
// return $this
// ->belongsToMany(CollectionRole::class, 'link_documents_collections', 'document_id', 'role_id');
// }
2018-08-06 12:30:51 +00:00
#region [person table]
//return all persons attached to this film
public function persons()
{
2018-10-18 14:51:46 +00:00
return $this
->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
->withPivot('role', 'sort_order', 'allow_email_contact');
2018-08-06 12:30:51 +00:00
}
/**
* Return all authors for this dataset
*
* @return \App\Person
*/
public function authors()
{
return $this
2018-09-10 13:09:10 +00:00
->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
2018-08-06 12:30:51 +00:00
->wherePivot('role', 'author');
}
/**
* Add author to dataset
*
2018-09-10 13:09:10 +00:00
* @param Person $user user to add
2018-08-06 12:30:51 +00:00
*
* @return void
*/
2018-09-10 13:09:10 +00:00
public function addAuthor(Person $user): void
2018-08-06 12:30:51 +00:00
{
$this->persons()->save($user, ['role' => 'author']);
}
/**
* Return all contributors for this dataset
*
* @return \App\Person
*/
public function contributors()
{
return $this
2018-09-10 13:09:10 +00:00
->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
2018-08-06 12:30:51 +00:00
->wherePivot('role', 'contributor');
}
#endregion
#region title table:
public function titles()
{
2019-01-24 15:56:39 +00:00
return $this->hasMany(Title::class, 'document_id', 'id');
2018-08-06 12:30:51 +00:00
}
2019-01-22 17:24:18 +00:00
2018-08-29 15:18:15 +00:00
public function addMainTitle(Title $title)
{
$title->type = 'main';
2019-01-22 17:24:18 +00:00
$this->titles()->save($title);
2018-08-29 15:18:15 +00:00
// $this->titles()->save($title, ['type' => 'main']);
}
2018-08-06 12:30:51 +00:00
/**
* Relation abstracts
*
2019-01-22 17:24:18 +00:00
* @return \App\Description
2018-08-06 12:30:51 +00:00
*/
public function abstracts()
{
2019-01-22 17:24:18 +00:00
return $this->hasMany(Description::class, 'document_id', 'id');
2018-08-06 12:30:51 +00:00
}
2019-01-22 17:24:18 +00:00
2019-01-24 15:56:39 +00:00
public function addMainAbstract(Description $title)
2018-08-29 15:18:15 +00:00
{
$title->type = 'abstract';
2019-01-24 15:56:39 +00:00
$this->abstracts()->save($title);
2018-08-29 15:18:15 +00:00
// $this->abstracts()->save($title, ['type' => 'abstract']);
}
2018-08-06 12:30:51 +00:00
2018-08-29 15:18:15 +00:00
#endregion title table
2018-08-06 12:30:51 +00:00
public function licenses()
{
2018-09-10 13:09:10 +00:00
return $this->belongsToMany(License::class, 'link_documents_licences', 'document_id', 'licence_id');
2018-08-06 12:30:51 +00:00
}
public function files()
{
2018-11-05 15:44:25 +00:00
return $this->hasMany(File::class, 'document_id', 'id');
2018-08-06 12:30:51 +00:00
}
2018-10-29 13:24:41 +00:00
public function references()
{
return $this->hasMany(\App\Models\DatasetReference::class, 'document_id', 'id');
}
2019-03-18 13:32:29 +00:00
public function subjects()
{
return $this->hasMany(\App\Models\Subject::class, 'document_id', 'id');
}
2018-10-29 13:24:41 +00:00
2018-08-06 12:30:51 +00:00
/**
* Get the xml-cache record associated with the dataset.
*
2018-09-10 13:09:10 +00:00
* @return \App\Models\XmlCache
2018-08-06 12:30:51 +00:00
*/
public function xmlCache()
{
2018-09-10 13:09:10 +00:00
return $this->hasOne(XmlCache::class, 'document_id', 'id');
2018-08-06 12:30:51 +00:00
}
public function scopeOrderByType($query)
{
return $query->orderBy('type');
}
/**
* Get earliest publication date.
*
* @param \Illuminate\Database\Eloquent\Builder $query sql-query
* @param string $column column
*
* @return \Carbon\Carbon\Date
*/
public function scopeEarliestPublicationDate($query, string $column = null)
{
if (!$column) {
$column = self::PUBLISHED_AT;
}
return $query->whereNotNull('server_date_published')
->where('server_state', 'published')
->orderBy('server_date_published', 'asc')
->first()
->server_date_published;
}
2019-02-12 11:21:35 +00:00
public function setServerState($targetType)
{
$this->attributes['server_state'] = $targetType;
//$this->server_state = $targetType;
}
2018-08-06 12:30:51 +00:00
public function hasProject()
{
return $this->project()->exists();
}
2018-11-27 17:06:11 +00:00
public function hasEmbargoPassed($now = null)
{
$embargoDate = $this->embargo_date;
if (is_null($embargoDate)) {
return true;
}
if (is_null($now)) {
$now = $dt = Carbon::now();
}
// Embargo has passed on the day after the specified date
// $embargoDate->setHour(23);
// $embargoDate->setMinute(59);
// $embargoDate->setSecond(59);
// $embargoDate->setTimezone('Z');
// $dt->year = 2015;
// $dt->month = 04;
// $dt->day = 21;
$embargoDate->hour = 23;
$embargoDate->minute = 59;
$embargoDate->second = 59;
return ($embargoDate->gt($now) == true);
}
2018-08-06 12:30:51 +00:00
}