Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / package / plugin / ACPTemplatePackageInstallationPlugin.class.php
CommitLineData
11ade432
AE
1<?php
2namespace wcf\system\package\plugin;
04727c8b
MS
3use wcf\data\application\Application;
4use wcf\data\package\Package;
11ade432 5use wcf\system\package\ACPTemplatesFileHandler;
870b0938 6use wcf\system\package\PackageArchive;
11ade432 7use wcf\system\WCF;
11ade432
AE
8
9/**
a17de04e 10 * Installs, updates and deletes ACP templates.
9f959ced 11 *
04727c8b 12 * @author Alexander Ebert, Matthias Schmidt
ca4ba303 13 * @copyright 2001-2014 WoltLab GmbH
11ade432
AE
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package com.woltlab.wcf
16 * @subpackage system.package.plugin
9f959ced 17 * @category Community Framework
11ade432 18 */
2f7488d7 19class ACPTemplatePackageInstallationPlugin extends AbstractPackageInstallationPlugin {
b5be6fb0 20 /**
0ad90fc3 21 * @see \wcf\system\package\plugin\AbstractPackageInstallationPlugin::$tableName
b5be6fb0 22 */
11ade432
AE
23 public $tableName = 'acp_template';
24
25 /**
0ad90fc3 26 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::install()
11ade432
AE
27 */
28 public function install() {
29 parent::install();
9f959ced 30
04727c8b
MS
31 $abbreviation = 'wcf';
32 if (isset($this->instruction['attributes']['application'])) {
33 $abbreviation = $this->instruction['attributes']['application'];
34 }
35 else if ($this->installation->getPackage()->isApplication) {
36 $abbreviation = Package::getAbbreviation($this->installation->getPackage()->package);
37 }
38
39 // absolute path to package dir
40 $packageDir = Application::getDirectory($abbreviation);
41
11ade432
AE
42 // extract files.tar to temp folder
43 $sourceFile = $this->installation->getArchive()->extractTar($this->instruction['value'], 'acptemplates_');
44
45 // create file handler
04727c8b 46 $fileHandler = new ACPTemplatesFileHandler($this->installation, $abbreviation);
11ade432 47
04727c8b
MS
48 // extract templates
49 $this->installation->extractFiles($packageDir.'acp/templates/', $sourceFile, $fileHandler);
11ade432
AE
50
51 // delete temporary sourceArchive
52 @unlink($sourceFile);
53 }
54
55 /**
0ad90fc3 56 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::uninstall()
11ade432
AE
57 */
58 public function uninstall() {
04727c8b
MS
59 // fetch ACP templates from log
60 $sql = "SELECT templateName, application
11ade432 61 FROM wcf".WCF_N."_acp_template
39bea7dd 62 WHERE packageID = ?";
11ade432
AE
63 $statement = WCF::getDB()->prepareStatement($sql);
64 $statement->execute(array($this->installation->getPackageID()));
04727c8b
MS
65
66 $templates = array();
11ade432 67 while ($row = $statement->fetchArray()) {
04727c8b
MS
68 if (!isset($templates[$row['application']])) {
69 $templates[$row['application']] = array();
70 }
71
72 $templates[$row['application']][] = 'acp/templates/'.$row['templateName'].'.tpl';
11ade432
AE
73 }
74
04727c8b
MS
75 foreach ($templates as $application => $templateNames) {
76 $this->installation->deleteFiles(Application::getDirectory($application), $templateNames, false, $this->installation->getPackage()->isApplication);
11ade432
AE
77
78 // delete log entries
79 parent::uninstall();
80 }
81 }
870b0938
AE
82
83 /**
84 * @see \wcf\system\package\plugin\IPackageInstallationPlugin::isValid()
85 */
86 public static function isValid(PackageArchive $archive, $instruction) {
87 if (preg_match('~\.(tar(\.gz)?|tgz)$~', $instruction)) {
88 // check if file actually exists
89 try {
90 if ($archive->getTar()->getIndexByFilename($instruction) === false) {
91 return false;
92 }
93 }
94 catch (\SystemException $e) {
95 return false;
96 }
97
98 return true;
99 }
100
101 return false;
102 }
11ade432 103}