Heimdall/app/Item.php

411 lines
10 KiB
PHP
Raw Permalink 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
// @codingStandardsIgnoreStart
2022-11-29 22:13:24 +00:00
/**
* App\Item
*
* @property int $id
* @property string $title
* @property string|null $colour
* @property string|null $icon
* @property string $url
* @property string|null $description
* @property int $pinned
* @property int $order
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $type
* @property int $user_id
* @property string|null $class
* @property string|null $appid
* @property string|null $appdescription
* @property-read \Illuminate\Database\Eloquent\Collection|Item[] $children
* @property-read int|null $children_count
* @property-read string $droppable
* @property-read \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\UrlGenerator|mixed|string $link
* @property-read string $link_icon
* @property-read string $link_target
* @property-read string $link_type
* @property-read \Illuminate\Database\Eloquent\Collection|Item[] $parents
* @property-read int|null $parents_count
* @property-read \App\User|null $user
* @method static \Database\Factories\ItemFactory factory(...$parameters)
* @method static Builder|Item newModelQuery()
* @method static Builder|Item newQuery()
* @method static Builder|Item ofType($type)
* @method static \Illuminate\Database\Query\Builder|Item onlyTrashed()
* @method static Builder|Item pinned()
* @method static Builder|Item query()
* @method static Builder|Item whereAppdescription($value)
* @method static Builder|Item whereAppid($value)
* @method static Builder|Item whereClass($value)
* @method static Builder|Item whereColour($value)
* @method static Builder|Item whereCreatedAt($value)
* @method static Builder|Item whereDeletedAt($value)
* @method static Builder|Item whereDescription($value)
* @method static Builder|Item whereIcon($value)
* @method static Builder|Item whereId($value)
* @method static Builder|Item whereOrder($value)
* @method static Builder|Item wherePinned($value)
* @method static Builder|Item whereTitle($value)
* @method static Builder|Item whereType($value)
* @method static Builder|Item whereUpdatedAt($value)
* @method static Builder|Item whereUrl($value)
* @method static Builder|Item whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|Item withTrashed()
* @method static \Illuminate\Database\Query\Builder|Item withoutTrashed()
* @mixin \Eloquent
*/
// @codingStandardsIgnoreEnd
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 string
*/
public function getTagClass(): string
{
$tags = $this->tags();
$slugs = [];
foreach ($tags as $tag) {
if ($tag->url) {
$slugs[] = 'tag-'.$tag->url;
}
}
return implode(' ', $slugs);
}
/**
* @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
2022-11-29 22:13:24 +00:00
* @return Application|null
*/
2022-11-29 22:13:24 +00:00
public static function applicationDetails($class): ?Application
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
2022-11-29 22:13:24 +00:00
return null;
2019-01-16 14:20:33 +00:00
}
/**
* @param $class
* @return string
*/
public static function getApplicationDescription($class): string
2019-01-16 14:20:33 +00:00
{
$details = self::applicationDetails($class);
2022-11-29 22:13:24 +00:00
if ($details !== null) {
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
}