Heimdall/app/Item.php

266 lines
6.3 KiB
PHP
Raw Normal View History

2018-01-29 12:41:57 +00:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\ClassLoader\ClassMapGenerator;
2018-02-03 15:46:14 +00:00
use Illuminate\Database\Eloquent\SoftDeletes;
2018-10-14 16:27:28 +00:00
use Illuminate\Database\Eloquent\Builder;
use App\User;
2019-06-13 14:40:26 +00:00
use App\ItemTag;
2019-01-16 14:20:33 +00:00
use App\Application;
2018-01-29 12:41:57 +00:00
class Item extends Model
{
2018-02-03 15:46:14 +00:00
use SoftDeletes;
2018-10-14 16:27:28 +00:00
protected static function boot()
{
parent::boot();
static::addGlobalScope('user_id', function (Builder $builder) {
$current_user = User::currentUser();
2019-06-18 12:41:02 +00:00
if($current_user) {
$builder->where('user_id', $current_user->id)->orWhere('user_id', 0);
} else {
$builder->where('user_id', 0);
}
2018-10-14 16:27:28 +00:00
});
}
2018-01-29 12:41:57 +00:00
//
2018-02-01 14:45:59 +00:00
protected $fillable = [
2018-10-23 14:53:56 +00:00
'title', 'url', 'colour', 'icon', 'description', 'pinned', 'order', 'type', 'class', 'user_id'
2018-02-01 14:45:59 +00:00
];
2018-02-03 15:46:14 +00:00
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* Scope a query to only include pinned items.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePinned($query)
{
return $query->where('pinned', 1);
}
2018-02-06 20:41:29 +00:00
public static function checkConfig($config)
{
if(empty($config)) {
$config = null;
} else {
$store = false;
2018-02-09 00:05:37 +00:00
//die(var_dump($config));
foreach($config as $key => $check) {
if($key == 'type') continue;
2018-02-09 00:05:37 +00:00
if($key == 'dataonly') continue;
if(!empty($check) && $check != '0') {
$store = true;
break;
}
}
2018-02-09 00:05:37 +00:00
//die(var_dump($store))
2018-06-05 16:18:49 +00:00
$config['enabled'] = ($store) ? true : false;
$config = json_encode($config);
}
return $config;
}
2018-02-17 00:13:38 +00:00
2019-06-13 14:40:26 +00:00
public function tags()
{
$id = $this->id;
$tags = ItemTag::select('tag_id')->where('item_id', $id)->pluck('tag_id')->toArray();
$tagdetails = Item::select('id', 'title', 'url', 'pinned')->whereIn('id', $tags)->get();
//print_r($tags);
if(in_array(0, $tags)) {
$details = new Item([
"id" => 0,
2019-06-13 18:07:38 +00:00
"title" => __('app.dashboard'),
2019-06-13 14:40:26 +00:00
"url" => '',
2019-06-13 18:07:38 +00:00
"pinned" => 0
2019-06-13 14:40:26 +00:00
]);
$tagdetails->prepend($details);
}
return $tagdetails;
}
2018-02-17 00:13:38 +00:00
public function parents()
{
return $this->belongsToMany('App\Item', 'item_tag', 'item_id', 'tag_id');
}
public function children()
{
return $this->belongsToMany('App\Item', 'item_tag', 'tag_id', 'item_id');
}
public function getLinkAttribute()
{
if((int)$this->type === 1) {
return '/tag/'.$this->url;
} else {
return $this->url;
}
}
public function getDroppableAttribute()
{
if((int)$this->type === 1) {
return ' droppable';
} else {
return '';
}
}
2018-02-19 23:15:09 +00:00
public function getLinkTargetAttribute()
2018-02-17 00:13:38 +00:00
{
2018-06-07 21:35:01 +00:00
$target = Setting::fetch('window_target');
if((int)$this->type === 1 || $target === 'current') {
2018-02-17 00:13:38 +00:00
return '';
} else {
return ' target="' . $target . '"';
2018-02-17 00:13:38 +00:00
}
}
2018-02-18 17:04:18 +00:00
public function getLinkIconAttribute()
{
if((int)$this->type === 1) {
return 'fa-tag';
} else {
return 'fa-arrow-alt-to-right';
}
}
2018-02-18 17:23:05 +00:00
public function getLinkTypeAttribute()
{
if((int)$this->type === 1) {
return 'tags';
} else {
return 'items';
}
}
2018-02-18 17:04:18 +00:00
public static function nameFromClass($class)
{
$explode = explode('\\', $class);
$name = end($explode);
return $name;
}
2018-02-17 00:13:38 +00:00
public function scopeOfType($query, $type)
{
switch($type) {
case 'item':
$typeid = 0;
break;
case 'tag':
$typeid = 1;
break;
}
return $query->where('type', $typeid);
}
2018-10-21 11:39:12 +00:00
public function enhanced()
{
2018-10-23 14:53:56 +00:00
if(isset($this->class) && !empty($this->class)) {
$app = new $this->class;
} else {
2018-10-29 19:01:25 +00:00
return false;
2018-10-23 14:53:56 +00:00
}
2018-10-21 11:39:12 +00:00
return (bool)($app instanceof \App\EnhancedApps);
}
public static function isEnhanced($class)
{
if($class === null || $class === 'null') return false;
$app = new $class;
return (bool)($app instanceof \App\EnhancedApps);
}
2019-01-16 14:47:32 +00:00
public static function isSearchProvider($class)
{
$app = new $class;
2019-01-18 15:21:50 +00:00
return ((bool)($app instanceof \App\SearchInterface)) ? $app : false;
2019-01-16 14:47:32 +00:00
}
2018-10-29 19:01:25 +00:00
public function enabled()
{
if($this->enhanced()) {
$config = $this->getconfig();
if($config) {
return (bool) $config->enabled;
}
}
return false;
}
2018-10-21 12:23:23 +00:00
public function getconfig()
2018-10-21 11:39:12 +00:00
{
2018-10-30 14:58:45 +00:00
$explode = explode('\\', $this->class);
if(!isset($this->description) || empty($this->description)) {
$config = new \stdClass;
$config->name = end($explode);
$config->enabled = false;
return $config;
}
2018-10-23 14:53:56 +00:00
2018-10-21 11:39:12 +00:00
$config = json_decode($this->description);
2018-10-21 12:23:23 +00:00
$config->name = end($explode);
2018-10-21 11:39:12 +00:00
$config->url = $this->url;
if(isset($config->override_url) && !empty($config->override_url)) {
$config->url = $config->override_url;
}
return $config;
}
2019-01-16 14:20:33 +00:00
public static function applicationDetails($class)
{
if(!empty($class)) {
$name = self::nameFromClass($class);
$application = Application::where('name', $name)->first();
if($application) return $application;
}
2018-10-21 11:39:12 +00:00
2019-01-16 14:20:33 +00:00
return false;
}
public static function getApplicationDescription($class)
{
$details = self::applicationDetails($class);
if($details !== false) {
return $details->description.' - '.$details->license;
}
return '';
}
2018-10-21 11:39:12 +00:00
2018-10-12 13:57:46 +00:00
/**
* Get the user that owns the item.
*/
public function user()
{
return $this->belongsTo('App\User');
}
2018-02-17 00:13:38 +00:00
2018-01-29 12:41:57 +00:00
}