This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDocumentFileEntity.php
More file actions
118 lines (99 loc) · 3.36 KB
/
DocumentFileEntity.php
File metadata and controls
118 lines (99 loc) · 3.36 KB
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
<?php
namespace tracker\models;
use tracker\Module;
use yii\helpers\FileHelper;
/**
* @author Evgeniy Tkachenko <[email protected]>
*/
class DocumentFileEntity
{
private $document;
public function __construct(Document $document)
{
$this->document = $document;
}
/**
* @return string name to download
*/
public function getDownloadName()
{
$number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $this->document->number);
$date = \Yii::$app->formatter->asDate($this->document->registered_at);
$name = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $this->document->name);
try {
$extensions = FileHelper::getExtensionsByMimeType($this->getMimeType());
} catch (\LogicException $e) {
$extensions = [];
}
if (isset($extensions[0])) {
$extension = $extensions[0];
} else {
$extension = pathinfo($this->getPath() . $this->document->file->filename, PATHINFO_EXTENSION);
}
return $number . ' ' . $date . ' ' . $name . '.' . $extension;
}
public function getPath()
{
$rootPath = $this->getDocumentsRootPath();
$categoryPath = $this->getDirByCategory($this->document->categoryModel);
$documentPath = $this->document->id;
return $rootPath . DIRECTORY_SEPARATOR . $categoryPath . DIRECTORY_SEPARATOR . $documentPath . DIRECTORY_SEPARATOR;
}
public function getMimeType()
{
$fullPathToFile = $this->getPath() . $this->document->file->filename;
if (!is_file($fullPathToFile)) {
throw new \LogicException();
}
return FileHelper::getMimeType($fullPathToFile);
}
/**
* @var string path's to move document files
*/
private $fromPath, $toPath;
/**
* @param null|DocumentCategory $category
*/
public function prepareToMoveCategory(DocumentCategory $category = null)
{
$this->fromPath = $this->getPath();
$this->toPath = $this->getDocumentsRootPath() . DIRECTORY_SEPARATOR . $this->getDirByCategory($category) . DIRECTORY_SEPARATOR . $this->document->id . '/';
}
/**
* @return bool
*/
public function moveToNewCategory()
{
$fromPath = $this->fromPath;
$toPath = $this->toPath;
if ($toPath === null || $fromPath === null || $toPath === $fromPath) {
return true;
}
try {
FileHelper::copyDirectory($fromPath, $toPath);
} catch (\yii\base\InvalidParamException $e) {
\Yii::error($e->getMessage() . "Copy from $fromPath to $toPath", Module::getIdentifier());
return false;
}
try {
FileHelper::removeDirectory($fromPath);
} catch (\yii\base\ErrorException $e) {
\Yii::error($e->getMessage() . "Remove $fromPath", Module::getIdentifier());
return false;
}
return true;
}
private function getDirByCategory(DocumentCategory $category = null)
{
if ($category === null) {
return 'no-category';
}
return $category->id;
}
private function getDocumentsRootPath()
{
/** @var Module $module */
$module = \Yii::$app->moduleManager->getModule(Module::getIdentifier());
return FileHelper::normalizePath($module->documentRootPath);
}
}