Feature #5718 » File.php

Alexandre Martinez, 2009-12-11 14:21

 
1
<?php
2
/**
3
 * Base class for file handling
4
 * 
5
 * @author Alexandre Martinez  <alexandre.martinez76@gmail.com>
6
 *
7
 */
8
abstract class Tx_Extbase_Domain_Model_File extends Tx_Extbase_DomainObject_AbstractEntity {
9
	
10
	/**
11
	 * FileReader class name
12
	 * @var string
13
	 */
14
	protected $readerClassName;
15
		
16
	/**
17
	 * Result object class name
18
	 * @var string 
19
	 */
20
	protected $resultClassName;
21
	
22
	/**
23
	 * Validator class
24
	 * @var string
25
	 */
26
	protected $validatorClassName;
27
	
28
	/**
29
	 * Name of the file
30
	 * @var string
31
	 * @notEmpty
32
	 */
33
	protected $name;
34
	
35
	/**
36
	 * Mime type of file
37
	 * @var string
38
	 * @notEmpty
39
	 */
40
	protected $type;
41
	
42
	/**
43
	 * Temp path for uploaded files
44
	 * @var string
45
	 */
46
	protected $tmp_name;
47
	
48
	/**
49
	 * Path for saved file
50
	 * @var string
51
	 */
52
	protected $path;
53
	
54
	/**
55
	 * Error code
56
	 * @var integer
57
	 */
58
	protected $error;
59
	
60
	/**
61
	 * Size of the file
62
	 * @var integer
63
	 */
64
	protected $size;
65

    
66
	/**
67
	 * Reader for the file, returns objects according to file content
68
	 * Depends on the file implementation
69
	 * @var Tx_Extabaseextended_Utility_AbstractFileReader
70
	 */
71
	protected $reader;
72
		
73
	/**
74
	 * Property mapper
75
	 * @var Tx_Extbase_Property_Mapper
76
	 */
77
	protected $propertyMapper;
78
	
79
	public function __construct($resultClassName='')
80
	{		
81
		if ($resultClassName) $this->resultClassName=$resultClassName;
82
		$reader=t3lib_div::makeInstance($this->readerClassName,$this->resultClassName,$this->validatorClassName);
83
		$this->injectReader($reader);
84
	}
85
	
86
	/**
87
	 * Injector for FileReader
88
	 * @param Tx_Extabaseextended_Utility_FileReader $reader
89
	 * @return void
90
	 */
91
	public function injectReader($reader) {
92
		$this->reader=$reader;
93
		if ($this->propertyMapper) $this->reader->injectPropertyMapper($this->propertyMapper);
94
		$this->reader->setFile($this);
95
	}
96
	
97
	/**
98
	 * Injector for propertyMapper
99
	 * @param Tx_Extbase_Property_Mapper $mapper
100
	 * @return unknown_type
101
	 */
102
	public function injectPropertyMapper(Tx_Extbase_Property_Mapper $mapper)
103
	{
104
		$this->propertyMapper=$mapper;
105
		if ($this->reader) $this->reader->injectPropertyMapper($mapper);
106
	}
107
	
108
	/**
109
	 * Setter for name
110
	 * @param string $name
111
	 * @return void
112
	 */
113
	public function setName($name)
114
	{
115
		$this->name=$name;
116
	}
117
	
118
	/**
119
	 * Getter for name
120
	 * @return string
121
	 */
122
	public function getName()
123
	{
124
		return $this->name;
125
	}
126
	
127
	/**
128
	 * Setter for type
129
	 * @param string $type
130
	 * @return void
131
	 */
132
	public function setType($type)
133
	{
134
		$this->type=$type;
135
	}
136
	
137
	/**
138
	 * Getter for type
139
	 * @return string
140
	 */
141
	public function getType()
142
	{
143
		return $this->type;
144
	}
145
	
146
	/**
147
	 * Setter for tmp_name
148
	 * @param string $name
149
	 * @return void
150
	 */
151
	public function setTmp_name($name)
152
	{
153
		$this->tmp_name=$name;
154
	}
155
	
156
	/**
157
	 * Getter for tmp_name
158
	 * @return string
159
	 */
160
	public function getTmp_name()
161
	{
162
		return $this->tmp_name;
163
	}
164
	
165
	/**
166
	 * Setter for error
167
	 * @param integer $error
168
	 * @return void
169
	 */
170
	public function setError($error)
171
	{
172
		$this->error=$error;
173
	}
174
	
175
	/**
176
	 * Getter for error
177
	 * @return integer
178
	 */
179
	public function getError()
180
	{
181
		return $this->error;
182
	}
183
	
184
	/**
185
	 * Setter for size
186
	 * @param integer $size
187
	 * @return void
188
	 */
189
	public function setSize($size)
190
	{
191
		$this->size=$size;
192
	}
193
	
194
	/**
195
	 * Getter for size
196
	 * @return integer
197
	 */
198
	public function getSize()
199
	{
200
		return $this->size;
201
	}
202
	
203
	/**
204
	 * Getter for path property
205
	 * @var boolean $absolute whether the path to return is relative or absolute
206
	 * @return string
207
	 */
208
	public function getPath($absolute=false)
209
	{		
210
		return ($absolute?PATH_site:"").$this->path;
211
	}
212
	
213
	/**
214
	 * Setter for path property
215
	 * @param string $path
216
	 * @return void
217
	 */
218
	public function setPath($path)
219
	{
220
		$this->path=$path;
221
	}
222
	
223
	/**
224
	 * Copies temp file in given folder
225
	 * @param string $folder
226
	 * @return void
227
	 */
228
	public function copyToFolder($folder)
229
	{
230
		$path=$folder."/".time()."_".$this->getName();
231
		t3lib_div::upload_copy_move($this->getTmp_name(),$path);
232
		$this->setPath($path);
233
	}
234
}
(2-2/4)