Update tpl-type page's template in a single method
authorAlexander Ebert <ebert@woltlab.com>
Wed, 20 Dec 2017 22:47:38 +0000 (23:47 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 20 Dec 2017 22:47:38 +0000 (23:47 +0100)
wcfsetup/install/files/lib/data/page/PageAction.class.php
wcfsetup/install/files/lib/data/page/PageEditor.class.php
wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php

index 95f3ed61420c83fc18757e3b1c2b4d7c9a2e4ab1..17be30f503700a88f23c6fc82a5fa004e8dedbb9 100644 (file)
@@ -135,8 +135,9 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction,
                // save template
                if ($page->pageType == 'tpl') {
                        if (!empty($this->parameters['content'])) {
+                               $pageEditor = new PageEditor($page);
                                foreach ($this->parameters['content'] as $languageID => $content) {
-                                       file_put_contents(WCF_DIR . 'templates/' . $page->getTplName(($languageID ?: null)) . '.tpl', $content['content']);
+                                       $pageEditor->updateTemplate($languageID ?: null, $content['content']);
                                }
                        }
                }
@@ -232,7 +233,7 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction,
                                // save template
                                if ($page->pageType == 'tpl') {
                                        foreach ($this->parameters['content'] as $languageID => $content) {
-                                               file_put_contents(WCF_DIR . 'templates/' . $page->getTplName(($languageID ?: null)) . '.tpl', $content['content']);
+                                               $page->updateTemplate($languageID ?: null, $content['content']);
                                        }
                                }
                                
index 7dfdd3f1b5b9aeed7316e253ef65aa4d6d6e43d1..6fa0eb0f2dd5e77403a7965cc3410730a97b5d9f 100644 (file)
@@ -29,6 +29,20 @@ class PageEditor extends DatabaseObjectEditor implements IEditableCachedObject {
         */
        protected static $baseClass = Page::class;
        
+       /**
+        * Creates or updates the page's template file.
+        * 
+        * @param       integer         $languageID     language id or `null`
+        * @param       string          $content        template content
+        */
+       public function updateTemplate($languageID, $content) {
+               if ($this->pageType !== 'tpl') {
+                       throw new \RuntimeException("Only tpl-type pages support template files.");
+               }
+               
+               file_put_contents(WCF_DIR . 'templates/' . $this->getTplName(($languageID ?: null)) . '.tpl', $content);
+       }
+       
        /**
         * @inheritDoc
         */
index c9aec695302c632ef4d56febdf5f0d81f7080b88..9025449f23ca28acf43ffc00d16e62fcad032c93 100644 (file)
@@ -336,7 +336,7 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                                        // generate template if page's type is 'tpl'
                                        $page = new Page($pageID);
                                        if ($page->pageType == 'tpl') {
-                                               file_put_contents(WCF_DIR . 'templates/' . $page->getTplName(($languageID ?: null)) . '.tpl', $content['content']);
+                                               (new PageEditor($page))->updateTemplate($languageID, $content['content']);
                                        }
                                }
                        }