This repository has been archived by the owner on Jun 21, 2018. It is now read-only.
/
VisualDatePaginator.php
99 lines (82 loc) · 2.27 KB
/
VisualDatePaginator.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
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace steky\nette\DatePaginator;
use \Nette\Application\UI\Control;
use \Nette\Application\UI\Form;
use \DateTime;
/**
* Visual paginator control.
*
* @author Martin Štekl <martin.stekl@gmail.com>
* @since 2012-07-21
*/
class VisualDatePaginator extends Control {
/**
* @persistent
* @var string
*/
public $date = null;
/** @var DatePaginator */
private $paginator = null;
/**
* @return \Nette\Forms\Form
*/
public function createComponentDateForm() {
$form = new Form();
$form->getElementPrototype()->style = 'margin: 0;';
$form->addDatePicker('paginatorDate')
->addRule(Form::VALID, 'Entered date is not valid!')
->addCondition(Form::FILLED)
->addRule(Form::RANGE, 'Entered date is not within allowed range.', array($this->getPaginator()->getOldestDate(), $this->getPaginator()->getNewestDate()));
$form['paginatorDate']->setDefaultValue($this->getPaginator()->getDate());
$form->onSuccess[] = callback($this, 'formSubmitted');
return $form;
}
/**
* @param \Nette\Application\UI\Form $form
*/
public function formSubmitted(Form $form) {
$values = $form->getValues();
$this->getPaginator()->setDate($values['paginatorDate']);
$this->date = $values['paginatorDate']->format('Y-m-d');
$this->redirect('this', array('date' => $this->date, ));
}
/**
* @return DatePaginator
*/
public function getPaginator() {
if ($this->paginator === null) {
$this->paginator = new DatePaginator();
}
return $this->paginator;
}
/**
* @param string|NULL
* @return\Nette\Templating\ITemplate
*/
protected function createTemplate($class = null) {
$template = parent::createTemplate($class);
// TODO - registrace vlastních helperů
return $template;
}
/**
* Renders paginator.
*
* @return void
*/
public function render() {
$paginator = $this->getPaginator();
$this->template->paginator = $paginator;
$this->template->setFile(__DIR__ . '/template.latte');
$this->template->render();
}
/**
* Loads state informations.
*
* @param array
* @return void
*/
public function loadState(array $params) {
parent::loadState($params);
$this->getPaginator()->setDate(new DateTime($this->date ?: 'now'));
}
}