Added ability to edit application groups
authorAlexander Ebert <ebert@woltlab.com>
Mon, 5 Nov 2012 01:15:09 +0000 (02:15 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 5 Nov 2012 01:15:09 +0000 (02:15 +0100)
wcfsetup/install/files/acp/js/WCF.ACP.js
wcfsetup/install/files/acp/templates/applicationGroupAdd.tpl
wcfsetup/install/files/acp/templates/applicationManagement.tpl
wcfsetup/install/files/lib/acp/form/ApplicationGroupAddForm.class.php
wcfsetup/install/files/lib/acp/form/ApplicationGroupEditForm.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/data/application/group/ApplicationGroupAction.class.php

index f8b33b61d562d149bc0db6ea4134046afe0307b0..e400a9ed7768f3f0ff12a810d34b392ae1b167ea 100644 (file)
@@ -23,12 +23,24 @@ WCF.ACP.Application.Group = { };
 
 /**
  * Provides the ability to remove application groups.
+ * 
+ * @param      string          redirectURL
  */
 WCF.ACP.Application.Group.Delete = Class.extend({
+       /**
+        * redirect URL
+        * @var string
+        */
+       _redirectURL: '',
+       
        /**
         * Initializes the WCF.ACP.Application.Group.Delete class.
+        * 
+        * @param       string          redirectURL
         */
-       init: function() {
+       init: function(redirectURL) {
+               this._redirectURL = redirectURL || '';
+               
                $('.jsDeleteApplicationGroup').click($.proxy(this._click, this));
        },
        
@@ -62,9 +74,14 @@ WCF.ACP.Application.Group.Delete = Class.extend({
                        },
                        success: $.proxy(function(data, textStatus, jqXHR) {
                                var $notification = new WCF.System.Notification(WCF.Language.get('wcf.acp.application.group.delete.success'));
-                               $notification.show(function() {
-                                       window.location.reload();
-                               });
+                               $notification.show($.proxy(function() {
+                                       if (this._redirectURL) {
+                                               window.location = this._redirectURL;
+                                       }
+                                       else {
+                                               window.location.reload();
+                                       }
+                               }, this));
                        }, this)
                });
        }
index 19276694c2886a01261110907089d313fa4c6bbb..5554d8c87eba48982807f945b43c3a826dfb33af 100644 (file)
@@ -1,8 +1,20 @@
-{include file='header' pageTitle='wcf.acp.application.group.add'}
+{include file='header' pageTitle='wcf.acp.application.group.'|concat:$action}
+
+<script type="text/javascript">
+       //<![CDATA[
+       $(function() {
+               new WCF.ACP.Application.Group.Delete('{link controller='ApplicationManagement'}{/link}');
+               
+               WCF.Language.addObject({
+                       'wcf.acp.application.group.delete.success': '{lang}wcf.acp.application.group.delete.success{/lang}'
+               });
+       });
+       //]]>
+</script>
 
 <header class="boxHeadline">
        <hgroup>
-               <h1>{lang}wcf.acp.application.group.add{/lang}</h1>
+               <h1>{lang}wcf.acp.application.group.{$action}{/lang}</h1>
        </hgroup>
 </header>
 
 <div class="contentNavigation">
        <nav>
                <ul>
+                       {if $action == 'edit'}<li><a class="button jsDeleteApplicationGroup" data-confirm-message="{lang}wcf.acp.application.group.delete.confirmMessage{/lang}" data-group-id="{@$applicationGroup->groupID}"><img src="{@RELATIVE_WCF_DIR}icon/delete.svg" class="icon24" /> <span>{lang}wcf.acp.application.group.delete{/lang}</span></a></li>{/if}
                        <li><a href="{link controller='ApplicationManagement'}{/link}" class="button"><img src="{@RELATIVE_WCF_DIR}icon/list.svg" alt="" /> <span>{lang}wcf.acp.application.management{/lang}</span></a></li>
                </ul>
        </nav>
 </div>
 
 <div class="container containerPadding marginTop">
-       <form method="post" action="{link controller='ApplicationGroupAdd'}{/link}">
+       <form method="post" action="{if $action == 'add'}{link controller='ApplicationGroupAdd'}{/link}{else}{link controller='ApplicationGroupEdit' id=$applicationGroup->groupID}{/link}{/if}">
                <fieldset>
                        <legend>{lang}wcf.acp.application.group.data{/lang}</legend>
                        <dl{if $errorField == 'groupName'} class="formError"{/if}>
                                <dt><label for="groupName">{lang}wcf.acp.application.group.groupName{/lang}</label></dt>
                                <dd>
-                                       <input type="text" name="groupName" id="groupName" value="{$groupName}" class="long" />
+                                       <input type="text" name="groupName" id="groupName" value="{$groupName}" class="long" required="required" />
                                        {if $errorField == 'groupName'}
                                                <small class="innerError">
                                                        {if $errorType == 'empty'}
index a0efef1c43433d63652a10ddee290fd1eed00f2a..c9b91a755be2b7952bde8853bb013d9233698594 100644 (file)
@@ -53,8 +53,8 @@
        <div class="contentNavigation">
                <nav>
                        <ul>
-                               <li><a href="{link controller='ApplicationGroupEdit' id=$applicationGroup->groupID}{/link}" class="button"><img src="{@RELATIVE_WCF_DIR}icon/edit.svg" class="icon24" /> <span>{lang}wcf.acp.application.group.edit{/lang}</span></a></li>
                                <li><a class="button jsDeleteApplicationGroup" data-confirm-message="{lang}wcf.acp.application.group.delete.confirmMessage{/lang}" data-group-id="{@$applicationGroup->groupID}"><img src="{@RELATIVE_WCF_DIR}icon/delete.svg" class="icon24" /> <span>{lang}wcf.acp.application.group.delete{/lang}</span></a></li>
+                               <li><a href="{link controller='ApplicationGroupEdit' id=$applicationGroup->groupID}{/link}" class="button"><img src="{@RELATIVE_WCF_DIR}icon/edit.svg" class="icon24" /> <span>{lang}wcf.acp.application.group.edit{/lang}</span></a></li>
                        </ul>
                </nav>
        </div>
index 762c53c1cceff8ef4c3b59275f48a440582ff233..aed21ff54d44e67e6d14e69959ec6f9232177319 100644 (file)
@@ -84,21 +84,7 @@ class ApplicationGroupAddForm extends ACPForm {
                parent::validate();
                
                // validate group name
-               if (empty($this->groupName)) {
-                       throw new UserInputException('groupName');
-               }
-               else {
-                       // check for duplicates
-                       $sql = "SELECT  COUNT(*) AS count
-                               FROM    wcf".WCF_N."_application_group
-                               WHERE   groupName = ?";
-                       $statement = WCF::getDB()->prepareStatement($sql);
-                       $statement->execute(array($this->groupName));
-                       $row = $statement->fetchArray();
-                       if ($row['count']) {
-                               throw new UserInputException('groupName', 'notUnique');
-                       }
-               }
+               $this->validateGroupName();
                
                // validate application package ids
                if (empty($this->applications)) {
@@ -131,6 +117,27 @@ class ApplicationGroupAddForm extends ACPForm {
                }
        }
        
+       /**
+        * Validates group name.
+        */
+       protected function validateGroupName() {
+               if (empty($this->groupName)) {
+                       throw new UserInputException('groupName');
+               }
+               else {
+                       // check for duplicates
+                       $sql = "SELECT  COUNT(*) AS count
+                               FROM    wcf".WCF_N."_application_group
+                               WHERE   groupName = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute(array($this->groupName));
+                       $row = $statement->fetchArray();
+                       if ($row['count']) {
+                               throw new UserInputException('groupName', 'notUnique');
+                       }
+               }
+       }
+       
        /**
         * @see wcf\form\IForm::save()
         */
diff --git a/wcfsetup/install/files/lib/acp/form/ApplicationGroupEditForm.class.php b/wcfsetup/install/files/lib/acp/form/ApplicationGroupEditForm.class.php
new file mode 100644 (file)
index 0000000..0baa766
--- /dev/null
@@ -0,0 +1,133 @@
+<?php
+namespace wcf\acp\form;
+use wcf\data\application\group\ApplicationGroup;
+use wcf\data\application\group\ApplicationGroupAction;
+use wcf\data\application\ViewableApplicationList;
+use wcf\system\exception\IllegalLinkException;
+use wcf\system\exception\UserInputException;
+use wcf\system\WCF;
+
+/**
+ * Shows the application group edit form.
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2012 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 ApplicationGroupEditForm extends ApplicationGroupAddForm {
+       /**
+        * application group object
+        * @var wcf\data\application\group\ApplicationGroup
+        */
+       public $applicationGroup = null;
+       
+       /**
+        * groupd id
+        * @var integer
+        */
+       public $groupID = 0;
+       
+       /**
+        * @see wcf\page\IPage::readParameters()
+        */
+       public function readParameters() {
+               if (isset($_REQUEST['id'])) $this->groupID = intval($_REQUEST['id']);
+               $this->applicationGroup = new ApplicationGroup($this->groupID);
+               if (!$this->applicationGroup->groupID) {
+                       throw new IllegalLinkException();
+               }
+               
+               parent::readParameters();
+       }
+       
+       /**
+        * Reads the list of available applications.
+        */
+       protected function readAvailableApplications() {
+               $applicationList = new ViewableApplicationList();
+               $applicationList->getConditionBuilder()->add("(application.groupID = ? OR application.groupID IS NULL)", array($this->applicationGroup->groupID));
+               $applicationList->sqlLimit = 0;
+               $applicationList->readObjects();
+               
+               $this->availableApplications = $applicationList->getObjects();
+               foreach ($this->availableApplications as $application) {
+                       if ($application->groupID == $this->applicationGroup->groupID) {
+                               $this->applications[] = $application->packageID;
+                       }
+               }
+       }
+       
+       /**
+        * @see wcf\acp\form\ApplicationGroupAddForm::validateGroupName()
+        */
+       protected function validateGroupName() {
+               if (empty($this->groupName)) {
+                       throw new UserInputException('groupName');
+               }
+               else {
+                       // check for duplicates
+                       $sql = "SELECT  COUNT(*) AS count
+                               FROM    wcf".WCF_N."_application_group
+                               WHERE   groupName = ?
+                                       AND groupID <> ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute(array(
+                               $this->groupName,
+                               $this->applicationGroup->groupID
+                       ));
+                       $row = $statement->fetchArray();
+                       if ($row['count']) {
+                               throw new UserInputException('groupName', 'notUnique');
+                       }
+               }
+       }
+       
+       /**
+        * @see wcf\page\IPage::readData()
+        */
+       public function readData() {
+               parent::readData();
+               
+               if (empty($_POST)) {
+                       $this->groupName = $this->applicationGroup->groupName;
+               }
+       }
+       
+       /**
+        * @see wcf\form\IForm::save()
+        */
+       public function save() {
+               ACPForm::save();
+               
+               // save group
+               $this->objectAction = new ApplicationGroupAction(array($this->applicationGroup), 'update', array(
+                       'applications' => $this->applications,
+                       'data' => array(
+                               'groupName' => $this->groupName
+                       )
+               ));
+               $this->objectAction->executeAction();
+               $this->saved();
+               
+               // show success.
+               WCF::getTPL()->assign(array(
+                       'success' => true
+               ));
+       }
+       
+       /**
+        * @see wcf\page\IPage::assignVariables()
+        */
+       public function assignVariables() {
+               parent::assignVariables();
+               
+               WCF::getTPL()->assign(array(
+                       'action' => 'edit',
+                       'applicationGroup' => $this->applicationGroup,
+                       'groupID' => $this->groupID
+               ));
+       }
+}
index be1c5aabfbdc3ab7de9c365d36529b705bc27cee..c57de1a275e12ff93c9d483f342fa970762561dd 100644 (file)
@@ -42,30 +42,80 @@ class ApplicationGroupAction extends AbstractDatabaseObjectAction {
        }
        
        /**
-        * @see wcf\data\AbstractDatabaseObjectAction::delete()
+        * @see wcf\data\AbstractDatabaseObjectAction::update()
         */
-       public function delete() {
-               $groupIDs = array();
-               foreach ($this->objects as $applicationGroup) {
-                       $groupIDs[] = $applicationGroup->groupID;
+       public function update() {
+               parent::update();
+               
+               // read list of currently associated applications
+               $applicationGroup = current($this->objects);
+               $applicationList = new ApplicationList();
+               $applicationList->getConditionBuilder()->add("application.groupID = ?", array($applicationGroup->groupID));
+               $applicationList->sqlLimit = 0;
+               $applicationList->readObjects();
+               
+               $updateApplications = $removeApplications = array();
+               foreach ($applicationList as $application) {
+                       $index = array_search($application->packageID, $this->parameters['applications']);
+                       if ($index === false) {
+                               $removeApplications[] = $application;
+                       }
+                       else {
+                               // already existing
+                               $updateApplications[] = $application;
+                               unset($this->parameters['applications'][$index]);
+                       }
+               }
+               
+               if (!empty($this->parameters['applications'])) {
+                       $applicationList = new ApplicationList();
+                       $applicationList->getConditionBuilder()->add("application.packageID IN (?)", $this->parameters['applications']);
+                       $applicationList->sqlLimit = 0;
+                       $applicationList->readObjects();
+                       $updateApplications = array_merge($updateApplications, $applicationList->getObjects());
+               }
+               
+               // rebuild current group
+               $applicationAction = new ApplicationAction($updateApplications, 'group', array('groupID' => $applicationGroup->groupID));
+               $applicationAction->executeAction();
+               
+               // remove applications from group
+               if (!empty($removeApplications)) {
+                       $applicationAction = new ApplicationAction($removeApplications, 'ungroup');
+                       $applicationAction->executeAction();
                }
                
-               // read all applications associated by affected groups
+               $this->clearCache();
+       }
+       
+       /**
+        * @see wcf\data\AbstractDatabaseObjectAction::delete()
+        */
+       public function delete() {
+               // read all associated applications
+               $applicationGroup = current($this->objects);
                $applicationList = new ApplicationList();
-               $applicationList->getConditionBuilder()->add("application.groupID IN (?)", array($groupIDs));
+               $applicationList->getConditionBuilder()->add("application.groupID = ?", array($applicationGroup->groupID));
                $applicationList->sqlLimit = 0;
                $applicationList->readObjects();
                
                $applicationAction = new ApplicationAction($applicationList->getObjects(), 'ungroup');
                $applicationAction->executeAction();
                
+               $this->clearCache();
+               
+               return parent::delete();
+       }
+       
+       /**
+        * Clears WCF cache.
+        */
+       protected function clearCache() {
                // delete language cache and compiled templates
                LanguageFactory::getInstance()->deleteLanguageCache();
                
                // delete WCF cache
                CacheHandler::getInstance()->clear(WCF_DIR.'cache', '*.php');
                CacheHandler::getInstance()->clear(WCF_DIR.'cache/templateListener', '*.php');
-               
-               return parent::delete();
        }
 }