1: <?php
2: namespace Budovy;
3:
4: class ScheduledTaskPresenter extends BasePresenter {
5:
6: public $scheduledTaskRepository;
7:
8:
9: public $scheduledTaskRealizationRepository;
10:
11:
12: public $documentRepository;
13:
14:
15: public $scheduledTaskFactory;
16:
17:
18: public $documentFactory;
19:
20:
21: public $contactBindingRepository;
22:
23:
24: public $unitRepository;
25:
26:
27: public $operationRepository;
28:
29:
30: public $priorityRepository;
31:
32:
33: public $httpRequest;
34:
35: protected function createComponentGrid() {
36: $session = $this->getSession()->getSection('history');
37: $session->secstask = (string)$this->getHttpRequest()->getUrl();
38:
39: return $this->scheduledTaskFactory->createDataGrid();
40: }
41:
42: protected function createComponentDocumentsGrid() {
43: return $this->documentFactory->createDocumentsDataGrid();
44: }
45:
46: public function actionEdit($id) {
47: $item = $this->scheduledTaskRepository->findById($id);
48:
49: if ($id > 0 && $item === false) {
50: $this->redirect('default');
51: }
52:
53: if ($id > 0) {
54: $this['editForm']['base']->setDefaults($item);
55: } else {
56: $this['editForm']['base']->setDefaults(array('start' => new \DateTime(), 'end' => new \DateTime()));
57: }
58:
59: $this->template->id = $id;
60: }
61:
62: public function renderPreview($id) {
63: $item = $this->scheduledTaskRepository->findById($id);
64:
65: if ($item === false) {
66: $this->redirect('default');
67: }
68:
69: $this->template->documentRepository = $this->documentRepository;
70: $this->template->priority = $this->priorityRepository->findById($item->id_priority);
71: $this->template->unit = $this->unitRepository->findById($item->id_unit);
72: $this->template->operation = $this->operationRepository->findById($item->id_operation);
73: $this->template->documents = $this->scheduledTaskRepository->getAssignedDocuments($item->id);
74: $this->template->approved = $this->scheduledTaskRepository->getApprovedInfo($item->id);
75:
76: $contact = $this->scheduledTaskRepository->getAssignedContact($item->id);
77:
78: if ($contact === false) {
79: $contact = $this->contactBindingRepository->getContact($item->id_unit, $item->id_operation);
80: }
81:
82: $this->template->contact = $contact;
83: $this->template->data = $item;
84: }
85:
86: public function actionRealization($id) {
87: $item = $this->scheduledTaskRepository->findById($id);
88:
89: if ($item === false) {
90: $this->redirect('default');
91: }
92:
93: $defaults = array('date' => new \DateTime(), 'price' => 0, 'id_scheduledtask' => $item->id);
94:
95: $assigned = $this->scheduledTaskRepository->getAssignedContact($item->id);
96:
97: if ($assigned === false) {
98: $contact = $this->contactBindingRepository->getContact($item->id_unit, $item->id_operation);
99:
100: if ($contact !== false) {
101: $defaults['id_contact'] = $contact->id;
102: }
103: } else {
104: $defaults['id_contact'] = $assigned->id;
105: }
106:
107: $this['realizationForm']['base']->setDefaults($defaults);
108: }
109:
110: public function actionDocuments($id) {
111: $item = $this->scheduledTaskRepository->findById($id);
112:
113: if ($item === false) {
114: $this->redirect('default');
115: }
116:
117: $assigned = array();
118:
119: foreach ($this->scheduledTaskRepository->getAssignedDocuments($item->id) as $row) {
120: $assigned[] = $row->id;
121: }
122:
123: $this['documentsGrid']->setId($item->id);
124: $this['documentsGrid']->setAssigned($assigned);
125:
126: $session = $this->getSession()->getSection('history');
127:
128: if (isset($session->secstask)) {
129: $this->template->backlink = $session->secstask;
130: } else {
131: $this->template->backlink = $this->link('default');
132: }
133:
134: $this->template->id = $item->id;
135: $this->template->name = $item->name;
136: }
137:
138: protected function createComponentEditForm() {
139: $session = $this->getSession()->getSection('history');
140:
141: if (isset($session->secstask)) {
142: $backlink = $session->secstask;
143: } else {
144: $backlink = $this->link('default');
145: }
146:
147: $form = $this->scheduledTaskFactory->createEditForm($backlink);
148: $form->onSuccess[] = $this->editFormSubmitted;
149:
150: return $form;
151: }
152:
153: protected function createComponentRealizationForm() {
154: $session = $this->getSession()->getSection('history');
155:
156: if (isset($session->secstask)) {
157: $backlink = $session->secstask;
158: } else {
159: $backlink = $this->link('default');
160: }
161:
162: $form = $this->scheduledTaskFactory->createRealizationForm($backlink);
163: $form->onSuccess[] = $this->realizationFormSubmitted;
164:
165: return $form;
166: }
167:
168: public function editFormSubmitted($form) {
169: $fdata = $form['base']->getValues();
170:
171: $id = $this->getParameter('id');
172: $contact = $fdata->id_contact;
173:
174: unset($fdata->id_contact);
175:
176: if ($id > 0) {
177: $this->scheduledTaskRepository->updateById($id, $fdata);
178:
179: $this->flashMessage("Záznam byl upraven.", "success");
180: } else {
181: $data = $this->scheduledTaskRepository->insertData($fdata);
182:
183: $id = $data->id;
184:
185: $this->flashMessage("Záznam byl vložen.", "success");
186: }
187:
188: 189: 190:
191: $filedatas = $form['document']->getValues();
192:
193: foreach ($filedatas->files as $file) {
194: if ($file->isOk()) {
195: $data = new \Nette\ArrayHash;
196: $data->name = $file->getName();
197: $data->filename = $file->getName();
198: $data->id_documenttype = $filedatas->id_documenttype;
199: $data->mime = $file->getContentType();
200: $data->data = $file->getContents();
201: $data->lastmod = date('Y-m-d H:i:s');
202:
203: $document = $this->documentRepository->insertData($data);
204:
205: $this->scheduledTaskRepository->assignDocument($id, $document->id);
206: }
207: }
208:
209: 210: 211:
212: if ($contact > 0) {
213: $this->scheduledTaskRepository->assignContact($id, $contact);
214: } else {
215: $this->scheduledTaskRepository->removeAssignedContact($id);
216: }
217:
218: $session = $this->getSession()->getSection('history');
219:
220: if (isset($session->secstask)) {
221: $backlink = $session->secstask;
222: } else {
223: $backlink = $this->link('default');
224: }
225:
226: $this->redirectUrl($backlink);
227: }
228:
229: public function realizationFormSubmitted($form) {
230: $fdata = $form['base']->getValues();
231:
232: $id = $this->getParameter('id');
233: $contact = $fdata->id_contact;
234:
235: unset($fdata->id_contact);
236:
237: $data = $this->scheduledTaskRealizationRepository->insertData($fdata);
238:
239: $id = $data->id;
240:
241: $this->flashMessage("Záznam byl vložen.", "success");
242:
243: 244: 245:
246: $filedatas = $form['document']->getValues();
247:
248: foreach ($filedatas->files as $file) {
249: if ($file->isOk()) {
250: $data = new \Nette\ArrayHash;
251: $data->name = $file->getName();
252: $data->filename = $file->getName();
253: $data->id_documenttype = $filedatas->id_documenttype;
254: $data->mime = $file->getContentType();
255: $data->data = $file->getContents();
256: $data->lastmod = date('Y-m-d H:i:s');
257:
258: $document = $this->documentRepository->insertData($data);
259:
260: $this->scheduledTaskRealizationRepository->assignDocument($id, $document->id);
261: }
262: }
263:
264: 265: 266:
267: if ($contact > 0) {
268: $this->scheduledTaskRealizationRepository->assignContact($id, $contact);
269: }
270:
271: $session = $this->getSession()->getSection('history');
272:
273: if (isset($session->secstask)) {
274: $backlink = $session->secstask;
275: } else {
276: $backlink = $this->link('default');
277: }
278:
279: $this->redirectUrl($backlink);
280: }
281:
282: public function handleDelete($id) {
283: try {
284: $this->scheduledTaskRepository->deleteById($id);
285: $this->flashMessage("Záznam byl smazán.", "success");
286: } catch (\PDOException $e) {
287: $this->flashMessage("Záznam nelze smazat!", "error");
288: }
289:
290: $this->redirect('this');
291: }
292:
293: public function handleAssign($id, $document) {
294: $this->scheduledTaskRepository->assignDocument($id, $document);
295:
296: $this->redirect('this');
297: }
298:
299: public function handleUnassign($id, $document) {
300: $this->scheduledTaskRepository->removeAssignedDocument($id, $document);
301:
302: $this->redirect('this');
303: }
304: }