Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / package / plugin / StylePackageInstallationPlugin.class.php
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\style\StyleEditor;
4 use wcf\data\style\StyleList;
5 use wcf\system\event\EventHandler;
6 use wcf\system\style\StyleHandler;
7
8 /**
9 * Installs, updates and deletes styles.
10 *
11 * @author Marcel Werk
12 * @copyright 2001-2019 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\System\Package\Plugin
15 */
16 class StylePackageInstallationPlugin extends AbstractPackageInstallationPlugin {
17 /**
18 * @inheritDoc
19 */
20 public $className = StyleEditor::class;
21
22 /**
23 * @inheritDoc
24 */
25 public function install() {
26 parent::install();
27
28 // extract style tar
29 $filename = $this->installation->getArchive()->extractTar($this->instruction['value'], 'style_');
30
31 // searches for non-tainted style for updating
32 $styleEditor = StyleHandler::getInstance()->getStyleByName($this->installation->getPackageName(), false);
33
34 // import style
35 $style = StyleEditor::import($filename, $this->installation->getPackageID(), $styleEditor, !PACKAGE_ID);
36
37 // set style as default
38 if (isset($this->instruction['attributes']['default'])) {
39 $style->setAsDefault();
40 }
41
42 // remove tmp file
43 @unlink($filename);
44 }
45
46 /**
47 * @inheritDoc
48 */
49 public function uninstall() {
50 // call uninstall event
51 EventHandler::getInstance()->fireAction($this, 'uninstall');
52
53 // get all style of this package
54 $isDefault = false;
55 $styleList = new StyleList();
56 $styleList->getConditionBuilder()->add("packageID = ?", [$this->installation->getPackageID()]);
57 $styleList->readObjects();
58
59 foreach ($styleList->getObjects() as $style) {
60 $styleEditor = new StyleEditor($style);
61 $styleEditor->delete();
62
63 $isDefault = $isDefault || $style->isDefault;
64 }
65
66 // default style deleted
67 if ($isDefault) {
68 $styleList = new StyleList();
69 $styleList->sqlOrderBy = 'style.styleID ASC';
70 $styleList->sqlLimit = 1;
71 $styleList->readObjects();
72 $style = $styleList->getSingleObject();
73
74 if ($style !== null) {
75 (new StyleEditor($style))->setAsDefault();
76 }
77 }
78 }
79 }