tethys/app/Models/Dataset.php

396 lines
11 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\Coverage;
use App\Models\Description;
use App\Models\File;
2018-09-10 13:09:10 +00:00
use App\Models\License;
use App\Models\Person;
2018-09-10 13:09:10 +00:00
use App\Models\Project;
2019-01-24 15:56:39 +00:00
use App\Models\Title;
use App\Models\User;
2018-09-10 13:09:10 +00:00
use App\Models\XmlCache;
use App\Models\DatasetIdentifier;
2018-11-27 17:06:11 +00:00
use Carbon\Carbon;
// use App\Models\GeolocationBox;
use Illuminate\Database\Eloquent\Model;
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',
'server_date_published',
'publisher_name',
'publish_id',
2019-02-19 17:52:52 +00:00
'creating_corporation',
'project_id',
'embargo_date',
'belongs_to_bibliography',
2019-04-11 16:52:10 +00:00
'editor_id',
'preferred_reviewer',
'preferred_reviewer_email',
2019-05-21 16:28:18 +00:00
'reviewer_id',
'reject_reviewer_note',
2019-05-28 17:02:21 +00:00
'reject_editor_note',
'reviewer_note_visible',
2019-02-19 17:52:52 +00:00
];
//protected $guarded = [];
/**
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 doi indentifier that owns the dataset.
*/
public function identifier()
{
return $this->hasOne(DatasetIdentifier::class, 'dataset_id', 'id');
}
/**
* Get the account that the dataset belongs to
*/
public function user()
{
return $this->belongsTo(User::class, 'account_id', 'id');
}
2019-04-11 16:52:10 +00:00
/**
* Get the editor of the dataset
2019-04-08 16:31:40 +00:00
*/
public function editor()
{
return $this->belongsTo(User::class, 'editor_id', 'id');
}
2019-04-11 16:52:10 +00:00
/**
* Get the editor of the dataset
*/
public function reviewer()
{
return $this->belongsTo(User::class, 'reviewer_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', 'contributor_type');
2018-08-06 12:30:51 +00:00
}
/**
* Return all authors for this dataset
*
* @return \App\Person
*/
public function authors()
{
return $this
->persons()
//->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
->wherePivot('role', 'author')
->orderBy('link_documents_persons.sort_order');
2018-08-06 12:30:51 +00:00
}
/**
* 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
->persons()
// ->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
->wherePivot('role', 'contributor')
->orderBy('link_documents_persons.sort_order');
2018-08-06 12:30:51 +00:00
}
#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
}
public function additionalTitles()
{
return $this->hasMany(Title::class, 'document_id', 'id')->where('type', '!=', 'Main');
}
2020-03-10 17:58:46 +00:00
public function mainTitle()
{
return $this->hasMany(Title::class, 'document_id', 'id')->where('type', 'Main')->first();
}
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
}
public function additionalAbstracts()
{
return $this->hasMany(Description::class, 'document_id', 'id')->where('type', '!=', 'Abstract');
}
2020-03-10 17:58:46 +00:00
public function mainAbstract()
{
return $this->hasMany(Description::class, 'document_id', 'id')->where('type', 'Abstract')->first();
}
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 license()
{
return $this->belongsToMany(License::class, 'link_documents_licences', 'document_id', 'licence_id')->first();
}
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');
}
// public function subjects()
// {
// return $this->hasMany(\App\Models\Subject::class, 'document_id', 'id');
// }
2019-03-18 13:32:29 +00:00
public function subjects()
{
return $this->belongsToMany(\App\Models\Subject::class, 'link_dataset_subjects', 'document_id', 'subject_id');
2019-03-18 13:32:29 +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
*/
2020-03-05 15:03:34 +00:00
public static function earliestPublicationDate(string $column = null)
2018-08-06 12:30:51 +00:00
{
if (!$column) {
$column = self::PUBLISHED_AT;
}
2020-03-05 15:03:34 +00:00
$result = Dataset::select('server_date_published')
->where('server_date_published', '<>', null)
2018-08-06 12:30:51 +00:00
->where('server_state', 'published')
->orderBy('server_date_published', 'asc')
->first();
//->server_date_published;
2020-03-05 15:03:34 +00:00
return $result;
2018-08-06 12:30:51 +00:00
}
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;
2018-11-27 17:06:11 +00:00
$embargoDate->minute = 59;
$embargoDate->second = 59;
return ($embargoDate->lessThan($now));
//return ($embargoDate->gt($now) == true);
2018-11-27 17:06:11 +00:00
}
2019-05-28 17:02:21 +00:00
public function getRemainingTimeAttribute()
{
$dateDiff = $this->server_date_modified->addDays(14);
2019-05-28 17:02:21 +00:00
if ($this->server_state == "approved") {
return Carbon::now()->diffInDays($dateDiff, false);
} else {
return 0;
}
}
2020-03-10 17:58:46 +00:00
public function geoLocation()
{
// return $this->coverage->x_min;
$geolocation =
'SOUTH-BOUND LATITUDE: ' . $this->coverage->x_min . ","
. ' * WEST-BOUND LONGITUDE: ' . $this->coverage->y_min . ","
. ' * NORTH-BOUND LATITUDE: ' . $this->coverage->x_max . ","
. ' * EAST-BOUND LONGITUDE: ' . $this->coverage->y_max;
$elevation = '';
if ($this->coverage->elevation_min != null && $this->coverage->elevation_max != null) {
$elevation = $elevation . '* ELEVATION MIN: ' . $this->coverage->elevation_min
. ', * ELEVATION MAX: ' . $this->coverage->elevation_max;
} elseif ($this->coverage->elevation_absolut != null) {
$elevation = $elevation . '* ELEVATION ABSOLUT: ' . $this->coverage->elevation_absolut;
}
$geolocation = $elevation == '' ? $geolocation : $geolocation . ", " . $elevation;
$depth = '';
if ($this->coverage->depth_min != null && $this->coverage->depth_max != null) {
$depth = $depth . '* DEPTH MIN: ' . $this->coverage->depth_min
. ', * DEPTH MAX: ' . $this->coverage->depth_max;
} elseif ($this->coverage->depth_absolut != null) {
$depth = $depth . '* DEPTH ABSOLUT: ' . $this->coverage->depth_absolut;
}
$geolocation = $depth == '' ? $geolocation : $geolocation . ", " . $depth;
$time = '';
if ($this->coverage->time_min != null && $this->coverage->time_max != null) {
$time = $time . '* TIME MIN: ' . $this->coverage->time_min
. ', * TIME MAX: ' . $this->coverage->time_max;
} elseif ($this->coverage->time_absolut != null) {
$time = $time . '* TIME ABSOLUT: ' . $this->coverage->time_absolut;
}
$geolocation = $time == '' ? $geolocation : $geolocation . ", " . $time;
2020-03-10 17:58:46 +00:00
return $geolocation;
}
2018-08-06 12:30:51 +00:00
}