Passed
Push — developer ( 57de19...8207c1 )
by Mariusz
35:57
created

EditView::checkPermission()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
/**
3
 * Edit view class.
4
 *
5
 * @package View
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace YF\Modules\Base\View;
14
15
class EditView extends \App\Controller\View
16
{
17
	/** {@inheritdoc} */
18 View Code Duplication
	public function checkPermission(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
	{
20
		parent::checkPermission($this->request);
0 ignored issues
show
Unused Code introduced by
The call to View::checkPermission() 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...
21
		$actionName = 'EditView';
22
		if ($this->request->isEmpty('record')) {
23
			$actionName = 'CreateView';
24
		}
25
		if (!\YF\Modules\Base\Model\Module::isPermitted($this->request->getModule(), $actionName)) {
26
			throw new \App\Exceptions\AppException('ERR_MODULE_PERMISSION_DENIED');
27
		}
28
	}
29
30
	/** {@inheritdoc} */
31
	public function process()
32
	{
33
		$moduleName = $this->request->getModule();
34
		if ($this->request->isEmpty('record')) {
35
			$recordModel = \YF\Modules\Base\Model\Record::getInstance($moduleName);
36
		} else {
37
			$recordModel = \YF\Modules\Base\Model\Record::getInstanceById($moduleName, $this->request->getInteger('record'), ['x-raw-data' => 1]);
38
		}
39
		$moduleStructure = $recordModel->getModuleModel()->getFieldsFromApi();
40
		$data = $recordModel->getData();
41
		$rawData = $recordModel->getRawData();
42
		$fields = [];
43
		foreach ($moduleStructure['fields'] as $field) {
44
			$fieldName = $field['name'];
45
			if ($field['isEditable']) {
46
				$fieldInstance = \YF\Modules\Base\Model\Field::getInstance($moduleName, $field);
47
				if (isset($data[$fieldName])) {
48
					$fieldInstance->setDisplayValue($data[$fieldName]);
49
					if (isset($rawData[$fieldName])) {
50
						$fieldInstance->setRawValue($rawData[$fieldName]);
51
					}
52
				} elseif (!empty($field['referenceList']) && 'Accounts' === current($field['referenceList'])) {
53
					$fieldInstance->setDisplayValue(\App\User::getUser()->get('parentName'));
54
					$fieldInstance->setRawValue(\App\User::getUser()->get('companyId'));
55
				} else {
56
					$fieldInstance->setIsNewRecord();
57
				}
58
				$fields[$field['blockId']][] = $fieldInstance;
59
			}
60
		}
61
		$this->viewer->assign('RECORD', $recordModel);
62
		$this->viewer->assign('FIELDS', $fields);
63
		$this->viewer->assign('BREADCRUMB_TITLE', $recordModel->getName());
64
		$this->viewer->assign('BLOCKS', $moduleStructure['blocks']);
65
		$this->viewer->view('Edit/EditView.tpl', $moduleName);
66
	}
67
68
	/** {@inheritdoc} */
69
	public function getFooterScripts(bool $loadForModule = true): array
70
	{
71
		return array_merge(
72
			parent::getFooterScripts(),
73
			$this->convertScripts([
74
				['layouts/' . \App\Viewer::getLayoutName() . '/modules/Base/resources/EditView.js'],
75
			], 'js')
76
		);
77
	}
78
}
79