ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilObjFolder.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
26 
27 require_once "./Services/Container/classes/class.ilContainer.php";
28 
37 class ilObjFolder extends ilContainer
38 {
39  public $folder_tree;
40  // bugfix mantis 24309: array for systematically numbering copied files
41  private static $duplicate_files = array();
42 
43 
50  public function __construct($a_id = 0, $a_call_by_reference = true)
51  {
52  global $DIC;
53 
54  $this->tree = $DIC->repositoryTree();
55  $this->lng = $DIC->language();
56  $this->rbacsystem = $DIC->rbac()->system();
57  $this->access = $DIC->access();
58  $this->type = "fold";
59  parent::__construct($a_id, $a_call_by_reference);
60  $this->lng->loadLanguageModule('fold');
61  }
62 
63  public function setFolderTree($a_tree)
64  {
65  $this->folder_tree = &$a_tree;
66  }
67 
76  public function cloneObject($a_target_id, $a_copy_id = 0, $a_omit_tree = false)
77  {
78  $new_obj = parent::cloneObject($a_target_id, $a_copy_id, $a_omit_tree);
79 
80  // Copy learning progress settings
81  include_once('Services/Tracking/classes/class.ilLPObjSettings.php');
82  $obj_settings = new ilLPObjSettings($this->getId());
83  $obj_settings->cloneSettings($new_obj->getId());
84  unset($obj_settings);
85 
86  return $new_obj;
87  }
88 
93  public function putInTree($a_parent)
94  {
96 
97  if (!is_object($this->folder_tree)) {
98  $this->folder_tree = &$tree;
99  }
100 
101  if ($this->withReferences()) {
102  // put reference id into tree
103  $this->folder_tree->insertNode($this->getRefId(), $a_parent);
104  } else {
105  // put object id into tree
106  $this->folder_tree->insertNode($this->getId(), $a_parent);
107  }
108  }
109 
118  public function cloneDependencies($a_target_id, $a_copy_id)
119  {
120  parent::cloneDependencies($a_target_id, $a_copy_id);
121 
122  include_once('Services/Object/classes/class.ilObjectActivation.php');
123  ilObjectActivation::cloneDependencies($this->getRefId(), $a_target_id, $a_copy_id);
124 
125  return true;
126  }
127 
128 
143  private static function recurseFolder($ref_id, $title, $tmpdir)
144  {
145  global $DIC;
146 
147  $tree = $DIC->repositoryTree();
148  $ilAccess = $DIC->access();
149 
150  $tmpdir .= DIRECTORY_SEPARATOR . ilUtil::getASCIIFilename($title);
151 
152  $temp_fs = $DIC->filesystem()->temp();
153  $temp_fs->createDir($tmpdir);
154 
155  $subtree = $tree->getChildsByTypeFilter($ref_id, array('fold', 'file'));
156 
157  $storage_fs = $DIC->filesystem()->storage();
158 
159  foreach ($subtree as $child) {
160  if (!$ilAccess->checkAccess('read', '', $child['ref_id'])) {
161  continue;
162  }
163  if (ilObject::_isInTrash($child['ref_id'])) {
164  continue;
165  }
166  if ($child['type'] === 'fold') {
167  ilObjFolder::recurseFolder($child["ref_id"], $child["title"], $tmpdir);
168  } else {
169  // enhance new_filename to full relative path to enable checking for identically named files
170  $new_filename = $tmpdir . DIRECTORY_SEPARATOR . ilUtil::getASCIIFilename($child["title"]);
172  $new_filename = $sanitizer->sanitize($new_filename);
173  if ($temp_fs->has($new_filename)) {
174  $new_filename = self::renameDuplicateFile($new_filename);
175  }
176 
177  // copy to temporal directory
178  $relative_path_of_file = LegacyPathHelper::createRelativePath(ilObjFile::_lookupAbsolutePath($child["obj_id"]));
179  if ($storage_fs->has($relative_path_of_file)) {
180  $s = $storage_fs->readStream($relative_path_of_file);
181  } else {
182  throw new ilFileException('Could not copy ' . $relative_path_of_file . ' to ' . $new_filename);
183  }
184  $temp_fs->writeStream($new_filename, $s);
185  }
186  }
187  }
188 
189 
199  private static function renameDuplicateFile($duplicate_filename)
200  {
201  // determine the copy_number that will be added to the filename either by obtaining it from
202  // the entry of the current file in the duplicate_files-array or use 1 if there is no entry yet
203  $copy_number = 1;
204  $duplicate_has_array_entry = false;
205  foreach (self::$duplicate_files as &$duplicate_file) {
206  if ($duplicate_file['file_name'] == $duplicate_filename) {
207  $duplicate_has_array_entry = true;
208  $copy_number = $duplicate_file['copy_number'];
209  // increment the copy_number for correctly renaming the next duplicate of this file
210  $duplicate_file['copy_number']++;
211  }
212  }
213 
214  // create an array entry for the duplicate file if there isn't one to ensure that the
215  // copy_number can be determined correctly for other duplicates of this file
216  if (!$duplicate_has_array_entry) {
217  self::$duplicate_files[] = [
218  'file_name' => $duplicate_filename,
219  'copy_number' => 2 // set as 2 because 1 is already used for this duplicate
220  ];
221  }
222 
223  // rename the file
224  $path = pathinfo($duplicate_filename, PATHINFO_DIRNAME);
225  $filename = pathinfo($duplicate_filename, PATHINFO_FILENAME);
226  $extension = pathinfo($duplicate_filename, PATHINFO_EXTENSION);
227  $new_filename = $path . "/" . $filename . " (" . $copy_number . ")." . $extension;
228 
229  return $new_filename;
230  }
231 
232 
233  public function downloadFolder()
234  {
235  $ilAccess = $this->access;
236 
237  if (!$ilAccess->checkAccess("read", "", $this->getRefId())) {
238  $this->ilErr->raiseError(get_class($this) . "::downloadFolder(): missing read permission!", $this->ilErr->WARNING);
239  }
240  if (ilObject::_isInTrash($this->getRefId())) {
241  $this->ilErr->raiseError(get_class($this) . "::downloadFolder(): object is trashed!", $this->ilErr->WARNING);
242  }
243 
244  $tmpdir = ilUtil::ilTempnam();
245  ilUtil::makeDir($tmpdir);
246  $basename = ilUtil::getAsciiFilename($this->getTitle());
247  $deliverFilename = $basename . ".zip";
248  $zipbasedir = $tmpdir . DIRECTORY_SEPARATOR . $basename;
249  $tmpzipfile = $tmpdir . DIRECTORY_SEPARATOR . $deliverFilename;
250 
251  try {
252  ilObjFolder::recurseFolder($this->getRefId(), $this->getTitle(), LegacyPathHelper::createRelativePath($tmpdir));
253  ilUtil::zip($zipbasedir, $tmpzipfile);
254  rename($tmpzipfile, $zipfile = ilUtil::ilTempnam());
255  ilUtil::delDir($tmpdir);
256  ilUtil::deliverFile($zipfile, $deliverFilename, '', false, true);
257  } catch (ilFileException $e) {
258  ilUtil::sendInfo($e->getMessage(), true);
259  }
260  }
261 
265  public function getViewMode()
266  {
267  $tree = $this->tree;
268 
269  // default: by type
271 
272  // always inherit from
273  $container_ref_id = $tree->checkForParentType($this->ref_id, 'grp');
274  if (!$container_ref_id) {
275  $container_ref_id = $tree->checkForParentType($this->ref_id, 'crs');
276  }
277  if ($container_ref_id) {
278  include_once("./Modules/Course/classes/class.ilObjCourseAccess.php");
279  $view_mode = ilObjCourseAccess::_lookupViewMode(ilObject::_lookupObjId($container_ref_id));
280  if ($view_mode == ilContainer::VIEW_SESSIONS ||
281  $view_mode == ilContainer::VIEW_BY_TYPE ||
282  $view_mode == ilContainer::VIEW_SIMPLE) {
283  $view = $view_mode;
284  }
285  }
286 
287  return $view;
288  }
289 
294  public function addAdditionalSubItemInformation(&$a_item_data)
295  {
296  include_once './Services/Object/classes/class.ilObjectActivation.php';
298  }
299 
307  public function read()
308  {
309  $tree = $this->tree;
310 
311  parent::read();
312 
313  // Inherit order type from parent course (if exists)
314  include_once('./Services/Container/classes/class.ilContainerSortingSettings.php');
316  }
317 } // END class.ilObjFolder
__construct($a_id=0, $a_call_by_reference=true)
Constructor public.
$path
Definition: aliased.php:25
Class ilObjFolder.
global $DIC
Definition: saml.php:7
withReferences()
determines wehter objects are referenced or not (got ref ids or not)
putInTree($a_parent)
insert folder into grp_tree
static _isInTrash($a_ref_id)
checks wether object is in trash
$s
Definition: pwgen.php:45
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static recurseFolder($ref_id, $title, $tmpdir)
private functions which iterates through all folders and files and create an according file structure...
static $duplicate_files
setFolderTree($a_tree)
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
static addAdditionalSubItemInformation(array &$a_item)
Parse item data for list entries.
getId()
get object id public
read()
Overwritten read method.
cloneObject($a_target_id, $a_copy_id=0, $a_omit_tree=false)
Clone folder.
static _lookupObjId($a_id)
if(empty( $files_to_scan)) $sanitizer
Definition: svg-scanner.php:90
addAdditionalSubItemInformation(&$a_item_data)
Add additional information to sub item, e.g.
getTitle()
get object title public
getViewMode()
Get container view mode.
Class to report exception.
cloneDependencies($a_target_id, $a_copy_id)
Clone object dependencies (crs items, preconditions)
Class ilContainer.
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
$filename
Definition: buildRTE.php:89
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
setOrderType($a_value)
static cloneDependencies($a_ref_id, $a_target_id, $a_copy_id)
Clone dependencies.
static _lookupAbsolutePath($obj_id, $a_version=null)
getRefId()
get reference id public
static _lookupSortMode($a_obj_id)
lookup sort mode
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static renameDuplicateFile($duplicate_filename)
bugfix mantis 24309: add "_copy_" followed by a number to the filename (in front of the file-type-ext...
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
static _lookupViewMode($a_id)
Lookup view mode.