Passed
Pull Request — master (#1545)
by Julius
03:01
created

Card::getCalendarObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Db;
25
26
use DateTime;
27
use Sabre\VObject\Component\VCalendar;
28
29
class Card extends RelationalEntity {
30
31
	protected $title;
32
	protected $description;
33
	protected $descriptionPrev;
34
	protected $stackId;
35
	protected $type;
36
	protected $lastModified;
37
	protected $lastEditor;
38
	protected $createdAt;
39
	protected $labels;
40
	protected $assignedUsers;
41
	protected $attachments;
42
	protected $attachmentCount;
43
	protected $owner;
44
	protected $order;
45
	protected $archived = false;
46
	protected $duedate;
47
	protected $notified = false;
48
	protected $deletedAt = 0;
49
	protected $commentsUnread = 0;
50
51
	private $databaseType = 'sqlite';
52
53
	const DUEDATE_FUTURE = 0;
54
	const DUEDATE_NEXT = 1;
55
	const DUEDATE_NOW = 2;
56
	const DUEDATE_OVERDUE = 3;
57
58
	public function __construct() {
59
		$this->addType('id', 'integer');
60
		$this->addType('stackId', 'integer');
61
		$this->addType('order', 'integer');
62
		$this->addType('lastModified', 'integer');
63
		$this->addType('createdAt', 'integer');
64
		$this->addType('archived', 'boolean');
65
		$this->addType('notified', 'boolean');
66
		$this->addType('deletedAt', 'integer');
67
		$this->addRelation('labels');
68
		$this->addRelation('assignedUsers');
69
		$this->addRelation('attachments');
70
		$this->addRelation('attachmentCount');
71
		$this->addRelation('participants');
72
		$this->addRelation('commentsUnread');
73
		$this->addResolvable('owner');
74
	}
75
76
	public function setDatabaseType($type) {
77
		$this->databaseType = $type;
78
	}
79
80
	public function getDuedate($isoFormat = false) {
81
		if ($this->duedate === null) {
82
			return null;
83
		}
84
		$dt = new DateTime($this->duedate);
85
		if (!$isoFormat && $this->databaseType === 'mysql') {
86
			return $dt->format('Y-m-d H:i:s');
87
		}
88
		return $dt->format('c');
89
	}
90
91
	public function jsonSerialize() {
92
		$json = parent::jsonSerialize();
93
		$json['overdue'] = self::DUEDATE_FUTURE;
94
		$due = strtotime($this->duedate);
95
96
		$today = new DateTime();
97
		$today->setTime(0, 0);
98
99
		$match_date = new DateTime($this->duedate);
100
101
		$match_date->setTime(0, 0);
102
103
		$diff = $today->diff($match_date);
104
		$diffDays = (integer) $diff->format('%R%a'); // Extract days count in interval
105
106
		if ($due !== false) {
107
			if ($diffDays === 1) {
108
				$json['overdue'] = self::DUEDATE_NEXT;
109
			}
110
			if ($diffDays === 0) {
111
				$json['overdue'] = self::DUEDATE_NOW;
112
			}
113
			if ($diffDays < 0) {
114
				$json['overdue'] = self::DUEDATE_OVERDUE;
115
			}
116
		}
117
		$json['duedate'] = $this->getDuedate(true);
118
		unset($json['notified']);
119
		unset($json['descriptionPrev']);
120
		return $json;
121
	}
122
123
	public function getCalendarObject(): VCalendar {
124
		$calendar = new VCalendar();
125
		$event = $calendar->createComponent('VEVENT');
126
		$event->UID = 'deck-cardevent' . $this->getId() . '@example.com';
127
		$event->DTSTAMP = new \DateTime($this->getDuedate());
128
		$event->DTSTART = new \DateTime($this->getDuedate());
129
		$event->DTEND = new \DateTime($this->getDuedate());
130
		$event->SUMMARY = $this->getTitle();
131
		$calendar->add($event);
132
		return $calendar;
133
	}
134
135
}
136