2018-01-29 12:41:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator;
|
2022-03-19 13:54:32 +00:00
|
|
|
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;
|
2022-11-25 00:35:56 +00:00
|
|
|
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;
|
2022-11-25 00:35:56 +00:00
|
|
|
use stdClass;
|
2022-03-19 13:54:32 +00:00
|
|
|
use Symfony\Component\ClassLoader\ClassMapGenerator;
|
2018-01-29 12:41:57 +00:00
|
|
|
|
2022-12-11 10:58:58 +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
|
|
|
|
*/
|
2022-12-11 10:58:58 +00:00
|
|
|
// @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;
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @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();
|
2022-03-19 13:54:32 +00:00
|
|
|
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 = [
|
2022-11-25 00:35:56 +00:00
|
|
|
'title',
|
|
|
|
'url',
|
|
|
|
'colour',
|
|
|
|
'icon',
|
|
|
|
'appdescription',
|
|
|
|
'description',
|
|
|
|
'pinned',
|
|
|
|
'order',
|
|
|
|
'type',
|
|
|
|
'class',
|
|
|
|
'user_id',
|
|
|
|
'tag_id',
|
|
|
|
'appid',
|
2018-02-01 14:45:59 +00:00
|
|
|
];
|
2018-02-01 23:10:08 +00:00
|
|
|
|
2022-03-19 13:54:34 +00:00
|
|
|
|
2018-02-03 15:46:14 +00:00
|
|
|
|
2018-02-03 00:22:42 +00:00
|
|
|
/**
|
|
|
|
* Scope a query to only include pinned items.
|
|
|
|
*
|
2022-11-25 00:35:56 +00:00
|
|
|
* @param Builder $query
|
|
|
|
* @return Builder
|
2018-02-03 00:22:42 +00:00
|
|
|
*/
|
2022-11-25 00:35:56 +00:00
|
|
|
public function scopePinned(Builder $query): Builder
|
2018-02-03 00:22:42 +00:00
|
|
|
{
|
|
|
|
return $query->where('pinned', 1);
|
|
|
|
}
|
2018-02-06 20:41:29 +00:00
|
|
|
|
2018-02-08 20:58:07 +00:00
|
|
|
public static function checkConfig($config)
|
|
|
|
{
|
2022-03-15 18:19:01 +00:00
|
|
|
// die(print_r($config));
|
2022-03-19 13:54:32 +00:00
|
|
|
if (empty($config)) {
|
2018-02-08 20:58:07 +00:00
|
|
|
$config = null;
|
|
|
|
} else {
|
|
|
|
$config = json_encode($config);
|
|
|
|
}
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
return $config;
|
2018-02-08 20:58:07 +00:00
|
|
|
}
|
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();
|
2022-03-19 13:54:32 +00:00
|
|
|
$tagdetails = self::select('id', 'title', 'url', 'pinned')->whereIn('id', $tags)->get();
|
2019-06-13 14:40:26 +00:00
|
|
|
//print_r($tags);
|
2022-03-19 13:54:32 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2019-06-13 14:40:26 +00:00
|
|
|
return $tagdetails;
|
|
|
|
}
|
|
|
|
|
2022-12-19 13:40:38 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTagClass(): string
|
|
|
|
{
|
|
|
|
$tags = $this->tags();
|
|
|
|
$slugs = [];
|
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if ($tag->url) {
|
|
|
|
$slugs[] = 'tag-'.$tag->url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(' ', $slugs);
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
|
|
|
public function parents(): BelongsToMany
|
2018-02-17 00:13:38 +00:00
|
|
|
{
|
2022-11-25 00:35:56 +00:00
|
|
|
return $this->belongsToMany(Item::class, 'item_tag', 'item_id', 'tag_id');
|
2018-02-17 00:13:38 +00:00
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
|
|
|
public function children(): BelongsToMany
|
2018-02-17 00:13:38 +00:00
|
|
|
{
|
2022-11-25 00:35:56 +00:00
|
|
|
return $this->belongsToMany(Item::class, 'item_tag', 'tag_id', 'item_id');
|
2018-02-17 00:13:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|UrlGenerator|mixed|string
|
|
|
|
*/
|
2018-02-17 00:13:38 +00:00
|
|
|
public function getLinkAttribute()
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDroppableAttribute(): string
|
2018-02-17 00:13:38 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if ((int) $this->type === 1) {
|
2018-02-17 00:13:38 +00:00
|
|
|
return ' droppable';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if ((int) $this->type === 1 || $target === 'current') {
|
2018-02-17 00:13:38 +00:00
|
|
|
return '';
|
|
|
|
} else {
|
2022-03-19 13:54:32 +00:00
|
|
|
return ' target="'.$target.'"';
|
2018-02-17 00:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLinkIconAttribute(): string
|
2018-02-18 17:04:18 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +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';
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLinkTypeAttribute(): string
|
2018-02-18 17:23:05 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +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
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $class
|
|
|
|
* @return false|mixed|string
|
|
|
|
*/
|
2018-11-06 14:52:45 +00:00
|
|
|
public static function nameFromClass($class)
|
|
|
|
{
|
|
|
|
$explode = explode('\\', $class);
|
|
|
|
$name = end($explode);
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-11-06 14:52:45 +00:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $query
|
|
|
|
* @param $type
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-02-17 00:13:38 +00:00
|
|
|
public function scopeOfType($query, $type)
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $class
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isEnhanced($class): bool
|
2018-11-06 14:52:45 +00:00
|
|
|
{
|
2022-04-12 10:48:03 +00:00
|
|
|
if (!class_exists($class, false) || $class === null || $class === 'null') {
|
2022-03-19 13:54:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-06 14:52:45 +00:00
|
|
|
$app = new $class;
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
return (bool) ($app instanceof EnhancedApps);
|
2018-11-06 14:52:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $class
|
|
|
|
* @return false|mixed
|
|
|
|
*/
|
2019-01-16 14:47:32 +00:00
|
|
|
public static function isSearchProvider($class)
|
|
|
|
{
|
2022-04-12 10:48:03 +00:00
|
|
|
if (!class_exists($class, false) || $class === null || $class === 'null') {
|
2022-03-26 18:59:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-16 14:47:32 +00:00
|
|
|
$app = new $class;
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
return ((bool) ($app instanceof SearchInterface)) ? $app : false;
|
2019-01-16 14:47:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function enabled(): bool
|
2018-10-29 19:01:25 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($this->enhanced()) {
|
2018-10-29 19:01:25 +00:00
|
|
|
$config = $this->getconfig();
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($config) {
|
2018-10-29 19:01:25 +00:00
|
|
|
return (bool) $config->enabled;
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-29 19:01:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! isset($this->description) || empty($this->description)) {
|
2022-11-25 00:35:56 +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;
|
2022-03-19 13:54:32 +00:00
|
|
|
|
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;
|
2022-03-19 13:54:32 +00:00
|
|
|
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
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-21 11:39:12 +00:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $class
|
2022-11-29 22:13:24 +00:00
|
|
|
* @return Application|null
|
2022-11-25 00:35:56 +00:00
|
|
|
*/
|
2022-11-29 22:13:24 +00:00
|
|
|
public static function applicationDetails($class): ?Application
|
2019-01-16 14:20:33 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! empty($class)) {
|
2019-01-16 14:20:33 +00:00
|
|
|
$name = self::nameFromClass($class);
|
|
|
|
$application = Application::where('name', $name)->first();
|
2022-03-19 13:54:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +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;
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
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.
|
2022-11-25 00:35:56 +00:00
|
|
|
*
|
|
|
|
* @return BelongsTo
|
2018-10-12 13:57:46 +00:00
|
|
|
*/
|
2022-11-25 00:35:56 +00:00
|
|
|
public function user(): BelongsTo
|
2018-10-12 13:57:46 +00:00
|
|
|
{
|
2022-11-25 00:35:56 +00:00
|
|
|
return $this->belongsTo(User::class);
|
2022-03-19 13:54:32 +00:00
|
|
|
}
|
2018-01-29 12:41:57 +00:00
|
|
|
}
|