1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Asset; |
10
|
|
|
use Chamilo\CoreBundle\Entity\ExtraField; |
11
|
|
|
use Chamilo\CoreBundle\Entity\ExtraFieldValues; |
12
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
13
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
14
|
|
|
use Chamilo\CoreBundle\Repository\SessionRepository; |
15
|
|
|
use Doctrine\DBAL\Schema\Schema; |
16
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
17
|
|
|
|
18
|
|
|
class Version20230204150030 extends AbstractMigrationChamilo |
19
|
|
|
{ |
20
|
|
|
public function getDescription(): string |
21
|
|
|
{ |
22
|
|
|
return 'Move extrafield session image to asset'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function up(Schema $schema): void |
26
|
|
|
{ |
27
|
|
|
$kernel = $this->container->get('kernel'); |
|
|
|
|
28
|
|
|
$rootPath = $kernel->getProjectDir(); |
29
|
|
|
|
30
|
|
|
$batchSize = self::BATCH_SIZE; |
31
|
|
|
$counter = 1; |
32
|
|
|
$dql = 'SELECT v FROM Chamilo\CoreBundle\Entity\ExtraFieldValues v'; |
33
|
|
|
$dql .= ' JOIN v.field f'; |
34
|
|
|
$dql .= ' WHERE f.variable = :variable AND f.itemType = :itemType'; |
35
|
|
|
$q = $this->entityManager->createQuery($dql); |
|
|
|
|
36
|
|
|
$q->setParameters([ |
37
|
|
|
'variable' => 'image', |
38
|
|
|
'itemType' => ExtraField::SESSION_FIELD_TYPE, |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$sessionRepo = $this->container->get(SessionRepository::class); |
42
|
|
|
|
43
|
|
|
/** @var ExtraFieldValues $item */ |
44
|
|
|
foreach ($q->toIterable() as $item) { |
45
|
|
|
$path = $item->getFieldValue(); |
46
|
|
|
if (empty($path)) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
$filePath = $rootPath.'/app/upload/'.$path; |
50
|
|
|
error_log('MIGRATIONS :: $filePath -- '.$filePath.' ...'); |
51
|
|
|
if ($this->fileExists($filePath)) { |
52
|
|
|
$fileName = basename($path); |
53
|
|
|
$mimeType = mime_content_type($filePath); |
54
|
|
|
$file = new UploadedFile($filePath, $fileName, $mimeType, null, true); |
55
|
|
|
$asset = (new Asset()) |
56
|
|
|
->setCategory(Asset::SESSION) |
57
|
|
|
->setTitle($fileName) |
58
|
|
|
->setFile($file) |
59
|
|
|
; |
60
|
|
|
$this->entityManager->persist($asset); |
61
|
|
|
$this->entityManager->flush(); |
62
|
|
|
$item->setAsset($asset); |
63
|
|
|
$this->entityManager->persist($item); |
64
|
|
|
|
65
|
|
|
$sessionId = $item->getItemId(); |
66
|
|
|
|
67
|
|
|
/** @var Session $session */ |
68
|
|
|
$session = $sessionRepo->find($sessionId); |
69
|
|
|
$session->setImage($asset); |
70
|
|
|
$sessionRepo->update($session); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (($counter % $batchSize) === 0) { |
74
|
|
|
$this->entityManager->flush(); |
75
|
|
|
} |
76
|
|
|
$counter++; |
77
|
|
|
} |
78
|
|
|
$this->entityManager->flush(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function down(Schema $schema): void {} |
82
|
|
|
} |
83
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.