Merge pull request #4820 from christianbeeznest/ofaj-20846-settings3

Migrations: Add my_files as resources files and fix form accordion - refs BT#20846
pull/4821/head
christianbeeznest 1 year ago committed by GitHub
commit c83154538e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      assets/css/app.scss
  2. 4
      public/main/inc/lib/formvalidator/FormValidator.class.php
  3. 7
      src/CoreBundle/Migrations/Schema/V200/Version20230216122900.php
  4. 117
      src/CoreBundle/Migrations/Schema/V200/Version20230720143000.php
  5. 98
      src/CoreBundle/Migrations/Schema/V200/Version20230720222140.php

@ -14,6 +14,12 @@
@import "~bootstrap-daterangepicker/daterangepicker.css";
//@import '~jquery-ui/themes/base/all.css';
@layer utilities {
.custom-collapse {
visibility: visible;
}
}
@layer components {
.card {
@apply rounded shadow-lg w-full;

@ -1037,7 +1037,7 @@ EOT;
});
</script>
<div class="mt-4 rounded-lg">
<div class="px-4 bg-gray-100 border border-gray-50" id="card_'.$id.'">
<div class="px-4 bg-gray-100 border border-gray-50 custom-collapse" id="card_'.$id.'">
<h5>
<a role="button"
class="'.(($open) ? 'collapse' : ' ').'"
@ -1052,7 +1052,7 @@ EOT;
</div>
<div
id="collapse_'.$id.'"
class="px-4 border border-gray-50 bg-white hidden collapse '.(($open) ? 'show' : ' ').'"
class="px-4 border border-gray-50 bg-white hidden collapse custom-collapse '.(($open) ? 'show' : ' ').'"
aria-labelledby="heading_'.$id.'" data-parent="#'.$parent.'">
<div id="collapse_contant_'.$id.'" class="card-body ">';

@ -188,6 +188,7 @@ class Version20230216122900 extends AbstractMigrationChamilo
'allow_career_users',
'community_managers_user_list',
'allow_social_map_fields',
'hide_username_in_course_chat',
],
'Admin' => [
'user_status_option_only_for_admin_enabled',
@ -380,7 +381,6 @@ class Version20230216122900 extends AbstractMigrationChamilo
'video_player_renderers',
],
'Chat' => [
'hide_username_in_course_chat',
'hide_chat_video',
'course_chat_restrict_to_coach',
],
@ -482,6 +482,7 @@ class Version20230216122900 extends AbstractMigrationChamilo
];
foreach ($configurationValues as $category => $variables) {
foreach ($variables as $variable) {
$category = strtolower($category);
$result = $connection
->executeQuery(
"SELECT COUNT(1) FROM settings_current WHERE variable = '$variable' AND category = '{$category}'"
@ -496,7 +497,7 @@ class Version20230216122900 extends AbstractMigrationChamilo
);
} else {
$this->addSql(
"UPDATE settings_current SET selected_value = '{$selectedValue}' WHERE variable = '$variable' AND category = '{$category}'"
"UPDATE settings_current SET selected_value = '{$selectedValue}', category = '{$category}' WHERE variable = '$variable' AND category = '{$category}'"
);
}
}
@ -764,7 +765,6 @@ class Version20230216122900 extends AbstractMigrationChamilo
'Chat' => [
'course_chat_restrict_to_coach',
'hide_chat_video',
'hide_username_in_course_chat',
],
'Editor' => [
'video_player_renderers',
@ -957,6 +957,7 @@ class Version20230216122900 extends AbstractMigrationChamilo
'user_status_option_only_for_admin_enabled',
],
'Profile' => [
'hide_username_in_course_chat',
'allow_social_map_fields',
'community_managers_user_list',
'allow_career_users',

@ -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…
Cancel
Save