From 861edae843f268db07149bc56cedb20ce764a13b Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 5 Nov 2012 02:15:09 +0100 Subject: [PATCH] Added ability to edit application groups --- wcfsetup/install/files/acp/js/WCF.ACP.js | 25 +++- .../acp/templates/applicationGroupAdd.tpl | 21 ++- .../acp/templates/applicationManagement.tpl | 2 +- .../form/ApplicationGroupAddForm.class.php | 37 +++-- .../form/ApplicationGroupEditForm.class.php | 133 ++++++++++++++++++ .../group/ApplicationGroupAction.class.php | 68 +++++++-- 6 files changed, 253 insertions(+), 33 deletions(-) create mode 100644 wcfsetup/install/files/lib/acp/form/ApplicationGroupEditForm.class.php diff --git a/wcfsetup/install/files/acp/js/WCF.ACP.js b/wcfsetup/install/files/acp/js/WCF.ACP.js index f8b33b61d5..e400a9ed77 100644 --- a/wcfsetup/install/files/acp/js/WCF.ACP.js +++ b/wcfsetup/install/files/acp/js/WCF.ACP.js @@ -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) }); } diff --git a/wcfsetup/install/files/acp/templates/applicationGroupAdd.tpl b/wcfsetup/install/files/acp/templates/applicationGroupAdd.tpl index 19276694c2..5554d8c87e 100644 --- a/wcfsetup/install/files/acp/templates/applicationGroupAdd.tpl +++ b/wcfsetup/install/files/acp/templates/applicationGroupAdd.tpl @@ -1,8 +1,20 @@ -{include file='header' pageTitle='wcf.acp.application.group.add'} +{include file='header' pageTitle='wcf.acp.application.group.'|concat:$action} + +
-

{lang}wcf.acp.application.group.add{/lang}

+

{lang}wcf.acp.application.group.{$action}{/lang}

@@ -17,19 +29,20 @@
-
+
{lang}wcf.acp.application.group.data{/lang}
- + {if $errorField == 'groupName'} {if $errorType == 'empty'} diff --git a/wcfsetup/install/files/acp/templates/applicationManagement.tpl b/wcfsetup/install/files/acp/templates/applicationManagement.tpl index a0efef1c43..c9b91a755b 100644 --- a/wcfsetup/install/files/acp/templates/applicationManagement.tpl +++ b/wcfsetup/install/files/acp/templates/applicationManagement.tpl @@ -53,8 +53,8 @@ diff --git a/wcfsetup/install/files/lib/acp/form/ApplicationGroupAddForm.class.php b/wcfsetup/install/files/lib/acp/form/ApplicationGroupAddForm.class.php index 762c53c1cc..aed21ff54d 100644 --- a/wcfsetup/install/files/lib/acp/form/ApplicationGroupAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/ApplicationGroupAddForm.class.php @@ -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 index 0000000000..0baa76619f --- /dev/null +++ b/wcfsetup/install/files/lib/acp/form/ApplicationGroupEditForm.class.php @@ -0,0 +1,133 @@ + + * @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 + )); + } +} diff --git a/wcfsetup/install/files/lib/data/application/group/ApplicationGroupAction.class.php b/wcfsetup/install/files/lib/data/application/group/ApplicationGroupAction.class.php index be1c5aabfb..c57de1a275 100644 --- a/wcfsetup/install/files/lib/data/application/group/ApplicationGroupAction.class.php +++ b/wcfsetup/install/files/lib/data/application/group/ApplicationGroupAction.class.php @@ -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(); } } -- 2.20.1