|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cv\Service; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Applications\Entity\Application; |
|
9
|
|
|
use Auth\Entity\UserInterface; |
|
10
|
|
|
use Core\Entity\FileMetadata; |
|
11
|
|
|
use Core\Entity\ImageMetadata; |
|
12
|
|
|
use Core\Entity\PermissionsInterface; |
|
13
|
|
|
use Core\Service\FileManager; |
|
14
|
|
|
use Cv\Entity\Attachment; |
|
15
|
|
|
use Cv\Entity\ContactImage; |
|
16
|
|
|
use Cv\Entity\Cv; |
|
17
|
|
|
use Cv\Entity\Cv as CvEntity; |
|
18
|
|
|
use Cv\Repository\Cv as CvRepository; |
|
19
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
20
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
21
|
|
|
use Doctrine\Persistence\ObjectRepository; |
|
22
|
|
|
use Psr\Container\ContainerInterface; |
|
23
|
|
|
|
|
24
|
|
|
class UploadHandler |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var CvRepository|ObjectRepository |
|
28
|
|
|
*/ |
|
29
|
|
|
private ObjectRepository $cvRepo; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var DocumentManager |
|
32
|
|
|
*/ |
|
33
|
|
|
private DocumentManager $dm; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var FileManager |
|
36
|
|
|
*/ |
|
37
|
|
|
private FileManager $fileManager; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
DocumentManager $dm, |
|
41
|
|
|
FileManager $fileManager |
|
42
|
|
|
) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->cvRepo = $dm->getRepository(CvEntity::class); |
|
45
|
|
|
$this->dm = $dm; |
|
46
|
|
|
$this->fileManager = $fileManager; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function factory(ContainerInterface $container): self |
|
50
|
|
|
{ |
|
51
|
|
|
$dm = $container->get(DocumentManager::class); |
|
52
|
|
|
$fileManager = $container->get(FileManager::class); |
|
53
|
|
|
|
|
54
|
|
|
return new self($dm, $fileManager); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Application $application |
|
59
|
|
|
* @param UserInterface $user |
|
60
|
|
|
* @return CvEntity |
|
61
|
|
|
* @since 0.26 |
|
62
|
|
|
*/ |
|
63
|
|
|
public function createFromApplication(Application $application, UserInterface $user) |
|
64
|
|
|
{ |
|
65
|
|
|
$repository = $this->cvRepo; |
|
66
|
|
|
$cv = $repository->create(); |
|
|
|
|
|
|
67
|
|
|
$cv->setContact($application->getContact()); |
|
68
|
|
|
|
|
69
|
|
|
$assignedUser = $application->getJob()->getUser(); |
|
70
|
|
|
$cv->setUser($assignedUser); |
|
71
|
|
|
|
|
72
|
|
|
$perms = $cv->getPermissions(); |
|
73
|
|
|
|
|
74
|
|
|
$perms->inherit($application->getPermissions()); |
|
75
|
|
|
// grant view permission to the user that issued this creation. |
|
76
|
|
|
$perms->grant($user, PermissionsInterface::PERMISSION_VIEW); |
|
77
|
|
|
// revoke change permission to the original applicant |
|
78
|
|
|
$perms->revoke($application->getUser(), PermissionsInterface::PERMISSION_CHANGE); |
|
79
|
|
|
|
|
80
|
|
|
$applicationAttachments = $application->getAttachments(); |
|
81
|
|
|
|
|
82
|
|
|
if (count($applicationAttachments) > 0) { |
|
83
|
|
|
$cvAttachments = []; |
|
84
|
|
|
$fileManager = $this->fileManager; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($applicationAttachments as $from) { |
|
87
|
|
|
//$gridfs = new \MongoGridFS($this->dm->getClient()); |
|
88
|
|
|
//$file = new \MongoGridFSFile($gridfs, $appAttachment->getFile()); |
|
89
|
|
|
|
|
90
|
|
|
/* |
|
91
|
|
|
$cvAttachment = new CvAttachment(); |
|
92
|
|
|
$cvAttachment->setName($applicationAttachment->getName()); |
|
93
|
|
|
$cvAttachment->setType($applicationAttachment->getType()); |
|
94
|
|
|
$cvAttachment->setUser($assignedUser); |
|
95
|
|
|
$cvAttachment->setFile($file); |
|
96
|
|
|
$cvAttachment->setDateUploaded($applicationAttachment->getDateUploaded()); |
|
97
|
|
|
*/ |
|
98
|
|
|
$fromMetadata = $from->getMetadata(); |
|
99
|
|
|
$metadata = new FileMetadata(); |
|
100
|
|
|
$metadata->setContentType($fromMetadata->getContentType()); |
|
101
|
|
|
$metadata->setUser($assignedUser); |
|
102
|
|
|
|
|
103
|
|
|
$fromStream = $fileManager->getStream($from); |
|
104
|
|
|
$toAttachment = $fileManager->uploadFromStream( |
|
105
|
|
|
Attachment::class, |
|
106
|
|
|
$metadata, |
|
107
|
|
|
$from->getName(), |
|
|
|
|
|
|
108
|
|
|
$fromStream |
|
109
|
|
|
); |
|
110
|
|
|
$cvAttachments[] = $toAttachment; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$cv->setAttachments(new ArrayCollection($cvAttachments)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $cv; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param CvEntity $cv |
|
121
|
|
|
* @param array $fileInfo |
|
122
|
|
|
* @return object|Attachment |
|
123
|
|
|
* @throws \Doctrine\ODM\MongoDB\LockException |
|
124
|
|
|
* @throws \Doctrine\ODM\MongoDB\Mapping\MappingException |
|
125
|
|
|
* @throws \Doctrine\ODM\MongoDB\MongoDBException |
|
126
|
|
|
*/ |
|
127
|
|
|
public function handleAttachmentUpload( |
|
128
|
|
|
Cv $cv, |
|
129
|
|
|
array $fileInfo |
|
130
|
|
|
) |
|
131
|
|
|
{ |
|
132
|
|
|
$dm = $this->dm; |
|
133
|
|
|
$fileManager = $this->fileManager; |
|
134
|
|
|
|
|
135
|
|
|
/* @var \Cv\Entity\Cv $resume */ |
|
136
|
|
|
$resume = $this->cvRepo->find($cv->getId()); |
|
137
|
|
|
$user = $resume->getUser(); |
|
138
|
|
|
|
|
139
|
|
|
$metadata = new FileMetadata(); |
|
140
|
|
|
$metadata->setUser($user); |
|
141
|
|
|
$metadata->setContentType($fileInfo['type']); |
|
142
|
|
|
$metadata->setName($fileInfo['name']); |
|
143
|
|
|
|
|
144
|
|
|
$file = $fileManager->uploadFromFile( |
|
145
|
|
|
Attachment::class, |
|
146
|
|
|
$metadata, |
|
147
|
|
|
$fileInfo['tmp_name'], |
|
148
|
|
|
$fileInfo['name'] |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
$cv->getAttachments()->add($file); |
|
152
|
|
|
$dm->persist($resume); |
|
153
|
|
|
$dm->flush(); |
|
154
|
|
|
|
|
155
|
|
|
return $file; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function handleImageUpload( |
|
159
|
|
|
Cv $cv, |
|
160
|
|
|
array $fileInfo |
|
161
|
|
|
) |
|
162
|
|
|
{ |
|
163
|
|
|
$dm = $this->dm; |
|
164
|
|
|
$fileManager = $this->fileManager; |
|
165
|
|
|
$metadata = new ImageMetadata(); |
|
166
|
|
|
|
|
167
|
|
|
$contact = $cv->getContact(); |
|
168
|
|
|
if(!is_null($contact->getImage())){ |
|
169
|
|
|
$contact->setImage(null); |
|
170
|
|
|
$dm->persist($cv); |
|
171
|
|
|
$dm->flush(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$metadata |
|
175
|
|
|
->setUser($cv->getUser()) |
|
176
|
|
|
->setContentType($fileInfo['type']) |
|
177
|
|
|
->setKey('original'); |
|
178
|
|
|
|
|
179
|
|
|
$dm->persist($cv->getUser()); |
|
180
|
|
|
|
|
181
|
|
|
$file = $fileManager->uploadFromFile( |
|
182
|
|
|
ContactImage::class, |
|
183
|
|
|
$metadata, |
|
184
|
|
|
$fileInfo['tmp_name'], |
|
185
|
|
|
$fileInfo['name'] |
|
186
|
|
|
); |
|
187
|
|
|
|
|
188
|
|
|
$cv->getContact()->setImage($file); |
|
189
|
|
|
$dm->persist($cv); |
|
190
|
|
|
$dm->flush(); |
|
191
|
|
|
} |
|
192
|
|
|
} |