ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
RepositoryDB.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\WebDAV\Mount;
22
24use InvalidArgumentException;
25
26class RepositoryDB implements Repository
27{
28 public const string TABLE_MOUNT_INSTRUCTIONS = 'webdav_instructions';
29
30 public function __construct(protected ilDBInterface $db)
31 {
32 }
33
34 public function createMountInstructionsDocumentEntry(Document $document): void
35 {
36 $this->db->insert(
37 self::TABLE_MOUNT_INSTRUCTIONS,
38 [
39 'id' => ['int', $document->getId()],
40 'title' => ['text', $document->getTitle()],
41 'uploaded_instructions' => ['clob', $document->getUploadedInstructions()],
42 'processed_instructions' => ['clob', $document->getProcessedInstructions()],
43 'lng' => ['text', $document->getLanguage()],
44 'creation_ts' => ['timestamp', $document->getCreationTs()],
45 'modification_ts' => ['timestamp', $document->getModificationTs()],
46 'owner_usr_id' => ['int', $document->getOwnerUsrId()],
47 'last_modification_usr_id' => ['int', $document->getLastModificationUsrId()],
48 'sorting' => ['int', $document->getSorting()],
49 ]
50 );
51 }
52
54 {
55 if (!$this->db->sequenceExists(self::TABLE_MOUNT_INSTRUCTIONS)) {
56 $this->db->createSequence(self::TABLE_MOUNT_INSTRUCTIONS);
57 }
58
59 return $this->db->nextId(self::TABLE_MOUNT_INSTRUCTIONS);
60 }
61
62 public function getHighestSortingNumber(): int
63 {
64 $query = "SELECT max(sorting) as max_sort FROM " . $this->db->quoteIdentifier(self::TABLE_MOUNT_INSTRUCTIONS);
65 $result = $this->db->query($query);
66
67 $row = $this->db->fetchAssoc($result);
68 return isset($row) && !is_null($row['max_sort']) ? (int) $row['max_sort'] : 0;
69 }
70
72 {
73 $query = "SELECT * FROM " . $this->db->quoteIdentifier(self::TABLE_MOUNT_INSTRUCTIONS)
74 . " WHERE id=" . $this->db->quote($id, 'int');
75
76 $result = $this->db->query($query);
77 $record = $this->db->fetchAssoc($result);
78
79 if (!$record) {
80 throw new InvalidArgumentException("Document with the id $id not found");
81 }
82
83 return $this->buildDocumentFromDatabaseRecord($record);
84 }
85
86 public function getMountInstructionsByLanguage(string $language): Document
87 {
88 $query = "SELECT * FROM " . $this->db->quoteIdentifier(self::TABLE_MOUNT_INSTRUCTIONS)
89 . " WHERE lng=" . $this->db->quote($language, 'text');
90
91 $result = $this->db->query($query);
92 $record = $this->db->fetchAssoc($result);
93
94 if (!$record) {
95 throw new InvalidArgumentException("Document for the language $language not found");
96 }
97
98 return $this->buildDocumentFromDatabaseRecord($record);
99 }
100
101 public function getAllMountInstructions(): array
102 {
103 $query = "SELECT * FROM " . $this->db->quoteIdentifier(self::TABLE_MOUNT_INSTRUCTIONS) . " ORDER BY sorting";
104 $result = $this->db->query($query);
105
106 $document_list = [];
107 while ($record = $this->db->fetchAssoc($result)) {
108 $document_list[] = $this->buildDocumentFromDatabaseRecord($record);
109 }
110
111 return $document_list;
112 }
113
114 public function doMountInstructionsExistByLanguage(string $language): int
115 {
116 $query = "SELECT id FROM " . $this->db->quoteIdentifier(self::TABLE_MOUNT_INSTRUCTIONS)
117 . " WHERE lng=" . $this->db->quote($language, 'text');
118
119 $result = $this->db->query($query);
120 $record = $this->db->fetchAssoc($result);
121
122 return ($record === null ? 0 : (int) $record['id']);
123 }
124
125 public function updateMountInstructions(Document $document): void
126 {
127 $this->db->update(
128 self::TABLE_MOUNT_INSTRUCTIONS,
129 [
130 'title' => ['text', $document->getTitle()],
131 'lng' => ['text', $document->getLanguage()],
132 'creation_ts' => ['timestamp', $document->getCreationTs()],
133 'modification_ts' => ['timestamp', $document->getModificationTs()],
134 'owner_usr_id' => ['int', $document->getOwnerUsrId()],
135 'last_modification_usr_id' => ['int', $document->getLastModificationUsrId()],
136 'sorting' => ['int', $document->getSorting()],
137 ],
138 [
139 'id' => ['int', $document->getId()],
140 ]
141 );
142 }
143
144 public function updateSortingValueById(int $id, int $a_new_sorting_value): void
145 {
146 $this->db->update(
147 self::TABLE_MOUNT_INSTRUCTIONS,
148 [
149 'sorting' => ['int', $a_new_sorting_value],
150 ],
151 [
152 'id' => ['int', $id],
153 ]
154 );
155 }
156
157 public function deleteMountInstructionsById(int $id): void
158 {
159 $query = "DELETE FROM " . $this->db->quoteIdentifier(self::TABLE_MOUNT_INSTRUCTIONS)
160 . ' WHERE id=' . $this->db->quote($id, 'integer');
161
162 $this->db->manipulate($query);
163 }
164
165 protected function buildDocumentFromDatabaseRecord(array $result): Document
166 {
167 return new Document(
168 (int) $result['id'],
169 $result['title'],
170 $result['uploaded_instructions'],
171 $result['processed_instructions'],
172 $result['lng'],
173 $result['creation_ts'],
174 $result['modification_ts'],
175 (int) $result['owner_usr_id'],
176 (int) $result['last_modification_usr_id'],
177 (int) $result['sorting']
178 );
179 }
180}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
buildDocumentFromDatabaseRecord(array $result)
__construct(protected ilDBInterface $db)
doMountInstructionsExistByLanguage(string $language)
updateSortingValueById(int $id, int $a_new_sorting_value)
getMountInstructionsByLanguage(string $language)
createMountInstructionsDocumentEntry(Document $document)
updateMountInstructions(Document $document)
Interface ilDBInterface.