Skip to content

Commit

Permalink
Admin: Load color themes to be selected by current access url - refs …
Browse files Browse the repository at this point in the history
…BT#21621
  • Loading branch information
AngelFQC committed Jun 26, 2024
1 parent cb2ac56 commit 3a18ca6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
6 changes: 4 additions & 2 deletions assets/vue/components/platform/ColorThemeSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const isServerThemesLoading = ref(true)
const loadThemes = async () => {
try {
const { items } = await themeService.findAll()
const { items } = await themeService.findAllByCurrentUrl()
serverThemes.value = items
serverThemes.value = items.map((accessUrlRelColorTheme) => accessUrlRelColorTheme.colorTheme)
modelValue.value = items.find((accessUrlRelColorTheme) => accessUrlRelColorTheme.active)?.colorTheme["@id"]
} catch (e) {
showErrorNotification(t("We could not retrieve the themes"))
} finally {
Expand Down
6 changes: 3 additions & 3 deletions assets/vue/services/colorThemeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const url = "/api/color_themes"
*
* @returns {Promise<{totalItems, items}>}
*/
async function findAll() {
return await baseService.getCollection(url)
async function findAllByCurrentUrl() {
return await baseService.getCollection("/api/access_url_rel_color_themes")
}

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ async function changePlatformColorTheme(iri) {
}

export default {
findAll,
updateTheme,
findAllByCurrentUrl,
changePlatformColorTheme,
}
11 changes: 10 additions & 1 deletion src/CoreBundle/Entity/AccessUrlRelColorTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@
namespace Chamilo\CoreBundle\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use Chamilo\CoreBundle\Repository\AccessUrlRelColorThemeRepository;
use Chamilo\CoreBundle\State\AccessUrlRelColorThemeStateProcessor;
use Chamilo\CoreBundle\State\AccessUrlRelColorThemeStateProvider;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
operations: [
new Post(),
new GetCollection(),
],
normalizationContext: [
'groups' => ['access_url_rel_color_theme:read'],
],
denormalizationContext: [
'groups' => ['access_url_rel_color_theme:write'],
],
paginationEnabled: false,
security: "is_granted('ROLE_ADMIN')",
provider: AccessUrlRelColorThemeStateProvider::class,
processor: AccessUrlRelColorThemeStateProcessor::class,
)]
#[ORM\Entity(repositoryClass: AccessUrlRelColorThemeRepository::class)]
Expand All @@ -38,11 +46,12 @@ final class AccessUrlRelColorTheme
#[ORM\JoinColumn(nullable: false)]
private ?AccessUrl $url = null;

#[Groups(['access_url_rel_color_theme:write'])]
#[Groups(['access_url_rel_color_theme:write', 'access_url_rel_color_theme:read'])]
#[ORM\ManyToOne(inversedBy: 'urls')]
#[ORM\JoinColumn(nullable: false)]
private ?ColorTheme $colorTheme = null;

#[Groups(['access_url_rel_color_theme:read'])]
#[ORM\Column]
private bool $active = false;

Expand Down
4 changes: 1 addition & 3 deletions src/CoreBundle/Entity/ColorTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace Chamilo\CoreBundle\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Chamilo\CoreBundle\State\ColorThemeStateProcessor;
Expand All @@ -23,7 +22,6 @@
operations: [
new Post(),
new Put(),
new GetCollection(),
],
denormalizationContext: [
'groups' => ['color_theme:write'],
Expand All @@ -41,7 +39,7 @@ class ColorTheme
#[ORM\Column]
private ?int $id = null;

#[Groups(['color_theme:write'])]
#[Groups(['color_theme:write', 'access_url_rel_color_theme:read'])]
#[ORM\Column(length: 255)]
private string $title;

Expand Down
10 changes: 8 additions & 2 deletions src/CoreBundle/ServiceHelper/AccessUrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public function isMultipleEnabled(): bool
return 1 === (int) $this->parameterBag->get('multiple_access_url');
}

public function getFirstAccessUrl(): AccessUrl
{
$urlId = $this->accessUrlRepository->getFirstId();

return $this->accessUrlRepository->find($urlId);
}

public function getCurrent(): AccessUrl
{
static $accessUrl;
Expand All @@ -33,8 +40,7 @@ public function getCurrent(): AccessUrl
return $accessUrl;
}

$urlId = $this->accessUrlRepository->getFirstId();
$accessUrl = $this->accessUrlRepository->find($urlId);
$accessUrl = $this->getFirstAccessUrl();

if ($this->isMultipleEnabled()) {
$url = $this->router->generate('index', [], UrlGeneratorInterface::ABSOLUTE_URL);
Expand Down
32 changes: 32 additions & 0 deletions src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Chamilo\CoreBundle\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Chamilo\CoreBundle\Entity\AccessUrlRelColorTheme;
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;

/**
* @template-implements ProviderInterface<AccessUrlRelColorTheme>
*/
class AccessUrlRelColorThemeStateProvider implements ProviderInterface
{
public function __construct(

Check warning on line 15 in src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php#L15

Added line #L15 was not covered by tests
private readonly AccessUrlHelper $accessUrlHelper,
) {}

Check warning on line 17 in src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php#L17

Added line #L17 was not covered by tests

/**
* @inheritdoc
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = [])

Check warning on line 22 in src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php#L22

Added line #L22 was not covered by tests
{
$colorThemes = $this->accessUrlHelper->getCurrent()->getColorThemes();

Check warning on line 24 in src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php#L24

Added line #L24 was not covered by tests

if (0 == $colorThemes->count()) {
$colorThemes = $this->accessUrlHelper->getFirstAccessUrl()->getColorThemes();

Check warning on line 27 in src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php#L26-L27

Added lines #L26 - L27 were not covered by tests
}

return $colorThemes;

Check warning on line 30 in src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/State/AccessUrlRelColorThemeStateProvider.php#L30

Added line #L30 was not covered by tests
}
}

0 comments on commit 3a18ca6

Please sign in to comment.