|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
|
6
|
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Entity\Asset; |
|
8
|
|
|
use Chamilo\CoreBundle\Entity\SystemTemplate; |
|
9
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
11
|
|
|
|
|
12
|
|
|
final class Version20230315115019 extends AbstractMigrationChamilo |
|
13
|
|
|
{ |
|
14
|
|
|
public function getDescription(): string |
|
15
|
|
|
{ |
|
16
|
|
|
return 'Migrate images files of system templates to asset'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function up(Schema $schema): void |
|
20
|
|
|
{ |
|
21
|
|
|
$container = $this->getContainer(); |
|
22
|
|
|
|
|
23
|
|
|
/** @var Kernel $kernel */ |
|
24
|
|
|
$kernel = $container->get('kernel'); |
|
25
|
|
|
$rootPath = $kernel->getProjectDir(); |
|
26
|
|
|
|
|
27
|
|
|
$em = $this->getEntityManager(); |
|
28
|
|
|
$connection = $em->getConnection(); |
|
29
|
|
|
$sql = 'SELECT * FROM system_template'; |
|
30
|
|
|
$result = $connection->executeQuery($sql); |
|
31
|
|
|
$all = $result->fetchAllAssociative(); |
|
32
|
|
|
|
|
33
|
|
|
$table = $schema->getTable('system_template'); |
|
34
|
|
|
|
|
35
|
|
|
if ($table->hasColumn('image')) { |
|
36
|
|
|
foreach ($all as $systemTemplate) { |
|
37
|
|
|
if (!empty($systemTemplate['image'])) { |
|
38
|
|
|
|
|
39
|
|
|
/** @var SystemTemplate $template */ |
|
40
|
|
|
$template = $em->find('ChamiloCoreBundle:SystemTemplate', $systemTemplate['id']); |
|
41
|
|
|
if ($template->hasImage()) { |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$filePath = $rootPath.'/app/home/default_platform_document/template_thumb/'.$systemTemplate['image']; |
|
46
|
|
|
if ($this->fileExists($filePath)) { |
|
47
|
|
|
$fileName = basename($filePath); |
|
48
|
|
|
$mimeType = mime_content_type($filePath); |
|
49
|
|
|
$file = new UploadedFile($filePath, $fileName, $mimeType, null, true); |
|
50
|
|
|
$asset = (new Asset()) |
|
51
|
|
|
->setCategory(Asset::SYSTEM_TEMPLATE) |
|
52
|
|
|
->setTitle($fileName) |
|
53
|
|
|
->setFile($file) |
|
54
|
|
|
; |
|
55
|
|
|
$em->persist($asset); |
|
56
|
|
|
$em->flush(); |
|
57
|
|
|
$template->setImage($asset); |
|
58
|
|
|
|
|
59
|
|
|
$em->persist($template); |
|
60
|
|
|
$em->flush(); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function down(Schema $schema): void |
|
68
|
|
|
{ |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|