ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilPCFileList.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
6require_once("./Services/COPage/classes/class.ilPageContent.php");
7
19{
20 public $list_node;
21
25 public function init()
26 {
27 $this->setType("flst");
28 }
29
30 public function setNode($a_node)
31 {
32 parent::setNode($a_node); // this is the PageContent node
33 $this->list_node = $a_node->first_child(); // this is the Table node
34 }
35
36 public function create(&$a_pg_obj, $a_hier_id, $a_pc_id = "")
37 {
38 $this->node = $this->createPageContentNode();
39 $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
40 $this->list_node = $this->dom->create_element("FileList");
41 $this->list_node = $this->node->append_child($this->list_node);
42 }
43
44 /*
45 function addItems($a_nr)
46 {
47 for ($i=1; $i<=$a_nr; $i++)
48 {
49 $new_item = $this->dom->create_element("ListItem");
50 $new_item = $this->list_node->append_child($new_item);
51 }
52 }*/
53
54 public function appendItem($a_id, $a_location, $a_format)
55 {
56 // File Item
57 $new_item = $this->dom->create_element("FileItem");
58 $new_item = $this->list_node->append_child($new_item);
59
60 // Identifier
61 $id_node = $this->dom->create_element("Identifier");
62 $id_node = $new_item->append_child($id_node);
63 $id_node->set_attribute("Catalog", "ILIAS");
64 $id_node->set_attribute("Entry", "il__file_" . $a_id);
65
66 // Location
67 $loc_node = $this->dom->create_element("Location");
68 $loc_node = $new_item->append_child($loc_node);
69 $loc_node->set_attribute("Type", "LocalFile");
70 $loc_node->set_content($a_location);
71
72 // Format
73 $form_node = $this->dom->create_element("Format");
74 $form_node = $new_item->append_child($form_node);
75 $form_node->set_content($a_format);
76 }
77
78 public function setListTitle($a_title, $a_language)
79 {
81 $this->dom,
82 $this->list_node,
83 "Title",
84 array("FileItem"),
85 $a_title,
86 array("Language" => $a_language)
87 );
88 }
89
90 public function getListTitle()
91 {
92 $chlds = $this->list_node->child_nodes();
93 for ($i = 0; $i < count($chlds); $i++) {
94 if ($chlds[$i]->node_name() == "Title") {
95 return $chlds[$i]->get_content();
96 }
97 }
98 return "";
99 }
100
101 public function getLanguage()
102 {
103 $chlds = $this->list_node->child_nodes();
104 for ($i = 0; $i < count($chlds); $i++) {
105 if ($chlds[$i]->node_name() == "Title") {
106 return $chlds[$i]->get_attribute("Language");
107 }
108 }
109 return "";
110 }
111
115 public function getFileList()
116 {
117 $files = array();
118
119 // File Item
120 $childs = $this->list_node->child_nodes();
121 for ($i = 0; $i < count($childs); $i++) {
122 if ($childs[$i]->node_name() == "FileItem") {
123 $id = $entry = "";
124 $pc_id = $childs[$i]->get_attribute("PCID");
125 $hier_id = $childs[$i]->get_attribute("HierId");
126 $class = $childs[$i]->get_attribute("Class");
127
128 // Identifier
129 $id_node = $childs[$i]->first_child();
130 if ($id_node->node_name() == "Identifier") {
131 $entry = $id_node->get_attribute("Entry");
132 if (substr($entry, 0, 9) == "il__file_") {
133 $id = substr($entry, 9);
134 }
135 }
136 $files[] = array("entry" => $entry, "id" => $id,
137 "pc_id" => $pc_id, "hier_id" => $hier_id,
138 "class" => $class);
139 }
140 }
141
142 return $files;
143 }
144
148 public function deleteFileItems($a_ids)
149 {
150 $files = array();
151
152 // File Item
153 $childs = $this->list_node->child_nodes();
154
155 for ($i = 0; $i < count($childs); $i++) {
156 if ($childs[$i]->node_name() == "FileItem") {
157 $id = $entry = "";
158 $pc_id = $childs[$i]->get_attribute("PCID");
159 $hier_id = $childs[$i]->get_attribute("HierId");
160
161 if (in_array($hier_id . ":" . $pc_id, $a_ids)) {
162 $childs[$i]->unlink($childs[$i]);
163 }
164 }
165 }
166 }
167
171 public function savePositions($a_pos)
172 {
173 asort($a_pos);
174
175 // File Item
176 $childs = $this->list_node->child_nodes();
177 $nodes = array();
178 for ($i = 0; $i < count($childs); $i++) {
179 if ($childs[$i]->node_name() == "FileItem") {
180 $id = $entry = "";
181 $pc_id = $childs[$i]->get_attribute("PCID");
182 $hier_id = $childs[$i]->get_attribute("HierId");
183 $nodes[$hier_id . ":" . $pc_id] = $childs[$i];
184 $childs[$i]->unlink($childs[$i]);
185 }
186 }
187
188 foreach ($a_pos as $k => $v) {
189 if (is_object($nodes[$k])) {
190 $nodes[$k] = $this->list_node->append_child($nodes[$k]);
191 }
192 }
193 }
194
198 public function getAllClasses()
199 {
200 $classes = array();
201
202 // File Item
203 $childs = $this->list_node->child_nodes();
204
205 for ($i = 0; $i < count($childs); $i++) {
206 if ($childs[$i]->node_name() == "FileItem") {
207 $classes[$childs[$i]->get_attribute("HierId") . ":" .
208 $childs[$i]->get_attribute("PCID")] = $childs[$i]->get_attribute("Class");
209 }
210 }
211
212 return $classes;
213 }
214
218 public function saveStyleClasses($a_class)
219 {
220 // File Item
221 $childs = $this->list_node->child_nodes();
222 for ($i = 0; $i < count($childs); $i++) {
223 if ($childs[$i]->node_name() == "FileItem") {
224 $childs[$i]->set_attribute(
225 "Class",
226 $a_class[$childs[$i]->get_attribute("HierId") . ":" .
227 $childs[$i]->get_attribute("PCID")]
228 );
229 }
230 }
231 }
232
237 public static function getLangVars()
238 {
239 return array("ed_edit_files", "ed_insert_filelist", "pc_flist");
240 }
241
250 public static function afterPageUpdate($a_page, DOMDocument $a_domdoc, $a_xml, $a_creation)
251 {
252 if (!$a_page->getImportMode()) {
253 // pc filelist
254 include_once("./Modules/File/classes/class.ilObjFile.php");
256 $a_page->getParentType() . ":pg",
257 $a_page->getId(),
258 0,
259 $a_page->getLanguage()
260 );
261 self::saveFileUsage($a_page, $a_domdoc);
262
263 foreach ($file_ids as $file) { // check, whether file object can be deleted
264 if (ilObject::_exists($file) && ilObject::_lookupType($file) == "file") {
265 $file_obj = new ilObjFile($file, false);
266 $usages = $file_obj->getUsages();
267 if (count($usages) == 0) { // delete, if no usage exists
268 if ($file_obj->getMode() == "filelist") { // non-repository object
269 $file_obj->delete();
270 }
271 }
272 }
273 }
274 }
275 }
276
282 public static function beforePageDelete($a_page)
283 {
284 $files = self::collectFileItems($a_page, $a_page->getDomDoc());
285
286 // delete all file usages
287 include_once("./Modules/File/classes/class.ilObjFile.php");
289 $a_page->getParentType() . ":pg",
290 $a_page->getId(),
291 false,
292 $a_page->getLanguage()
293 );
294
295 include_once("./Modules/File/classes/class.ilObjFile.php");
296 foreach ($files as $file_id) {
297 if (ilObject::_exists($file_id)) {
298 $file_obj = new ilObjFile($file_id, false);
299 $file_obj->delete();
300 }
301 }
302 }
303
312 public static function afterPageHistoryEntry($a_page, DOMDocument $a_old_domdoc, $a_old_xml, $a_old_nr)
313 {
314 self::saveFileUsage($a_page, $a_old_domdoc, $a_old_nr);
315 }
316
320 public static function saveFileUsage($a_page, $a_domdoc, $a_old_nr = 0)
321 {
322 $file_ids = self::collectFileItems($a_page, $a_domdoc);
323 include_once("./Modules/File/classes/class.ilObjFile.php");
324 ilObjFile::_deleteAllUsages($a_page->getParentType() . ":pg", $a_page->getId(), $a_old_nr, $a_page->getLanguage());
325 foreach ($file_ids as $file_id) {
327 $file_id,
328 $a_page->getParentType() . ":pg",
329 $a_page->getId(),
330 $a_old_nr,
331 $a_page->getLanguage()
332 );
333 }
334 }
335
339 public static function collectFileItems($a_page, $a_domdoc)
340 {
341 $xpath = new DOMXPath($a_domdoc);
342 $nodes = $xpath->query('//FileItem/Identifier');
343 $file_ids = array();
344 foreach ($nodes as $node) {
345 $id_arr = explode("_", $node->getAttribute("Entry"));
346 $file_id = $id_arr[count($id_arr) - 1];
347 if ($file_id > 0 && ($id_arr[1] == "" || $id_arr[1] == IL_INST_ID || $id_arr[1] == 0)) {
348 $file_ids[$file_id] = $file_id;
349 }
350 }
351 // file items in download links
352 $xpath = new DOMXPath($a_domdoc);
353 $nodes = $xpath->query("//IntLink[@Type='File']");
354 foreach ($nodes as $node) {
355 $t = $node->getAttribute("Target");
356 if (substr($t, 0, 9) == "il__dfile") {
357 $id_arr = explode("_", $t);
358 $file_id = $id_arr[count($id_arr) - 1];
359 $file_ids[$file_id] = $file_id;
360 }
361 }
362 return $file_ids;
363 }
364}
An exception for terminatinating execution or to throw for unit testing.
const IL_INSERT_AFTER
static setFirstOptionalElement( $doc, $parent_node, $a_node_name, $a_successors, $a_content, $a_attributes, $a_remove_childs=true)
searches for an element $a_node_name within the childs of $parent_node if no node is found,...
Class ilObjFile.
static _getFilesOfObject($a_type, $a_id, $a_usage_hist_nr=0, $a_usage_lang="-")
get all files of an object
static _saveUsage($a_file_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_usage_lang="-")
save usage
static _deleteAllUsages($a_type, $a_id, $a_usage_hist_nr=0, $a_usage_lang="-")
static delete all usages of
static _exists($a_id, $a_reference=false, $a_type=null)
checks if an object exists in object_data@access public
static _lookupType($a_id, $a_reference=false)
lookup object type
Class ilPCFileList.
static afterPageHistoryEntry($a_page, DOMDocument $a_old_domdoc, $a_old_xml, $a_old_nr)
After page history entry has been created.
appendItem($a_id, $a_location, $a_format)
static getLangVars()
Get lang vars needed for editing.
create(&$a_pg_obj, $a_hier_id, $a_pc_id="")
static collectFileItems($a_page, $a_domdoc)
Get all file items that are used within the page.
setNode($a_node)
Set xml node of page content.
static beforePageDelete($a_page)
Before page is being deleted.
getAllClasses()
Get all style classes.
static afterPageUpdate($a_page, DOMDocument $a_domdoc, $a_xml, $a_creation)
After page has been updated (or created)
saveStyleClasses($a_class)
Save style classes of file items.
savePositions($a_pos)
Save positions of file items.
deleteFileItems($a_ids)
Delete file items.
init()
Init page content component.
static saveFileUsage($a_page, $a_domdoc, $a_old_nr=0)
Save file usages.
getFileList()
Get list of files.
setListTitle($a_title, $a_language)
Class ilPageContent.
createPageContentNode($a_set_this_node=true)
Create page content node (always use this method first when adding a new element)
setType($a_type)
Set Type.
$i
Definition: metadata.php:24