33namespace Neusta \Pimcore \PresentationBundle ;
44
55use Pimcore \Extension \Bundle \Installer \AbstractInstaller ;
6- use Pimcore \Model \Asset \Image \Thumbnail ;
6+ use Pimcore \Model \Asset \Image \Thumbnail \ Config as ThumbnailConfig ;
77use Pimcore \Model \Document \DocType ;
88
99class Installer extends AbstractInstaller
1010{
11+ private const THUMBNAIL_CONFIG_BACKGROUND_IMAGE = 'pimcore-presentation-bundle-background-image ' ;
12+
1113 public function install (): void
1214 {
1315 $ this ->installDocumentTypes ();
1416 $ this ->installThumbnailConfiguration ();
1517 }
1618
19+ public function uninstall (): void
20+ {
21+ $ this ->uninstallDocumentTypes ();
22+ $ this ->uninstallThumbnailConfiguration ();
23+ }
24+
1725 public function isInstalled (): bool
1826 {
19- return false ;
27+ $ documentTypeDefinitions = $ this ->getDocumentTypeDefinitions ();
28+ foreach ($ documentTypeDefinitions as $ documentTypeDefinition ) {
29+ if (!DocType::getById ($ documentTypeDefinition ['id ' ])) {
30+ return false ;
31+ }
32+ }
33+
34+ return true ;
2035 }
2136
2237 public function canBeInstalled (): bool
2338 {
2439 return !$ this ->isInstalled ();
2540 }
2641
42+ public function canBeUninstalled (): bool
43+ {
44+ return $ this ->isInstalled ();
45+ }
46+
2747 public function needsReloadAfterInstall (): bool
2848 {
2949 return true ;
3050 }
3151
3252 private function installDocumentTypes (): void
3353 {
34- $ typeDefinitionsFile =
35- __DIR__ . \DIRECTORY_SEPARATOR .
36- 'Resources ' . \DIRECTORY_SEPARATOR .
37- 'config ' . \DIRECTORY_SEPARATOR .
38- 'document-types.php ' ;
39- $ typeDefinitions = include $ typeDefinitionsFile ;
40- foreach ($ typeDefinitions as $ typeDefinition ) {
41- $ this ->installDocumentType ($ typeDefinition );
54+ $ documentTypeDefinitions = $ this ->getDocumentTypeDefinitions ();
55+ foreach ($ documentTypeDefinitions as $ documentTypeDefinition ) {
56+ $ this ->installDocumentType ($ documentTypeDefinition );
57+ }
58+ }
59+
60+ private function uninstallDocumentTypes (): void
61+ {
62+ $ documentTypeDefinitions = $ this ->getDocumentTypeDefinitions ();
63+ foreach ($ documentTypeDefinitions as $ documentTypeDefinition ) {
64+ if ($ docType = DocType::getById ($ documentTypeDefinition ['id ' ])) {
65+ $ docType ->delete ();
66+ }
4267 }
4368 }
4469
@@ -53,31 +78,58 @@ private function installDocumentTypes(): void
5378 * 'priority':int,
5479 * 'creationDate':int,
5580 * 'modificationDate':int
56- * } $typeDefinition
81+ * } $documentTypeDefinition
5782 */
58- private function installDocumentType (array $ typeDefinition ): void
83+ private function installDocumentType (array $ documentTypeDefinition ): void
5984 {
6085 $ model = new DocType ();
61- $ model ->setId ($ typeDefinition ['id ' ]);
62- $ model ->setName ($ typeDefinition ['name ' ]);
63- $ model ->setGroup ($ typeDefinition ['group ' ]);
64- $ model ->setController ($ typeDefinition ['controller ' ]);
65- $ model ->setTemplate ($ typeDefinition ['template ' ]);
66- $ model ->setType ($ typeDefinition ['type ' ]);
67- $ model ->setPriority ($ typeDefinition ['priority ' ]);
68- $ model ->setCreationDate ($ typeDefinition ['creationDate ' ]);
69- $ model ->setModificationDate ($ typeDefinition ['modificationDate ' ]);
86+ $ model ->setId ($ documentTypeDefinition ['id ' ]);
87+ $ model ->setName ($ documentTypeDefinition ['name ' ]);
88+ $ model ->setGroup ($ documentTypeDefinition ['group ' ]);
89+ $ model ->setController ($ documentTypeDefinition ['controller ' ]);
90+ $ model ->setTemplate ($ documentTypeDefinition ['template ' ]);
91+ $ model ->setType ($ documentTypeDefinition ['type ' ]);
92+ $ model ->setPriority ($ documentTypeDefinition ['priority ' ]);
93+ $ model ->setCreationDate ($ documentTypeDefinition ['creationDate ' ]);
94+ $ model ->setModificationDate ($ documentTypeDefinition ['modificationDate ' ]);
7095 $ model ->save ();
7196 }
7297
7398 private function installThumbnailConfiguration (): void
7499 {
75- if (Thumbnail \Config ::exists (' pimcore-presentation-bundle-background-image ' )) {
100+ if (ThumbnailConfig ::exists (self :: THUMBNAIL_CONFIG_BACKGROUND_IMAGE )) {
76101 return ;
77102 }
78103
79- $ thumbnailConfig = new Thumbnail \ Config ();
80- $ thumbnailConfig ->setName (' pimcore-presentation-bundle-background-image ' );
104+ $ thumbnailConfig = new ThumbnailConfig ();
105+ $ thumbnailConfig ->setName (self :: THUMBNAIL_CONFIG_BACKGROUND_IMAGE );
81106 $ thumbnailConfig ->save ();
82107 }
108+
109+ private function uninstallThumbnailConfiguration (): void
110+ {
111+ if ($ thumbnailConfig = ThumbnailConfig::getByName (self ::THUMBNAIL_CONFIG_BACKGROUND_IMAGE )) {
112+ $ thumbnailConfig ->delete ();
113+ }
114+ }
115+
116+ /**
117+ * @return array<int, array{
118+ * 'id':string,
119+ * 'name':string,
120+ * 'group':string,
121+ * 'controller':string,
122+ * 'template':string,
123+ * 'type':string,
124+ * 'priority':int,
125+ * 'creationDate':int,
126+ * 'modificationDate':int
127+ * }>
128+ */
129+ private function getDocumentTypeDefinitions (): array
130+ {
131+ return require dirname (__DIR__ ) . \DIRECTORY_SEPARATOR .
132+ 'config ' . \DIRECTORY_SEPARATOR .
133+ 'document-types.php ' ;
134+ }
83135}
0 commit comments