ILIAS  release_8 Revision v8.24
class.ilPCFileList.php
Go to the documentation of this file.
1<?php
2
25{
27
28 public function init(): void
29 {
30 $this->setType("flst");
31 }
32
33 public function setNode(php4DOMElement $a_node): void
34 {
35 parent::setNode($a_node); // this is the PageContent node
36 $this->list_node = $a_node->first_child(); // this is the Table node
37 }
38
39 public function create(
40 ilPageObject $a_pg_obj,
41 string $a_hier_id,
42 string $a_pc_id = ""
43 ): void {
44 $this->node = $this->createPageContentNode();
45 $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
46 $this->list_node = $this->dom->create_element("FileList");
47 $this->list_node = $this->node->append_child($this->list_node);
48 }
49
50 public function appendItem(
51 int $a_id,
52 string $a_location,
53 string $a_format
54 ): void {
55 // File Item
56 $new_item = $this->dom->create_element("FileItem");
57 $new_item = $this->list_node->append_child($new_item);
58
59 // Identifier
60 $id_node = $this->dom->create_element("Identifier");
61 $id_node = $new_item->append_child($id_node);
62 $id_node->set_attribute("Catalog", "ILIAS");
63 $id_node->set_attribute("Entry", "il__file_" . $a_id);
64
65 // Location
66 $loc_node = $this->dom->create_element("Location");
67 $loc_node = $new_item->append_child($loc_node);
68 $loc_node->set_attribute("Type", "LocalFile");
69 $loc_node->set_content($a_location);
70
71 // Format
72 $form_node = $this->dom->create_element("Format");
73 $form_node = $new_item->append_child($form_node);
74 $form_node->set_content($a_format);
75 }
76
77 public function setListTitle(
78 string $a_title,
79 string $a_language
80 ): void {
81 ilDOMUtil::setFirstOptionalElement(
82 $this->dom,
83 $this->list_node,
84 "Title",
85 array("FileItem"),
86 $a_title,
87 array("Language" => $a_language)
88 );
89 }
90
91 public function getListTitle(): string
92 {
93 $chlds = $this->list_node->child_nodes();
94 for ($i = 0; $i < count($chlds); $i++) {
95 if ($chlds[$i]->node_name() == "Title") {
96 return $chlds[$i]->get_content();
97 }
98 }
99 return "";
100 }
101
102 public function getLanguage(): string
103 {
104 $chlds = $this->list_node->child_nodes();
105 for ($i = 0; $i < count($chlds); $i++) {
106 if ($chlds[$i]->node_name() == "Title") {
107 return $chlds[$i]->get_attribute("Language");
108 }
109 }
110 return "";
111 }
112
116 public function getFileList(): array
117 {
118 $files = array();
119
120 // File Item
121 $childs = $this->list_node->child_nodes();
122 for ($i = 0; $i < count($childs); $i++) {
123 if ($childs[$i]->node_name() == "FileItem") {
124 $id = $entry = "";
125 $pc_id = $childs[$i]->get_attribute("PCID");
126 $hier_id = $childs[$i]->get_attribute("HierId");
127 $class = $childs[$i]->get_attribute("Class");
128
129 // Identifier
130 $id_node = $childs[$i]->first_child();
131 if ($id_node->node_name() == "Identifier") {
132 $entry = $id_node->get_attribute("Entry");
133 if (substr($entry, 0, 9) == "il__file_") {
134 $id = substr($entry, 9);
135 }
136 }
137 $files[] = array("entry" => $entry, "id" => $id,
138 "pc_id" => $pc_id, "hier_id" => $hier_id,
139 "class" => $class);
140 }
141 }
142
143 return $files;
144 }
145
149 public function deleteFileItems(array $a_ids): void
150 {
151 // File Item
152 $childs = $this->list_node->child_nodes();
153
154 for ($i = 0; $i < count($childs); $i++) {
155 if ($childs[$i]->node_name() == "FileItem") {
156 $id = $entry = "";
157 $pc_id = $childs[$i]->get_attribute("PCID");
158 $hier_id = $childs[$i]->get_attribute("HierId");
159
160 if (in_array($hier_id . ":" . $pc_id, $a_ids)) {
161 $childs[$i]->unlink($childs[$i]);
162 }
163 }
164 }
165 }
166
170 public function savePositions(array $a_pos): void
171 {
172 asort($a_pos);
173
174 // File Item
175 $childs = $this->list_node->child_nodes();
176 $nodes = array();
177 for ($i = 0; $i < count($childs); $i++) {
178 if ($childs[$i]->node_name() == "FileItem") {
179 $pc_id = $childs[$i]->get_attribute("PCID");
180 $hier_id = $childs[$i]->get_attribute("HierId");
181 $nodes[$hier_id . ":" . $pc_id] = $childs[$i];
182 $childs[$i]->unlink($childs[$i]);
183 }
184 }
185
186 foreach ($a_pos as $k => $v) {
187 if (is_object($nodes[$k])) {
188 $nodes[$k] = $this->list_node->append_child($nodes[$k]);
189 }
190 }
191 }
192
196 public function getAllClasses(): array
197 {
198 $classes = array();
199
200 // File Item
201 $childs = $this->list_node->child_nodes();
202
203 for ($i = 0; $i < count($childs); $i++) {
204 if ($childs[$i]->node_name() == "FileItem") {
205 $classes[$childs[$i]->get_attribute("HierId") . ":" .
206 $childs[$i]->get_attribute("PCID")] = $childs[$i]->get_attribute("Class");
207 }
208 }
209
210 return $classes;
211 }
212
216 public function saveStyleClasses(array $a_class): void
217 {
218 // File Item
219 $childs = $this->list_node->child_nodes();
220 for ($i = 0; $i < count($childs); $i++) {
221 if ($childs[$i]->node_name() == "FileItem") {
222 $childs[$i]->set_attribute(
223 "Class",
224 $a_class[$childs[$i]->get_attribute("HierId") . ":" .
225 $childs[$i]->get_attribute("PCID")]
226 );
227 }
228 }
229 }
230
235 public static function getLangVars(): array
236 {
237 return array("ed_edit_files", "ed_insert_filelist", "pc_flist");
238 }
239
243 public static function afterPageUpdate(
244 ilPageObject $a_page,
245 DOMDocument $a_domdoc,
246 string $a_xml,
247 bool $a_creation
248 ): void {
249 if (!$a_page->getImportMode()) {
250 // pc filelist
251 $file_ids = ilObjFile::_getFilesOfObject(
252 $a_page->getParentType() . ":pg",
253 $a_page->getId(),
254 0,
255 $a_page->getLanguage()
256 );
257 self::saveFileUsage($a_page, $a_domdoc);
258
259 foreach ($file_ids as $file) { // check, whether file object can be deleted
260 if (ilObject::_exists($file) && ilObject::_lookupType($file) == "file") {
261 $file_obj = new ilObjFile($file, false);
262 $usages = $file_obj->getUsages();
263 if (count($usages) == 0) { // delete, if no usage exists
264 if ($file_obj->getMode() == "filelist") { // non-repository object
265 $file_obj->delete();
266 }
267 }
268 }
269 }
270 }
271 }
272
276 public static function beforePageDelete(
277 ilPageObject $a_page
278 ): void {
279 $files = self::collectFileItems($a_page, $a_page->getDomDoc());
280
281 // delete all file usages
282 ilObjFile::_deleteAllUsages(
283 $a_page->getParentType() . ":pg",
284 $a_page->getId(),
285 false,
286 $a_page->getLanguage()
287 );
288
289 foreach ($files as $file_id) {
290 if (ilObject::_exists($file_id)) {
291 $file_obj = new ilObjFile($file_id, false);
292 $file_obj->delete();
293 }
294 }
295 }
296
300 public static function afterPageHistoryEntry(
301 ilPageObject $a_page,
302 DOMDocument $a_old_domdoc,
303 string $a_old_xml,
304 int $a_old_nr
305 ): void {
306 self::saveFileUsage($a_page, $a_old_domdoc, $a_old_nr);
307 }
308
312 public static function saveFileUsage(
313 ilPageObject $a_page,
314 DOMDocument $a_domdoc,
315 int $a_old_nr = 0
316 ): void {
317 $file_ids = self::collectFileItems($a_page, $a_domdoc);
318 ilObjFile::_deleteAllUsages($a_page->getParentType() . ":pg", $a_page->getId(), $a_old_nr, $a_page->getLanguage());
319 foreach ($file_ids as $file_id) {
320 ilObjFile::_saveUsage(
321 (int) $file_id,
322 $a_page->getParentType() . ":pg",
323 $a_page->getId(),
324 $a_old_nr,
325 $a_page->getLanguage()
326 );
327 }
328 }
329
333 public static function collectFileItems(
334 ilPageObject $a_page,
335 DOMDocument $a_domdoc
336 ): array {
337 $xpath = new DOMXPath($a_domdoc);
338 $nodes = $xpath->query('//FileItem/Identifier');
339 $file_ids = array();
340 foreach ($nodes as $node) {
341 $id_arr = explode("_", $node->getAttribute("Entry"));
342 $file_id = $id_arr[count($id_arr) - 1];
343 if ($file_id > 0 && ($id_arr[1] == "" || $id_arr[1] == IL_INST_ID || $id_arr[1] == 0)) {
344 $file_ids[$file_id] = $file_id;
345 }
346 }
347 // file items in download links
348 $xpath = new DOMXPath($a_domdoc);
349 $nodes = $xpath->query("//IntLink[@Type='File']");
350 foreach ($nodes as $node) {
351 $t = $node->getAttribute("Target");
352 if (substr($t, 0, 9) == "il__dfile") {
353 $id_arr = explode("_", $t);
354 $file_id = $id_arr[count($id_arr) - 1];
355 $file_ids[$file_id] = $file_id;
356 }
357 }
358 return $file_ids;
359 }
360
361 public static function deleteHistoryLowerEqualThan(
362 string $parent_type,
363 int $page_id,
364 string $lang,
365 int $delete_lower_than_nr
366 ): void {
367 $file_ids = self::_deleteHistoryUsagesLowerEqualThan(
368 $parent_type,
369 $page_id,
370 $delete_lower_than_nr,
371 $lang
372 );
373
374 foreach ($file_ids as $file_id) {
375 if (ilObject::_lookupType($file_id) === "file") {
376 $file = new ilObjFile($file_id, false);
377 $usages = $file->getUsages();
378 if (count($usages) == 0) {
379 $file->delete();
380 }
381 }
382 }
383 }
384
385 protected static function _deleteHistoryUsagesLowerEqualThan(
386 string $parent_type,
387 int $a_id,
388 int $a_usage_hist_nr,
389 string $a_lang = "-"
390 ): array {
391 global $DIC;
392
393 $hist_repo = $DIC->copage()->internal()->repo()->history();
394
395 $file_ids = [];
396 foreach ($hist_repo->getHistoryNumbersOlderEqualThanNr(
397 $a_usage_hist_nr,
398 $parent_type,
399 $a_id,
400 $a_lang
401 ) as $old_nr) {
402 foreach (ilObjFile::_getFilesOfObject($parent_type . ":pg", $a_id, $old_nr, $a_lang) as $file_id) {
403 $file_ids[$file_id] = $file_id;
404 }
405 ilObjFile::_deleteAllUsages($parent_type . ":pg", $a_id, $old_nr, $a_lang);
406 }
407 return $file_ids;
408 }
409}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_INSERT_AFTER
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjFile.
static _lookupType(int $id, bool $reference=false)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setNode(php4DOMElement $a_node)
Set xml node of page content.
static beforePageDelete(ilPageObject $a_page)
Before page is being deleted.
static getLangVars()
Get lang vars needed for editing.
static afterPageUpdate(ilPageObject $a_page, DOMDocument $a_domdoc, string $a_xml, bool $a_creation)
After page has been updated (or created)
static collectFileItems(ilPageObject $a_page, DOMDocument $a_domdoc)
Get all file items that are used within the page.
getAllClasses()
Get all style classes.
static saveFileUsage(ilPageObject $a_page, DOMDocument $a_domdoc, int $a_old_nr=0)
Save file usages.
static deleteHistoryLowerEqualThan(string $parent_type, int $page_id, string $lang, int $delete_lower_than_nr)
Overwrite in derived classes, if old history entries are being deleted.
php4DOMElement $list_node
static _deleteHistoryUsagesLowerEqualThan(string $parent_type, int $a_id, int $a_usage_hist_nr, string $a_lang="-")
saveStyleClasses(array $a_class)
Save style classes of file items.
setListTitle(string $a_title, string $a_language)
savePositions(array $a_pos)
Save positions of file items.
create(ilPageObject $a_pg_obj, string $a_hier_id, string $a_pc_id="")
static afterPageHistoryEntry(ilPageObject $a_page, DOMDocument $a_old_domdoc, string $a_old_xml, int $a_old_nr)
After page history entry has been created.
deleteFileItems(array $a_ids)
Delete file items.
init()
Init object.
appendItem(int $a_id, string $a_location, string $a_format)
getFileList()
Get list of files.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createPageContentNode(bool $a_set_this_node=true)
Create page content node (always use this method first when adding a new element)
setType(string $a_type)
Set Type.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
insertContent(ilPageContent $a_cont_obj, string $a_pos, int $a_mode=IL_INSERT_AFTER, string $a_pcid="", bool $remove_placeholder=true)
insert a content node before/after a sibling or as first child of a parent
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
$i
Definition: metadata.php:41
$lang
Definition: xapiexit.php:26