Commit | Line | Data |
---|---|---|
11ade432 AE |
1 | <?php |
2 | namespace wcf\data\template; | |
3 | use wcf\data\DatabaseObjectEditor; | |
4 | use wcf\system\io\File; | |
ac9b0f6e | 5 | use wcf\system\Regex; |
11ade432 | 6 | use wcf\system\WCF; |
691ea087 | 7 | use wcf\util\DirectoryUtil; |
40e87278 | 8 | use wcf\util\FileUtil; |
11ade432 AE |
9 | |
10 | /** | |
a17de04e | 11 | * Provides functions to edit templates. |
11ade432 AE |
12 | * |
13 | * @author Marcel Werk | |
c839bd49 | 14 | * @copyright 2001-2018 WoltLab GmbH |
11ade432 | 15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 16 | * @package WoltLabSuite\Core\Data\Template |
4bef7e27 MS |
17 | * |
18 | * @method Template getDecoratedObject() | |
19 | * @mixin Template | |
11ade432 AE |
20 | */ |
21 | class TemplateEditor extends DatabaseObjectEditor { | |
22 | /** | |
4bef7e27 | 23 | * @inheritDoc |
11ade432 | 24 | */ |
4bef7e27 | 25 | protected static $baseClass = Template::class; |
11ade432 AE |
26 | |
27 | /** | |
4bef7e27 | 28 | * @inheritDoc |
01d8af08 | 29 | * @return Template |
11ade432 | 30 | */ |
4bef7e27 | 31 | public static function create(array $parameters = []) { |
11ade432 AE |
32 | // obtain default values |
33 | if (!isset($parameters['packageID'])) $parameters['packageID'] = PACKAGE_ID; | |
12aa6f89 | 34 | if (!isset($parameters['lastModificationTime'])) $parameters['lastModificationTime'] = TIME_NOW; |
11ade432 | 35 | |
01d8af08 | 36 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
12aa6f89 | 37 | return parent::create($parameters); |
11ade432 AE |
38 | } |
39 | ||
40 | /** | |
41 | * Saves the source of this template. | |
42 | * | |
e3369fd2 | 43 | * @param string $source |
11ade432 AE |
44 | */ |
45 | public function setSource($source) { | |
46 | $path = $this->getPath(); | |
47 | // create dir | |
48 | $folder = dirname($path); | |
49 | if (!file_exists($folder)) { | |
50 | mkdir($folder, 0777); | |
51 | } | |
52 | ||
e3369fd2 | 53 | // set source |
11ade432 AE |
54 | $file = new File($path); |
55 | $file->write($source); | |
56 | $file->close(); | |
1232bce2 | 57 | FileUtil::makeWritable($path); |
11ade432 AE |
58 | } |
59 | ||
60 | /** | |
61 | * Renames the file of this template. | |
62 | * | |
63 | * @param string $name | |
64 | * @param integer $templateGroupID | |
65 | */ | |
12aa6f89 | 66 | public function rename($name, $templateGroupID = 0) { |
11ade432 AE |
67 | // get current path |
68 | $currentPath = $this->getPath(); | |
a17de04e | 69 | |
e3369fd2 | 70 | // get new path |
12aa6f89 | 71 | if ($templateGroupID != $this->templateGroupID) { |
11ade432 AE |
72 | // get folder name |
73 | $sql = "SELECT templateGroupFolderName | |
74 | FROM wcf".WCF_N."_template_group | |
75 | WHERE templateGroupID = ?"; | |
76 | $statement = WCF::getDB()->prepareStatement($sql); | |
4bef7e27 | 77 | $statement->execute([$templateGroupID]); |
11ade432 | 78 | $row = $statement->fetchArray(); |
12aa6f89 | 79 | $this->object->data['templateGroupFolderName'] = $row['templateGroupFolderName']; |
11ade432 AE |
80 | } |
81 | ||
82 | // delete compiled templates | |
83 | $this->deleteCompiledFiles(); | |
84 | ||
85 | // rename | |
12aa6f89 | 86 | $this->object->data['templateName'] = $name; |
11ade432 AE |
87 | $newPath = $this->getPath(); |
88 | ||
89 | // move file | |
90 | @rename($currentPath, $newPath); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Deletes this template. | |
95 | */ | |
96 | public function delete() { | |
97 | $this->deleteFile(); | |
98 | ||
99 | $sql = "DELETE FROM wcf".WCF_N."_template | |
100 | WHERE templateID = ?"; | |
101 | $statement = WCF::getDB()->prepareStatement($sql); | |
4bef7e27 | 102 | $statement->execute([$this->templateID]); |
11ade432 AE |
103 | } |
104 | ||
105 | /** | |
106 | * Deletes the file of this template. | |
107 | */ | |
108 | public function deleteFile() { | |
109 | // delete source | |
110 | @unlink($this->getPath()); | |
111 | ||
112 | // delete compiled templates | |
113 | $this->deleteCompiledFiles(); | |
114 | } | |
115 | ||
d2146d87 | 116 | /** |
4bef7e27 | 117 | * @inheritDoc |
d2146d87 | 118 | */ |
4bef7e27 | 119 | public static function deleteAll(array $objectIDs = []) { |
d2146d87 MW |
120 | $list = new TemplateList(); |
121 | $list->setObjectIDs($objectIDs); | |
122 | $list->readObjects(); | |
123 | foreach ($list as $template) { | |
124 | $editor = new TemplateEditor($template); | |
125 | $editor->deleteFile(); | |
126 | } | |
127 | ||
128 | return parent::deleteAll($objectIDs); | |
129 | } | |
130 | ||
11ade432 AE |
131 | /** |
132 | * Deletes the compiled files of this template. | |
133 | */ | |
134 | public function deleteCompiledFiles() { | |
6555d8b1 | 135 | DirectoryUtil::getInstance(WCF_DIR . 'templates/compiled/')->removePattern(new Regex($this->templateGroupID . '_' . $this->application . '_.*_' . preg_quote($this->templateName) . '.php$')); |
11ade432 AE |
136 | } |
137 | } |