1: <?php
2: namespace Budovy;
3:
4: class PeriodicTaskPresenter extends BasePresenter {
5:
6: public $periodicTaskRepository;
7:
8:
9: public $periodicTaskRealizationRepository;
10:
11:
12: public $documentRepository;
13:
14:
15: public $periodicTaskFactory;
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->secptask = (string)$this->getHttpRequest()->getUrl();
38:
39: return $this->periodicTaskFactory->createDataGrid();
40: }
41:
42: protected function createComponentDocumentsGrid() {
43: return $this->documentFactory->createDocumentsDataGrid();
44: }
45:
46: public function renderPreview($id) {
47: $item = $this->periodicTaskRepository->findById($id);
48:
49: if ($item === false) {
50: $this->redirect('default');
51: }
52:
53: $this->template->documentRepository = $this->documentRepository;
54: $this->template->periodicTaskRepository = $this->periodicTaskRepository;
55: $this->template->priority = $this->priorityRepository->findById($item->id_priority);
56: $this->template->unit = $this->unitRepository->findById($item->id_unit);
57: $this->template->operation = $this->operationRepository->findById($item->id_operation);
58: $this->template->documents = $this->periodicTaskRepository->getAssignedDocuments($item->id);
59:
60: $contact = $this->periodicTaskRepository->getAssignedContact($item->id);
61:
62: if ($contact === false) {
63: $contact = $this->contactBindingRepository->getContact($item->id_unit, $item->id_operation);
64: }
65:
66: $this->template->contact = $contact;
67: $this->template->data = $item;
68: }
69:
70: public function actionEdit($id) {
71: $item = $this->periodicTaskRepository->findById($id);
72:
73: if ($id > 0 && $item === false) {
74: $this->redirect('default');
75: }
76:
77: if ($id > 0) {
78: $this['editForm']['base']->setDefaults($item);
79: } else {
80: $this['editForm']['base']->setDefaults(array('start' => new \DateTime(), 'end' => new \DateTime()));
81: }
82:
83: $this->template->id = $id;
84: }
85:
86: public function actionRealization($id) {
87: $item = $this->periodicTaskRepository->findById($id);
88:
89: if ($item === false) {
90: $this->redirect('default');
91: }
92:
93: $defaults = array('date' => new \DateTime(), 'price' => 0, 'id_periodictask' => $item->id);
94:
95: $assigned = $this->periodicTaskRepository->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->periodicTaskRepository->findById($id);
112:
113: if ($item === false) {
114: $this->redirect('default');
115: }
116:
117: $assigned = array();
118:
119: foreach ($this->periodicTaskRepository->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->secptask)) {
129: $this->template->backlink = $session->secptask;
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->secptask)) {
142: $backlink = $session->secptask;
143: } else {
144: $backlink = $this->link('default');
145: }
146:
147: $form = $this->periodicTaskFactory->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->secptask)) {
157: $backlink = $session->secptask;
158: } else {
159: $backlink = $this->link('default');
160: }
161:
162: $form = $this->periodicTaskFactory->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: $fdata->months = 0;
177:
178: for ($n = 1; $n <= 12; $n++) {
179: $fdata->months += $fdata->{'m' . $n} * pow(2, $n - 1);
180:
181: unset($fdata->{'m' . $n});
182: }
183:
184: if ($id > 0) {
185: $this->periodicTaskRepository->updateById($id, $fdata);
186:
187: $this->flashMessage("Záznam byl upraven.", "success");
188: } else {
189: $data = $this->periodicTaskRepository->insertData($fdata);
190:
191: $id = $data->id;
192:
193: $this->flashMessage("Záznam byl vložen.", "success");
194: }
195:
196: 197: 198:
199: $filedatas = $form['document']->getValues();
200:
201: foreach ($filedatas->files as $file) {
202: if ($file->isOk()) {
203: $data = new \Nette\ArrayHash;
204: $data->name = $file->getName();
205: $data->filename = $file->getName();
206: $data->id_documenttype = $filedatas->id_documenttype;
207: $data->mime = $file->getContentType();
208: $data->data = $file->getContents();
209: $data->lastmod = date('Y-m-d H:i:s');
210:
211: $document = $this->documentRepository->insertData($data);
212:
213: $this->periodicTaskRepository->assignDocument($id, $document->id);
214: }
215: }
216:
217: 218: 219:
220: if ($contact > 0) {
221: $this->periodicTaskRepository->assignContact($id, $contact);
222: } else {
223: $this->periodicTaskRepository->removeAssignedContact($id);
224: }
225:
226: $session = $this->getSession()->getSection('history');
227:
228: if (isset($session->secptask)) {
229: $backlink = $session->secptask;
230: } else {
231: $backlink = $this->link('default');
232: }
233:
234: $this->redirectUrl($backlink);
235: }
236:
237: public function realizationFormSubmitted($form) {
238: $fdata = $form['base']->getValues();
239:
240: $id = $this->getParameter('id');
241: $contact = $fdata->id_contact;
242:
243: unset($fdata->id_contact);
244:
245: $data = $this->periodicTaskRealizationRepository->insertData($fdata);
246:
247: $id = $data->id;
248:
249: $this->flashMessage("Záznam byl vložen.", "success");
250:
251: 252: 253:
254: $filedatas = $form['document']->getValues();
255:
256: foreach ($filedatas->files as $file) {
257: if ($file->isOk()) {
258: $data = new \Nette\ArrayHash;
259: $data->name = $file->getName();
260: $data->filename = $file->getName();
261: $data->id_documenttype = $filedatas->id_documenttype;
262: $data->mime = $file->getContentType();
263: $data->data = $file->getContents();
264: $data->lastmod = date('Y-m-d H:i:s');
265:
266: $document = $this->documentRepository->insertData($data);
267:
268: $this->periodicTaskRealizationRepository->assignDocument($id, $document->id);
269: }
270: }
271:
272: 273: 274:
275: if ($contact > 0) {
276: $this->periodicTaskRealizationRepository->assignContact($id, $contact);
277: }
278:
279: $session = $this->getSession()->getSection('history');
280:
281: if (isset($session->secptask)) {
282: $backlink = $session->secptask;
283: } else {
284: $backlink = $this->link('default');
285: }
286:
287: $this->redirectUrl($backlink);
288: }
289:
290: public function handleDelete($id) {
291: try {
292: $this->periodicTaskRepository->deleteById($id);
293: $this->flashMessage("Záznam byl smazán.", "success");
294: } catch (\PDOException $e) {
295: $this->flashMessage("Záznam nelze smazat!", "error");
296: }
297:
298: $this->redirect('this');
299: }
300:
301: public function handleAssign($id, $document) {
302: $this->periodicTaskRepository->assignDocument($id, $document);
303:
304: $this->redirect('this');
305: }
306:
307: public function handleUnassign($id, $document) {
308: $this->periodicTaskRepository->removeAssignedDocument($id, $document);
309:
310: $this->redirect('this');
311: }
312: }