Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Certificate: Add custom themes folders - refs BT#21007 #4987

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
<body>
<table border="0" bgcolor="#92c647" cellpadding="0" cellspacing="0" align="center" width="80%">
<tr>
<td bgcolor="#92c647"><img src="{{ url('index') ~ '/build/css/themes/chamilo/images/header_top.png' }}" style="display: block;"></td>
<td bgcolor="#92c647">
<img src="{{ url('index') ~ 'build/css/themes/' ~ chamilo_settings_get('stylesheets.stylesheets') ~ '/images/header_top.png' }}" style="display: block;">
</td>
</tr>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="#92c647" width=58 height=91>
<img src="{{ _p.web_css_theme }}images/lado-b.png" style="display:block;">
<img src="{{ url('index') ~ 'build/css/themes/' ~ chamilo_settings_get('stylesheets.stylesheets') ~ '/images/lado-b.png' }}" style="display:block;">
</td>
<td bgcolor="#92c647" width=700 height=91 style="font-family:CourierSans-Light; font-weight: bold; line-height: 47px; color:#FFF; padding-bottom: 10px; font-size: 45px;">
{{ 'CertificateHeader' | get_lang }}
</td>
<td bgcolor="#92c647" width=58 height=91>
<img src="{{ url('index') ~ '/build/css/themes/chamilo/images/lado-header.png' }}" style="display:block;">
<img src="{{ url('index') ~ 'build/css/themes/' ~ chamilo_settings_get('stylesheets.stylesheets') ~ '/images/lado-header.png' }}" style="display:block;">
</td>
</tr>
</table>
Expand All @@ -30,7 +32,9 @@
<td>
<table bgcolor="#FFFFFF" border="0" cellspacing="0" cellpadding="0" width="100%" height=900>
<tr>
<td bgcolor="#92c647" height=755><img src="{{ url('index') ~ '/build/css/themes/chamilo/images/lado-a.png' }}" style="display:block;"></td>
<td bgcolor="#92c647" height=755>
<img src="{{ url('index') ~ 'build/css/themes/' ~ chamilo_settings_get('stylesheets.stylesheets') ~ '/images/lado-a.png' }}" style="display:block;">
</td>
<td height=755 style="font-family:CourierSans-Light; line-height: 22px; color:#40ad49; padding: 40px; font-size: 18px;" valign="top">
<h3 style="color: #672290; font-size: 24px;">
{{ complete_name }}
Expand Down Expand Up @@ -63,7 +67,7 @@
<br />
</td>
<td height=755 bgcolor="#92c647">
<img src="{{ url('index') ~ '/build/css/themes/chamilo/images/lado-b.png' }}" style="display:block;">
<img src="{{ url('index') ~ 'build/css/themes/' ~ chamilo_settings_get('stylesheets.stylesheets') ~ '/images/lado-b.png' }}" style="display:block;">
</td>
</tr>
</table>
Expand All @@ -73,11 +77,15 @@
<td>
<table border="0" cellspacing="0" cellpadding="0" width="100%" height=91>
<tr>
<td bgcolor="#92c647" width=58 height=91><img src="{{ url('index') ~ '/build/css/themes/chamilo/images/lado-b.png' }}" style="display:block;"></td>
<td bgcolor="#92c647" width=58 height=91>
<img src="{{ url('index') ~ 'build/css/themes/' ~ chamilo_settings_get('stylesheets.stylesheets') ~ '/images/lado-b.png' }}" style="display:block;">
</td>
<td bgcolor="#92c647" width=500 height=91 style="font-family:CourierSans-Light; line-height: 18px; color:#FFF;">
{{ 'CertificateFooter' | get_lang }}
</td>
<td bgcolor="#92c647" width=245 height=91><img src="{{ url('index') ~ '/build/css/themes/chamilo/images/lado-footer.png' }}" style="display:block;"></td>
<td bgcolor="#92c647" width=245 height=91>
<img src="{{ url('index') ~ 'build/css/themes/' ~ chamilo_settings_get('stylesheets.stylesheets') ~ '/images/lado-footer.png' }}" style="display:block;">
</td>
</tr>
</table>
</td>
Expand Down
127 changes: 127 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20231110194300.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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/';

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;
}
}
}

$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);
}
}
5 changes: 5 additions & 0 deletions src/CoreBundle/Resources/config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,8 @@ services:
class: Chamilo\CoreBundle\Settings\WebServiceSettingsSchema
tags:
- {name: sylius.settings_schema, alias: chamilo_core.settings.webservice, namespace: webservice}

chamilo_core.settings.stylesheets:
class: Chamilo\CoreBundle\Settings\StylesheetsSettingsSchema
tags:
- { name: sylius.settings_schema, alias: chamilo_core.settings.stylesheets, namespace: stylesheets }
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'ticket': 'ticket-confirmation',
'tracking': 'chart-box',
'webservice': 'cloud-sync',
'stylesheets': 'language-css3',
}
%}
{% for namespace in schemas|keys %}
Expand Down
74 changes: 74 additions & 0 deletions src/CoreBundle/Settings/StylesheetsSettingsSchema.php
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));
}
}
Loading