Passed
Push — master ( 4954b5...87483c )
by Georgi
03:11
created

App::addFavIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php 
2
3
namespace Epesi\Core;
4
5
use atk4\ui\App as BaseApp;
6
use atk4\ui\jsExpression;
7
use Epesi\Core\System\Integration\Modules\Concerns\HasLinks;
8
use Epesi\Core\System\SystemCore;
9
use Epesi\Core\System\Integration\Modules\ModuleManager;
10
11
class App extends BaseApp
12
{
13
	use HasLinks;
14
	
15
	public $version = '2.0.0-alpha1';
16
	
17
	public $always_run = false;
18
	protected $url_building_ext = '';
19
	
20
	public function __construct($defaults = [])
21
	{
22
		$this->cdn = array_merge($this->cdn, config('epesi.app.cdn', []));
23
24
		$this->collectTemplates();
25
		
26
		parent::__construct([
27
				'title' => config('epesi.app.title', 'EPESI'),
28
		]);
29
	}
30
	
31
	final public function collectTemplates()
32
	{
33
		//TODO: set the skin from admin / user selection
34
		$this->skin = config('epesi.app.skin', $this->skin);
35
36
		$this->template_dir = array_merge(ModuleManager::collect('templates', $this->skin), $this->template_dir?: []);
0 ignored issues
show
Bug introduced by
It seems like $this->template_dir ?: array() can also be of type string; however, parameter $array2 of array_merge() does only seem to accept array|null, 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

36
		$this->template_dir = array_merge(ModuleManager::collect('templates', $this->skin), /** @scrutinizer ignore-type */ $this->template_dir?: []);
Loading history...
37
	}
38
	
39
	final public static function module()
40
	{
41
		return SystemCore::class;
42
	}
43
	
44
	public function response()
45
	{
46
		return response($this->render());
47
	}
48
	
49
// 	public function getViewJS($actions)
50
// 	{
51
// 		$ready = new jsFunction($actions);
52
		
53
// 		return "<script page-pjax>\n".
54
// 				(new jQuery($ready))->jsRender().
55
// 				'</script>';
56
// 	}
57
	
58
	public function render()
59
	{	
60
		$this->module()::requireCSS('epesi.css');
61
		
62
		$this->addCsrfToken();
63
		
64
		$this->addFavIcon();
65
		
66
// 		$this->enablePjax();
67
		
68
		ob_start();
69
		
70
		$this->run();
71
		
72
		return ob_get_clean();
73
	}
74
	
75
	/**
76
	 * Initialize JS and CSS includes.
77
	 */
78
	public function initIncludes()
79
	{
80
// 		$this->requireJS(asset('js/app.js'));
81
// 		$this->requireCSS(asset('css/app.css'));
82
				
83
		//TODO: include below in app.js and app.css
84
		// jQuery
85
		$urlJs = $this->cdn['jquery']?? 'storage/system/js';
86
		$this->requireJS($urlJs.'/jquery.min.js');
87
		
88
		// Semantic UI
89
		$urlJs = $this->cdn['semantic-ui']?? 'storage/system/js';
90
		$urlCss = $this->cdn['semantic-ui']?? 'storage/system/css';
91
		$this->requireJS($urlJs.'/semantic.min.js');
92
		$this->requireCSS($urlCss.'/semantic.min.css');
93
		
94
		// Serialize Object
95
		$urlJs = $this->cdn['serialize-object']?? 'storage/system/js';
96
		$this->requireJS($urlJs.'/jquery.serialize-object.min.js');
97
		
98
		// Agile UI
99
		$urlJs = $this->cdn['atk']?? 'storage/system/js';
100
		$urlCss = $this->cdn['atk']?? 'storage/system/css';
101
		$this->requireJS($urlJs.'/atkjs-ui.min.js');
102
		$this->requireCSS($urlCss.'/agileui.css');
103
		
104
		// Draggable
105
		$urlJs = $this->cdn['draggable']?? 'storage/system/js';
106
		$this->requireJS($urlJs.'/draggable.bundle.js');
107
		
108
		// jQuery niceScroll	
109
		$urlJs = $this->cdn['jquery-nicescroll']?? 'storage/system/js';
110
		$this->requireJS($urlJs.'/jquery.nicescroll.js');
111
	}
112
	
113
	public function addCsrfToken()
114
	{
115
		$this->html->template->appendHTML('meta', $this->getTag('meta', ['name' => 'csrf-token', 'content' => csrf_token()]));
116
117
		$this->addJS('$.ajaxSetup({
118
			headers: {
119
				\'X-CSRF-TOKEN\': $(\'meta[name="csrf-token"]\').attr(\'content\')
120
			}
121
		})');
122
	}
123
	
124
	public function addFavIcon()
125
	{
126
		$this->html->template->appendHTML('HEAD', $this->getTag('link', ['rel' => 'shortcut icon', 'href' => config('epesi.app.favicon', url('favicon.png'))]));
0 ignored issues
show
Bug introduced by
array('rel' => 'shortcut...', url('favicon.png'))) of type array<string,mixed|string> is incompatible with the type string expected by parameter $attr of atk4\ui\App::getTag(). ( Ignorable by Annotation )

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

126
		$this->html->template->appendHTML('HEAD', $this->getTag('link', /** @scrutinizer ignore-type */ ['rel' => 'shortcut icon', 'href' => config('epesi.app.favicon', url('favicon.png'))]));
Loading history...
127
	}
128
	
129
	public function enablePjax()
130
	{
131
		// pjax library
132
// 		$this->requireJS('https://cdn.jsdelivr.net/npm/pjax/pjax.js');
133
		
134
// 		$this->html->template->appendHTML('HEAD', '<script>
135
// 			$(function(){
136
// 				var pjax = new Pjax({
137
// 						"elements": ".pjax", 
138
// 						"selectors": [".atk-layout", "head > script[page-pjax]", "head > title"]
139
// 				});
140
// 			});
141
			
142
// 		</script>');
143
		
144
		// pjax-api library
145
		$this->requireJS('https://cdn.jsdelivr.net/npm/pjax-api@latest');
146
147
		$this->html->template->appendHTML('HEAD', '<script>
148
149
			$(function(){		
150
				const { Pjax } = require("pjax-api");		
151
				new Pjax({
152
						"links": ".pjax", 
153
						"areas": [".atk-layout", "head > script[page-pjax]", "head > title"]
154
				});
155
			});
156
			
157
		</script>');
158
159
		// common
160
		$this->addJs('$(".pjax").click(function(e) {
161
				window.onbeforeunload(e);
162
				
163
				if (e.returnValue == "unsaved" && ! confirm("Unsaved data. Continue?")) {
164
 					e.stopImmediatePropagation();
165
					window.stop();
166
 				}
167
		});');		
168
	}
169
	
170
	public function addJs($js, $args = [])
171
	{
172
		$this->html->js(true, new jsExpression($js, $args));
173
	}
174
175
	/**
176
	 * Override default method to make sure js script is included only once
177
	 * 
178
	 * {@inheritDoc}
179
	 * @see \atk4\ui\App::requireJS()
180
	 */
181
	public function requireJS($url, $isAsync = false, $isDefer = false)
182
	{
183
		static $cache;
184
		
185
		$key = md5(serialize(func_get_args()));
186
		
187
		if (! isset($cache[$key])) {
188
			$cache[$key] = true;
189
			
190
			parent::requireJS($url, $isAsync, $isDefer);
191
		}
192
				
193
		return $this;
194
	}
195
		
196
	/**
197
	 * Set the breadcrumb location
198
	 * 
199
	 * @param array|string $location
200
	 * @return null
201
	 */
202
	public function setLocation($location)
203
	{
204
		return $this->layout->setLocation($location);
205
	}
206
	
207
	public function packageInfo()
208
	{
209
		$content = file_get_contents(__DIR__ . '/../composer.json');
210
		
211
		return json_decode($content, true);
212
	}
213
}