1: <?php
2: namespace Budovy;
3:
4: class DocumentPresenter extends BasePresenter {
5:
6: public $documentRepository;
7:
8:
9: public $periodicTaskRepository;
10:
11:
12: public $periodicTaskRealizationRepository;
13:
14:
15: public $scheduledTaskRepository;
16:
17:
18: public $scheduledTaskRealizationRepository;
19:
20:
21: public $breakdownRepository;
22:
23:
24: public $documentFactory;
25:
26: protected function createComponentGrid() {
27: $session = $this->getSession()->getSection('history');
28: $session->secdoctype = (string)$this->getHttpRequest()->getUrl();
29:
30: return $this->documentFactory->createDataGrid();
31: }
32:
33: public function renderPreview($id) {
34: $item = $this->documentRepository->findById($id);
35:
36: if ($item === false) {
37: $this->redirect('default');
38: }
39:
40: $this->template->periodicTaskRepository = $this->periodicTaskRepository;
41: $this->template->scheduledtasks = $this->scheduledTaskRepository->getTasksByDocument($item->id);
42: $this->template->scheduledtasksrealization = $this->scheduledTaskRealizationRepository->getTasksByDocument($item->id);
43: $this->template->periodictasks = $this->periodicTaskRepository->getTasksByDocument($item->id);
44: $this->template->periodictasksrealization = $this->periodicTaskRealizationRepository->getTasksByDocument($item->id);
45: $this->template->breakdowns = $this->breakdownRepository->getTasksByDocument($item->id);
46: $this->template->data = $item;
47: }
48:
49: public function actionEdit($id) {
50: $item = $this->documentRepository->findById($id);
51:
52: if ($id > 0 && $item === false) {
53: $this->redirect('default');
54: }
55:
56: if ($id > 0) {
57: $this['editForm']->setDefaults($item);
58: }
59:
60: $this->template->id = $id;
61: }
62:
63: public function renderEdit($id) {
64: $this->template->data = $this->documentRepository->getDocumentsMeta($id);
65: }
66:
67: protected function createComponentEditForm() {
68: $session = $this->getSession()->getSection('history');
69:
70: if (isset($session->secdoctype)) {
71: $backlink = $session->secdoctype;
72: } else {
73: $backlink = $this->link('default');
74: }
75:
76: $form = $this->documentFactory->createEditForm($backlink);
77: $form->onValidate[] = $this->editFormValidate;
78: $form->onSuccess[] = $this->editFormSubmitted;
79:
80: return $form;
81: }
82:
83: public function editFormValidate($form) {
84: $fdata = $form->getValues();
85:
86: $id = $this->getParameter('id');
87:
88: if ($id == 0 && !$fdata->file->isOk()) {
89: $form->addError('Vyberte prosím soubor!');
90: }
91: }
92:
93: public function editFormSubmitted($form) {
94: $fdata = $form->getValues();
95:
96: $id = $this->getParameter('id');
97:
98: $data = new \Nette\ArrayHash;
99:
100: $data->name = $fdata->name;
101: $data->id_documenttype = $fdata->id_documenttype;
102:
103: if ($fdata->file->isOk()) {
104: $data->mime = $fdata->file->getContentType();
105: $data->data = $fdata->file->getContents();
106: $data->filename = $fdata->file->getName();
107: $data->lastmod = date('Y-m-d H:i:s');
108: }
109:
110: if ($id > 0) {
111: $this->documentRepository->updateById($id, $data);
112:
113: $this->flashMessage("Záznam byl upraven.", "success");
114: } else {
115: $this->documentRepository->insertData($data);
116:
117: $this->flashMessage("Záznam byl vložen.", "success");
118: }
119:
120: $session = $this->getSession()->getSection('history');
121:
122: if (isset($session->secdoctype)) {
123: $backlink = $session->secdoctype;
124: } else {
125: $backlink = $this->link('default');
126: }
127:
128: $this->redirectUrl($backlink);
129: }
130:
131: public function handleDelete($id) {
132: try {
133: $this->documentRepository->deleteById($id);
134: $this->flashMessage("Záznam byl smazán.", "success");
135: } catch (\PDOException $e) {
136: $this->flashMessage("Záznam nelze smazat!", "error");
137: }
138:
139: $this->redirect('this');
140: }
141: }