Resolve language item-related PIP GUI todos
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / package / ACPTemplatesFileHandler.class.php
1 <?php
2 namespace wcf\system\package;
3 use wcf\data\package\Package;
4 use wcf\system\database\util\PreparedStatementConditionBuilder;
5 use wcf\system\exception\SystemException;
6 use wcf\system\WCF;
7
8 /**
9 * File handler implementation for the installation of ACP template files.
10 *
11 * @author Alexander Ebert, Matthias Schmidt
12 * @copyright 2001-2018 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\System\Package
15 */
16 class ACPTemplatesFileHandler extends PackageInstallationFileHandler {
17 /**
18 * template type supports template groups
19 * @var boolean
20 */
21 protected $supportsTemplateGroups = false;
22
23 /**
24 * name of the database table where the installed files are logged
25 * @var string
26 */
27 protected $tableName = 'acp_template';
28
29 /**
30 * @inheritDoc
31 */
32 public function checkFiles(array $files) {
33 if ($this->packageInstallation->getPackage()->package != 'com.woltlab.wcf') {
34 // check if files are existing already
35 if (!empty($files)) {
36 foreach ($files as &$file) {
37 $file = substr($file, 0, -4);
38 }
39 unset($file);
40
41 // get by other packages registered files
42 $conditions = new PreparedStatementConditionBuilder();
43 $conditions->add('packageID <> ?', [$this->packageInstallation->getPackageID()]);
44 $conditions->add('templateName IN (?)', [$files]);
45 $conditions->add('application = ?', [$this->application]);
46 if ($this->supportsTemplateGroups) {
47 $conditions->add("templateGroupID IS NULL");
48 }
49
50 $sql = "SELECT packageID, templateName
51 FROM wcf".WCF_N."_".$this->tableName."
52 ".$conditions;
53 $statement = WCF::getDB()->prepareStatement($sql);
54 $statement->execute($conditions->getParameters());
55 $lockedFiles = $statement->fetchMap('templateName', 'packageID');
56
57 // check if acp templates from the package beeing
58 // installed are in conflict with already installed
59 // files
60 if (!$this->packageInstallation->getPackage()->isApplication && !empty($lockedFiles)) {
61 foreach ($files as $file) {
62 if (isset($lockedFiles[$file])) {
63 $owningPackage = new Package($lockedFiles[$file]);
64
65 throw new SystemException("A package can't overwrite templates from other packages. Only an update from the package which owns the template can do that. (Package '".$this->packageInstallation->getPackage()->package."' tries to overwrite template '".$file."', which is owned by package '".$owningPackage->package."')");
66 }
67 }
68 }
69 }
70 }
71 }
72
73 /**
74 * @inheritDoc
75 */
76 public function logFiles(array $files) {
77 // remove file extension
78 foreach ($files as &$file) {
79 $file = substr($file, 0, -4);
80 }
81 unset($file);
82
83 // fetch already installed acp templates
84 $conditions = new PreparedStatementConditionBuilder();
85 $conditions->add('packageID = ?', [$this->packageInstallation->getPackageID()]);
86 $conditions->add('templateName IN (?)', [$files]);
87 $conditions->add('application = ?', [$this->application]);
88
89 $sql = "SELECT templateName
90 FROM wcf".WCF_N."_".$this->tableName."
91 ".$conditions;
92 $statement = WCF::getDB()->prepareStatement($sql);
93 $statement->execute($conditions->getParameters());
94
95 while ($templateName = $statement->fetchColumn()) {
96 $index = array_search($templateName, $files);
97
98 if ($index !== false) {
99 unset($files[$index]);
100 }
101 }
102
103 if (!empty($files)) {
104 $sql = "INSERT INTO wcf".WCF_N."_".$this->tableName."
105 (packageID, templateName, application)
106 VALUES (?, ?, ?)";
107 $statement = WCF::getDB()->prepareStatement($sql);
108
109 foreach ($files as $file) {
110 $statement->execute([
111 $this->packageInstallation->getPackageID(),
112 $file,
113 $this->application
114 ]);
115 }
116 }
117 }
118 }