Merge pull request #4820 from christianbeeznest/ofaj-20846-settings3
Migrations: Add my_files as resources files and fix form accordion - refs BT#20846pull/4821/head
commit
c83154538e
@ -0,0 +1,117 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Entity\PersonalFile; |
||||
use Chamilo\CoreBundle\Entity\ResourceNode; |
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
use Doctrine\ORM\Query\Expr\Join; |
||||
use Symfony\Component\HttpFoundation\File\UploadedFile; |
||||
|
||||
final class Version20230720143000 extends AbstractMigrationChamilo |
||||
{ |
||||
|
||||
public function getDescription(): string |
||||
{ |
||||
return 'Migrate my_files of users to personal_files '; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$container = $this->getContainer(); |
||||
$em = $this->getEntityManager(); |
||||
|
||||
$kernel = $container->get('kernel'); |
||||
$rootPath = $kernel->getProjectDir(); |
||||
|
||||
$q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u'); |
||||
/** @var User $userEntity */ |
||||
foreach ($q->toIterable() as $userEntity) { |
||||
|
||||
$id = $userEntity->getId(); |
||||
$path = "users/{$id}/"; |
||||
|
||||
$variable = 'split_users_upload_directory'; |
||||
// Query the 'selected_value' from the 'settings_current' table where the 'variable' is 'split_users_upload_directory' |
||||
$query = $em->createQuery('SELECT s.selectedValue FROM Chamilo\CoreBundle\Entity\SettingsCurrent s WHERE s.variable = :variable') |
||||
->setParameter('variable', $variable); |
||||
|
||||
// Get the result of the query (it should return a single row with the 'selected_value' column) |
||||
$result = $query->getOneOrNullResult(); |
||||
$settingValueAsString = 'false'; |
||||
if (null !== $result) { |
||||
// Convert the 'selected_value' to a string and store it in $settingValueAsString |
||||
$settingValue = $result['selectedValue']; |
||||
$settingValueAsString = (string) $settingValue; |
||||
} |
||||
|
||||
// If the 'split_users_upload_directory' setting is 'true', adjust the path accordingly |
||||
if ('true' === $settingValueAsString) { |
||||
$path = 'users/' . substr((string) $id, 0, 1) . '/' . $id . '/'; |
||||
} |
||||
|
||||
$baseDir = $rootPath.'/app/upload/'.$path; |
||||
|
||||
// Check if the base directory exists, if not, continue to the next user |
||||
if (!is_dir($baseDir)) { |
||||
continue; |
||||
} |
||||
|
||||
// Get all the files in the 'my_files' directory |
||||
$myFilesDir = $baseDir . 'my_files/'; |
||||
$files = glob($myFilesDir . '*'); |
||||
|
||||
// $files now contains a list of all files in the 'my_files' directory |
||||
foreach ($files as $file) { |
||||
|
||||
if (!is_file($file)) { |
||||
continue; |
||||
} |
||||
|
||||
$title = basename($file); |
||||
$queryBuilder = $em->createQueryBuilder(); |
||||
|
||||
// Build the query to join the ResourceNode and PersonalFile tables |
||||
$queryBuilder |
||||
->select('p') |
||||
->from(ResourceNode::class, 'n') |
||||
->innerJoin(PersonalFile::class, 'p', Join::WITH, 'p.resourceNode = n.id') |
||||
->where('n.title = :title') |
||||
->andWhere('n.creator = :creator') |
||||
->setParameter('title', $title) |
||||
->setParameter('creator', $id); |
||||
|
||||
$result = $queryBuilder->getQuery()->getOneOrNullResult(); |
||||
|
||||
if ($result) { |
||||
// Skip creating a new entity and log a message |
||||
error_log('MIGRATIONS :: $file -- ' . $file . ' (Skipped: Already exists) ...'); |
||||
continue; |
||||
} |
||||
|
||||
error_log('MIGRATIONS :: $file -- ' . $file . ' ...'); |
||||
// Create a new PersonalFile entity if it doesn't already exist |
||||
$personalFile = new PersonalFile(); |
||||
$personalFile->setTitle($title); // Set the file name as the title |
||||
$personalFile->setCreator($userEntity); |
||||
$personalFile->setParentResourceNode($userEntity->getResourceNode()->getId()); |
||||
$personalFile->setResourceName($title); |
||||
$mimeType = mime_content_type($file); |
||||
$uploadedFile = new UploadedFile($file, $title, $mimeType, null, true); |
||||
$personalFile->setUploadFile($uploadedFile); |
||||
|
||||
// Save the object to the database |
||||
$em->persist($personalFile); |
||||
$em->flush(); |
||||
|
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,98 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Entity\SocialPost; |
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
|
||||
use Chamilo\CoreBundle\Repository\Node\PersonalFileRepository; |
||||
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
||||
use Doctrine\DBAL\Connection; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
final class Version20230720222140 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Rename my_files path by resource file path'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$container = $this->getContainer(); |
||||
$em = $this->getEntityManager(); |
||||
/** @var Connection $connection */ |
||||
$connection = $em->getConnection(); |
||||
|
||||
$kernel = $container->get('kernel'); |
||||
$rootPath = $kernel->getProjectDir(); |
||||
|
||||
$userRepo = $container->get(UserRepository::class); |
||||
|
||||
/** @var PersonalFileRepository $personalRepo */ |
||||
$personalRepo = $container->get(PersonalFileRepository::class); |
||||
|
||||
$q = $em->createQuery('SELECT s FROM Chamilo\CoreBundle\Entity\SocialPost s'); |
||||
|
||||
/** @var SocialPost $socialPost */ |
||||
foreach ($q->toIterable() as $socialPost) { |
||||
$content = $socialPost->getContent(); |
||||
|
||||
// Define the regular expression pattern to match URLs containing "my_files/" |
||||
$pattern = '/(href|src)="[^"]*\/users\/(\d+)\/\d+\/my_files\/([^"]+)"/i'; |
||||
preg_match_all($pattern, $content, $matches); |
||||
|
||||
// Combine the URLs found in href and src attributes |
||||
$allUrls = array_merge($matches[0]); |
||||
|
||||
if (!empty($allUrls)) { |
||||
foreach ($allUrls as $url) { |
||||
|
||||
// Define a regular expression to search for the "/upload/users" part, numeric values, and filename in the URL |
||||
$pattern = '/\/upload\/users\/(\d+)\/(\d+)\/my_files\/([^\/"]+)/i'; |
||||
|
||||
// Perform the regular expression search in the URL |
||||
if (preg_match($pattern, $url, $matches)) { |
||||
$folderId = (int) $matches[1]; |
||||
$userId = (int) $matches[2]; |
||||
$filename = $matches[3]; |
||||
|
||||
// Get the full file path |
||||
$filePath = $rootPath . "/app/upload/users/{$folderId}/{$userId}/my_files/{$filename}"; |
||||
|
||||
// Check if the path corresponds to a file |
||||
if (is_file($filePath)) { |
||||
// Output the user id, folder id, and filename |
||||
error_log('User ID: ' . $userId . ', Folder ID: ' . $folderId . ', Filename: ' . $filename); |
||||
$user = $userRepo->find($userId); |
||||
$personalFile = $personalRepo->getResourceByCreatorFromTitle( |
||||
$filename, |
||||
$user, |
||||
$user->getResourceNode() |
||||
); |
||||
|
||||
$newUrl = $personalRepo->getResourceFileUrl($personalFile); |
||||
if (!empty($newUrl)) { |
||||
// Perform the replacement of the old URL with the new URL in the content |
||||
$content = preg_replace('/(src|href)="[^"]*\/users\/(\d+)\/\d+\/my_files\/([^"]+)"/i', '$1="' . $newUrl . '"', $content); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Set the updated content back to the social post entity |
||||
$socialPost->setContent($content); |
||||
|
||||
// Persist the updated social post entity |
||||
$em->persist($socialPost); |
||||
$em->flush(); |
||||
|
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue