2018-01-29 12:41:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
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;
|
2022-03-19 13:54:32 +00:00
|
|
|
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();
|
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-01-29 12:41:57 +00:00
|
|
|
//
|
2018-02-01 14:45:59 +00:00
|
|
|
protected $fillable = [
|
2022-03-30 12:14:05 +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.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-17 00:13:38 +00:00
|
|
|
public function parents()
|
|
|
|
{
|
2022-03-19 13:54:33 +00:00
|
|
|
return $this->belongsToMany(\App\Item::class, 'item_tag', 'item_id', 'tag_id');
|
2018-02-17 00:13:38 +00:00
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-02-17 00:13:38 +00:00
|
|
|
public function children()
|
|
|
|
{
|
2022-03-19 13:54:33 +00:00
|
|
|
return $this->belongsToMany(\App\Item::class, 'item_tag', 'tag_id', 'item_id');
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDroppableAttribute()
|
|
|
|
{
|
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 '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:04:18 +00:00
|
|
|
public function getLinkIconAttribute()
|
|
|
|
{
|
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
|
|
|
|
2018-02-18 17:23:05 +00:00
|
|
|
public function getLinkTypeAttribute()
|
|
|
|
{
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-06 14:52:45 +00:00
|
|
|
public static function isEnhanced($class)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
return (bool) ($app instanceof \App\EnhancedApps);
|
2018-11-06 14:52:45 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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)) {
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-16 14:20:33 +00:00
|
|
|
public static function applicationDetails($class)
|
|
|
|
{
|
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
|
|
|
|
2019-01-16 14:20:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getApplicationDescription($class)
|
|
|
|
{
|
|
|
|
$details = self::applicationDetails($class);
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($details !== false) {
|
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.
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
2022-03-19 13:54:33 +00:00
|
|
|
return $this->belongsTo(\App\User::class);
|
2022-03-19 13:54:32 +00:00
|
|
|
}
|
2018-01-29 12:41:57 +00:00
|
|
|
}
|