Passed
Push — master ( 3c4a6d...7cfef9 )
by Georgi
02:49
created

Form   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 68
c 1
b 0
f 0
dl 0
loc 144
rs 10
wmc 29

9 Methods

Rating   Name   Duplication   Size   Complexity  
B addElements() 0 58 11
A getValues() 0 3 1
A confirmLeave() 0 5 1
A setValues() 0 9 4
A addGroupDisplayRules() 0 6 1
A addFieldsDisplayRules() 0 2 2
A validate() 0 24 6
A addFieldRules() 0 9 2
A submit() 0 3 1
1
<?php
2
3
namespace Epesi\Core\UI\Seeds;
4
5
use atk4\ui\Form as BaseForm;
6
use atk4\ui\jsExpression;
7
use atk4\ui\jsFunction;
8
use Epesi\Core\UI\Traits\Notifies;
9
10
class Form extends BaseForm
11
{
12
	use Notifies;
13
	
14
	public $buttonSave = null;
15
	
16
	protected $fieldRules = [];
17
	protected $validationRules = [];
18
	
19
	public function addElements($elements, $parent = null) {
20
		$parent = $parent?: $this;
21
		
22
		foreach ($elements as $name => $desc) {
23
			$name = $desc['name']?? $name;
24
			
25
			$this->addFieldRules($name, $desc['rules']?? []);
26
			
27
			switch ($desc['type']?? 'field') {
28
				case 'field':
29
					$desc = is_string($desc)? [
30
					'decorator' => [$desc]
31
					]: $desc;
32
					
33
					$field = $parent->addField($name, $desc['decorator']?? [], $desc['options']?? []);
34
					
35
					if ($default = $desc['default']) {
36
						$field->set($default);
37
					}
38
					
39
					if ($desc['display']?? false) {
40
						$this->addFieldsDisplayRules([$name => $desc['display']]);
41
					}
42
					break;
43
					
44
				case 'group':
45
					$seed = $desc['seed']?? [$name];
46
					
47
					$group = $parent->addGroup($seed);
48
					
49
					$this->addElements($desc['elements'], $group);
50
					
51
					if ($desc['display']?? false) {
52
						$this->addGroupDisplayRules([$name => $desc['display']]);
53
					}
54
					break;
55
					
56
				case 'header':
57
					$seed = $desc['seed']?? [$name];
58
					
59
					$parent->addHeader($seed);
1 ignored issue
show
Bug introduced by
It seems like $seed can also be of type array; however, parameter $title of atk4\ui\Form::addHeader() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
					$parent->addHeader(/** @scrutinizer ignore-type */ $seed);
Loading history...
60
					break;
61
					
62
				case 'view':
63
					$seed = $desc['seed']?? ['Label', $name];
64
					
65
					$region = $desc['region']?? null;
66
					
67
					$this->add($seed, $region);
68
					break;
69
					
70
				default:
71
					;
72
					break;
73
			}
74
		}
75
		
76
		return $this;
77
	}
78
	
79
	public function addFieldsDisplayRules($fieldsDisplayRules) {
80
		$this->setFieldsDisplayRules(array_merge($this->fieldsDisplayRules?: [], $fieldsDisplayRules));
81
	}
82
	
83
	public function addGroupDisplayRules($groupDisplayRules) {
84
		$fieldsDisplayRules = $this->fieldsDisplayRules;
85
		
86
		$this->setGroupDisplayRules($groupDisplayRules);
87
		
88
		$this->addFieldsDisplayRules($fieldsDisplayRules);
89
	}
90
	
91
	public function addFieldRules($field, $rules = []) {
92
		if (! $rules) return;
93
		
94
		$this->fieldRules[$field] = $rules['rules']?? [
95
				'identifier' => $field,
96
				'rules' => $rules
97
		];
98
		
99
		return $this;
100
	}
101
	
102
	public function setValues($values)
103
	{
104
		foreach ($values?: [] as $name => $value) {
105
			if (! $field = $this->fields[$name]?? null) continue;
106
			
107
			$field->set($value);
108
		}
109
		
110
		return $this;
111
	}
112
	
113
	public function getValues()
114
	{
115
		return $this->model->get();
116
	}
117
	
118
	public function validate($callback)
119
	{
120
		$this->setApiConfig([
121
				'beforeSend' => new jsFunction(['settings'], [new jsExpression('return $(this).form("is valid")')]),
122
		]);
123
		
124
		$this->setFormConfig([
125
				'fields' => $this->fieldRules
126
		]);
127
		
128
		$this->onSubmit(function ($form) use ($callback) {
129
			$errors = [];
130
			foreach ($this->validationRules?: [] as $ruleCallback) {
131
				if (! is_callable($ruleCallback)) continue;
132
				
133
				$ruleErrors = $ruleCallback($form);
134
				
135
				$errors = array_merge($errors, $ruleErrors?: []);
136
			}
137
			
138
			return $errors?: $callback($this);
139
		});
140
			
141
			return $this;
142
	}
143
	
144
	public function submit()
145
	{
146
		return $this->js()->form('submit');
147
	}
148
	
149
	public function confirmLeave($confirm = true)
150
	{
151
		$this->canLeave = ! $confirm;
152
		
153
		return $this;
154
	}
155
}