ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCloudFileTree.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Modules/Cloud/exceptions/class.ilCloudException.php");
5 include_once("class.ilCloudFileNode.php");
6 include_once("class.ilCloudConnector.php");
7 include_once("class.ilCloudUtil.php");
8 
21 {
22 
27  protected $id = 0;
28 
32  protected $root_node = null;
33 
38  protected $root_path = "";
42  protected $item_list = array();
43 
47  protected $id_to_path_map = array();
48 
52  protected $service_name = "";
53 
57  protected $case_sensitive = false;
58 
66  public function __construct($root_path = "/", $root_id = "root", $id, $service_name)
67  {
68  $this->setId($id);
69  $this->root_node = $this->createNode($root_path, $root_id, true);
72  $this->setCaseSensitive($service->isCaseSensitive());
73  }
74 
78  protected function setId($id)
79  {
80  $this->id = $id;
81  }
82 
86  public function getId()
87  {
88  return $this->id;
89  }
90 
91 
95  protected function setRootPath($path = "/")
96  {
97  $this->root_path = ilCloudUtil::normalizePath($path);
98  }
99 
103  public function getRootPath()
104  {
105  return $this->root_path;
106  }
107 
111  protected function setServiceName($service_name)
112  {
113  $this->service_name = $service_name;
114  }
115 
119  public function getServiceName()
120  {
121  return $this->service_name;
122  }
123 
127  public function isCaseSensitive()
128  {
129  return $this->case_sensitive;
130  }
131 
136  {
137  $this->case_sensitive = $case_sensitive;
138  }
139 
143  public function getRootNode()
144  {
145  return $this->root_node;
146  }
147 
153  protected function createNode($path = "/", $id, $is_dir = false)
154  {
156  $this->item_list[$node->getPath()] = $node;
157  $this->id_to_path_map[$node->getId()] = $node->getPath();
158  $node->setIsDir($is_dir);
159  return $node;
160  }
161 
169  public function addNode($path, $id,$is_Dir, $modified = null, $size = 0)
170  {
172  $node = $this->getNodeFromPath($path);
173 
174  //node does not yet exist
175  if (!$node)
176  {
177  if ($this->getNodeFromId($id))
178  {
180  }
181  $path_of_parent = ilCloudUtil::normalizePath(dirname($path));
182  $node_parent = $this->getNodeFromPath($path_of_parent);
183  if(!$node_parent)
184  {
186  }
187  $node = $this->createNode($path, $id, $is_Dir);
188  $node->setParentId($node_parent->getId());
189  $node_parent->addChild($node->getPath());
190  }
191 
192  $node->setSize($size);
193  $node->setModified($modified);
194 
195  return $node;
196 
197  }
198 
202  public function removeNode($path)
203  {
204  $node = $this->getNodeFromPath($path);
205  $parent = $this->getNodeFromId($node->getParentId());
206  $parent->removeChild($path);
207  unset($this->item_list[$node->getPath()]);
208  unset($this->id_to_path_map[$node->getId()]);
209  }
210 
214  public function getItemList()
215  {
216  return $this->item_list;
217  }
218 
223  public function getNodeFromPath($path = "/")
224  {
225  if (!$this->isCaseSensitive() || $this->item_list[$path])
226  {
227  return $this->item_list[$path];
228  }
229 
230  foreach (array_keys($this->item_list) as $item)
231  {
232  if (strtolower($item) == strtolower($path))
233  {
234  return $this->item_list[$item];
235  }
236  }
237 
238  return null;
239  }
240 
245  public function getNodeFromId($id)
246  {
247  return $this->item_list[$this->id_to_path_map[$id]];
248  }
249 
255  {
256  $node = $this->getNodeFromPath($path);
257  if(!$node)
258  {
260  }
261  $node->setLoadingComplete(true);
262  }
263 
267  public function updateFileTree($current_path)
268  {
269  $node = $this->getNodeFromPath($current_path);
270 
271  if (!$node)
272  {
273  $this->updateFileTree(dirname($current_path));
274  $node = $this->getNodeFromPath($current_path);
275  }
276  if (!$node->getLoadingComplete())
277  {
278  $this->addItemsFromService($node->getId());
279  }
280  $this->storeFileTreeToSession();
281  }
282 
287  public function addItemsFromService($folder_id)
288  {
289  try
290  {
291  $node = $this->getNodeFromId($folder_id);
292  if(!$node)
293  {
295  }
296  $service = ilCloudConnector::getServiceClass($this->getServiceName(), $this->getId());
297  $service->addToFileTree($this, $node->getPath());
298  } catch (Exception $e)
299  {
300  if ($e instanceof ilCloudException)
301  {
302  throw $e;
303  }
304  throw new ilCloudException(ilCloudException::ADD_ITEMS_FROM_SERVICE_FAILED,$e->getMessage());
305  }
306  }
307 
313  public function addFolderToService($id, $folder_name)
314  {
315  try
316  {
317  if ($folder_name == null)
318  {
319  throw new ilCloudException(ilCloudException::INVALID_INPUT, $folder_name);
320  }
321  $current_node = $this->getNodeFromId($id);
322  $path = ilCloudUtil::joinPaths($current_node->getPath(), ilCloudUtil::normalizePath($folder_name));
323 
324  if($this->getNodeFromPath($path) != null)
325  {
327  }
328 
329 
330  $current_node->setLoadingComplete(false);
331  $this->storeFileTreeToSession();
332 
333  $service = ilCloudConnector::getServiceClass($this->getServiceName(), $this->getId());
334  $service->createFolder($path, $this);
335 
336  $this->addItemsFromService($current_node->getId());
337  $new_path = ilCloudUtil::joinPaths($current_node->getPath(), $folder_name);
338  $new_node = $this->getNodeFromPath($new_path);
339  return $new_node;
340 
341  } catch (Exception $e)
342  {
343  if ($e instanceof ilCloudException)
344  {
345  throw $e;
346  }
347  throw new ilCloudException(ilCloudException::FOLDER_CREATION_FAILED, $e->getMessage());
348  }
349 
350  }
351 
358  public function uploadFileToService($current_id, $tmp_name, $file_name)
359  {
360  $plugin = ilCloudConnector::getPluginClass($this->getServiceName(), $this->getId());
361  $max_file_size = $plugin->getMaxFileSize();
362 
363  if($max_file_size >= filesize($tmp_name)/(1024 * 1024))
364  {
365  $current_node = $this->getNodeFromId($current_id);
366 
367  $current_node->setLoadingComplete(false);
368  $this->storeFileTreeToSession();
369 
370  try
371  {
372  $service = ilCloudConnector::getServiceClass($this->getServiceName(), $this->getId());
373  $service->putFile($tmp_name, $file_name, $current_node->getPath(), $this);
374 
375  } catch (Exception $e)
376  {
377  if ($e instanceof ilCloudException)
378  {
379  throw $e;
380  }
381  throw new ilCloudException(ilCloudException::UPLOAD_FAILED, $e->getMessage());
382  }
383  }
384  else
385  {
386  throw new ilCloudException(ilCloudException::UPLOAD_FAILED_MAX_FILESIZE, filesize($tmp_name) / (1024 * 1024) . " MB");
387  }
388  }
389 
394  public function deleteFromService($id)
395  {
396  $item_node = $this->getNodeFromId($id);
397 
398  try
399  {
400  $service = ilCloudConnector::getServiceClass($this->getServiceName(), $this->getId());
401  $service->deleteItem($item_node->getPath(), $this);
402  $this->removeNode($item_node->getPath());
403  $this->storeFileTreeToSession();
404  } catch (Exception $e)
405  {
406  if ($e instanceof ilCloudException)
407  {
408  throw $e;
409  }
410  throw new ilCloudException(ilCloudException::DELETE_FAILED, $e->getMessage());
411  }
412  }
413 
418  public function downloadFromService($id)
419  {
420  try
421  {
422  $service = ilCloudConnector::getServiceClass($this->getServiceName(), $this->getId());
423  $node = $this->getNodeFromId($id);
424  $service->getFile($node->getPath(), $this);
425  } catch (Exception $e)
426  {
427  if ($e instanceof ilCloudException)
428  {
429  throw $e;
430  }
431  throw new ilCloudException(ilCloudException::DOWNLOAD_FAILED, $e->getMessage());
432  }
433  }
434 
435  public function storeFileTreeToSession()
436  {
437  $_SESSION['ilCloudFileTree'] = null;
438  $_SESSION['ilCloudFileTree'] = serialize($this);
439  }
440 
444  public static function getFileTreeFromSession()
445  {
446  if(isset($_SESSION['ilCloudFileTree']))
447  {
448  return unserialize($_SESSION['ilCloudFileTree']);
449  }
450  else
451  {
452  return false;
453  }
454 
455  }
456 
457  public static function clearFileTreeSession()
458  {
459  $_SESSION['ilCloudFileTree'] = null;
460  }
461 
467  public function orderListAlphabet($path1, $path2)
468  {
469  $node1 = $this->getNodeFromPath($path1);
470  $node2 = $this->getNodeFromPath($path2);
471  if ($node1->getIsDir() != $node2->getIsDir())
472  {
473  return $node2->getIsDir() ? +1 : -1;
474  }
475  $nameNode1 = strtolower(basename($node1->getPath()));
476  $nameNode2 = strtolower(basename($node2->getPath()));
477  return ($nameNode1 > $nameNode2) ? +1 : -1;
478  }
479 
485  {
486  $children = $node->getChildrenPathes();
487  usort($children, array("ilCloudFileTree", "orderListAlphabet"));
488  return $children;
489  }
490 
494  public function getListForJSONEncode()
495  {
496  $list = array();
497  foreach ($this->getItemList() as $path => $node)
498  {
499  $list[$node->getId()] = $node->getJSONEncode();
500  }
501  return $list;
502  }
503 }
504 ?>