|
1
|
|
|
<?php |
|
2
|
|
|
namespace Serverfireteam\Panel; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
5
|
|
|
|
|
6
|
|
|
class Link extends Model { |
|
7
|
|
|
|
|
8
|
|
|
protected $fillable = ['url', 'display', 'show_menu']; |
|
9
|
|
|
|
|
10
|
|
|
protected $table = 'links'; |
|
11
|
|
|
|
|
12
|
|
|
static $cache = []; |
|
13
|
|
|
public static function getAllLinks($forceRefresh = false) // allCached( |
|
14
|
|
|
{ |
|
15
|
|
|
if (!isset(self::$cache['all']) || $forceRefresh) { |
|
16
|
|
|
self::$cache['all'] = Link::all(); |
|
17
|
|
|
} |
|
18
|
|
|
return self::$cache['all']; |
|
19
|
|
|
} |
|
20
|
|
|
public static function getUrls($forceRefresh = false) // returnUrls( |
|
21
|
|
|
{ |
|
22
|
|
|
if (!isset(self::$cache['all_urls']) || $forceRefresh) { |
|
23
|
|
|
$configs = Link::getAllLinks($forceRefresh); |
|
24
|
|
|
self::$cache['all_urls'] = $configs->pluck('url')->toArray(); |
|
25
|
|
|
} |
|
26
|
|
|
return self::$cache['all_urls']; |
|
27
|
|
|
} |
|
28
|
|
|
public static function getMainUrls($forceRefresh = false) |
|
29
|
|
|
{ |
|
30
|
|
|
if (!isset(self::$cache['main_urls']) || $forceRefresh) { |
|
31
|
|
|
$configs = Link::where('main', '=', true)->get(['url']); |
|
32
|
|
|
self::$cache['main_urls'] = $configs->pluck('url')->toArray(); |
|
33
|
|
|
} |
|
34
|
|
|
return self::$cache['main_urls']; |
|
35
|
|
|
} |
|
36
|
|
|
public function addNewLink($url, $label, $visibility, $checkExistence = false) // getAndSave( |
|
37
|
|
|
{ |
|
38
|
|
|
if ($checkExistence && $this->isLinkExist($url, $label)) |
|
39
|
|
|
{ |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
$this->url = $url; |
|
43
|
|
|
$this->display = $label; |
|
44
|
|
|
$this->show_menu = $visibility; |
|
45
|
|
|
$this->save(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* check given url and display label if they added before |
|
50
|
|
|
* @param $url |
|
51
|
|
|
* @param $label |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isLinkExist($url, $label) |
|
55
|
|
|
{ |
|
56
|
|
|
//if you call exists() against a non existent record then it gives error: Call to a member function exists() on null |
|
57
|
|
|
//Link::where('url', '=', $url)->exists() |
|
58
|
|
|
$linkCount = Link::where('url', '=', $url)->where('display', '=', $label)->count(); //->first(); if ($link === null) |
|
59
|
|
|
if ($linkCount <= 0) { |
|
60
|
|
|
// link doesn't exist |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |