-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.php
More file actions
127 lines (105 loc) · 4.53 KB
/
Range.php
File metadata and controls
127 lines (105 loc) · 4.53 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Range;
use Latte\Engine,
Nette\Application\UI\Form,
Nette\Bridges,
Nette\Http\Request,
Nette\Forms\Controls,
Nette\Localization\ITranslator;
/** @author Lubomir Andrisek */
class Range extends Controls\BaseControl implements IRangeFactory {
/** @var ITranslator */
private $translator;
/** @var string */
private $basePath;
/** @var Array */
private $cookies;
/** @var Array */
private $defaults;
/** @var string */
private $type;
/** @var string */
private $key;
/** @var string */
protected $label;
/** @var string */
private $id;
/** @return IRangeFactory */
public function create() {
return $this;
}
public function __construct($label = null, Array $defaults = [], Request $request, ITranslator $translator) {
parent::__construct($label);
$url = $request->getUrl();
$this->cookies = $request->getCookies();
$this->defaults = $defaults;
$this->label = $label;
$this->translator = $translator;
$this->basePath = $url->scheme . '://' . $url->host . $url->scriptPath . 'vendor/landrisek/nette-range';
}
public function attached($form) {
parent::attached($form);
if ($form instanceof Form) {
$parent = $this->getForm()->getParent();
$this->key = (is_object($parent)) ? $parent->getName() : $this->getForm()->getName();
$this->key .= '-' . $this->getName() . '-';
$this->cookies[$this->key . '>'] = (isset($this->cookies[$this->key . '>'])) ? $this->cookies[$this->key . '>'] : $this->defaults['>'];
$this->cookies[$this->key . '<'] = (isset($this->cookies[$this->key . '<'])) ? $this->cookies[$this->key . '<'] : $this->defaults['<'];
$this->id = (is_object($parent)) ? $parent->getName() . '-' . $this->getForm()->getName() : $this->getForm()->getName();
}
}
/** setters */
public function setCookieKey($key) {
$this->key = $key;
return $this;
}
/** getters */
public function getControl() {
if ((bool) strpbrk($this->cookies[$this->key . '>'], 1234567890) and
strtotime($this->cookies[$this->key . '>']) and
(bool) strpbrk($this->cookies[$this->key . '<'], 1234567890) and
strtotime($this->cookies[$this->key . '<'])) {
$this->type = 'date';
return '<div class="buffer"><div id="date-slider"></div></div>';
} elseif(preg_match('/\./', $this->cookies[$this->key . '>']) or preg_match('/\./', $this->cookies[$this->key . '<'])) {
$this->type = 'float';
return '<label for="amount">' . $this->translator->translate($this->label) . ':</label><input type="text" id="amount" readonly style="border:0; font-weight:bold;">' .
'<div class="buffer"><div id="float-slider"></div></div>';
} else {
$this->type = 'edit';
return '<div class="buffer"><div id="edit-slider"></div></div>';
}
}
public function getValue() {
return ['>' => $this->cookies[$this->key . '>'],
'<' => $this->cookies[$this->key . '<']
];
}
public function renderHead() {
$latte = new Engine();
$template = new Bridges\ApplicationLatte\Template($latte);
$template->basePath = $this->basePath;
$template->setFile(__DIR__ . '/templates/head.latte');
return $template->render();
}
public function renderFooter() {
$latte = new Engine();
$template = new Bridges\ApplicationLatte\Template($latte);
$template->link = $this->getForm()->getPresenter()->link('this');
$template->basePath = $this->basePath;
$template->key = $this->key;
$template->id = $this->id;
$template->min = $this->defaults['min'];
$template->max = $this->defaults['max'];
$template->type = $this->type;
$template->from = (isset($this->cookies[$this->key . '>'])) ? $this->cookies[$this->key . '>'] : $this->defaults['>'];
$template->to = (isset($this->cookies[$this->key . '<'])) ? $this->cookies[$this->key . '<'] : $this->defaults['<'];
$template->setFile(__DIR__ . '/templates/footer.latte');
$template->setTranslator($this->translator);
return $template->render();
}
}
interface IRangeFactory {
/** @return Range */
function create();
}