CommonDataSettings::setAncestors()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
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\View\ActionBar;
0 ignored issues
show
Bug introduced by
The type Epesi\Core\Layout\View\ActionBar 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...
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'));
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(),
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] ?? null;
106
	}
107
	
108
	public function nodes()
109
	{
110
	    $nodes = Models\CommonData::create();
111
112
	    $nodes->addCondition('parent', $this->parent());
113
114
	    $nodes->addHook('beforeInsert', function($node, & $data) {
115
	        $data['parent'] = $this->recall('parent');
116
	    });
117
	        
118
	    $nodes->getAction('edit')->enabled = function($row = null) {
119
	        return $row ? !$row['readonly'] : true;
120
	    };
121
	    
122
	    return $nodes;
123
	}
124
}
125