Test Failed
Push — master ( 4d2995...9287ab )
by Georgi
04:21
created

App::collectTemplates()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A App::response() 0 3 1
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\Modules\Concerns\HasLinks;
8
use Epesi\Core\System\SystemCore;
9
use Epesi\Core\System\Modules\ModuleManager;
10
use Illuminate\Session\TokenMismatchException;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13
class App extends BaseApp
14
{
15
	use HasLinks;
16
	
17
	public $version = '2.0.0-alpha1';
18
	
19
	public $cdn = [
20
			'semantic-ui'      => 'https://cdn.jsdelivr.net/npm/[email protected]/dist',
21
	];
22
	
23
	public $always_run = false;
24
	
25
	protected $url_building_ext = '';
26
	
27
	public function __construct($defaults = [])
28
	{
29
		parent::__construct([
30
				'title' => config('epesi.app.title', 'EPESI'),
31
		        'cdn' => array_merge($this->cdn, (array) config('epesi.app.cdn')),
32
				//TODO: set the skin from admin / user selection
33
		        'skin' => config('epesi.app.skin', $this->skin),
34
		        'template_dir' => array_merge(ModuleManager::collect('templates', $this->skin), (array) $this->template_dir)
35
		]);
36
	}
37
	
38
	final public static function module()
39
	{
40
		return SystemCore::class;
41
	}
42
	
43
	public function response()
44
	{
45
		return response($this->render());
46
	}
47
	
48
// 	public function getViewJS($actions)
49
// 	{
50
// 		$ready = new jsFunction($actions);
51
		
52
// 		return "<script page-pjax>\n".
53
// 				(new jQuery($ready))->jsRender().
54
// 				'</script>';
55
// 	}
56
	
57
	public function render()
58
	{	
59
		$this->module()::requireCSS('epesi.css');
60
		
61
		$this->addCsrfToken();
62
		
63
		$this->addFavIcon();
64
		
65
// 		$this->enablePjax();
66
		
67
		ob_start();
68
		
69
		$this->run();
70
		
71
		return ob_get_clean();
72
	}
73
	
74
	public function renderException($exception)
75
	{
76
	    ob_start();
77
	    if ($exception instanceof TokenMismatchException) {
78
	        $this->jsRedirectHomepage(__('Session expired! Redirecting to your home page ...'));
79
	    }
80
	    elseif ($exception instanceof NotFoundHttpException) {
81
	        $this->jsRedirectHomepage(__('Requested page not found! Redirecting to your home page ...'));
82
	    }	    
83
	    else {
84
	        $this->caughtException($exception);
85
	    }
86
	    
87
	    return ob_get_clean();
88
	}
89
	
90
	public function jsRedirectHomepage($message)
91
	{
92
	    $homepageUrl = url(\Epesi\Core\HomePage\Database\Models\HomePage::pathOfUser());
0 ignored issues
show
Bug introduced by
The type Epesi\Core\HomePage\Database\Models\HomePage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
	    
94
	    $redirectJs = $this->jsRedirectConfirm($homepageUrl, $message)->jsRender();
95
	    
96
	    if ($this->isJsonRequest()) {
97
	        $this->outputResponseJSON([
98
	                'success'   => true,
99
	                'message'   => $message,
100
	                'atkjs'   => $redirectJs
101
	        ]);
102
	    }
103
	    else {
104
	        $this->outputResponseHTML('<script>' . $redirectJs . '</script>');
105
	    }
106
	}
107
	
108
	public function jsRedirectConfirm($page, $message)
109
	{
110
	    $redirectJs = $this->jsRedirect($page)->jsRender();
111
	    
112
	    return new jsExpression("if (confirm([])) { $redirectJs }", [$message]);
113
	}
114
	
115
	/**
116
	 * Initialize JS and CSS includes.
117
	 */
118
	public function initIncludes()
119
	{
120
// 		$this->requireJS(asset('js/app.js'));
121
// 		$this->requireCSS(asset('css/app.css'));
122
				
123
		//TODO: include below in app.js and app.css
124
		
125
		$localJs = url('storage/system/js');
126
		$localCss = url('storage/system/css');
127
		
128
		// jQuery
129
		$urlJs = $this->cdn['jquery']?? $localJs;
130
		$this->requireJS($urlJs.'/jquery.min.js');
131
		
132
		// Semantic UI
133
		$urlJs = $this->cdn['semantic-ui']?? $localJs;
134
		$urlCss = $this->cdn['semantic-ui']?? $localCss;
135
		$this->requireJS($urlJs.'/semantic.min.js');
136
		$this->requireCSS($urlCss.'/semantic.min.css');
137
		
138
		// Serialize Object
139
		$urlJs = $this->cdn['serialize-object']?? $localJs;
140
		$this->requireJS($urlJs.'/jquery.serialize-object.min.js');
141
		
142
		// Agile UI
143
		$urlJs = $this->cdn['atk']?? $localJs;
144
		$urlCss = $this->cdn['atk']?? $localCss;
145
		$this->requireJS($urlJs.'/atkjs-ui.min.js');
146
		$this->requireCSS($urlCss.'/agileui.css');
147
		
148
		// Draggable
149
		$urlJs = $this->cdn['draggable']?? $localJs;
150
		$this->requireJS($urlJs.'/draggable.bundle.js');
151
		
152
		// jQuery niceScroll	
153
		$urlJs = $this->cdn['jquery-nicescroll']?? $localJs;
154
		$this->requireJS($urlJs.'/jquery.nicescroll.js');
155
		
156
		// clipboard.js
157
		$urlJs = $this->cdn['clipboardjs']?? $localJs;
158
		$this->requireJS($urlJs.'/clipboard.js');
159
	}
160
	
161
	public function addCsrfToken()
162
	{
163
		$this->html->template->appendHTML('meta', $this->getTag('meta', ['name' => 'csrf-token', 'content' => csrf_token()]));
164
165
		$this->addJS('$.ajaxSetup({
166
			headers: {
167
				\'X-CSRF-TOKEN\': $(\'meta[name="csrf-token"]\').attr(\'content\')
168
			}
169
		})');
170
	}
171
	
172
	public function addFavIcon()
173
	{
174
		$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

174
		$this->html->template->appendHTML('HEAD', $this->getTag('link', /** @scrutinizer ignore-type */ ['rel' => 'shortcut icon', 'href' => config('epesi.app.favicon', url('favicon.png'))]));
Loading history...
175
	}
176
	
177
	public function enablePjax()
178
	{
179
		// pjax library
180
// 		$this->requireJS('https://cdn.jsdelivr.net/npm/pjax/pjax.js');
181
		
182
// 		$this->html->template->appendHTML('HEAD', '<script>
183
// 			$(function(){
184
// 				var pjax = new Pjax({
185
// 						"elements": ".pjax", 
186
// 						"selectors": [".atk-layout", "head > script[page-pjax]", "head > title"]
187
// 				});
188
// 			});
189
			
190
// 		</script>');
191
		
192
		// pjax-api library
193
		$this->requireJS('https://cdn.jsdelivr.net/npm/pjax-api@latest');
194
195
		$this->html->template->appendHTML('HEAD', '<script>
196
197
			$(function(){		
198
				const { Pjax } = require("pjax-api");		
199
				new Pjax({
200
						"links": ".pjax", 
201
						"areas": [".atk-layout", "head > script[page-pjax]", "head > title"]
202
				});
203
			});
204
			
205
		</script>');
206
207
		// common
208
		$this->addJs('$(".pjax").click(function(e) {
209
				window.onbeforeunload(e);
210
				
211
				if (e.returnValue == "unsaved" && ! confirm("Unsaved data. Continue?")) {
212
 					e.stopImmediatePropagation();
213
					window.stop();
214
 				}
215
		});');		
216
	}
217
	
218
	public function addJs($js, $args = [])
219
	{
220
		$this->html->js(true, new jsExpression($js, $args));
221
	}
222
223
	/**
224
	 * Override default method to make sure js script is included only once
225
	 * 
226
	 * {@inheritDoc}
227
	 * @see \atk4\ui\App::requireJS()
228
	 */
229
	public function requireJS($url, $isAsync = false, $isDefer = false)
230
	{
231
		static $cache;
232
		
233
		$key = md5(serialize(func_get_args()));
234
		
235
		if (! isset($cache[$key])) {
236
			$cache[$key] = true;
237
			
238
			parent::requireJS($url, $isAsync, $isDefer);
239
		}
240
				
241
		return $this;
242
	}
243
		
244
	/**
245
	 * Set the breadcrumb location
246
	 * 
247
	 * @param array|string $location
248
	 * @return null
249
	 */
250
	public function setLocation($location)
251
	{
252
		return $this->layout->setLocation($location);
0 ignored issues
show
Bug introduced by
The method setLocation() does not exist on atk4\ui\Layout\Generic. ( Ignorable by Annotation )

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

252
		return $this->layout->/** @scrutinizer ignore-call */ setLocation($location);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
253
	}
254
	
255
	public function packageInfo()
256
	{
257
		$content = file_get_contents(__DIR__ . '/../composer.json');
258
		
259
		return json_decode($content, true);
260
	}
261
}