Heimdall/app/Item.php

258 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2018-01-29 12:41:57 +00:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Builder;
2018-01-29 12:41:57 +00:00
use Illuminate\Database\Eloquent\Model;
2018-02-03 15:46:14 +00:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Symfony\Component\ClassLoader\ClassMapGenerator;
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();
if ($current_user) {
2022-11-14 12:21:47 +00:00
$builder->where('user_id', $current_user->getId())->orWhere('user_id', 0);
2019-06-18 12:41:02 +00:00
} 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 = [
'title', 'url', 'colour', 'icon', 'appdescription', 'description', 'pinned', 'order', 'type', 'class', 'user_id', 'tag_id', 'appid',
2018-02-01 14:45:59 +00:00
];
2022-03-19 13:54:34 +00:00
2018-02-03 15:46:14 +00:00
/**
* 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)
{
2022-03-15 18:19:01 +00:00
// die(print_r($config));
if (empty($config)) {
$config = null;
} else {
$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 = self::select('id', 'title', 'url', 'pinned')->whereIn('id', $tags)->get();
2019-06-13 14:40:26 +00:00
//print_r($tags);
if (in_array(0, $tags)) {
$details = new self([
'id' => 0,
'title' => __('app.dashboard'),
'url' => '',
'pinned' => 0,
2019-06-13 14:40:26 +00:00
]);
$tagdetails->prepend($details);
}
2019-06-13 14:40:26 +00:00
return $tagdetails;
}
2018-02-17 00:13:38 +00:00
public function parents()
{
return $this->belongsToMany(\App\Item::class, 'item_tag', 'item_id', 'tag_id');
2018-02-17 00:13:38 +00:00
}
2018-02-17 00:13:38 +00:00
public function children()
{
return $this->belongsToMany(\App\Item::class, 'item_tag', 'tag_id', 'item_id');
2018-02-17 00:13:38 +00:00
}
public function getLinkAttribute()
{
if ((int) $this->type === 1) {
2022-03-14 07:56:02 +00:00
return url('tag/'.$this->url);
2018-02-17 00:13:38 +00:00
} else {
return $this->url;
}
}
public function getDroppableAttribute()
{
if ((int) $this->type === 1) {
2018-02-17 00:13:38 +00:00
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) {
2018-02-18 17:04:18 +00:00
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) {
2018-02-18 17:23:05 +00:00
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) {
2018-02-17 00:13:38 +00:00
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()
{
2022-03-15 20:09:22 +00:00
/*if(isset($this->class) && !empty($this->class)) {
2018-10-23 14:53:56 +00:00
$app = new $this->class;
} else {
2018-10-29 19:01:25 +00:00
return false;
2018-10-23 14:53:56 +00:00
}
2022-03-15 20:09:22 +00:00
return (bool)($app instanceof \App\EnhancedApps);*/
return $this->description !== null;
2018-10-21 11:39:12 +00:00
}
public static function isEnhanced($class)
{
if (!class_exists($class, false) || $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)
{
if (!class_exists($class, false) || $class === null || $class === 'null') {
return false;
}
2019-01-16 14:47:32 +00:00
$app = new $class;
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()) {
2018-10-29 19:01:25 +00:00
$config = $this->getconfig();
if ($config) {
2018-10-29 19:01:25 +00:00
return (bool) $config->enabled;
}
}
2018-10-29 19:01:25 +00:00
return false;
}
2018-10-21 12:23:23 +00:00
public function getconfig()
2018-10-21 11:39:12 +00:00
{
2022-03-15 20:09:22 +00:00
// $explode = explode('\\', $this->class);
2018-10-30 14:58:45 +00:00
if (! isset($this->description) || empty($this->description)) {
2018-10-30 14:58:45 +00:00
$config = new \stdClass;
2022-03-15 20:09:22 +00:00
// $config->name = end($explode);
2018-10-30 14:58:45 +00:00
$config->enabled = false;
2022-03-11 19:03:03 +00:00
$config->override_url = null;
$config->apikey = null;
2018-10-30 14:58:45 +00:00
return $config;
}
2018-10-21 11:39:12 +00:00
$config = json_decode($this->description);
2018-10-21 12:23:23 +00:00
2022-03-15 20:09:22 +00:00
// $config->name = end($explode);
2018-10-21 12:23:23 +00:00
2018-10-21 11:39:12 +00:00
$config->url = $this->url;
if (isset($config->override_url) && ! empty($config->override_url)) {
2018-10-21 11:39:12 +00:00
$config->url = $config->override_url;
2022-03-11 19:03:03 +00:00
} else {
$config->override_url = null;
2018-10-21 11:39:12 +00:00
}
2018-10-21 11:39:12 +00:00
return $config;
}
2019-01-16 14:20:33 +00:00
public static function applicationDetails($class)
{
if (! empty($class)) {
2019-01-16 14:20:33 +00:00
$name = self::nameFromClass($class);
$application = Application::where('name', $name)->first();
if ($application) {
return $application;
}
2019-01-16 14:20:33 +00:00
}
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) {
2019-01-16 14:20:33 +00:00
return $details->description.' - '.$details->license;
}
2019-01-16 14:20:33 +00:00
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::class);
}
2018-01-29 12:41:57 +00:00
}