Passed
Push — master ( f2668a...e0a017 )
by Julito
07:49
created

Version20191206150000::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
nc 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Entity\Asset;
8
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Connection;
11
use Doctrine\DBAL\Schema\Schema;
12
use Symfony\Component\HttpFoundation\File\UploadedFile;
13
14
/**
15
 * Extra fields.
16
 */
17
class Version20191206150000 extends AbstractMigrationChamilo
18
{
19
    public function up(Schema $schema): void
20
    {
21
        $table = $schema->getTable('extra_field');
22
        if (false === $table->hasColumn('helper_text')) {
23
            $this->addSql('ALTER TABLE extra_field ADD helper_text text DEFAULT NULL AFTER display_text');
24
        }
25
        $this->addSql('ALTER TABLE extra_field_values CHANGE value value LONGTEXT DEFAULT NULL;');
26
        if (false === $table->hasColumn('description')) {
27
            $this->addSql('ALTER TABLE extra_field ADD description LONGTEXT DEFAULT NULL');
28
        }
29
30
        $table = $schema->getTable('extra_field_values');
31
        if (!$table->hasIndex('idx_efv_item')) {
32
            $this->addSql('CREATE INDEX idx_efv_item ON extra_field_values (item_id)');
33
        }
34
35
        // Migrate extra field fields
36
        $container = $this->getContainer();
37
        $doctrine = $container->get('doctrine');
38
        $em = $doctrine->getManager();
39
        /** @var Connection $connection */
40
        $connection = $em->getConnection();
41
42
        $kernel = $container->get('kernel');
43
        $rootPath = $kernel->getProjectDir();
44
45
        $batchSize = self::BATCH_SIZE;
46
        $counter = 1;
47
        $q = $em->createQuery('SELECT v FROM Chamilo\CoreBundle\Entity\ExtraFieldValues v');
48
49
        $fieldWithFiles = [\ExtraField::FIELD_TYPE_FILE, \ExtraField::FIELD_TYPE_FILE_IMAGE];
50
51
        /** @var ExtraFieldValues $item */
52
        foreach ($q->toIterable() as $item) {
53
            if (in_array($item->getField()->getFieldType(), $fieldWithFiles)) {
54
                $path = $item->getValue();
55
                if (empty($path)) {
56
                    continue;
57
                }
58
                $filePath = $rootPath.'/app/upload/'.$path;
59
                if (file_exists($filePath) && !is_dir($filePath)) {
60
                    $fileName = basename($path);
61
                    $mimeType = mime_content_type($filePath);
62
                    $file = new UploadedFile($filePath, $fileName, $mimeType, null, true);
63
                    $asset = new Asset();
64
                    $asset
65
                        ->setCategory(Asset::EXTRA_FIELD)
66
                        ->setTitle($fileName)
67
                        ->setFile($file)
68
                    ;
69
                    $em->persist($asset);
70
                    $em->flush();
71
                    $item->setValue($asset->getId());
72
                    $em->persist($item);
73
                }
74
            }
75
76
            if (0 === $counter % $batchSize) {
77
                $em->flush();
78
                $em->clear(); // Detaches all objects from Doctrine!
79
            }
80
            $counter++;
81
        }
82
    }
83
}
84