--- /dev/null
+<?php
+namespace wcf\data\page\menu\item;
+use wcf\data\ProcessibleDatabaseObject;
+use wcf\system\application\ApplicationHandler;
+use wcf\system\menu\page\DefaultPageMenuItemProvider;
+use wcf\system\menu\ITreeMenuItem;
+use wcf\system\request\LinkHandler;
+
+/**
+ * Represents an page menu item.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2011 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package com.woltlab.wcf
+ * @subpackage data.page.menu.item
+ * @category Community Framework
+ */
+class PageMenuItem extends ProcessibleDatabaseObject implements ITreeMenuItem {
+ /**
+ * @see wcf\data\DatabaseObject::$databaseTableName
+ */
+ protected static $databaseTableName = 'page_menu_item';
+
+ /**
+ * @see wcf\data\DatabaseObject::$databaseTableIndexName
+ */
+ protected static $databaseTableIndexName = 'menuItemID';
+
+ /**
+ * @see wcf\data\ProcessibleDatabaseObject::$processorInterface
+ */
+ protected static $processorInterface = 'wcf\system\menu\page\IPageMenuItemProvider';
+
+ /**
+ * @see wcf\data\ProcessibleDatabaseObject::getProcessor()
+ */
+ public function getProcessor() {
+ if (parent::getProcessor() === null) {
+ $this->processor = new DefaultPageMenuItemProvider($this);
+ }
+
+ return $this->processor;
+ }
+
+ /**
+ * @see wcf\system\menu\ITreeMenuItem::getLink()
+ */
+ public function getLink() {
+ $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($this->packageID);
+
+ $parameters = array();
+ if ($abbreviation) {
+ $parameters['application'] = $abbreviation;
+ }
+
+ return LinkHandler::getInstance()->getLink(null, $parameters, $this->menuItemLink);
+ }
+}
--- /dev/null
+<?php
+namespace wcf\data\page\menu\item;
+use wcf\data\DatabaseObjectEditor;
+use wcf\data\IEditableCachedObject;
+use wcf\system\cache\CacheHandler;
+use wcf\system\WCF;
+
+/**
+ * Provides functions to edit page menu items.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2011 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package com.woltlab.wcf
+ * @subpackage data.page.menu.item
+ * @category Community Framework
+ */
+class PageMenuItemEditor extends DatabaseObjectEditor implements IEditableCachedObject {
+ /**
+ * @see wcf\data\DatabaseObjectDecorator::$baseClass
+ */
+ protected static $baseClass = 'wcf\data\page\menu\item\PageMenuItem';
+
+ /**
+ * @see wcf\data\IEditableObject::create()
+ *
+ * @todo Handle language id and create related language item
+ */
+ public static function create(array $parameters = array()) {
+ // calculate show order
+ $parameters['showOrder'] = self::getShowOrder($parameters['showOrder'], $parameters['menuPosition']);
+
+ return parent::create($parameters);
+ }
+
+ /**
+ * @see wcf\data\IEditableObject::update()
+ *
+ * @todo Handle language id and update related language item
+ */
+ public function update(array $parameters = array()) {
+ if (isset($parameters['menuPosition']) && isset($parameters['showOrder'])) {
+ $this->updateShowOrder($parameters['showOrder'], $parameters['menuPosition']);
+ }
+
+ parent::update($parameters);
+ }
+
+ /**
+ * @see wcf\data\IEditableObject::delete()
+ */
+ public function delete() {
+ // update show order
+ $sql = "UPDATE wcf".WCF_N."_page_menu_item
+ SET showOrder = showOrder - 1
+ WHERE showOrder >= ?
+ AND menuPosition = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array(
+ $this->showOrder,
+ $this->menuPosition
+ ));
+
+ parent::delete();
+ }
+
+ /**
+ * Updates the positions of a page menu item directly.
+ *
+ * @param integer $menuItemID
+ * @param string $menuPosition
+ * @param integer $showOrder
+ */
+ public static function setShowOrder($menuItemID, $menuPosition = 'header', $showOrder = 1) {
+ // Update
+ $sql = "UPDATE wcf".WCF_N."_page_menu_item
+ SET showOrder = ?,
+ menuPosition = ?
+ WHERE menuItemID = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array(
+ $showOrder,
+ $menuPosition,
+ $menuItemID
+ ));
+ }
+
+ /**
+ * Updates show order for current menu item.
+ *
+ * @param integer $showOrder
+ * @param string $menuPosition
+ */
+ protected function updateShowOrder($showOrder, $menuPosition) {
+ if ($menuPosition == $this->menuPosition) {
+ if ($this->showOrder != $showOrder) {
+ if ($showOrder < $this->showOrder) {
+ $sql = "UPDATE wcf".WCF_N."_page_menu_item
+ SET showOrder = showOrder + 1
+ WHERE showOrder >= ?
+ AND showOrder < ?
+ AND menuPosition = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array(
+ $showOrder,
+ $this->showOrder,
+ $menuPosition
+ ));
+ }
+ else if ($showOrder > $this->showOrder) {
+ $sql = "UPDATE wcf".WCF_N."_page_menu_item
+ SET showOrder = showOrder - 1
+ WHERE showOrder <= ?
+ AND showOrder > ?
+ AND menuPosition = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array(
+ $showOrder,
+ $this->showOrder,
+ $menuPosition
+ ));
+ }
+ }
+ }
+ else {
+ $sql = "UPDATE wcf".WCF_N."_page_menu_item
+ SET showOrder = showOrder - 1
+ WHERE showOrder >= ?
+ AND menuPosition = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array(
+ $this->showOrder,
+ $this->menuPosition
+ ));
+
+ $sql = "UPDATE wcf".WCF_N."_page_menu_item
+ SET showOrder = showOrder + 1
+ WHERE showOrder >= ?
+ AND menuPosition = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array(
+ $showOrder,
+ $menuPosition
+ ));
+ }
+ }
+
+ /**
+ * Returns show order for a new menu item.
+ *
+ * @param integer $showOrder
+ * @param string $menuPosition
+ * @return integer
+ */
+ protected static function getShowOrder($showOrder, $menuPosition) {
+ if ($showOrder == 0) {
+ // get next number in row
+ $sql = "SELECT MAX(showOrder) AS showOrder
+ FROM wcf".WCF_N."_page_menu_item
+ WHERE menuPosition = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array($menuPosition));
+ $row = $statement->fetchArray();
+ if (!empty($row)) $showOrder = intval($row['showOrder']) + 1;
+ else $showOrder = 1;
+ }
+ else {
+ $sql = "UPDATE wcf".WCF_N."_page_menu_item
+ SET showOrder = showOrder + 1
+ WHERE showOrder >= ?
+ AND menuPosition = ?";
+ $statement = WCF::getDB()->prepareStatement($sql);
+ $statement->execute(array(
+ $showOrder,
+ $menuPosition
+ ));
+ }
+
+ return $showOrder;
+ }
+
+ /**
+ * @see wcf\data\IEditableCachedObject::resetCache()
+ */
+ public static function resetCache() {
+ CacheHandler::getInstance()->clear(WCF_DIR.'cache', 'cache.pageMenu-*.php');
+ }
+}