-
Notifications
You must be signed in to change notification settings - Fork 0
/
Radioset.php
87 lines (71 loc) · 2.22 KB
/
Radioset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Logikos\Forms\Element;
use Phalcon\Tag;
use Phalcon\Forms\Element;
use Phalcon\Forms\Element\Radio;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\ElementInterface;
use Logikos\Forms\Tag\Radioset as RadiosetTag;
use InvalidArgumentException;
class Radioset extends Select implements ElementInterface {
protected $_rendercallback;
protected $_radiotemplate;
public function setRadioTemplate($view) {
$this->_radiotemplate = $view;
}
public function getRadioTemplate() {
return $this->_radiotemplate;
}
public function setRenderCallback($callback) {
if (!is_callable($callback))
throw new InvalidArgumentException("RenderCallback must be callable...");
$this->_rendercallback = $callback;
}
public function getRenderCallback() {
if ($this->getRadioTemplate())
$this->setRenderCallback([$this,'renderWithTemplate']);
return $this->_rendercallback;
}
public function render($attributes=null) {
$params = $this->prepareAttributes($attributes);
$params[0] = $this->getName();
$params[1] = $this->getOptions();
$params[2] = $this->getRenderCallback();
return RadiosetTag::RadiosetField($params);
}
public function renderWithTemplate($name,$value,$label,$curent_value) {
$attr = [
'type' => 'radio',
'name' => $name,
'value' => $value,
'id' => null
];
$radio = new Radio($name,$attr);
$radio->setDefault($curent_value);
return $this->getPartialFromView(
$this->_radiotemplate,
[
'form' => $this->getForm(),
'element' => $radio,
'name' => $name,
'value' => $value,
'label' => $label
]
);
}
/**
* @return \Phalcon\Mvc\ViewBaseInterface
*/
protected function getView() {
/* @var $form \Phalcon\Forms\Form */
$form = $this->getForm();
if (isset($this->view) && $this->view instanceof ViewBaseInterface)
return $this->view;
return $form->getDI()->get('view');
}
protected function getPartialFromView($partialPath, $params = null) {
ob_start();
$this->getView()->partial($partialPath,$params);
return ob_get_clean();
}
}