ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ImportantPageDBRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Wiki\Navigation;
22
25
30{
32 protected \ilDBInterface $db;
33
34 public function __construct(
37 ) {
38 $this->data = $data;
39 $this->db = $db;
40 }
41
42 protected function getPageInfoFromRecord(array $rec): ImportantPage
43 {
44 return $this->data->importantPage(
45 (int) $rec["page_id"],
46 (int) $rec["ord"],
47 (int) $rec["indent"]
48 );
49 }
50
54 public function getList(int $wiki_id): \Iterator
55 {
56 $set = $this->db->query(
57 "SELECT * FROM il_wiki_imp_pages WHERE " .
58 " wiki_id = " . $this->db->quote($wiki_id, "integer") . " ORDER BY ord ASC "
59 );
60 while ($rec = $this->db->fetchAssoc($set)) {
61 yield $this->getPageInfoFromRecord($rec);
62 }
63 }
64
65 public function getListAsArray(int $wiki_id): array
66 {
67 $ipages = [];
68 foreach ($this->getList($wiki_id) as $ip) {
69 $ipages[$ip->getId()]["page_id"] = $ip->getId();
70 $ipages[$ip->getId()]["ord"] = $ip->getOrder();
71 $ipages[$ip->getId()]["indent"] = $ip->getIndent();
72 $ipages[$ip->getId()]["wiki_id"] = $wiki_id;
73 }
74 return $ipages;
75 }
76
77 protected function getMaxOrdNr(
78 int $wiki_id
79 ): int {
80 $set = $this->db->query(
81 "SELECT MAX(ord) as m FROM il_wiki_imp_pages WHERE " .
82 " wiki_id = " . $this->db->quote($wiki_id, "integer")
83 );
84 $rec = $this->db->fetchAssoc($set);
85 return (int) $rec["m"];
86 }
87
88
89 public function add(
90 int $wiki_id,
91 int $page_id,
92 int $nr = 0,
93 int $indent = 0
94 ): void {
95 if (!$this->isImportantPage($wiki_id, $page_id)) {
96 if ($nr === 0) {
97 $nr = $this->getMaxOrdNr($wiki_id) + 10;
98 }
99
100 $this->db->manipulate("INSERT INTO il_wiki_imp_pages " .
101 "(wiki_id, ord, indent, page_id) VALUES (" .
102 $this->db->quote($wiki_id, "integer") . "," .
103 $this->db->quote($nr, "integer") . "," .
104 $this->db->quote($indent, "integer") . "," .
105 $this->db->quote($page_id, "integer") .
106 ")");
107 }
108 }
109
110 public function isImportantPage(
111 int $wiki_id,
112 int $page_id
113 ): bool {
114 $set = $this->db->query(
115 "SELECT * FROM il_wiki_imp_pages WHERE " .
116 " wiki_id = " . $this->db->quote($wiki_id, "integer") . " AND " .
117 " page_id = " . $this->db->quote($page_id, "integer")
118 );
119 if ($this->db->fetchAssoc($set)) {
120 return true;
121 }
122 return false;
123 }
124
125 public function removeImportantPage(
126 int $wiki_id,
127 int $page_id
128 ): void {
129 $this->db->manipulate(
130 "DELETE FROM il_wiki_imp_pages WHERE "
131 . " wiki_id = " . $this->db->quote($wiki_id, "integer")
132 . " AND page_id = " . $this->db->quote($page_id, "integer")
133 );
134 $this->fixImportantPagesNumbering($wiki_id);
135 }
136
138 int $wiki_id,
139 array $a_ord,
140 array $a_indent
141 ): bool {
142 $ipages = $this->getListAsArray($wiki_id);
143
144 foreach ($ipages as $k => $v) {
145 if (isset($a_ord[$v["page_id"]])) {
146 $ipages[$k]["ord"] = (int) $a_ord[$v["page_id"]];
147 }
148 if (isset($a_indent[$v["page_id"]])) {
149 $ipages[$k]["indent"] = (int) $a_indent[$v["page_id"]];
150 }
151 }
152 $ipages = \ilArrayUtil::sortArray($ipages, "ord", "asc", true);
153
154 // fix indentation: no 2 is allowed after a 0
155 $c_indent = 0;
156 $fixed = false;
157 foreach ($ipages as $k => $v) {
158 if ($v["indent"] == 2 && $c_indent == 0) {
159 $ipages[$k]["indent"] = 1;
160 $fixed = true;
161 }
162 $c_indent = $ipages[$k]["indent"];
163 }
164
165 $ord = 10;
166 reset($ipages);
167 foreach ($ipages as $k => $v) {
168 $this->db->manipulate(
169 $q = "UPDATE il_wiki_imp_pages SET " .
170 " ord = " . $this->db->quote($ord, "integer") . "," .
171 " indent = " . $this->db->quote($v["indent"], "integer") .
172 " WHERE wiki_id = " . $this->db->quote($wiki_id, "integer") .
173 " AND page_id = " . $this->db->quote($v["page_id"], "integer")
174 );
175 $ord += 10;
176 }
177
178 return $fixed;
179 }
180
181 protected function fixImportantPagesNumbering(
182 int $wiki_id
183 ): void {
184 $ipages = $this->getListAsArray($wiki_id);
185 // fix indentation: no 2 is allowed after a 0
186 $c_indent = 0;
187 foreach ($ipages as $k => $v) {
188 if ($v["indent"] == 2 && $c_indent == 0) {
189 $ipages[$k]["indent"] = 1;
190 }
191 $c_indent = $ipages[$k]["indent"];
192 }
193
194 $ord = 10;
195 foreach ($ipages as $k => $v) {
196 $this->db->manipulate(
197 $q = "UPDATE il_wiki_imp_pages SET " .
198 " ord = " . $this->db->quote($ord, "integer") .
199 ", indent = " . $this->db->quote($v["indent"], "integer") .
200 " WHERE wiki_id = " . $this->db->quote($v["wiki_id"], "integer") .
201 " AND page_id = " . $this->db->quote($v["page_id"], "integer")
202 );
203 $ord += 10;
204 }
205 }
206
207 public function getImportantPageIds(int $wiki_id): array
208 {
209 $set = $this->db->query(
210 "SELECT DISTINCT page_id FROM il_wiki_imp_pages WHERE " .
211 " wiki_id = " . $this->db->quote($wiki_id, "integer")
212 );
213 $ids = [];
214 while ($rec = $this->db->fetchAssoc($set)) {
215 $ids[] = (int) $rec["page_id"];
216 }
217 return $ids;
218 }
219
220}
Repository internal data service.
saveOrderingAndIndentation(int $wiki_id, array $a_ord, array $a_indent)
add(int $wiki_id, int $page_id, int $nr=0, int $indent=0)
__construct(InternalDataService $data, \ilDBInterface $db)
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
Interface ilDBInterface.
if(!file_exists('../ilias.ini.php'))
$q
Definition: shib_logout.php:23