Added template administration (WIP)
authorMarcel Werk <burntime@woltlab.com>
Thu, 25 Apr 2013 17:35:36 +0000 (19:35 +0200)
committerMarcel Werk <burntime@woltlab.com>
Thu, 25 Apr 2013 17:35:36 +0000 (19:35 +0200)
wcfsetup/install/files/acp/templates/templateAdd.tpl
wcfsetup/install/files/acp/templates/templateList.tpl
wcfsetup/install/files/lib/acp/form/TemplateAddForm.class.php
wcfsetup/install/files/lib/acp/form/TemplateEditForm.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php
wcfsetup/install/files/lib/data/template/Template.class.php
wcfsetup/install/files/lib/data/template/TemplateAction.class.php
wcfsetup/install/files/lib/data/template/TemplateEditor.class.php
wcfsetup/install/files/lib/data/template/TemplateList.class.php

index 7cf910fd120851ca09cf56b4012a791ef28606e0..d7b06a59ea18437bb59f17a7b0e55d533cc56d10 100644 (file)
@@ -17,7 +17,7 @@
 <div class="contentNavigation">
        <nav>
                <ul>
-                       <li><a href="{link controller='TemplateList'}{/link}" class="button"><span class="icon icon16 icon-list"></span> <span>{lang}wcf.acp.menu.link.template.list{/lang}</span></a></li>
+                       <li><a href="{link controller='TemplateList'}{if $action == 'edit'}templateGroupID={@$template->templateGroupID}{/if}{/link}" class="button"><span class="icon icon16 icon-list"></span> <span>{lang}wcf.acp.menu.link.template.list{/lang}</span></a></li>
                        
                        {event name='contentNavigationButtons'}
                </ul>
                                        </dd>
                                </dl>
                                
-                               <dl{if $errorField == 'templateName'} class="formError"{/if}>
+                               <dl{if $errorField == 'tplName'} class="formError"{/if}>
                                        <dt><label for="tplName">{lang}wcf.global.name{/lang}</label></dt>
                                        <dd>
                                                <input type="text" id="tplName" name="tplName" value="{$tplName}" required="required" class="long" />
-                                               {if $errorField == 'templateName'}
+                                               {if $errorField == 'tplName'}
                                                        <small class="innerError">
                                                                {if $errorType == 'empty'}
                                                                        {lang}wcf.global.form.error.empty{/lang}
index 914f2ff42172769607286e4d8f3a574b871641a2..577e17ba540346bc5a24fa4a9e1e7f08d57c25f5 100644 (file)
@@ -95,7 +95,7 @@
                                                        {event name='rowButtons'}
                                                </td>
                                                <td class="columnID">{@$template->templateID}</td>
-                                               <td class="columnTitle columnTemplateName">{if $template->templateGroupID}<a href="{link controller='TemplateEdit' id=$template->templateID}{/link}">{$template->templateName}</a>{else}{$template->templateName}{/if}</td>
+                                               <td class="columnTitle columnTemplateName">{if $template->packageDir}[{$template->getPackageAbbreviation()}] {/if}{if $template->templateGroupID}<a href="{link controller='TemplateEdit' id=$template->templateID}{/link}">{$template->templateName}</a>{else}{$template->templateName}{/if}</td>
                                                <td class="columnDate columnLastModificationTime">{@$template->lastModificationTime|time}</td>
                                                
                                                {event name='columns'}
index c89bfb1411ecd2c2c5be170b3f6d2c521910776d..7ad09cde43b25ba1d32faeb2dd9a45f2119ab4bb 100644 (file)
@@ -102,7 +102,7 @@ class TemplateAddForm extends AbstractForm {
                        throw new UserInputException('tplName');
                }
        
-               if (!preg_match('^/[a-z0-9_\-]+$/i', $this->tplName)) {
+               if (!preg_match('/^[a-z0-9_\-]+$/i', $this->tplName)) {
                        throw new UserInputException('tplName', 'notValid');
                }
                
@@ -144,9 +144,9 @@ class TemplateAddForm extends AbstractForm {
                parent::save();
                
                $this->objectAction = new TemplateAction(array(), 'create', array('data' => array(
-                       'templateName' => $this->templateName,
+                       'templateName' => $this->tplName,
                        'packageID' => $this->packageID,
-                       'templateGroupID' => ($this->templateGroupID)
+                       'templateGroupID' => $this->templateGroupID
                ), 'source' => $this->templateSource));
                $this->objectAction->executeAction();
                $this->saved();
diff --git a/wcfsetup/install/files/lib/acp/form/TemplateEditForm.class.php b/wcfsetup/install/files/lib/acp/form/TemplateEditForm.class.php
new file mode 100644 (file)
index 0000000..b02af42
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+namespace wcf\acp\form;
+use wcf\data\template\group\TemplateGroup;
+use wcf\data\template\group\TemplateGroupAction;
+use wcf\data\template\group\TemplateGroupList;
+use wcf\data\template\Template;
+use wcf\data\template\TemplateAction;
+use wcf\form\AbstractForm;
+use wcf\system\exception\IllegalLinkException;
+use wcf\system\exception\UserInputException;
+use wcf\system\WCF;
+use wcf\util\StringUtil;
+
+/**
+ * Shows the form for adding new templates.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2013 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage acp.form
+ * @category   Community Framework
+ */
+class TemplateEditForm extends TemplateAddForm {
+       /**
+        * @see wcf\page\AbstractPage::$activeMenuItem
+        */
+       public $activeMenuItem = 'wcf.acp.menu.link.template';
+       
+       /**
+        * template id
+        * @var integer
+        */
+       public $templateID = 0;
+       
+       /**
+        * template object
+        * @var wcf\data\template\Template
+        */
+       public $template = null;
+       
+       /**
+        * @see wcf\page\IPage::readParameters()
+        */
+       public function readParameters() {
+               parent::readParameters();
+       
+               if (isset($_REQUEST['id'])) $this->templateID = intval($_REQUEST['id']);
+               $this->template = new Template($this->templateID);
+               if (!$this->template->templateID || !$this->template->templateGroupID) {
+                       throw new IllegalLinkException();
+               }
+               $this->packageID = $this->template->packageID;
+       }
+       
+       /**
+        * @see wcf\acp\form\TemplateAddForm::validateName()
+        */
+       protected function validateName() {
+               if ($this->tplName != $this->template->templateName) {
+                       parent::validateName();
+               }
+       }
+       
+       /**
+        * @see wcf\acp\form\TemplateAddForm::validateName()
+        */
+       protected function validateGroup() {
+               if ($this->templateGroupID != $this->template->templateGroupID) {
+                       parent::validateName();
+               }
+       }
+       
+       /**
+        * @see wcf\form\IForm::save()
+        */
+       public function save() {
+               AbstractForm::save();
+               
+               $this->objectAction = new TemplateAction(array($this->template), 'update', array('data' => array(
+                       'templateName' => $this->tplName,
+                       'templateGroupID' => $this->templateGroupID,
+                       'lastModificationTime' => TIME_NOW
+               ), 'source' => $this->templateSource));
+               $this->objectAction->executeAction();
+               $this->saved();
+               
+               // show success
+               WCF::getTPL()->assign(array(
+                       'success' => true
+               ));
+       }
+       
+       /**
+        * @see wcf\page\IPage::readData()
+        */
+       public function readData() {
+               parent::readData();
+       
+               if (!count($_POST)) {
+                       $this->tplName = $this->template->templateName;
+                       $this->templateSource = $this->template->getSource();
+                       $this->templateGroupID = $this->template->templateGroupID;
+               }
+       }
+       
+       /**
+        * @see wcf\page\IPage::assignVariables()
+        */
+       public function assignVariables() {
+               parent::assignVariables();
+               
+               WCF::getTPL()->assign(array(
+                       'action' => 'edit',
+                       'templateID' => $this->templateID,
+                       'template' => $this->template
+               ));
+       }
+}
index b504e9f8aeefe06b333efee1fd93a1534a0fe970..b9f2404d34a7a35bb32742b18c6fe8fe49e7545d 100644 (file)
@@ -5,6 +5,7 @@ use wcf\data\template\group\TemplateGroupList;
 use wcf\form\AbstractForm;
 use wcf\system\exception\UserInputException;
 use wcf\system\WCF;
+use wcf\util\FileUtil;
 use wcf\util\StringUtil;
 
 /**
@@ -59,7 +60,10 @@ class TemplateGroupAddForm extends AbstractForm {
                parent::readFormParameters();
                
                if (isset($_POST['templateGroupName'])) $this->templateGroupName = StringUtil::trim($_POST['templateGroupName']);
-               if (isset($_POST['templateGroupFolderName'])) $this->templateGroupFolderName = StringUtil::trim($_POST['templateGroupFolderName']);
+               if (!empty($_POST['templateGroupFolderName'])) {
+                       $this->templateGroupFolderName = StringUtil::trim($_POST['templateGroupFolderName']);
+                       if ($this->templateGroupFolderName) $this->templateGroupFolderName = FileUtil::addTrailingSlash($this->templateGroupFolderName);
+               }
                if (isset($_POST['parentTemplateGroupID'])) $this->parentTemplateGroupID = intval($_POST['parentTemplateGroupID']);
        }
        
@@ -81,6 +85,10 @@ class TemplateGroupAddForm extends AbstractForm {
                        throw new UserInputException('templateGroupName');
                }
        
+               if (!preg_match('/^[a-z0-9_\-]+\/$/i', $this->templateGroupName)) {
+                       throw new UserInputException('templateGroupName', 'notValid');
+               }
+               
                $sql = "SELECT  COUNT(*) AS count
                        FROM    wcf".WCF_N."_template_group
                        WHERE   templateGroupName = ?";
index 13600a0f8bbe63f454a33d45f80bb00d9f5da772..e7e06abb94789d8af9c371f9732630e13fb834f7 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\data\template;
+use wcf\data\package\Package;
 use wcf\data\DatabaseObject;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\WCF;
@@ -32,7 +33,8 @@ class Template extends DatabaseObject {
         */
        public function __construct($id, $row = null, DatabaseObject $object = null) {
                if ($id !== null) {
-                       $sql = "SELECT          template.*, template_group.templateGroupFolderName, package.packageDir
+                       $sql = "SELECT          template.*, template_group.templateGroupFolderName,
+                                               package.package, package.packageDir
                                FROM            wcf".WCF_N."_template template
                                LEFT JOIN       wcf".WCF_N."_template_group template_group
                                ON              (template_group.templateGroupID = template.templateGroupID)
@@ -72,6 +74,15 @@ class Template extends DatabaseObject {
                return @file_get_contents($this->getPath());
        }
        
+       /**
+        * Returns the abbreviation of the package name.
+        *
+        * @return      string
+        */
+       public function getPackageAbbreviation() {
+               return Package::getAbbreviation($this->package);
+       }
+       
        /**
         * Searches in templates.
         * 
index 94f5319d2559e56fe041c4d8a031f03442253405..b6e11450b1ebec3dec0c7c7c89dd2201a08482cd 100644 (file)
@@ -32,4 +32,39 @@ class TemplateAction extends AbstractDatabaseObjectAction {
         * @see wcf\data\AbstractDatabaseObjectAction::$permissionsUpdate
         */
        protected $permissionsUpdate = array('admin.template.canManageTemplate');
+       
+       /**
+        * @see wcf\data\AbstractDatabaseObjectAction::create()
+        */
+       public function create() {
+               $template = parent::create();
+               
+               if (isset($this->parameters['source'])) {
+                       $editor = new TemplateEditor($template);
+                       $editor->setSource($this->parameters['source']);
+               }
+               
+               return $template;
+       }
+       
+       /**
+        * @see wcf\data\AbstractDatabaseObjectAction::update()
+        */
+       public function update() {
+               parent::update();
+       
+               foreach ($this->objects as $template) {
+                       // rename file
+                       $templateName = (isset($this->parameters['data']['templateName']) ? $this->parameters['data']['templateName'] : $template->templateName);
+                       $templateGroupID = (isset($this->parameters['data']['templateGroupID']) ? $this->parameters['data']['templateGroupID'] : $template->templateGroupID);
+                       if ($templateName != $template->templateName || $templateGroupID != $template->templateGroupID) {
+                               $template->rename($templateName, $templateGroupID);
+                       }
+                       
+                       // update source
+                       if (isset($this->parameters['source'])) {
+                               $template->setSource($this->parameters['source']);
+                       }
+               }
+       }
 }
index 3c26a65273d176d8cc8434459b02fa11b201f621..2239fe7f65db135eba9020481de1177965567f9a 100644 (file)
@@ -28,8 +28,9 @@ class TemplateEditor extends DatabaseObjectEditor {
        public static function create(array $parameters = array()) {
                // obtain default values
                if (!isset($parameters['packageID'])) $parameters['packageID'] = PACKAGE_ID;
+               if (!isset($parameters['lastModificationTime'])) $parameters['lastModificationTime'] = TIME_NOW;
                
-               parent::create($parameters);
+               return parent::create($parameters);
        }
        
        /**
@@ -58,13 +59,12 @@ class TemplateEditor extends DatabaseObjectEditor {
         * @param       string          $name
         * @param       integer         $templateGroupID
         */
-       protected function rename($name, $templateGroupID = 0) {
+       public function rename($name, $templateGroupID = 0) {
                // get current path
                $currentPath = $this->getPath();
                
                // get new path         
-               $this->data['templateGroupFolderName'] = '';
-               if ($templateGroupID != 0) {
+               if ($templateGroupID != $this->templateGroupID) {
                        // get folder name
                        $sql = "SELECT  templateGroupFolderName
                                FROM    wcf".WCF_N."_template_group
@@ -72,14 +72,14 @@ class TemplateEditor extends DatabaseObjectEditor {
                        $statement = WCF::getDB()->prepareStatement($sql);
                        $statement->execute(array($templateGroupID));
                        $row = $statement->fetchArray();
-                       $this->data['templateGroupFolderName'] = $row['templateGroupFolderName'];
+                       $this->object->data['templateGroupFolderName'] = $row['templateGroupFolderName'];
                }
                
                // delete compiled templates
                $this->deleteCompiledFiles();
                
                // rename
-               $this->data['templateName'] = $name;
+               $this->object->data['templateName'] = $name;
                $newPath = $this->getPath();
                
                // move file
index 96b553bf0f5ad203dc54d6083d7f74ffc92f0d44..0e8a8c40f848281757d2eec2534a2b2f28a05021 100644 (file)
@@ -17,4 +17,14 @@ class TemplateList extends DatabaseObjectList {
         * @see wcf\data\DatabaseObjectList::$className
         */
        public $className = 'wcf\data\template\Template';
+       
+       /**
+        * Creates a new TemplateList object.
+        */
+       public function __construct() {
+               parent::__construct();
+               
+               $this->sqlSelects = 'package.package, package.packageDir';
+               $this->sqlJoins = " LEFT JOIN wcf".WCF_N."_package package ON (package.packageID = template.packageID)";
+       }
 }