/
CpController.php
111 lines (93 loc) · 2.66 KB
/
CpController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Statamic\Http\Controllers\CP;
use Illuminate\Auth\Access\AuthorizationException as LaravelAuthException;
use Illuminate\Http\Request;
use Statamic\Exceptions\AuthorizationException;
use Statamic\Facades\File;
use Statamic\Facades\Folder;
use Statamic\Facades\YAML;
use Statamic\Http\Controllers\Controller;
use Statamic\Statamic;
use Statamic\Support\Str;
/**
* The base control panel controller.
*/
class CpController extends Controller
{
/**
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* Create a new CpController.
*
* @param \Illuminate\Http\Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Get all the template names from the current theme.
*
* @return array
*/
public function templates()
{
$templates = [];
foreach (Folder::disk('resources')->getFilesByTypeRecursively('templates', 'html') as $path) {
$parts = explode('/', $path);
array_shift($parts);
$templates[] = Str::removeRight(implode('/', $parts), '.html');
}
return $templates;
}
public function themes()
{
$themes = [];
foreach (Folder::disk('themes')->getFolders('/') as $folder) {
$name = $folder;
// Get the name if one exists in a meta file
if (File::disk('themes')->exists($folder.'/meta.yaml')) {
$meta = YAML::parse(File::disk('themes')->get($folder.'/meta.yaml'));
$name = array_get($meta, 'name', $folder);
}
$themes[] = compact('folder', 'name');
}
return $themes;
}
/**
* 404.
*/
public function pageNotFound()
{
return response()->view('statamic::errors.404', [], 404);
}
public function authorize($ability, $args = [], $message = null)
{
$message = $message ?? __('This action is unauthorized.');
try {
return parent::authorize($ability, $args);
} catch (LaravelAuthException $e) {
throw new AuthorizationException($message);
}
}
public function authorizeIf($condition, $ability, $args = [], $message = null)
{
if ($condition) {
return $this->authorize($ability, $args, $message);
}
}
public function authorizePro()
{
if (! Statamic::pro()) {
throw new AuthorizationException(__('Statamic Pro is required.'));
}
}
public function authorizeProIf($condition)
{
if ($condition) {
return $this->authorizePro();
}
}
}