1: <?php
2: namespace Budovy;
3:
4: class ScheduledTaskRealizationPresenter extends BasePresenter {
5:
6: public $scheduledTaskRepository;
7:
8:
9: public $scheduledTaskRealizationRepository;
10:
11:
12: public $documentRepository;
13:
14:
15: public $scheduledTaskRealizationFactory;
16:
17:
18: public $documentFactory;
19:
20:
21: public $contactBindingRepository;
22:
23:
24: public $unitRepository;
25:
26:
27: public $operationRepository;
28:
29:
30: public $httpRequest;
31:
32: protected function createComponentGrid() {
33: $session = $this->getSession()->getSection('history');
34: $session->secsrtask = (string)$this->getHttpRequest()->getUrl();
35:
36: return $this->scheduledTaskRealizationFactory->createDataGrid();
37: }
38:
39: protected function createComponentDocumentsGrid() {
40: return $this->documentFactory->createDocumentsDataGrid();
41: }
42:
43: public function actionEdit($id) {
44: $item = $this->scheduledTaskRealizationRepository->findById($id);
45:
46: if ($id > 0 && $item === false) {
47: $this->redirect('default');
48: }
49:
50: if ($id > 0) {
51: $this['editForm']['base']->setDefaults($item);
52: } else {
53: $this['editForm']['base']->setDefaults(array('date' => new \DateTime(), 'price' => 0));
54: }
55:
56: $this->template->id = $id;
57: }
58:
59: public function actionDocuments($id) {
60: $item = $this->scheduledTaskRealizationRepository->findById($id);
61:
62: if ($item === false) {
63: $this->redirect('default');
64: }
65:
66: $assigned = array();
67:
68: foreach ($this->scheduledTaskRealizationRepository->getAssignedDocuments($item->id) as $row) {
69: $assigned[] = $row->id;
70: }
71:
72: $this['documentsGrid']->setId($item->id);
73: $this['documentsGrid']->setAssigned($assigned);
74:
75: $session = $this->getSession()->getSection('history');
76:
77: if (isset($session->secsrtask)) {
78: $this->template->backlink = $session->secsrtask;
79: } else {
80: $this->template->backlink = $this->link('default');
81: }
82:
83: $this->template->id = $item->id;
84: }
85:
86: public function renderPreview($id) {
87: $item = $this->scheduledTaskRealizationRepository->findById($id);
88:
89: if ($item === false) {
90: $this->redirect('default');
91: }
92:
93: $this->template->documentRepository = $this->documentRepository;
94:
95: $task = $this->scheduledTaskRepository->findById($item->id_scheduledtask);
96:
97: $this->template->task = $task;
98: $this->template->unit = $this->unitRepository->findById($task->id_unit);
99: $this->template->operation = $this->operationRepository->findById($task->id_operation);
100: $this->template->contact = $this->scheduledTaskRealizationRepository->getAssignedContact($item->id);
101: $this->template->documents = $this->scheduledTaskRealizationRepository->getAssignedDocuments($item->id);
102: $this->template->data = $item;
103: }
104:
105: protected function createComponentEditForm() {
106: $session = $this->getSession()->getSection('history');
107:
108: if (isset($session->secsrtask)) {
109: $backlink = $session->secsrtask;
110: } else {
111: $backlink = $this->link('default');
112: }
113:
114: $form = $this->scheduledTaskRealizationFactory->createEditForm($backlink);
115: $form->onSuccess[] = $this->editFormSubmitted;
116:
117: return $form;
118: }
119:
120: public function editFormSubmitted($form) {
121: $fdata = $form['base']->getValues();
122:
123: $id = $this->getParameter('id');
124: $contact = $fdata->id_contact;
125:
126: unset($fdata->id_contact);
127:
128: if ($id > 0) {
129: $this->scheduledTaskRealizationRepository->updateById($id, $fdata);
130:
131: $this->flashMessage("Záznam byl upraven.", "success");
132: } else {
133: $data = $this->scheduledTaskRealizationRepository->insertData($fdata);
134:
135: $id = $data->id;
136:
137: $this->flashMessage("Záznam byl vložen.", "success");
138: }
139:
140: 141: 142:
143: $filedatas = $form['document']->getValues();
144:
145: foreach ($filedatas->files as $file) {
146: if ($file->isOk()) {
147: $data = new \Nette\ArrayHash;
148: $data->name = $file->getName();
149: $data->filename = $file->getName();
150: $data->id_documenttype = $filedatas->id_documenttype;
151: $data->mime = $file->getContentType();
152: $data->data = $file->getContents();
153: $data->lastmod = date('Y-m-d H:i:s');
154:
155: $document = $this->documentRepository->insertData($data);
156:
157: $this->scheduledTaskRealizationRepository->assignDocument($id, $document->id);
158: }
159: }
160:
161: 162: 163:
164: if ($contact > 0) {
165: $this->scheduledTaskRealizationRepository->assignContact($id, $contact);
166: } else {
167: $this->scheduledTaskRealizationRepository->removeAssignedContact($id);
168: }
169:
170: $session = $this->getSession()->getSection('history');
171:
172: if (isset($session->secsrtask)) {
173: $backlink = $session->secsrtask;
174: } else {
175: $backlink = $this->link('default');
176: }
177:
178: $this->redirectUrl($backlink);
179: }
180:
181: public function handleDelete($id) {
182: try {
183: $this->scheduledTaskRealizationRepository->deleteById($id);
184: $this->flashMessage("Záznam byl smazán.", "success");
185: } catch (\PDOException $e) {
186: $this->flashMessage("Záznam nelze smazat!", "error");
187: }
188:
189: $this->redirect('this');
190: }
191:
192: public function handleAssign($id, $document) {
193: $this->scheduledTaskRealizationRepository->assignDocument($id, $document);
194:
195: $this->redirect('this');
196: }
197:
198: public function handleUnassign($id, $document) {
199: $this->scheduledTaskRealizationRepository->removeAssignedDocument($id, $document);
200:
201: $this->redirect('this');
202: }
203: }