Passed
Push — master ( 35a5e3...2ddbf5 )
by Georgi
03:24
created

LogoSettings::storage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Epesi\Core\System\Logo;
4
5
use Epesi\Core\System\Integration\Modules\ModuleView;
6
use Illuminate\Support\Facades\Auth;
7
use Epesi\Core\System\Seeds\Form;
8
use Epesi\Core\System\Database\Models\Variable;
9
use Illuminate\Support\Facades\Storage;
10
use Epesi\Core\Layout\Seeds\ActionBar;
11
use atk4\ui\View;
12
13
class LogoSettings extends ModuleView
14
{
15
	protected $label = 'Title & Logo';
16
	
17
	protected static $defaultLogo = 'epesi-logo.png';
18
	
19
	public static function access()
20
	{
21
		return Auth::user()->can('modify system settings');
22
	}
23
	
24
	public function body()
25
	{
26
		$layout = $this->add(['View'])->addStyle('max-width:1200px;margin:auto;');
27
		$layout->add(['Header', __($this->label)]);
28
		$segment = $layout->add(['View', ['ui' => 'segment']]);
29
30
		$form = $segment->add(new Form());
31
32
		$form->addField('title', __('Base page title'))->set(Variable::get('system.title'));
33
		
34
		$form->addField('custom_logo', ['CheckBox', 'caption' => __('Use custom logo')])->set((bool) Variable::get('system.logo'));
35
		
36
		$logo = $form->addField('logo', [
37
				'UploadImg', 
38
				'defaultSrc' => url('logo'), 
39
				'thumbnail' => (new View(['element'=>'img', 'class' => ['right', 'floated', 'image'], 'ui' => true]))->setStyle('max-width', '150px'),
40
				'placeholder' => __('Upload file to replace system logo')
41
		]);
42
		
43
		$form->addFieldsDisplayRules(['logo' => ['custom_logo' => 'checked']]);
44
		
45
		$logo->onDelete(function($fileName) use ($logo) {
0 ignored issues
show
Bug introduced by
The method onDelete() does not exist on atk4\ui\FormField\Generic. It seems like you code against a sub-type of atk4\ui\FormField\Generic such as atk4\ui\FormField\Upload. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
		$logo->/** @scrutinizer ignore-call */ 
46
         onDelete(function($fileName) use ($logo) {
Loading history...
46
			$this->storage()->delete(self::alias() . '/tmp/' . $fileName);
47
			
48
			$logo->setThumbnailSrc(asset('storage/' . self::alias() . '/' . self::$defaultLogo));
0 ignored issues
show
Bug introduced by
The method setThumbnailSrc() does not exist on atk4\ui\FormField\Generic. It seems like you code against a sub-type of atk4\ui\FormField\Generic such as atk4\ui\FormField\UploadImg. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
			$logo->/** @scrutinizer ignore-call */ 
49
          setThumbnailSrc(asset('storage/' . self::alias() . '/' . self::$defaultLogo));
Loading history...
49
		});
50
51
		$logo->onUpload(function ($files) use ($form, $logo) {
0 ignored issues
show
Bug introduced by
The method onUpload() does not exist on atk4\ui\FormField\Generic. It seems like you code against a sub-type of atk4\ui\FormField\Generic such as atk4\ui\FormField\Upload. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
		$logo->/** @scrutinizer ignore-call */ 
52
         onUpload(function ($files) use ($form, $logo) {
Loading history...
52
			if ($files === 'error')	return $form->error('logo', __('Error uploading image'));
0 ignored issues
show
Bug introduced by
It seems like __('Error uploading image') can also be of type array; however, parameter $str of atk4\ui\Form::error() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
			if ($files === 'error')	return $form->error('logo', /** @scrutinizer ignore-type */ __('Error uploading image'));
Loading history...
53
			
54
			$tmpPath = self::alias() . '/tmp/' . $files['name'];
55
			
56
			$logo->setThumbnailSrc(asset('storage/' . $tmpPath));
57
	
58
			$this->storage()->put($tmpPath, file_get_contents($files['tmp_name']));
59
		});
60
					
61
		$form->onSubmit(function($form) {
62
			if ($name = $form->model['custom_logo']? $form->model['logo']: null) {
63
				$storage = $this->storage();
64
				$from = self::alias() . '/tmp/' . $name;
65
				$to = self::alias() . '/' . $name;				
66
				
67
				if ($storage->exists($to)) {
68
					$storage->delete($to);
69
				}
70
				
71
				$storage->move($from, $to);
72
			}
73
	
74
			Variable::put('system.logo', $name);
75
			
76
			Variable::put('system.title', $form->model['title']);
77
			
78
			return $form->notify(__('Title and logo updated! Refresh page to see changes ...'));
79
		});
80
			
81
		ActionBar::addButton('back')->link(url('view/system'));
82
			
83
		ActionBar::addButton('save')->on('click', $form->submit());
84
	}
85
	
86
	public static function getLogoFile()
87
	{
88
		return self::storage()->path(self::alias() . '/' . Variable::get('system.logo', self::$defaultLogo));
89
	}
90
	
91
	public static function getTitle()
92
	{
93
		return Variable::get('system.title', config('epesi.app.title'));
94
	}
95
96
	public static function storage()
97
	{
98
		return Storage::disk('public');
99
	}
100
	
101
}
102