-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:chamilo/chamilo-lms
Showing
6 changed files
with
229 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/CoreBundle/Migrations/Schema/V200/Version20231110194300.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
/* For licensing terms, see /license.txt */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; | ||
|
||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; | ||
use Doctrine\DBAL\Schema\Schema; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
final class Version20231110194300 extends AbstractMigrationChamilo | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return "Copy custom theme folder to assets and update webpack.config"; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$container = $this->getContainer(); | ||
$doctrine = $container->get('doctrine'); | ||
$em = $doctrine->getManager(); | ||
|
||
$kernel = $container->get('kernel'); | ||
$rootPath = $kernel->getProjectDir(); | ||
|
||
$customThemesFolders = [ | ||
'academica', | ||
'chamilo', | ||
'chamilo_red', | ||
'cosmic_campus', | ||
'holi', | ||
'readable', | ||
'sober_brown', | ||
'baby_orange', | ||
'chamilo_electric_blue', | ||
'chamilo_sport_red', | ||
'delicious_bordeaux', | ||
'journal', | ||
'royal_purple', | ||
'spacelab', | ||
'beach', | ||
'chamilo_green', | ||
'cool_blue', | ||
'empire_green', | ||
'kiddy', | ||
'silver_line', | ||
'steel_grey', | ||
'blue_lagoon', | ||
'chamilo_orange', | ||
'corporate', | ||
'fruity_orange', | ||
'medical', | ||
'simplex', | ||
'tasty_olive' | ||
]; | ||
|
||
|
||
$sourceDir = $rootPath.'/app/Resources/public/css/themes'; | ||
$destinationDir = $rootPath.'/assets/css/themes/'; | ||
$chamiloDefaultCssPath = $destinationDir . 'chamilo/default.css'; | ||
|
||
if (!file_exists($sourceDir)) { | ||
return; | ||
} | ||
|
||
$finder = new Finder(); | ||
$finder->directories()->in($sourceDir)->depth('== 0'); | ||
$newThemes = []; | ||
foreach ($finder as $folder) { | ||
$folderName = $folder->getRelativePathname(); | ||
|
||
if (!in_array($folderName, $customThemesFolders, true)) { | ||
$sourcePath = $folder->getRealPath(); | ||
$destinationPath = $destinationDir . $folderName; | ||
|
||
if (!file_exists($destinationPath)) { | ||
$this->copyDirectory($sourcePath, $destinationPath); | ||
$newThemes[] = $folderName; | ||
|
||
if (file_exists($chamiloDefaultCssPath)) { | ||
$newThemeDefaultCssPath = $destinationPath . '/default.css'; | ||
copy($chamiloDefaultCssPath, $newThemeDefaultCssPath); | ||
} | ||
} | ||
} | ||
} | ||
|
||
$this->updateWebpackConfig($rootPath, $newThemes); | ||
} | ||
|
||
private function copyDirectory($src, $dst): void | ||
{ | ||
$dir = opendir($src); | ||
@mkdir($dst); | ||
while (false !== ($file = readdir($dir))) { | ||
if (($file != '.') && ($file != '..')) { | ||
if (is_dir($src . '/' . $file)) { | ||
$this->copyDirectory($src . '/' . $file, $dst . '/' . $file); | ||
} else { | ||
copy($src . '/' . $file, $dst . '/' . $file); | ||
} | ||
} | ||
} | ||
closedir($dir); | ||
} | ||
|
||
private function updateWebpackConfig(string $rootPath, array $newThemes): void | ||
{ | ||
$webpackConfigPath = $rootPath . '/webpack.config.js'; | ||
|
||
if (!file_exists($webpackConfigPath)) { | ||
return; | ||
} | ||
|
||
$content = file_get_contents($webpackConfigPath); | ||
|
||
$pattern = "/(const themes = \[\n\s*)([^\]]*?)(\s*\];)/s"; | ||
$replacement = function($matches) use ($newThemes) { | ||
$existingThemesString = rtrim($matches[2], ", \n"); | ||
$newThemesString = implode("',\n '", $newThemes); | ||
$formattedNewThemesString = $existingThemesString . | ||
(empty($existingThemesString) ? '' : ",\n '") . $newThemesString . "'"; | ||
return $matches[1] . $formattedNewThemesString . $matches[3]; | ||
}; | ||
|
||
$newContent = preg_replace_callback($pattern, $replacement, $content); | ||
|
||
file_put_contents($webpackConfigPath, $newContent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* For licensing terms, see /license.txt */ | ||
|
||
namespace Chamilo\CoreBundle\Settings; | ||
|
||
use Sylius\Bundle\SettingsBundle\Schema\AbstractSettingsBuilder; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
|
||
class StylesheetsSettingsSchema extends AbstractSettingsSchema | ||
{ | ||
private ParameterBagInterface $parameterBag; | ||
|
||
#[Required] | ||
public function setParameterBag(ParameterBagInterface $parameterBag): void | ||
{ | ||
$this->parameterBag = $parameterBag; | ||
} | ||
|
||
public function buildSettings(AbstractSettingsBuilder $builder): void | ||
{ | ||
$builder | ||
->setDefaults( | ||
[ | ||
'stylesheets' => 'chamilo', | ||
] | ||
); | ||
$allowedTypes = [ | ||
'stylesheets' => ['string'], | ||
]; | ||
$this->setMultipleAllowedTypes($allowedTypes, $builder); | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder): void | ||
{ | ||
$builder | ||
->add('stylesheets', ChoiceType::class, [ | ||
'choices' => $this->getThemeChoices(), | ||
'label' => 'Select Stylesheet Theme', | ||
]) | ||
; | ||
} | ||
|
||
private function getThemeChoices(): array | ||
{ | ||
$projectDir = $this->parameterBag->get('kernel.project_dir'); | ||
$themesDirectory = $projectDir . '/assets/css/themes/'; | ||
|
||
$finder = new Finder(); | ||
$choices = []; | ||
|
||
$finder->directories()->in($themesDirectory)->depth('== 0'); | ||
if ($finder->hasResults()) { | ||
foreach ($finder as $folder) { | ||
$folderName = $folder->getRelativePathname(); | ||
$choices[$this->formatFolderName($folderName)] = $folderName; | ||
} | ||
} | ||
|
||
return $choices; | ||
} | ||
|
||
|
||
private function formatFolderName(string $name): string | ||
{ | ||
return ucwords(str_replace('_', ' ', $name)); | ||
} | ||
} |