Passed
Push — developer ( 44b1b4...e20091 )
by Mariusz
167:14 queued 131:59
created

View::convertScripts()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 5
nop 2
1
<?php
2
/**
3
 * Controller class for views.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Tomasz Kur <[email protected]>
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 */
12
13
namespace App\Controller;
14
15
/**
16
 * Controller class for views.
17
 */
18
abstract class View extends Base
0 ignored issues
show
Coding Style introduced by
View does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
19
{
20
	/** @var \App\Viewer Viewer object. */
21
	protected $viewer;
22
23
	/** {@inheritdoc} */
24
	public function __construct(\App\Request $request)
25
	{
26
		parent::__construct($request);
27
		$this->viewer = new \App\Viewer();
28
		$this->viewer->assign('MODULE_NAME', $this->getModuleNameFromRequest($this->request));
0 ignored issues
show
Unused Code introduced by
The call to View::getModuleNameFromRequest() has too many arguments starting with $this->request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
29
		$this->viewer->assign('VIEW', $this->request->getByType('view', \App\Purifier::ALNUM));
30
		$this->viewer->assign('LANGUAGE', \App\Language::getLanguage());
31
		$this->viewer->assign('LANG', \App\Language::getShortLanguageName());
32
		$this->viewer->assign('USER', \App\User::getUser());
33
		$this->viewer->assign('ACTION_NAME', $this->request->getAction());
34
	}
35
36
	/**
37
	 * Check permission.
38
	 *
39
	 * @throws \App\Exceptions\NoPermitted
40
	 *
41
	 * @return void
42
	 */
43
	public function checkPermission(): void
44
	{
45
		$this->getModuleNameFromRequest($this->request);
0 ignored issues
show
Unused Code introduced by
The call to View::getModuleNameFromRequest() has too many arguments starting with $this->request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
		if (!\App\User::getUser()->isPermitted($this->moduleName)) {
47
			throw new \App\Exceptions\AppException('ERR_MODULE_PERMISSION_DENIED');
48
		}
49
	}
50
51
	/** {@inheritdoc} */
52
	public function preProcess($display = true): void
53
	{
54
		if ($this->loginRequired()) {
55
			$this->viewer->assign('MENU', \YF\Modules\Base\Model\Menu::getInstance($this->viewer->getTemplateVars('MODULE_NAME'))->getMenu());
56
		}
57
		$this->viewer->assign('PAGE_TITLE', (\Conf\Config::$siteName ?: \App\Language::translate('LBL_CUSTOMER_PORTAL')) . ' ' . $this->getPageTitle());
58
		$this->viewer->assign('CSS_FILE', $this->getHeaderCss());
59
		if ($display) {
60
			$this->preProcessDisplay();
61
		}
62
	}
63
64
	/**
65
	 * Get page title.
66
	 *
67
	 * @return string
68
	 */
69
	public function getPageTitle(): string
70
	{
71
		$title = \App\Language::translateModule($this->moduleName);
72
		$pageTitle = $this->getBreadcrumbTitle();
73
		if (isset($this->pageTitle)) {
74
			$pageTitle = \App\Language::translate($this->pageTitle);
0 ignored issues
show
Bug introduced by
The property pageTitle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
		}
76
		if ($pageTitle) {
77
			$title .= ' - ' . $pageTitle;
78
		}
79
		return $title;
80
	}
81
82
	/**
83
	 * Get breadcrumb title.
84
	 *
85
	 * @return string
86
	 */
87
	public function getBreadcrumbTitle(): string
88
	{
89
		if (!empty($this->pageTitle)) {
90
			return $this->pageTitle;
91
		}
92
		return '';
93
	}
94
95
	/**
96
	 * Convert scripts path.
97
	 *
98
	 * @param array  $files
99
	 * @param string $fileExtension
100
	 *
101
	 * @return array
102
	 */
103
	public function convertScripts(array $files, string $fileExtension): array
104
	{
105
		$scriptsInstances = [];
106
		foreach ($files as $file) {
107
			[$fileName, $isOptional] = array_pad($file, 2, false);
0 ignored issues
show
Bug introduced by
The variable $isOptional does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $fileName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
108
			$path = ROOT_DIRECTORY . "/public_html/$fileName";
109
			$script = new \App\Script();
110
			$script->set('type', $fileExtension);
111
			$minFilePath = str_replace('.' . $fileExtension, '.min.' . $fileExtension, $fileName);
112
			if (\App\Config::$minScripts && file_exists(ROOT_DIRECTORY . "/public_html/$minFilePath")) {
113
				$scriptsInstances[] = $script->set('src', PUBLIC_DIRECTORY . $minFilePath . self::getTime($path));
114
			} elseif (file_exists($path)) {
115
				$scriptsInstances[] = $script->set('src', PUBLIC_DIRECTORY . $fileName . self::getTime($path));
116
			} elseif ($isOptional) {
117
				\App\Log::info('File not found: ' . $path);
118
			} else {
119
				\App\Log::warning('File not found: ' . $path);
120
			}
121
		}
122
		return $scriptsInstances;
123
	}
124
125
	/**
126
	 * Gets file modification time.
127
	 *
128
	 * @param string $path
129
	 *
130
	 * @return string
131
	 */
132
	public static function getTime(string $path): string
133
	{
134
		$url = '';
135
		if ($fs = filemtime($path)) {
136
			$url = '?s=' . $fs;
137
		}
138
		return $url;
139
	}
140
141
	/**
142
	 * Retrieves css styles that need to loaded in the page.
143
	 *
144
	 * @return \App\Script[]
145
	 */
146
	public function getHeaderCss(): array
147
	{
148
		return $this->convertScripts([
149
			['libraries/@fortawesome/fontawesome-free/css/all.css'],
150
			['libraries/@mdi/font/css/materialdesignicons.css'],
151
			['libraries/bootstrap/dist/css/bootstrap.css'],
152
			['libraries/chosen-js/chosen.css'],
153
			['libraries/bootstrap-chosen/bootstrap-chosen.css'],
154
			['libraries/jQuery-Validation-Engine/css/validationEngine.jquery.css'],
155
			['libraries/select2/dist/css/select2.css'],
156
			['libraries/select2-theme-bootstrap4/dist/select2-bootstrap.css'],
157
			['libraries/datatables.net-bs4/css/dataTables.bootstrap4.css'],
158
			['libraries/datatables.net-responsive-bs4/css/responsive.bootstrap4.css'],
159
			['libraries/bootstrap-datepicker/dist/css/bootstrap-datepicker3.css'],
160
			['libraries/bootstrap-daterangepicker/daterangepicker.css'],
161
			['libraries/clockpicker/dist/bootstrap4-clockpicker.css'],
162
			['libraries/jstree-bootstrap-theme/dist/themes/proton/style.css'],
163
			['libraries/jstree/dist/themes/default/style.css'],
164
			['libraries/@pnotify/core/dist/PNotify.css'],
165
			['libraries/@pnotify/confirm/dist/PNotifyConfirm.css'],
166
			['libraries/@pnotify/bootstrap4/dist/PNotifyBootstrap4.css'],
167
			['libraries/@pnotify/mobile/dist/PNotifyMobile.css'],
168
			['libraries/@pnotify/desktop/dist/PNotifyDesktop.css'],
169
			['layouts/resources/icons/additionalIcons.css'],
170
			['layouts/resources/icons/yfm.css'],
171
			['layouts/resources/icons/yfi.css'],
172
			['layouts/' . \App\Viewer::getLayoutName() . '/skins/basic/Main.css'],
173
		], 'css');
174
	}
175
176
	/**
177
	 * Pre process display.
178
	 *
179
	 * @return void
180
	 */
181
	protected function preProcessDisplay(): void
182
	{
183
		if (\App\Session::has('systemError')) {
184
			$this->viewer->assign('ERRORS', \App\Session::get('systemError'));
185
			\App\Session::unset('systemError');
186
		}
187
		$this->viewer->view($this->preProcessTplName(), $this->moduleName);
188
	}
189
190
	/**
191
	 * Get preprocess tpl name.
192
	 *
193
	 * @return string
194
	 */
195
	protected function preProcessTplName(): string
196
	{
197
		return 'Header.tpl';
198
	}
199
200
	/**
201
	 * Get process tpl name.
202
	 *
203
	 * @return string
204
	 */
205
	protected function processTplName(): string
206
	{
207
		return $this->request->getAction() . '/Index.tpl';
208
	}
209
210
	/** {@inheritdoc}  */
211
	protected function postProcessTplName(): string
212
	{
213
		return 'Footer.tpl';
214
	}
215
216
	/** {@inheritdoc} */
217
	public function postProcess(): void
218
	{
219
		if (null === $this->viewer->getTemplateVars('SHOW_FOOTER_BAR')) {
220
			$this->viewer->assign('SHOW_FOOTER_BAR', true);
221
		}
222
		$this->viewer->assign('JS_FILE', $this->getFooterScripts());
223
		if (\App\Config::$debugApi && \App\Session::has('debugApi') && \App\Session::get('debugApi')) {
224
			$this->viewer->assign('DEBUG_API', \App\Session::get('debugApi'));
225
			$this->viewer->view('DebugApi.tpl', $this->moduleName);
226
			\App\Session::set('debugApi', false);
227
		}
228
		$this->viewer->view($this->postProcessTplName(), $this->moduleName);
229
	}
230
231
	/**
232
	 * Scripts.
233
	 *
234
	 * @return \App\Script[]
235
	 */
236
	public function getFooterScripts(): array
237
	{
238
		$moduleName = $this->getModuleNameFromRequest();
239
		$action = $this->request->getAction();
240
		$languageHandlerShortName = \App\Language::getShortLanguageName();
241
		$fileName = ["libraries/jQuery-Validation-Engine/js/languages/jquery.validationEngine-$languageHandlerShortName.js"];
242
		if (!file_exists(PUBLIC_DIRECTORY . $fileName[0])) {
243
			$fileName = ['libraries/jQuery-Validation-Engine/js/languages/jquery.validationEngine-en.js'];
244
		}
245
		return $this->convertScripts([
246
			['libraries/jquery/dist/jquery.js'],
247
			['libraries/jquery.class.js/jquery.class.js'],
248
			['libraries/block-ui/jquery.blockUI.js'],
249
			['libraries/@pnotify/core/dist/PNotify.js'],
250
			['libraries/@pnotify/mobile/dist/PNotifyMobile.js'],
251
			['libraries/@pnotify/desktop/dist/PNotifyDesktop.js'],
252
			['libraries/@pnotify/confirm/dist/PNotifyConfirm.js'],
253
			['libraries/@pnotify/bootstrap4/dist/PNotifyBootstrap4.js'],
254
			['libraries/@pnotify/font-awesome5/dist/PNotifyFontAwesome5.js'],
255
			['libraries/popper.js/dist/umd/popper.js'],
256
			['libraries/bootstrap/dist/js/bootstrap.js'],
257
			['libraries/bootstrap-datepicker/dist/js/bootstrap-datepicker.js'],
258
			['libraries/bootstrap-daterangepicker/daterangepicker.js'],
259
			['libraries/bootbox/dist/bootbox.min.js'],
260
			['libraries/chosen-js/chosen.jquery.js'],
261
			['libraries/select2/dist/js/select2.full.js'],
262
			['libraries/moment/min/moment.min.js'],
263
			['libraries/inputmask/dist/jquery.inputmask.js'],
264
			['libraries/datatables.net/js/jquery.dataTables.js'],
265
			['libraries/datatables.net-bs4/js/dataTables.bootstrap4.js'],
266
			['libraries/datatables.net-responsive/js/dataTables.responsive.js'],
267
			['libraries/datatables.net-responsive-bs4/js/responsive.bootstrap4.js'],
268
			['libraries/jQuery-Validation-Engine/js/jquery.validationEngine.js'],
269
			$fileName,
270
			['libraries/jstree/dist/jstree.js'],
271
			['libraries/clockpicker/dist/bootstrap4-clockpicker.js'],
272
			['layouts/resources/validator/BaseValidator.js'],
273
			['layouts/resources/validator/FieldValidator.js'],
274
			['layouts/resources/helper.js'],
275
			['layouts/resources/Field.js'],
276
			['layouts/resources/Connector.js'],
277
			['layouts/resources/app.js'],
278
			['layouts/resources/Fields.js'],
279
			['layouts/resources/ProgressIndicator.js'],
280
			['layouts/' . \App\Viewer::getLayoutName() . '/modules/Base/resources/Header.js'],
281
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/Base/resources/{$action}.js"],
282
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/{$moduleName}/resources/{$action}.js", true],
283
		], 'js');
284
	}
285
286
	/** {@inheritdoc} */
287
	public function validateRequest()
288
	{
289
		$this->request->validateReadAccess();
290
	}
291
292
	/** {@inheritdoc} */
293
	public function postProcessAjax()
294
	{
295
	}
296
297
	/** {@inheritdoc} */
298
	public function preProcessAjax()
299
	{
300
	}
301
302
	/**
303
	 * Get module name from request.
304
	 *
305
	 * @return string
306
	 */
307
	private function getModuleNameFromRequest(): string
308
	{
309
		if (empty($this->moduleName)) {
310
			$this->moduleName = $this->request->getModule();
311
		}
312
		return $this->moduleName;
313
	}
314
}
315