1: <?php
2: namespace Budovy;
3:
4: class PeriodicTaskRepository extends Repository {
5: 6: 7: 8: 9:
10: public function findById($id) {
11: return $this->getTable()->select('periodictask.*, :periodictask_contact.contact.id AS id_contact,
12: months & 0x1 AS m1,
13: months & 0x2 AS m2,
14: months & 0x4 AS m3,
15: months & 0x8 AS m4,
16: months & 0x10 AS m5,
17: months & 0x20 AS m6,
18: months & 0x40 AS m7,
19: months & 0x80 AS m8,
20: months & 0x100 AS m9,
21: months & 0x200 AS m10,
22: months & 0x400 AS m11,
23: months & 0x800 AS m12')->get($id);
24: }
25:
26: 27: 28: 29: 30:
31: function getPeriodNames() {
32: return array(
33: 'každý den',
34: 'každý pracovní den',
35: 'každý víkendový den',
36: 'první den v měsíci',
37: 'první pracovní den v měsíci',
38: 'první pracovní den v týdnu',
39: 'první víkendový den v týdnu',
40: 'poslední pracovní den v týdnu',
41: 'poslední den v měsíci',
42: 'poslední pracovní den v měsíci',
43: 'první víkendový den v měsíci'
44: );
45: }
46:
47: 48: 49: 50: 51: 52:
53: function getPeriodName($period) {
54: $periods = $this->getPeriodNames();
55:
56: if (isset($periods[$period])) {
57: return $periods[$period];
58: }
59:
60: return '';
61: }
62:
63: 64: 65: 66: 67:
68: function getPairData() {
69: return $this->getTable()->select('id, name')->order('name ASC')->fetchPairs('id', 'name');
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79: 80:
81: function getGridSelection($filter, $order, $page, $limit) {
82: $selection = $this->getTable()
83: ->select('periodictask.*, unit.name AS unit, unit.place.name AS place, operation.name AS operation')
84: ->select(':periodictask_contact.contact.id AS id_contact, CONCAT(:periodictask_contact.contact.firstname, \' \', :periodictask_contact.contact.lastname) AS contact')
85: ->select('priority.name AS priority');
86:
87: if (isset($order[0])) {
88: $selection->order(implode(' ', $order));
89: }
90:
91: $selection->page($page, $limit);
92:
93: return $this->addGridFilter($selection, $filter);
94: }
95:
96: 97: 98: 99: 100: 101:
102: function getGridCount($filter) {
103: $selection = $this->getTable()->select('COUNT(*) AS cnt');
104:
105: $result = $this->addGridFilter($selection, $filter)->fetch();
106:
107: return $result->cnt;
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: private function addGridFilter(\Nette\Database\Table\Selection $selection, $filter) {
118: $filters = array();
119:
120: foreach ($filter as $k => $v) {
121: if ($k == 'unit') {
122: $filters['id_unit=?'] = $v;
123: } else if ($k == 'operation') {
124: $filters['id_operation=?'] = $v;
125: } else if ($k == 'place') {
126: $filters['unit.id_place=?'] = $v;
127: } else if ($k == 'priority') {
128: $filters['id_priority=?'] = $v;
129: } else if ($k == 'contact') {
130: $filters[':periodictask_contact.contact.id=?'] = $v;
131: } else {
132: $filters['`periodictask`.' . $k . ' LIKE ?'] = "%$v%";
133: }
134: }
135:
136: return $selection->where($filters);
137: }
138:
139: 140: 141: 142: 143: 144:
145: function getAssignedDocuments($id_periodictask) {
146: return $this->getTable()->select(':periodictask_document.document.id')->where('periodictask.id', $id_periodictask)->where(':periodictask_document.document.id IS NOT NULL');
147: }
148:
149: 150: 151: 152: 153: 154:
155: function assignDocument($id_periodictask, $id_document) {
156: $this->getTable()->get($id_periodictask)->related('periodictask_document')->insert(array('id_document' => $id_document));
157:
158: }
159:
160: 161: 162: 163: 164: 165:
166: function removeAssignedDocument($id_periodictask, $id_document) {
167: $this->connection->table('periodictask_document')->where(array('id_periodictask' => $id_periodictask, 'id_document' => $id_document))->delete();
168: }
169:
170: 171: 172: 173: 174: 175:
176: function getAssignedContact($id_periodictask) {
177: return $this->getTable()->select(':periodictask_contact.contact.*')->where('periodictask.id', $id_periodictask)->where(':periodictask_contact.contact.id IS NOT NULL')->fetch();
178: }
179:
180: 181: 182: 183: 184: 185: 186:
187: function assignContact($id_periodictask, $id_contact) {
188: try {
189: $this->connection->table('periodictask_contact')->insert(array('id_periodictask' => $id_periodictask, 'id_contact' => $id_contact));
190: } catch (\PDOException $e) {
191: if ($e->getCode() == '23000') {
192: $this->connection->table('periodictask_contact')->where(array('id_periodictask' => $id_periodictask))->update(array('id_contact' => $id_contact));
193: } else {
194: throw $e;
195: }
196: }
197: }
198:
199: 200: 201: 202: 203:
204: function removeAssignedContact($id_periodictask) {
205: $this->connection->table('periodictask_contact')->where('id_periodictask', $id_periodictask)->delete();
206: }
207:
208: 209: 210: 211: 212: 213: 214: 215:
216: function checkPeriod($period, $date) {
217: $datetime = clone $date;
218:
219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231:
232:
233: switch ($period) {
234: case 0:
235: return true;
236:
237: case 1:
238: return (int)$datetime->format('N') < 6;
239:
240: case 2:
241: return (int)$datetime->format('N') > 5;
242:
243: case 3:
244: return (int)$datetime->format('j') == 1;
245:
246: case 4:
247: $datetime->modify('+' . (1 - $datetime->format('j')) . ' day');
248:
249: switch ($datetime->format('N')) {
250: case 6:
251: $datetime->modify('+2 day');
252: break;
253:
254: case 7:
255: $datetime->modify('+1 day');
256: break;
257: }
258:
259: return $datetime->diff($date)->days == 0;
260:
261: break;
262:
263: case 5:
264: return (int)$datetime->format('N') == 1;
265:
266: case 6:
267: return (int)$datetime->format('N') == 6;
268:
269: case 7:
270: return (int)$datetime->format('N') == 5;
271:
272: case 8:
273: $datetime->modify('+' . ($datetime->format('t') - $datetime->format('j')) . ' day');
274:
275: return $datetime->diff($date)->days == 0;
276:
277: case 9:
278: $datetime->modify('+' . ($datetime->format('t') - $datetime->format('j')) . ' day');
279:
280: switch ($datetime->format('N')) {
281: case 6:
282: $datetime->modify('-1 day');
283: break;
284:
285: case 7:
286: $datetime->modify('-2 day');
287: break;
288: }
289:
290: return $datetime->diff($date)->days == 0;
291:
292: case 10:
293: $datetime->modify('+' . (1 - $datetime->format('j')) . ' day');
294:
295: switch ($datetime->format('N')) {
296: case 1:
297: case 2:
298: case 3:
299: case 4:
300: case 5:
301: $datetime->modify('+' . (6 - $datetime->format('N')) . ' day');
302: break;
303:
304: case 7:
305: $datetime->modify('+6 day');
306: break;
307: }
308:
309: return $datetime->diff($date)->days == 0;
310:
311: default:
312: throw new \UnknownPeriod("Unknown period: " . $period);
313: }
314: }
315:
316: 317: 318: 319: 320: 321: 322:
323: function getTasksByDateRange($start, $end) {
324: $startdate = clone $start;
325: $startdate->setDate($startdate->format('Y'), $startdate->format('n'), 1);
326:
327: $enddate = clone $end;
328: $enddate->setDate($enddate->format('Y'), $enddate->format('n'), 1);
329:
330: $selection = $this->getTable();
331:
332: $month = 0;
333:
334: for (; $startdate <= $enddate; $startdate->modify('+1 month')) {
335: $month += pow(2, $startdate->format('n') - 1);
336: }
337:
338: return $selection->where(array('`months` & ?' => $month));
339: }
340:
341: 342: 343: 344: 345: 346:
347: function getTasksStartedAt($start) {
348: $startdate = new \DateTime($start);
349:
350: $periods = array();
351:
352: for ($n = 0; $n <= 9; $n++) {
353: if ($this->checkPeriod($n, $startdate)) {
354: $periods[] = $n;
355: }
356: }
357:
358: return $this->getTable()->where('`months` & ? AND period ?', pow(2, $startdate->format('n') - 1), $periods);
359: }
360:
361: 362: 363: 364: 365: 366:
367: function getTasksByCustomer($id_contact) {
368: return $this->getTable()->select('DISTINCT `periodictask`.*')->where(':periodictask_contact.id_contact=? OR :periodictask_realization:periodictask_realization_contact.id_contact=?', $id_contact, $id_contact);
369: }
370:
371: 372: 373: 374: 375: 376:
377: function getTasksByDocument($id_document) {
378: return $this->getTable()->where(':periodictask_document.id_document=?', $id_document);
379: }
380:
381: 382: 383: 384: 385:
386: function getTasksNotice() {
387: return $this->getTable()
388: ->select('periodictask.*, unit.name AS unit, operation.name AS operation, :periodictask_contact.contact.email')
389: ->select('CURDATE() + INTERVAL(operation.daysbefore) ? AS `date`', new \Nette\Database\SqlLiteral('day'))
390: ->where('`months`&POW(2,MONTH(CURDATE() + INTERVAL(operation.daysbefore) ?) - 1)', new \Nette\Database\SqlLiteral('day'));
391: }
392: }