|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Form |
|
5
|
|
|
* @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua) |
|
6
|
|
|
* @author Aleksandr Torosh <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Application\Form; |
|
10
|
|
|
|
|
11
|
|
|
use Phalcon\Forms\Element\Hidden; |
|
12
|
|
|
use Phalcon\Forms\Element\Check; |
|
13
|
|
|
use Phalcon\Forms\Element\File; |
|
14
|
|
|
use Application\Form\Element\Image; |
|
15
|
|
|
|
|
16
|
|
|
abstract class Form extends \Phalcon\Forms\Form |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
protected $helper; |
|
20
|
|
|
|
|
21
|
|
|
public function renderDecorated($name) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!$this->has($name)) { |
|
24
|
|
|
return "form element '$name' not found<br />"; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$this->helper = $this->getDI()->get('helper'); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$element = $this->get($name); |
|
|
|
|
|
|
30
|
|
|
$messages = $this->getMessagesFor($element->getName()); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$html = ''; |
|
33
|
|
|
if (count($messages)) { |
|
34
|
|
|
$html .= '<div class="ui error message">'; |
|
35
|
|
|
$html .= '<div class="header">' . $this->helper->translate('Ошибка валидации формы') . '</div>'; |
|
36
|
|
|
foreach ($messages as $message) { |
|
|
|
|
|
|
37
|
|
|
$html .= '<p>' . $message . '</p>'; |
|
38
|
|
|
} |
|
39
|
|
|
$html .= '</div>'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($element instanceof Hidden) { |
|
43
|
|
|
echo $element; |
|
44
|
|
|
} else { |
|
45
|
|
|
switch (true) { |
|
46
|
|
|
case $element instanceof Check: |
|
47
|
|
|
$html .= '<div class="field checkbox">'; |
|
48
|
|
|
$html .= '<div class="ui toggle checkbox">'; |
|
49
|
|
|
$html .= $element; |
|
50
|
|
|
if ($element->getLabel()) { |
|
51
|
|
|
$html .= '<label>'; |
|
52
|
|
|
$html .= $element->getLabel(); |
|
53
|
|
|
$html .= '</label>'; |
|
54
|
|
|
} |
|
55
|
|
|
$html .= '</div>'; |
|
56
|
|
|
$html .= '</div>'; |
|
57
|
|
|
break; |
|
58
|
|
|
|
|
59
|
|
|
case $element instanceof Image: |
|
60
|
|
|
$html = $this->renderImage($element); |
|
61
|
|
|
break; |
|
62
|
|
|
|
|
63
|
|
|
case $element instanceof File: |
|
64
|
|
|
$html .= '<div class="inline field">'; |
|
65
|
|
|
$html .= $this->makeLabel($element); |
|
66
|
|
|
$html .= $element; |
|
67
|
|
|
$html .= '</div>'; |
|
68
|
|
|
break; |
|
69
|
|
|
|
|
70
|
|
|
default: |
|
71
|
|
|
$html .= '<div class="field">'; |
|
72
|
|
|
$html .= $this->makeLabel($element); |
|
73
|
|
|
$html .= $element; |
|
74
|
|
|
$html .= '</div>'; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $html; |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function renderAll() |
|
83
|
|
|
{ |
|
84
|
|
|
$html = ''; |
|
85
|
|
|
if ($this->getElements()) { |
|
86
|
|
|
foreach ($this->getElements() as $element) { |
|
87
|
|
|
$html .= $this->renderDecorated($element->getName()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $html; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function makeLabel($element) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($element->getLabel()) { |
|
96
|
|
|
return '<label for="' . $element->getName() . '">' . $this->helper->translate($element->getLabel()) . '</label>'; |
|
97
|
|
|
} else { |
|
98
|
|
|
return ''; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param Image $element |
|
104
|
|
|
* @return string $html |
|
105
|
|
|
*/ |
|
106
|
|
|
private function renderImage($element) |
|
107
|
|
|
{ |
|
108
|
|
|
$html = '<div class="form-group">'; |
|
109
|
|
|
|
|
110
|
|
|
if ($element->getLabel()) { |
|
111
|
|
|
$html .= '<label>' . $element->getLabel() . '</label>'; |
|
112
|
|
|
} |
|
113
|
|
|
if ($element->getValue()) { |
|
114
|
|
|
$html .= '<section onclick="selectText(this);">' . $element->getValue() . '</section>'; |
|
115
|
|
|
} else { |
|
116
|
|
|
$html .= '<br>'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$html .= '<div class="fileinput fileinput-new" data-provides="fileinput"> |
|
120
|
|
|
<div class="fileinput-preview thumbnail" data-trigger="fileinput" |
|
121
|
|
|
style="width: 200px; min-height: 100px">'; |
|
122
|
|
|
|
|
123
|
|
|
if ($element->getValue()) { |
|
124
|
|
|
$url = $this->getDI()->get('url'); |
|
|
|
|
|
|
125
|
|
|
$html .= '<img src="' . $url->path() . $element->getValue() . '" width="200">'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$html .= '</div> |
|
129
|
|
|
<div> |
|
130
|
|
|
<span class="btn btn-default btn-file"> |
|
131
|
|
|
<span class="fileinput-new">Select image</span> |
|
132
|
|
|
<span class="fileinput-exists">Change</span> |
|
133
|
|
|
<input type="file" name="'.$element->getName() . '"> |
|
134
|
|
|
</span> |
|
135
|
|
|
<a href="#" class="btn btn-default fileinput-exists" |
|
136
|
|
|
data-dismiss="fileinput">Remove</a> |
|
137
|
|
|
</div> |
|
138
|
|
|
</div> |
|
139
|
|
|
</div>'; |
|
140
|
|
|
|
|
141
|
|
|
return $html; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.