Skip to content

Commit 8cd73f9

Browse files
committed
Internal: Change migration for plugin with settings by access url #2675
1 parent ce02ea3 commit 8cd73f9

File tree

7 files changed

+308
-95
lines changed

7 files changed

+308
-95
lines changed

Diff for: public/main/inc/lib/plugin.lib.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,10 @@ public function getInstalledPluginsInCurrentUrl()
192192
*
193193
* @return array
194194
*/
195-
public function getOfficialPlugins()
195+
public static function getOfficialPlugins(): array
196196
{
197-
static $officialPlugins = null;
198197
// Please keep this list alphabetically sorted
199-
$officialPlugins = [
198+
return [
200199
'advanced_subscription',
201200
'ai_helper',
202201
'azure_active_directory',
@@ -252,8 +251,6 @@ public function getOfficialPlugins()
252251
'userremoteservice',
253252
'zoom',
254253
];
255-
256-
return $officialPlugins;
257254
}
258255

259256
/**

Diff for: src/CoreBundle/Entity/AccessUrl.php

+37
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ class AccessUrl extends AbstractResource implements ResourceInterface, Stringabl
153153
#[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelColorTheme::class, cascade: ['persist'], orphanRemoval: true)]
154154
private Collection $colorThemes;
155155

156+
/**
157+
* @var Collection<int, AccessUrlRelPlugin>
158+
*/
159+
#[ORM\OneToMany(mappedBy: 'url', targetEntity: AccessUrlRelPlugin::class, orphanRemoval: true)]
160+
private Collection $plugins;
161+
156162
public function __construct()
157163
{
158164
$this->description = '';
@@ -166,6 +172,7 @@ public function __construct()
166172
$this->courseCategory = new ArrayCollection();
167173
$this->children = new ArrayCollection();
168174
$this->colorThemes = new ArrayCollection();
175+
$this->plugins = new ArrayCollection();
169176
}
170177

171178
public function __toString(): string
@@ -547,4 +554,34 @@ public function getColorThemeByTheme(ColorTheme $theme): ?AccessUrlRelColorTheme
547554

548555
return $this->colorThemes->matching($criteria)->first() ?: null;
549556
}
557+
558+
/**
559+
* @return Collection<int, AccessUrlRelPlugin>
560+
*/
561+
public function getPlugins(): Collection
562+
{
563+
return $this->plugins;
564+
}
565+
566+
public function addPlugin(AccessUrlRelPlugin $plugin): static
567+
{
568+
if (!$this->plugins->contains($plugin)) {
569+
$this->plugins->add($plugin);
570+
$plugin->setUrl($this);
571+
}
572+
573+
return $this;
574+
}
575+
576+
public function removePlugin(AccessUrlRelPlugin $plugin): static
577+
{
578+
if ($this->plugins->removeElement($plugin)) {
579+
// set the owning side to null (unless already changed)
580+
if ($plugin->getUrl() === $this) {
581+
$plugin->setUrl(null);
582+
}
583+
}
584+
585+
return $this;
586+
}
550587
}

Diff for: src/CoreBundle/Entity/AccessUrlRelPlugin.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/* For licensing terms, see /license.txt */
4+
5+
declare(strict_types=1);
6+
7+
namespace Chamilo\CoreBundle\Entity;
8+
9+
use Chamilo\CoreBundle\Repository\AccessUrlRelPluginRepository;
10+
use Doctrine\ORM\Mapping as ORM;
11+
12+
#[ORM\Entity(repositoryClass: AccessUrlRelPluginRepository::class)]
13+
class AccessUrlRelPlugin
14+
{
15+
#[ORM\Id]
16+
#[ORM\GeneratedValue]
17+
#[ORM\Column]
18+
private ?int $id = null;
19+
20+
#[ORM\ManyToOne(inversedBy: 'accessUrlRelPlugins')]
21+
#[ORM\JoinColumn(nullable: false)]
22+
private ?Plugin $plugin = null;
23+
24+
#[ORM\ManyToOne(inversedBy: 'plugins')]
25+
#[ORM\JoinColumn(nullable: false)]
26+
private ?AccessUrl $url = null;
27+
28+
#[ORM\Column]
29+
private ?bool $active = null;
30+
31+
#[ORM\Column(nullable: true)]
32+
private ?array $configuration = null;
33+
34+
public function getId(): ?int
35+
{
36+
return $this->id;
37+
}
38+
39+
public function getPlugin(): ?Plugin
40+
{
41+
return $this->plugin;
42+
}
43+
44+
public function setPlugin(?Plugin $plugin): static
45+
{
46+
$this->plugin = $plugin;
47+
48+
return $this;
49+
}
50+
51+
public function getUrl(): ?AccessUrl
52+
{
53+
return $this->url;
54+
}
55+
56+
public function setUrl(?AccessUrl $url): static
57+
{
58+
$this->url = $url;
59+
60+
return $this;
61+
}
62+
63+
public function isActive(): ?bool
64+
{
65+
return $this->active;
66+
}
67+
68+
public function setActive(bool $active): static
69+
{
70+
$this->active = $active;
71+
72+
return $this;
73+
}
74+
75+
public function getConfiguration(): ?array
76+
{
77+
return $this->configuration;
78+
}
79+
80+
public function setConfiguration(?array $configuration): static
81+
{
82+
$this->configuration = $configuration;
83+
84+
return $this;
85+
}
86+
}

Diff for: src/CoreBundle/Entity/Plugin.php

+48-50
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,49 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/* For licensing terms, see /license.txt */
64

5+
declare(strict_types=1);
6+
77
namespace Chamilo\CoreBundle\Entity;
88

9-
use ApiPlatform\Metadata\ApiResource;
9+
use Doctrine\Common\Collections\ArrayCollection;
10+
use Doctrine\Common\Collections\Collection;
1011
use Doctrine\ORM\Mapping as ORM;
11-
use Symfony\Component\Serializer\Annotation\Groups;
1212

13-
#[ApiResource]
1413
#[ORM\Entity]
1514
#[ORM\Table(name: 'plugin')]
1615
class Plugin
1716
{
17+
public const SOURCE_THIRD_PARTY = 'third_party';
18+
public const SOURCE_OFFICIAL = 'official';
19+
1820
#[ORM\Id]
1921
#[ORM\GeneratedValue]
2022
#[ORM\Column(type: 'integer')]
2123
private ?int $id = null;
2224

2325
#[ORM\Column(type: 'string', length: 255, unique: true)]
24-
#[Groups(['plugin:read', 'plugin:write'])]
2526
private string $title;
2627

2728
#[ORM\Column(type: 'boolean')]
28-
#[Groups(['plugin:read', 'plugin:write'])]
2929
private bool $installed = false;
3030

31-
#[ORM\Column(type: 'boolean')]
32-
#[Groups(['plugin:read', 'plugin:write'])]
33-
private bool $active = false;
34-
3531
#[ORM\Column(type: 'string', length: 20)]
36-
#[Groups(['plugin:read', 'plugin:write'])]
37-
private string $version;
32+
private string $installedVersion;
3833

39-
#[ORM\Column(type: 'integer')]
40-
#[Groups(['plugin:read', 'plugin:write'])]
41-
private int $accessUrlId;
34+
#[ORM\Column(type: 'string', length: 20, options: ['default' => self::SOURCE_THIRD_PARTY])]
35+
private string $source = 'third_party';
4236

43-
#[ORM\Column(type: 'json', nullable: true)]
44-
#[Groups(['plugin:read', 'plugin:write'])]
45-
private ?array $configuration = [];
37+
/**
38+
* @var Collection<int, AccessUrlRelPlugin>
39+
*/
40+
#[ORM\OneToMany(mappedBy: 'plugin', targetEntity: AccessUrlRelPlugin::class, orphanRemoval: true)]
41+
private Collection $urls;
4642

47-
#[ORM\Column(type: 'string', length: 20, options: ["default" => "third_party"])]
48-
#[Groups(['plugin:read', 'plugin:write'])]
49-
private string $source = 'third_party';
43+
public function __construct()
44+
{
45+
$this->urls = new ArrayCollection();
46+
}
5047

5148
public function getId(): ?int
5249
{
@@ -61,6 +58,7 @@ public function getTitle(): string
6158
public function setTitle(string $title): self
6259
{
6360
$this->title = $title;
61+
6462
return $this;
6563
}
6664

@@ -72,61 +70,61 @@ public function isInstalled(): bool
7270
public function setInstalled(bool $installed): self
7371
{
7472
$this->installed = $installed;
75-
return $this;
76-
}
77-
78-
public function isActive(): bool
79-
{
80-
return $this->active;
81-
}
8273

83-
public function setActive(bool $active): self
84-
{
85-
$this->active = $active;
8674
return $this;
8775
}
8876

89-
public function getVersion(): string
77+
public function getInstalledVersion(): string
9078
{
91-
return $this->version;
79+
return $this->installedVersion;
9280
}
9381

94-
public function setVersion(string $version): self
82+
public function setInstalledVersion(string $installedVersion): self
9583
{
96-
$this->version = $version;
84+
$this->installedVersion = $installedVersion;
85+
9786
return $this;
9887
}
9988

100-
public function getAccessUrlId(): int
89+
public function getSource(): string
10190
{
102-
return $this->accessUrlId;
91+
return $this->source;
10392
}
10493

105-
public function setAccessUrlId(int $accessUrlId): self
94+
public function setSource(string $source): self
10695
{
107-
$this->accessUrlId = $accessUrlId;
96+
$this->source = $source;
97+
10898
return $this;
10999
}
110100

111-
public function getConfiguration(): ?array
101+
/**
102+
* @return Collection<int, AccessUrlRelPlugin>
103+
*/
104+
public function getUrls(): Collection
112105
{
113-
return $this->configuration;
106+
return $this->urls;
114107
}
115108

116-
public function setConfiguration(?array $configuration): self
109+
public function addUrl(AccessUrlRelPlugin $url): static
117110
{
118-
$this->configuration = $configuration;
111+
if (!$this->urls->contains($url)) {
112+
$this->urls->add($url);
113+
$url->setPlugin($this);
114+
}
115+
119116
return $this;
120117
}
121118

122-
public function getSource(): string
119+
public function removeUrl(AccessUrlRelPlugin $url): static
123120
{
124-
return $this->source;
125-
}
121+
if ($this->urls->removeElement($url)) {
122+
// set the owning side to null (unless already changed)
123+
if ($url->getPlugin() === $this) {
124+
$url->setPlugin(null);
125+
}
126+
}
126127

127-
public function setSource(string $source): self
128-
{
129-
$this->source = $source;
130128
return $this;
131129
}
132130
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
/* For licensing terms, see /license.txt */
4+
35
declare(strict_types=1);
46

57
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
@@ -17,23 +19,17 @@ public function getDescription(): string
1719
public function up(Schema $schema): void
1820
{
1921
// Create the plugin table
20-
$this->addSql("
21-
CREATE TABLE plugin (
22-
id INT AUTO_INCREMENT PRIMARY KEY,
23-
title VARCHAR(255) UNIQUE NOT NULL,
24-
installed TINYINT(1) NOT NULL DEFAULT 0,
25-
active TINYINT(1) NOT NULL DEFAULT 0,
26-
version VARCHAR(20) NOT NULL DEFAULT '1.0.0',
27-
access_url_id INT NOT NULL,
28-
configuration JSON DEFAULT NULL,
29-
source ENUM('official', 'third_party') NOT NULL DEFAULT 'third_party'
30-
);
31-
");
22+
$this->addSql("CREATE TABLE plugin (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, installed TINYINT(1) NOT NULL, installed_version VARCHAR(20) NOT NULL, source VARCHAR(20) DEFAULT 'third_party' NOT NULL, UNIQUE INDEX UNIQ_E96E27942B36786B (title), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC");
23+
24+
$this->addSql("CREATE TABLE access_url_rel_plugin (id INT AUTO_INCREMENT NOT NULL, plugin_id INT NOT NULL, url_id INT NOT NULL, active TINYINT(1) NOT NULL, configuration LONGTEXT DEFAULT NULL COMMENT '(DC2Type:json)', INDEX IDX_7167B425EC942BCF (plugin_id), INDEX IDX_7167B42581CFDAE7 (url_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC");
25+
$this->addSql("ALTER TABLE access_url_rel_plugin ADD CONSTRAINT FK_7167B425EC942BCF FOREIGN KEY (plugin_id) REFERENCES plugin (id)");
26+
$this->addSql("ALTER TABLE access_url_rel_plugin ADD CONSTRAINT FK_7167B42581CFDAE7 FOREIGN KEY (url_id) REFERENCES access_url (id)");
3227
}
3328

3429
public function down(Schema $schema): void
3530
{
3631
// Drop the plugin table if rolling back the migration
32+
$this->addSql("DROP TABLE access_url_rel_plugin;");
3733
$this->addSql("DROP TABLE plugin;");
3834
}
3935
}

0 commit comments

Comments
 (0)