1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8namespace Tiki\FileGallery;
9
10class Definition
11{
12	private $info;
13
14	function __construct($info)
15	{
16		$this->info = $info;
17		if (isset($this->info['id']) && ! isset($this->info['galleryId'])) {
18			$this->info['galleryId'] = $this->info['id'];
19		}
20		$this->handler = $this->getHandler($info);
21	}
22
23	/**
24	 * Get file wrapper object responsible for accessing the underlying storage.
25	 * @see FileWrapper\WrapperInterface for supported methods.
26	 */
27	function getFileWrapper($file)
28	{
29		return $this->handler->getFileWrapper($file);
30	}
31
32	/**
33	 * @see Handler\HandlerInterface
34	 */
35	function delete($file)
36	{
37		$this->handler->delete($file);
38	}
39
40	/**
41	 * @see Handler\HandlerInterface
42	 */
43	function uniquePath($file) {
44		return $this->handler->uniquePath($file);
45	}
46
47	/**
48	 * @see Handler\HandlerInterface
49	 */
50	function isWritable() {
51		return $this->handler->isWritable();
52	}
53
54	function getInfo()
55	{
56		return $this->info;
57	}
58
59	/**
60	 * Updates file contents based on chosen underlying storage.
61	 * Currently, we have: db storage or filesystem storage.
62	 * This method updates the file contents to be in the right place.
63	 */
64	function fixFileLocation($file) {
65		global $prefs;
66
67		if ($file->path) {
68			$handler = new Handler\FileSystem($prefs['fgal_use_dir']);
69		} else {
70			$handler = new Handler\Preloaded;
71		}
72
73		$data = $handler->getFileWrapper($file)->getContents();
74		$orig = $file->clone();
75
76		if ($file->replaceContents($data)) {
77			if ($handler->getFileWrapper($file) != $file->getWrapper()) {
78				$handler->delete($orig);
79			}
80		}
81	}
82
83	private function getHandler($info)
84	{
85		switch ($info['type']) {
86			case 'podcast':
87			case 'vidcast':
88				return new Handler\Podcast();
89			case 'system':
90			default:
91				return new Handler\System();
92		}
93	}
94}
95