parent
6304f4c3e8
commit
6ad7d141b7
@ -0,0 +1,69 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Asset; |
||||
use Chamilo\CoreBundle\Entity\ExtraFieldValues; |
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Connection; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
use Symfony\Component\HttpFoundation\File\UploadedFile; |
||||
|
||||
/** |
||||
* Extra fields. |
||||
*/ |
||||
class Version20191206150030 extends AbstractMigrationChamilo |
||||
{ |
||||
public function up(Schema $schema): void |
||||
{ |
||||
// Migrate extra field fields |
||||
$container = $this->getContainer(); |
||||
$doctrine = $container->get('doctrine'); |
||||
$em = $doctrine->getManager(); |
||||
/** @var Connection $connection */ |
||||
$connection = $em->getConnection(); |
||||
|
||||
$kernel = $container->get('kernel'); |
||||
$rootPath = $kernel->getProjectDir(); |
||||
|
||||
$batchSize = self::BATCH_SIZE; |
||||
$counter = 1; |
||||
$q = $em->createQuery('SELECT v FROM Chamilo\CoreBundle\Entity\ExtraFieldValues v'); |
||||
|
||||
$fieldWithFiles = \ExtraField::getExtraFieldTypesWithFiles(); |
||||
|
||||
/** @var ExtraFieldValues $item */ |
||||
foreach ($q->toIterable() as $item) { |
||||
if (in_array($item->getField()->getFieldType(), $fieldWithFiles)) { |
||||
$path = $item->getValue(); |
||||
if (empty($path)) { |
||||
continue; |
||||
} |
||||
$filePath = $rootPath.'/app/upload/'.$path; |
||||
if (file_exists($filePath) && !is_dir($filePath)) { |
||||
$fileName = basename($path); |
||||
$mimeType = mime_content_type($filePath); |
||||
$file = new UploadedFile($filePath, $fileName, $mimeType, null, true); |
||||
$asset = new Asset(); |
||||
$asset |
||||
->setCategory(Asset::EXTRA_FIELD) |
||||
->setTitle($fileName) |
||||
->setFile($file) |
||||
; |
||||
$em->persist($asset); |
||||
$em->flush(); |
||||
$item->setValue($asset->getId()); |
||||
$em->persist($item); |
||||
} |
||||
} |
||||
|
||||
if (0 === $counter % $batchSize) { |
||||
$em->flush(); |
||||
$em->clear(); // Detaches all objects from Doctrine! |
||||
} |
||||
$counter++; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,61 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Chamilo\CourseBundle\Repository\CLpRepository; |
||||
use Chamilo\Kernel; |
||||
use Doctrine\DBAL\Connection; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
class Version20210221082033 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Migrate c_lp images'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$container = $this->getContainer(); |
||||
/** @var Kernel $kernel */ |
||||
$kernel = $container->get('kernel'); |
||||
$rootPath = $kernel->getProjectDir(); |
||||
$doctrine = $container->get('doctrine'); |
||||
|
||||
|
||||
$em = $doctrine->getManager(); |
||||
/** @var Connection $connection */ |
||||
$connection = $em->getConnection(); |
||||
$lpRepo = $container->get(CLpRepository::class); |
||||
|
||||
$q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||||
/** @var Course $course */ |
||||
foreach ($q->toIterable() as $course) { |
||||
$courseId = $course->getId(); |
||||
$sql = "SELECT * FROM c_lp WHERE c_id = $courseId |
||||
ORDER BY iid"; |
||||
$result = $connection->executeQuery($sql); |
||||
$items = $result->fetchAllAssociative(); |
||||
foreach ($items as $itemData) { |
||||
$id = $itemData['iid']; |
||||
$lp = $lpRepo->find($id); |
||||
if ($lp && !empty($lp->getPreviewImage())) { |
||||
$path = $lp->getPreviewImage(); |
||||
$filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/learning_path/images/'.$path; |
||||
if (file_exists($rootPath)) { |
||||
$this->addLegacyFileToResource($filePath, $lpRepo, $lp, $lp->getIid(), $path); |
||||
$em->persist($lp); |
||||
$em->flush(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
$em->flush(); |
||||
$em->clear(); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue