1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @name OpenImporter |
4
|
|
|
* @copyright OpenImporter contributors |
5
|
|
|
* @license BSD https://opensource.org/licenses/BSD-3-Clause |
6
|
|
|
* |
7
|
|
|
* @version 1.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace OpenImporter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Just a way to collect a bunch of stuff to be used to build a form. |
14
|
|
|
* |
15
|
|
|
* @class Form |
16
|
|
|
*/ |
17
|
|
|
class Form |
18
|
|
|
{ |
19
|
|
|
/** @var string (via magic see Template) */ |
20
|
|
|
public $title; |
21
|
|
|
|
22
|
|
|
/** @var string (via magic see Template) */ |
23
|
|
|
public $description; |
24
|
|
|
|
25
|
|
|
/** @var array (via magic see Template) */ |
26
|
|
|
public $submit; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
protected $data = array(); |
30
|
|
|
|
31
|
|
|
/** @var null */ |
32
|
|
|
protected $lng; |
33
|
|
|
|
34
|
|
|
public function __construct($lng) |
35
|
|
|
{ |
36
|
|
|
$this->lng = $lng; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __set($key, $val) |
40
|
|
|
{ |
41
|
|
|
if ($key === 'options') |
42
|
|
|
{ |
43
|
|
|
throw new FormException('Use Form::addOptions or Form::addField to set new fields'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->data[$key] = $val; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function __get($key) |
50
|
|
|
{ |
51
|
|
|
return $this->data[$key] ?? null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function addOption($field) |
55
|
|
|
{ |
56
|
|
|
switch ($field['type']) |
57
|
|
|
{ |
58
|
|
|
case 'text': |
59
|
|
|
$this->data['options'][] = array( |
60
|
|
|
'id' => $field['id'], |
61
|
|
|
'label' => $this->lng->get($field['label']), |
62
|
|
|
'value' => $field['default'] ?? '', |
63
|
|
|
'correct' => isset($field['correct']) ? $this->lng->get($field['correct']) : '', |
64
|
|
|
'validate' => !empty($field['validate']), |
65
|
|
|
'type' => 'text', |
66
|
|
|
); |
67
|
|
|
break; |
68
|
|
|
case 'password': |
69
|
|
|
$this->data['options'][] = array( |
70
|
|
|
'id' => $field['id'], |
71
|
|
|
'label' => $this->lng->get($field['label']), |
72
|
|
|
'correct' => $this->lng->get($field['correct']), |
73
|
|
|
'type' => 'password', |
74
|
|
|
); |
75
|
|
|
break; |
76
|
|
|
case 'steps': |
77
|
|
|
$this->data['options'][] = array( |
78
|
|
|
'id' => $field['id'], |
79
|
|
|
'label' => $this->lng->get($field['label']), |
80
|
|
|
'value' => $field['default'], |
81
|
|
|
'type' => 'steps', |
82
|
|
|
); |
83
|
|
|
break; |
84
|
|
|
default: |
85
|
|
|
$this->data['options'][] = array( |
86
|
|
|
'id' => $field['id'], |
87
|
|
|
'label' => $this->lng->get($field['label']), |
88
|
|
|
'value' => 1, |
89
|
|
|
'attributes' => $field['checked'] == 'checked' ? ' checked="checked"' : '', |
90
|
|
|
'type' => 'checkbox', |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function addSeparator() |
96
|
|
|
{ |
97
|
|
|
$this->data['options'][] = array(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function addField($field) |
101
|
|
|
{ |
102
|
|
|
if (is_object($field)) |
103
|
|
|
{ |
104
|
|
|
return $this->addField($this->makeFieldArray($field)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$field['id'] = 'field' . $field['id']; |
108
|
|
|
|
109
|
|
|
return $this->addOption($field); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function makeFieldArray($field) |
113
|
|
|
{ |
114
|
|
|
if ($field->attributes()->{'type'} === 'text') |
115
|
|
|
{ |
116
|
|
|
return array( |
117
|
|
|
'id' => $field->attributes()->{'id'}, |
118
|
|
|
'label' => $field->attributes()->{'label'}, |
119
|
|
|
'default' => $field->attributes()->{'default'} ?? '', |
120
|
|
|
'type' => 'text', |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return array( |
125
|
|
|
'id' => $field->attributes()->{'id'}, |
126
|
|
|
'label' => $field->attributes()->{'label'}, |
127
|
|
|
'checked' => $field->attributes()->{'checked'}, |
128
|
|
|
'type' => 'checkbox', |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|