aboutsummaryrefslogtreecommitdiff
path: root/src/Models/Note.php
blob: 901f5aaf0c816cb9716a821f4dc6da5186aafd5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php


namespace Micropoly\Models;


use Micropoly\BoundVal;
use Micropoly\DbQuery;
use Micropoly\Search\SearchExpr;
use SQLite3;

class Note
{
    private bool $savedToDb = false;
    private string $id;
    private string $content;
    private array $tags = [];
    private bool $trash = false;

    /**
     * Note constructor.
     */
    public function __construct()
    {
        $this->id = uniqid("", true);
    }

    /**
     * @param SQLite3 $db
     * @param DbQuery $query
     * @return self[]
     */
    private static function fromQuery(SQLite3 $db, DbQuery $query): array
    {
        $out = [];

        foreach ($query->fetchRowsAssoc($db) as $row) {
            $note = new self();

            $note->savedToDb = true;
            $note->id = $row["id"];
            $note->content = $row["content"];
            $note->trash = (bool)(int)$row["trash"];

            $out[$row["id"]] = $note;
        }

        if (!empty($out)) {
            $q = (new DbQuery("SELECT tag, note_id FROM tags WHERE note_id IN (" . DbQuery::valueListPlaceholders($out) . ")"))
                ->bindMultiText(array_keys($out));

            foreach ($q->fetchRows($db) as [$tag, $id]) {
                $out[$id]->tags[] = $tag;
            }
        }

        return $out;
    }

    /**
     * @param SQLite3 $db
     * @param array $ids
     * @return self[] indexes by id
     */
    public static function byIds(SQLite3 $db, array $ids): array
    {
        if (empty($ids))
            return [];

        $query = (new DbQuery("
            SELECT note.id, content.content, note.trash
            FROM notes note
            INNER JOIN note_contents content
                ON content.rowid = note.content_row
            WHERE id IN (" . DbQuery::valueListPlaceholders($ids) . ")
        "))->bindMultiText($ids);

        return self::fromQuery($db, $query);
    }

    public static function byId(SQLite3 $db, string $id): ?self
    {
        return self::byIds($db, [$id])[$id] ?? null;
    }

    /**
     * @return string
     */
    public function getId(): string
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getContent(): string
    {
        return $this->content;
    }

    /**
     * @param string $content
     * @return Note
     */
    public function setContent(string $content): Note
    {
        $this->content = $content;
        return $this;
    }

    /**
     * @return array
     */
    public function getTags(): array
    {
        return $this->tags;
    }

    /**
     * @param array $tags
     * @return Note
     */
    public function setTags(array $tags): Note
    {
        $tags = array_map("trim", $tags);
        $tags = array_filter($tags);
        $tags = array_unique($tags);

        $this->tags = $tags;
        return $this;
    }

    /**
     * @return bool
     */
    public function isTrash(): bool
    {
        return $this->trash;
    }

    /**
     * @param bool $trash
     * @return Note
     */
    public function setTrash(bool $trash): Note
    {
        $this->trash = $trash;
        return $this;
    }

    private function deleteContent(SQLite3 $db)
    {
        (new DbQuery("DELETE FROM note_contents WHERE rowid IN (SELECT content_row FROM notes WHERE id = ?)"))
            ->bind(1, BoundVal::ofText($this->id))
            ->exec($db);
    }

    private function deleteTags(SQLite3 $db)
    {
        (new DbQuery("DELETE FROM tags WHERE note_id = ?"))
            ->bind(1, BoundVal::ofText($this->id))
            ->exec($db);
    }

    public function save(SQLite3 $db)
    {
        if ($this->savedToDb)
            $this->update($db);
        else
            $this->insert($db);
    }

    private function insert(SQLite3 $db)
    {
        $db->exec("BEGIN");

        $this->deleteContent($db);

        DbQuery::insertKV($db, "note_contents", ["content" => BoundVal::ofText($this->content)]);
        $rowid = (new DbQuery("SELECT last_insert_rowid()"))->fetchRow($db)[0];

        DbQuery::insertKV($db, "notes", [
            "id" => BoundVal::ofText($this->id),
            "content_row" => BoundVal::ofInt($rowid),
            "trash" => BoundVal::ofInt($this->trash ? 0 : 1),
        ]);

        $this->writeTags($db);

        $db->exec("COMMIT");
    }

    private function update(SQLite3 $db)
    {
        $db->exec("BEGIN");

        $this->deleteTags($db);

        (new DbQuery("
            UPDATE note_contents
            SET content = :content
            WHERE rowid = (
                SELECT content_row
                FROM notes
                WHERE id = :id
            )
        "))
            ->bind("content", BoundVal::ofText($this->content))
            ->bind("id", BoundVal::ofText($this->id))
            ->exec($db);

        $this->writeTags($db);

        (new DbQuery("
            UPDATE notes
            SET changed_at = CURRENT_TIMESTAMP,
                trash = :trash
            WHERE id = :id
        "))
            ->bind("id", BoundVal::ofText($this->id))
            ->bind("trash", BoundVal::ofInt($this->trash ? 0 : 1))
            ->exec($db);

        $db->exec("COMMIT");
    }

    /**
     * @param SQLite3 $db
     */
    private function writeTags(SQLite3 $db): void
    {
        $this->deleteTags($db);
        DbQuery::insert($db,
            "tags",
            ["note_id", "tag"],
            array_map(fn($t) => [BoundVal::ofText($this->id), BoundVal::ofText($t)], $this->tags)
        );
    }

    public function delete(SQLite3 $db): void
    {
        $this->deleteTags($db);
        $this->deleteContent($db);
        (new DbQuery("DELETE FROM notes WHERE id = ?"))
            ->bind(1, BoundVal::ofText($this->id))
            ->exec($db);
        $this->savedToDb = false;
    }
}