From: Marcel Werk Date: Sat, 16 Apr 2016 13:37:22 +0000 (+0200) Subject: Improved box to page management X-Git-Tag: 3.0.0_Beta_1~1877 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=59011cd8900941e30f99429969aff9a83cbfedfa;p=GitHub%2FWoltLab%2FWCF.git Improved box to page management --- diff --git a/wcfsetup/install/files/acp/templates/pageAdd.tpl b/wcfsetup/install/files/acp/templates/pageAdd.tpl index 37ff9dc5b6..4d1b85d80f 100644 --- a/wcfsetup/install/files/acp/templates/pageAdd.tpl +++ b/wcfsetup/install/files/acp/templates/pageAdd.tpl @@ -175,6 +175,26 @@ + +
+
+ + {if $errorField == 'boxIDs'} + + {if $errorType == 'empty'} + {lang}wcf.global.form.error.empty{/lang} + {else} + {lang}wcf.acp.page.boxIDs.error.{@$errorType}{/lang} + {/if} + + {/if} +
+ + {event name='dataFields'} diff --git a/wcfsetup/install/files/lib/acp/form/PageAddForm.class.php b/wcfsetup/install/files/lib/acp/form/PageAddForm.class.php index 16a5b43e63..36072c5da0 100644 --- a/wcfsetup/install/files/lib/acp/form/PageAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/PageAddForm.class.php @@ -2,6 +2,8 @@ namespace wcf\acp\form; use wcf\data\application\Application; use wcf\data\application\ApplicationList; +use wcf\data\box\Box; +use wcf\data\box\BoxList; use wcf\data\page\Page; use wcf\data\page\PageAction; use wcf\data\page\PageEditor; @@ -84,6 +86,12 @@ class PageAddForm extends AbstractForm { */ public $availableApplications = []; + /** + * list of available boxes + * @var Box[] + */ + public $availableBoxes = []; + /** * page custom URL * @var string[] @@ -120,6 +128,12 @@ class PageAddForm extends AbstractForm { */ public $metaKeywords = []; + /** + * list of box ids + * @var integer[] + */ + public $boxIDs = []; + /** * @inheritDoc */ @@ -132,6 +146,12 @@ class PageAddForm extends AbstractForm { $applicationList = new ApplicationList(); $applicationList->readObjects(); $this->availableApplications = $applicationList->getObjects(); + + // get boxes + $boxList = new BoxList(); + $boxList->sqlOrderBy = 'box.name'; + $boxList->readObjects(); + $this->availableBoxes = $boxList->getObjects(); } /** @@ -153,6 +173,7 @@ class PageAddForm extends AbstractForm { if (isset($_POST['content']) && is_array($_POST['content'])) $this->content = ArrayUtil::trim($_POST['content']); if (isset($_POST['metaDescription']) && is_array($_POST['metaDescription'])) $this->metaDescription = ArrayUtil::trim($_POST['metaDescription']); if (isset($_POST['metaKeywords']) && is_array($_POST['metaKeywords'])) $this->metaKeywords = ArrayUtil::trim($_POST['metaKeywords']); + if (isset($_POST['boxIDs']) && is_array($_POST['boxIDs'])) $this->boxIDs = ArrayUtil::toIntegerArray($_POST['boxIDs']); } /** @@ -172,6 +193,8 @@ class PageAddForm extends AbstractForm { $this->validateController(); $this->validateCustomUrl(); + + $this->validateBoxIDs(); } /** @@ -242,12 +265,53 @@ class PageAddForm extends AbstractForm { } } + /** + * Validates box ids. + */ + protected function validateBoxIDs() { + foreach ($this->boxIDs as $boxID) { + if (!isset($this->availableBoxes[$boxID])) { + throw new UserInputException('boxIDs'); + } + } + } + + /** + * Prepares box to page assignments + * + * @return mixed[] + */ + protected function getBoxToPage() { + $boxToPage = []; + foreach ($this->availableBoxes as $box) { + if ($box->visibleEverywhere) { + if (!in_array($box->boxID, $this->boxIDs)) { + $boxToPage[] = [ + 'boxID' => $box->boxID, + 'visible' => 0 + ]; + } + } + else { + if (in_array($box->boxID, $this->boxIDs)) { + $boxToPage[] = [ + 'boxID' => $box->boxID, + 'visible' => 1 + ]; + } + } + } + + return $boxToPage; + } + /** * @inheritDoc */ public function save() { parent::save(); + // prepare page content $content = []; if ($this->isMultilingual) { foreach (LanguageFactory::getInstance()->getLanguages() as $language) { @@ -281,7 +345,7 @@ class PageAddForm extends AbstractForm { 'isMultilingual' => $this->isMultilingual, 'identifier' => '', 'controller' => $this->controller - ]), 'content' => $content]); + ]), 'content' => $content, 'boxToPage' => $this->getBoxToPage()]); /** @var Page $page */ $page = $this->objectAction->executeAction()['returnValues']; @@ -310,6 +374,20 @@ class PageAddForm extends AbstractForm { $this->customURL = $this->title = $this->content = $this->metaDescription = $this->metaKeywords = []; } + /** + * @inheritDoc + */ + public function readData() { + parent::readData(); + + // set default values + if (empty($_POST)) { + foreach ($this->availableBoxes as $box) { + if ($box->visibleEverywhere) $this->boxIDs[] = $box->boxID; + } + } + } + /** * @inheritDoc */ @@ -333,9 +411,11 @@ class PageAddForm extends AbstractForm { 'content' => $this->content, 'metaDescription' => $this->metaDescription, 'metaKeywords' => $this->metaKeywords, + 'boxIDs' => $this->boxIDs, 'availableApplications' => $this->availableApplications, 'availableLanguages' => LanguageFactory::getInstance()->getLanguages(), 'availablePageTypes' => $availablePageTypes, + 'availableBoxes' => $this->availableBoxes, 'pageNodeList' => (new PageNodeTree())->getNodeList() ]); } diff --git a/wcfsetup/install/files/lib/acp/form/PageEditForm.class.php b/wcfsetup/install/files/lib/acp/form/PageEditForm.class.php index b77c24768b..78858b5916 100644 --- a/wcfsetup/install/files/lib/acp/form/PageEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/PageEditForm.class.php @@ -120,7 +120,7 @@ class PageEditForm extends PageAddForm { ); } - $this->objectAction = new PageAction(array($this->page), 'update', array('data' => array_merge($this->additionalFields, $data), 'content' => $content)); + $this->objectAction = new PageAction(array($this->page), 'update', array('data' => array_merge($this->additionalFields, $data), 'content' => $content, 'boxToPage' => $this->getBoxToPage())); $this->objectAction->executeAction(); } @@ -158,6 +158,20 @@ class PageEditForm extends PageAddForm { $this->metaKeywords[$languageID] = $content['metaKeywords']; $this->customURL[$languageID] = $content['customURL']; } + + $this->boxIDs = []; + foreach ($this->availableBoxes as $box) { + if ($box->visibleEverywhere) { + if (!in_array($box->boxID, $this->page->getBoxIDs())) { + $this->boxIDs[] = $box->boxID; + } + } + else { + if (in_array($box->boxID, $this->page->getBoxIDs())) { + $this->boxIDs[] = $box->boxID; + } + } + } } } diff --git a/wcfsetup/install/files/lib/data/page/Page.class.php b/wcfsetup/install/files/lib/data/page/Page.class.php index 739f3f0f4d..92634afc7d 100644 --- a/wcfsetup/install/files/lib/data/page/Page.class.php +++ b/wcfsetup/install/files/lib/data/page/Page.class.php @@ -62,6 +62,12 @@ class Page extends DatabaseObject { */ protected $pageHandler; + /** + * box to page assignments + * @var integer[] + */ + protected $boxIDs; + /** * Returns true if the active user can delete this page. * @@ -243,6 +249,27 @@ class Page extends DatabaseObject { return $this->name; } + /** + * Returns box to page assignments. + * + * @return integer[] + */ + public function getBoxIDs() { + if ($this->boxIDs === null) { + $this->boxIDs = []; + $sql = "SELECT boxID + FROM wcf" . WCF_N . "_box_to_page + WHERE pageID = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([$this->pageID]); + while ($row = $statement->fetchArray()) { + $this->boxIDs[] = $row['boxID']; + } + } + + return $this->boxIDs; + } + /** * Returns the page with the given identifier. * diff --git a/wcfsetup/install/files/lib/data/page/PageAction.class.php b/wcfsetup/install/files/lib/data/page/PageAction.class.php index 7a4ff9b486..9091a0d1eb 100644 --- a/wcfsetup/install/files/lib/data/page/PageAction.class.php +++ b/wcfsetup/install/files/lib/data/page/PageAction.class.php @@ -76,6 +76,22 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction, } } + // save box to page assignments + if (!empty($this->parameters['boxToPage'])) { + $sql = "INSERT INTO wcf".WCF_N."_box_to_page + (boxID, pageID, visible) + VALUES (?, ?, ?)"; + $statement = WCF::getDB()->prepareStatement($sql); + + foreach ($this->parameters['boxToPage'] as $boxData) { + $statement->execute([ + $boxData['boxID'], + $page->pageID, + $boxData['visible'] + ]); + } + } + return $page; } @@ -112,6 +128,30 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction, } } } + + // save box to page assignments + if (!empty($this->parameters['boxToPage'])) { + $sql = "DELETE FROM wcf".WCF_N."_box_to_page + WHERE pageID = ?"; + $deleteStatement = WCF::getDB()->prepareStatement($sql); + + $sql = "INSERT INTO wcf".WCF_N."_box_to_page + (boxID, pageID, visible) + VALUES (?, ?, ?)"; + $insertStatement = WCF::getDB()->prepareStatement($sql); + + foreach ($this->objects as $page) { + $deleteStatement->execute([$page->pageID]); + + foreach ($this->parameters['boxToPage'] as $boxData) { + $insertStatement->execute([ + $boxData['boxID'], + $page->pageID, + $boxData['visible'] + ]); + } + } + } } /**