1: <?php
2: namespace Budovy;
3:
4: class ContactPresenter extends BasePresenter {
5:
6: public $contactRepository;
7:
8:
9: public $scheduledTaskRepository;
10:
11:
12: public $breakdownRepository;
13:
14:
15: public $periodicTaskRepository;
16:
17:
18: public $contactFactory;
19:
20: protected function createComponentGrid() {
21: $session = $this->getSession()->getSection('history');
22: $session->seccontact = (string)$this->getHttpRequest()->getUrl();
23:
24: return $this->contactFactory->createDataGrid();
25: }
26:
27: public function renderPreview($id) {
28: $item = $this->contactRepository->findById($id);
29:
30: if ($item === false) {
31: $this->redirect('default');
32: }
33:
34: $this->template->data = $item;
35: $this->template->periodicTaskRepository = $this->periodicTaskRepository;
36: $this->template->scheduledtasks = $this->scheduledTaskRepository->getTasksByCustomer($item->id);
37: $this->template->periodictasks = $this->periodicTaskRepository->getTasksByCustomer($item->id);
38: $this->template->breakdowns = $this->breakdownRepository->getTasksByCustomer($item->id);
39: }
40:
41: public function actionEdit($id) {
42: $item = $this->contactRepository->findById($id);
43:
44: if ($id > 0 && $item === false) {
45: $this->redirect('default');
46: }
47:
48: if ($id > 0) {
49: $this['editForm']->setDefaults($item);
50: }
51:
52: $this->template->id = $id;
53: }
54:
55: protected function createComponentEditForm() {
56: $session = $this->getSession()->getSection('history');
57:
58: if (isset($session->seccontact)) {
59: $backlink = $session->seccontact;
60: } else {
61: $backlink = $this->link('default');
62: }
63:
64: $form = $this->contactFactory->createEditForm($backlink);
65: $form->onSuccess[] = $this->editFormSubmitted;
66:
67: return $form;
68: }
69:
70: public function editFormSubmitted($form) {
71: $fdata = $form->getValues();
72:
73: $id = $this->getParameter('id');
74:
75: if ($id > 0) {
76: $this->contactRepository->updateById($id, $fdata);
77:
78: $this->flashMessage("Záznam byl upraven.", "success");
79: } else {
80: $this->contactRepository->insertData($fdata);
81:
82: $this->flashMessage("Záznam byl vložen.", "success");
83: }
84:
85: $session = $this->getSession()->getSection('history');
86:
87: if (isset($session->seccontact)) {
88: $backlink = $session->seccontact;
89: } else {
90: $backlink = $this->link('default');
91: }
92:
93: $this->redirectUrl($backlink);
94: }
95:
96: public function handleDelete($id) {
97: try {
98: $this->contactRepository->deleteById($id);
99: $this->flashMessage("Záznam byl smazán.", "success");
100: } catch (\PDOException $e) {
101: $this->flashMessage("Záznam nelze smazat!", "error");
102: }
103:
104: $this->redirect('this');
105: }
106: }