all models into model folder

This commit is contained in:
Arno Kaimbacher 2018-09-10 15:09:10 +02:00
parent 98f50a2b6f
commit 6990469c67
46 changed files with 325 additions and 203 deletions

15
app/Models/Collection.php Normal file
View file

@ -0,0 +1,15 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Dataset;
class Collection extends Model
{
public function documents()
{
return $this->belongsToMany(Dataset::class, 'link_documents_collections', 'collection_id', 'document_id');
}
}

198
app/Models/Dataset.php Normal file
View file

@ -0,0 +1,198 @@
<?php
namespace App\Models;
use App\Library\Xml\DatasetExtension;
use App\Models\Collection;
use App\Models\License;
use App\Models\Project;
use App\Models\Title;
use App\Models\Person;
use App\Models\XmlCache;
use Illuminate\Database\Eloquent\Model;
class Dataset extends Model
{
use DatasetExtension;
protected $table = 'documents';
//public $timestamps = false; //default true
// customize the names of the columns used to store the timestamps:
const CREATED_AT = 'created_at';
const UPDATED_AT = 'server_date_modified';
const PUBLISHED_AT = 'server_date_published';
protected $fillable = [
'type',
'server_state',
'creating_corporation',
'project_id',
'embargo_date',
'belongs_to_bibliography',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'server_date_created',
'server_date_modified',
'server_date_published',
];
//protected $dateFormat = 'Y-m-d';
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
// $this->_init();
}
/**
* Get the project that the dataset belongs to.
*/
public function project()
{
return $this->belongsTo(Project::class, 'project_id', 'id');
}
public function collections()
{
return $this
->belongsToMany(Collection::class, 'link_documents_collections', 'document_id', 'collection_id');
}
#region [person table]
//return all persons attached to this film
public function persons()
{
return $this->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
->withPivot('role');
}
/**
* Return all authors for this dataset
*
* @return \App\Person
*/
public function authors()
{
return $this
->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
->wherePivot('role', 'author');
}
/**
* Add author to dataset
*
* @param Person $user user to add
*
* @return void
*/
public function addAuthor(Person $user): void
{
$this->persons()->save($user, ['role' => 'author']);
}
/**
* Return all contributors for this dataset
*
* @return \App\Person
*/
public function contributors()
{
return $this
->belongsToMany(Person::class, 'link_documents_persons', 'document_id', 'person_id')
->wherePivot('role', 'contributor');
}
#endregion
#region title table:
public function titlesAbstracts()
{
return $this->hasMany(Title::class, 'document_id', 'id');
}
public function titles()
{
return $this->hasMany(Title::class, 'document_id', 'id')
->where('type', 'main');
}
public function addMainTitle(Title $title)
{
$title->type = 'main';
$this->titlesAbstracts()->save($title);
// $this->titles()->save($title, ['type' => 'main']);
}
/**
* Relation abstracts
*
* @return \App\Title
*/
public function abstracts()
{
return $this->hasMany(Title::class, 'document_id', 'id')
->where('type', 'abstract');
}
public function addMainAbstract(Title $title)
{
$title->type = 'abstract';
$this->titlesAbstracts()->save($title);
// $this->abstracts()->save($title, ['type' => 'abstract']);
}
#endregion title table
public function licenses()
{
return $this->belongsToMany(License::class, 'link_documents_licences', 'document_id', 'licence_id');
}
public function files()
{
return $this->hasMany(\App\Models\File::class, 'document_id', 'id');
}
/**
* Get the xml-cache record associated with the dataset.
*
* @return \App\Models\XmlCache
*/
public function xmlCache()
{
return $this->hasOne(XmlCache::class, 'document_id', 'id');
}
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;
}
public function hasProject()
{
return $this->project()->exists();
}
}

View file

@ -0,0 +1,112 @@
<?php
namespace App\Models;
use App\Models\Dataset;
/**
* DocumentFinder short summary.
*
* DocumentFinder description.
*
* @version 1.0
* @author kaiarn
*/
class DatasetFinder
{
/**
* @var \Illuminate\Database\Eloquent\Builder
*/
private $select = null;
/**
* Create new instance of Opus_DocumentList class. The created object
* allows to get custom subsets (or lists) of all existing Opus_Documents.
*/
public function __construct()
{
// $table = Opus_Db_TableGateway::getInstance(self::$_tableGatewayClass);
// $this->_db = $table->getAdapter();
// $this->select = $this->_db->select()->from(array('d' => 'documents'));
$this->select = Dataset::query(); //>select('name', 'email as user_email')
}
/**
* Add constraints to be applied on the result set.
*
* @param string $serverStateArray
* @return DatasetFinder Fluent interface.
*/
public function setServerStateInList($serverStateArray)
{
$this->select->whereIn('server_state', $serverStateArray);
//$this->select->where('server_state IN (?)', $serverStateArray);
return $this;
}
/**
* Add constraints to be applied on the result set.
*
* @param string $type
* @return DatasetFinder Fluent interface.
*/
public function setType($type)
{
$this->select->where('type', $type);
return $this;
}
/**
* Add constraints to be applied on the result set.
*
* @param string $type
* @return DatasetFinder Fluent interface.
*/
public function setServerState($serverState)
{
//$this->select->where('server_state', '=', $serverState);
$this->select->where('server_state', 'LIKE', "%".$serverState."%");
return $this;
}
/**
* Returns a list of distinct document types for the given constraint set.
*
* @return array
*/
public function groupedTypesPlusCount()
{
//$this->select->reset('columns');
$test = $this->select
//->select("type") // "count(DISTINCT id)");
->selectRaw('type, count(DISTINCT id) as count')
->groupBy('type')
->pluck('count', 'type')
->toArray();
return $test;
}
/**
* Returns the number of (distinct) documents for the given constraint set.
*
* @return int
*/
public function count()
{
$this->select->count();
}
/**
* Returns a list of (distinct) document ids for the given constraint set.
*
* NOTE: It was not possible to make sure only DISTINCT identifiers are returned. Therefore array_unique is used.
* See OPUSVIER-3644 for more information.
*
* @return array
*/
public function ids()
{
//return array_unique($this->_db->fetchCol($this->getSelectIds()));
return $this->select->pluck('id')->toArray();
}
}

View file

@ -2,7 +2,7 @@
namespace App\Models;
use App\Dataset;
use App\Models\Dataset;
use App\Models\HashValue;
use Illuminate\Database\Eloquent\Model;

13
app/Models/Language.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Language extends Model
{
//protected $table = 'languages';
public $timestamps = false;
protected $fillable = [];
}

30
app/Models/License.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Dataset;
class License extends Model
{
protected $table = 'document_licences';
public $timestamps = false;
protected $fillable = [
'active',
'desc_text',
'desc_text',
'desc_text',
'language',
'link_licence',
'link_logo',
'mime_type',
'name_long',
'pod_allowed',
'sort_order',
];
public function datasets()
{
return $this->belongsToMany(Dataset::class, 'link_documents_licences', 'licence_id', 'document_id');
}
}

View file

@ -3,7 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\User;
use App\Models\User;
use App\Models\ModelTrait;
class Page extends Model
@ -62,7 +62,8 @@ class Page extends Model
*/
public function getViewButtonAttribute()
{
return '<a target="_blank" href="'. route('frontend.pages.show', $this->page_slug) .'" class="btn btn-flat btn-default">
return '<a target="_blank" href="
'. route('frontend.pages.show', $this->page_slug) .' " class="btn btn-flat btn-default">
<i data-toggle="tooltip" data-placement="top" title="View Page" class="fa fa-eye"></i>
</a>';
}

52
app/Models/Person.php Normal file
View file

@ -0,0 +1,52 @@
<?php
namespace App\Models;
use App\Models\Dataset;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $fillable = [
'academic_title',
'last_name',
'first_name',
'email',
'identifier_orcid',
'status',
];
protected $table = 'persons';
public $timestamps = false;
protected $appends = ['full_name'];
public function documents()
{
return $this->belongsToMany(Dataset::class, 'link_documents_persons', 'person_id', 'document_id')
->withPivot('role');
}
// public function scopeNotLimit($query)
// {
// return $query->where('borrow', '<', 3);
// }
/**
* Get the user's full name.
* see https://laravel.com/docs/5.6/eloquent-serialization
* @return string
*/
public function getFullNameAttribute()
{
return $this->first_name . " " . $this->last_name;
}
public function scopeActive($query)
{
return $query->where('status', 1);
}
public function scopeOrderByName($query)
{
return $query->orderBy('last_name');
}
}

30
app/Models/Project.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Dataset;
class Project extends Model
{
//protected $table = 'projects';
// for using $input = $request->all();
//$project = Project::create($input);
protected $fillable = [
'name', 'label'
];
public function documents()
{
//model, foreign key on the Document model is project_id, local id of category
return $this->hasMany(Dataset::class, 'project_id', 'id');
}
// public function books()
// {
// //model, foreign key on the Book model is project_id, local id of category
// return $this->hasMany('App\Book', 'project_id', 'id');
// }
}

View file

@ -4,6 +4,7 @@ namespace App\Models;
//use App\Library\Xml\DatasetExtension;
use Illuminate\Database\Eloquent\Model;
use App\Models\Dataset;
class Title extends Model
{
@ -16,6 +17,6 @@ class Title extends Model
public function dataset()
{
return $this->belongsTo(\App\Dataset::class, 'document_id', 'id');
return $this->belongsTo(Dataset::class, 'document_id', 'id');
}
}

97
app/Models/User.php Normal file
View file

@ -0,0 +1,97 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Collection;
class User extends Authenticatable
{
// use Authenticatable, CanResetPassword, Authorizable;
use Notifiable;
// use HasRoles;
use EntrustUserTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'accounts';
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['login', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function setPasswordAttribute($password)
{
if ($password) {
$this->attributes['password'] = app('hash')->needsRehash($password) ? Hash::make($password) : $password;
}
}
public function getAvatarUrl()
{
return "https://www.gravatar.com/avatar/" . md5($this->email) . "?d=mm";
}
public function getRoleNames(): Collection
{
return $this->roles->pluck('name');
}
//public function roles()
//{
// return $this->belongsToMany(\App\Role::class, 'link_accounts_roles', 'account_id', 'role_id');
//}
public function is($roleName)
{
foreach ($this->roles()->get() as $role) {
if ($role->name == $roleName) {
return true;
}
}
return false;
}
// public function assignRole($role)
// {
// return $this->roles()->attach($role);
// }
// public function revokeRole($role)
// {
// return $this->roles()->detach($role);
// }
// public function hasRole($name)
// {
// foreach ($this->roles as $role)
// {
// if ($role->name === $name)
// {
// return true;
// }
// return false;
// }
// return false;
// }
}

82
app/Models/XmlCache.php Normal file
View file

@ -0,0 +1,82 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Dataset;
class XmlCache extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'document_xml_cache';
public $timestamps = false;
/**
* primaryKey
*
* @var integer
* @access protected
*/
protected $primaryKey = null;
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['document_id', 'xml_version', 'server_date_modified', 'xml_data'];
/**
* Get the dataset that owns the xml cache.
*/
public function dataset()
{
return $this->belongsTo(Dataset::class, 'document_id', 'id');
}
/**
* Get dom document of 'xml_data' string
*
* @return \DOMDocument
*/
public function getDomDocument()
{
$dom = new \DOMDocument('1.0', 'utf-8');
$xmlData = $this->xml_data;
$dom->loadXML($xmlData);
return $dom;
}
/**
* Check if a document in a specific xml version is already cached or not.
*
* @param mixed $datasetId
* @param mixed $xmlVersion
* @param mixed $serverDateModified
* @return bool Returns true on cached hit else false.
*/
//public function scopeHasValidEntry($query, $datasetId, $xmlVersion, $serverDateModified)
//{
// //$select = $this->_table->select()->from($this->_table);
// $query->where('document_id = ?', $datasetId)
// ->where('xml_version = ?', $xmlVersion)
// ->where('server_date_modified = ?', $serverDateModified);
// $row = $query->get();
// if (null === $row)
// {
// return false;
// }
// else
// {
// return true;
// }
//}
}