Passed
Push — master ( c62a09...4c7916 )
by Georgi
06:55
created

CommonDataSettings::getNodeForm()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 23
rs 9.8666
1
<?php
2
3
namespace Epesi\Base\CommonData;
4
5
use Epesi\Core\System\Modules\ModuleView;
0 ignored issues
show
Bug introduced by
The type Epesi\Core\System\Modules\ModuleView 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...
6
use Illuminate\Support\Facades\Auth;
7
use Epesi\Core\Layout\Seeds\ActionBar;
8
use atk4\ui\jsExpression;
9
use atk4\core\SessionTrait;
10
11
class CommonDataSettings extends ModuleView
12
{
13
    use SessionTrait;
14
    
15
	protected $label = 'Common Data';
16
	
17
	protected $ancestors;
18
19
	protected $grid;
20
	
21
	public static function access()
22
	{
23
		return Auth::user()->can('modify system settings');
24
	}
25
	
26
	public function body()
27
	{
28
		ActionBar::addItemButton('back')->link(url('view/system'));
0 ignored issues
show
Bug introduced by
The method addItemButton() does not exist on Epesi\Core\Layout\Seeds\ActionBar. ( Ignorable by Annotation )

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

28
		ActionBar::/** @scrutinizer ignore-call */ 
29
             addItemButton('back')->link(url('view/system'));

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...
29
		
30
		$this->setAncestors();
31
		
32
		$this->setLocation();
33
		
34
		$this->displayGrid();
35
	}
36
	
37
	public function setAncestors()
38
	{
39
	    $parent = $this->stickyGet('parent');
40
	    
41
	    if (isset($parent)) {
42
	        $this->memorize('parent', $parent?: null);
43
	    }
44
45
	    $this->ancestors = Models\CommonData::ancestorsAndSelf($this->recall('parent'));
46
	}
47
	
48
	public function setLocation()
49
	{
50
	    $location = [['label' => $this->label, 'link' => '?parent=0']];	    
51
		foreach (Models\CommonData::create()->withID($this->ancestors) as $node) {
52
		    $location[] = [
53
		            'label' => $node['value'] ?: $node['key'], 
54
		            'link' => '?parent=' . $node['id']
55
		    ];
56
		}
57
58
		$this->label = null;
59
		
60
		$this->location($location);
61
		
62
		return $this;
63
	}
64
	
65
	public function displayGrid()
66
	{		
67
		$this->grid = $this->add([
68
				'CRUD',
69
				'editFields' => ['key', 'value'],
70
				'addFields' => ['key', 'value'],
71
				'displayFields' => ['key', 'value', 'readonly'],
72
		        'notifyDefault' => ['jsToast', 'settings' => ['message' => __('Data is saved!'), 'class' => 'success']],
73
				'paginator' => false,
74
		        'menu' => ActionBar::instance(),
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Epesi\Core\Layout\Seeds\ActionBar. ( Ignorable by Annotation )

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

74
		        'menu' => ActionBar::/** @scrutinizer ignore-call */ instance(),

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...
75
		        'quickSearch' => ['key', 'value']
76
		]);
77
78
		$this->grid->setModel($this->nodes()->setOrder('position'));
79
		
80
		$this->grid->addActionButton(['icon' => 'level down', 'attr' => ['title' => __('Drilldown')]], new jsExpression(
81
		        'document.location=\'?parent=\'+[]',
82
		        [$this->grid->jsRow()->data('id')]
83
		));
84
85
		$this->grid->addDragHandler()->onReorder(function ($order) {
86
			$result = true;
87
			foreach ($this->nodes() as $node) {
88
				$result &= $node->save(['position' => array_search($node->id, $order)]);
89
			}
90
			
91
			$notifier = $result? $this->notifySuccess(__('Items reordered!')): $this->notifyError(__('Error saving order!'));
92
			
93
			return $this->grid->jsSave($notifier);
94
		});
95
96
		if ($this->ancestors && $this->grid->menu) {
97
		    $this->grid->menu->addItem([__('Level Up'), 'icon' => 'level up'], '?parent=' . $this->parent(1));
98
		}
99
		
100
		return $this;
101
	}
102
	
103
	public function parent($level = 0)
104
	{
105
	    return $this->ancestors[$level] ?? 0;
106
	}
107
	
108
	public function nodes()
109
	{
110
	    $nodes = Models\CommonData::create();
111
	    
112
	    //@TODO: remove below when adding null condition fixed
113
	    if ($parent = $this->parent()) {
114
	        $nodes->addCondition('parent', $parent);
115
	    }
116
	    else {
117
	        $nodes->addCondition($nodes->expr('parent is NULL'));
118
	    }
119
120
	    $nodes->addHook('beforeInsert', function($node, & $data) {
121
	        $data['parent'] = $this->recall('parent');
122
	    });
123
	        
124
	    $nodes->getAction('edit')->enabled = function($row = null) {
125
	        return $row ? !$row['readonly'] : true;
126
	    };
127
	    
128
	    return $nodes;
129
	}
130
}
131