Heimdall/app/Item.php

336 lines
7.4 KiB
PHP
Raw Normal View History

2018-01-29 12:41:57 +00:00
<?php
namespace App;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Database\Eloquent\Builder;
2022-11-26 13:35:36 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2018-01-29 12:41:57 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2018-02-03 15:46:14 +00:00
use Illuminate\Database\Eloquent\SoftDeletes;
use stdClass;
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;
2022-11-26 13:35:36 +00:00
use HasFactory;
/**
* @return void
*/
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-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 Builder $query
* @return Builder
*/
public function scopePinned(Builder $query): Builder
{
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;
}
/**
* @return BelongsToMany
*/
public function parents(): BelongsToMany
2018-02-17 00:13:38 +00:00
{
return $this->belongsToMany(Item::class, 'item_tag', 'item_id', 'tag_id');
2018-02-17 00:13:38 +00:00
}
/**
* @return BelongsToMany
*/
public function children(): BelongsToMany
2018-02-17 00:13:38 +00:00
{
return $this->belongsToMany(Item::class, 'item_tag', 'tag_id', 'item_id');
2018-02-17 00:13:38 +00:00
}
/**
* @return \Illuminate\Contracts\Foundation\Application|UrlGenerator|mixed|string
*/
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;
}
}
/**
* @return string
*/
public function getDroppableAttribute(): string
2018-02-17 00:13:38 +00:00
{
if ((int) $this->type === 1) {
2018-02-17 00:13:38 +00:00
return ' droppable';
} else {
return '';
}
}
/**
* @return string
*/
public function getLinkTargetAttribute(): string
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
}
}
/**
* @return string
*/
public function getLinkIconAttribute(): string
2018-02-18 17:04:18 +00:00
{
if ((int) $this->type === 1) {
2018-02-18 17:04:18 +00:00
return 'fa-tag';
} else {
return 'fa-arrow-alt-to-right';
}
}
/**
* @return string
*/
public function getLinkTypeAttribute(): string
2018-02-18 17:23:05 +00:00
{
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
/**
* @param $class
* @return false|mixed|string
*/
public static function nameFromClass($class)
{
$explode = explode('\\', $class);
$name = end($explode);
return $name;
}
/**
* @param $query
* @param $type
* @return mixed
*/
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);
}
/**
* @return bool
*/
public function enhanced(): bool
2018-10-21 11:39:12 +00:00
{
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
}
/**
* @param $class
* @return bool
*/
public static function isEnhanced($class): bool
{
if (!class_exists($class, false) || $class === null || $class === 'null') {
return false;
}
$app = new $class;
return (bool) ($app instanceof EnhancedApps);
}
/**
* @param $class
* @return false|mixed
*/
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 SearchInterface)) ? $app : false;
2019-01-16 14:47:32 +00:00
}
/**
* @return bool
*/
public function enabled(): bool
2018-10-29 19:01:25 +00:00
{
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;
}
/**
* @return mixed|stdClass
*/
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)) {
$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;
}
/**
* @param $class
* @return false
*/
public static function applicationDetails($class): bool
2019-01-16 14:20:33 +00:00
{
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;
}
/**
* @param $class
* @return string
*/
public static function getApplicationDescription($class): string
2019-01-16 14:20:33 +00:00
{
$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.
*
* @return BelongsTo
2018-10-12 13:57:46 +00:00
*/
public function user(): BelongsTo
2018-10-12 13:57:46 +00:00
{
return $this->belongsTo(User::class);
}
2018-01-29 12:41:57 +00:00
}