1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
<?php
/**
* File.php
* model class for table files
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author André Noack <noack@data-quest.de>
* @copyright 2016 Stud.IP Core-Group
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*
* @property string $id database column
* @property string $user_id database column
* @property string $mime_type database column
* @property string $name database column
* @property string|null $filetype database column
* @property int $size database column
* @property JSONArrayObject|null $metadata database column
* @property string $author_name database column
* @property int|null $is_accessible database column
* @property int $mkdate database column
* @property int $chdate database column
* @property SimpleORMapCollection<FileRef> $refs has_many FileRef
* @property User $owner belongs_to User
* @property mixed $extension additional field
* @property mixed $path additional field
*/
class File extends SimpleORMap
{
/**
* @param array $config
*/
protected static function configure($config = [])
{
$config['db_table'] = 'files';
$config['belongs_to']['owner'] = [
'class_name' => User::class,
'foreign_key' => 'user_id',
];
$config['has_many']['refs'] = [
'class_name' => FileRef::class,
'assoc_foreign_key' => 'file_id',
];
$config['additional_fields']['extension'] = true;
$config['additional_fields']['path'] = true;
$config['serialized_fields']['metadata'] = JSONArrayObject::class;
$config['registered_callbacks']['after_delete'][] = 'deleteDataFile';
$config['registered_callbacks']['before_create'][] = 'cbSetAuthor';
parent::configure($config);
}
/**
* Returns the file extension of a file.
*
* @return string A string with the file extension.
*/
public function getExtension()
{
return pathinfo($this->name, PATHINFO_EXTENSION);
}
/**
* Returns the path to the file in the operating system's file system.
*
* @return null|string Returns the operating system's file system path of the file or null on failure.
*/
public function getPath()
{
if (!$this->id || !in_array($this->filetype, ['StandardFile', 'LibraryFile', 'LibraryRequestFile'])) {
return null;
}
return $GLOBALS['UPLOAD_PATH'] . '/' . substr($this->id, 0, 2) . '/' . $this->id;
}
/**
* Deletes the data file associated with the File object.
*
* @return bool Returns true on success and false on failure.
*/
public function deleteDataFile()
{
return file_exists($this->getPath()) && unlink($this->getPath());
}
/**
* Connects the File object to a physical file that is stored in the operating system's file system.
*
* @param string $path_to_file The path to the physical file.
* @return bool Returns true on success and false on failure.
*/
public function connectWithDataFile($path_to_file)
{
$newpath = $this->getPath();
if (!is_dir(pathinfo($newpath, PATHINFO_DIRNAME))) {
@mkdir(pathinfo($newpath, PATHINFO_DIRNAME));
}
if (is_uploaded_file($path_to_file)) {
if (!@move_uploaded_file($path_to_file, $newpath)) {
return false;
}
} else if (!file_exists($path_to_file) || !@copy($path_to_file, $newpath)) {
return false;
}
$this->size = filesize($newpath);
return true;
}
/**
* This callback is called before creating a File object.
* It assures that the author is set to a valid user
* by setting the user_id and author_name field.
* In case no user_id is set the ID of the current user is used.
* In case the author_name field is not set either the name of the user,
* referenced by user_id is set or the name of the current user is set.
*/
public function cbSetAuthor()
{
if (!$this->user_id) {
$this->user_id = User::findCurrent()->id;
}
if (!$this->author_name) {
$user = (User::findCurrent() && $this->user_id === User::findCurrent()->id)
? User::findCurrent()
: $this->owner;
$this->author_name = $user ? $user->getFullName('no_title') : "";
}
}
}
|