Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / profile / menu / item / UserProfileMenuItemAction.class.php
index 5c96401a69672607c95cdae9d981a74e0ee4b056..b99dadf2c79ede59ff8636274c6a0837971a2138 100644 (file)
@@ -1,17 +1,21 @@
 <?php
 namespace wcf\data\user\profile\menu\item;
 use wcf\data\AbstractDatabaseObjectAction;
+use wcf\data\ISortableAction;
+use wcf\system\cache\builder\UserProfileMenuCacheBuilder;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\UserInputException;
 use wcf\system\menu\user\profile\UserProfileMenu;
+use wcf\system\WCF;
+use wcf\util\ArrayUtil;
 
 /**
  * Executes user profile menu item-related actions.
  * 
  * @author     Alexander Ebert
- * @copyright  2001-2017 WoltLab GmbH
+ * @copyright  2001-2018 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\Data\User\Profile\Menu\Item
  * 
@@ -19,7 +23,7 @@ use wcf\system\menu\user\profile\UserProfileMenu;
  * @method     UserProfileMenuItemEditor[]     getObjects()
  * @method     UserProfileMenuItemEditor       getSingleObject()
  */
-class UserProfileMenuItemAction extends AbstractDatabaseObjectAction {
+class UserProfileMenuItemAction extends AbstractDatabaseObjectAction implements ISortableAction {
        /**
         * @inheritDoc
         */
@@ -29,7 +33,12 @@ class UserProfileMenuItemAction extends AbstractDatabaseObjectAction {
         * menu item
         * @var UserProfileMenuItem
         */
-       protected $menuItem = null;
+       protected $menuItem;
+       
+       /**
+        * @inheritDoc
+        */
+       protected $requireACP = ['updatePosition'];
        
        /**
         * Validates menu item.
@@ -69,4 +78,59 @@ class UserProfileMenuItemAction extends AbstractDatabaseObjectAction {
                        'template' => $contentManager->getContent($this->parameters['data']['userID'])
                ];
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validateUpdatePosition() {
+               WCF::getSession()->checkPermissions(['admin.user.canManageUserOption']);
+               
+               if (!isset($this->parameters['data']['structure'][0])) {
+                       throw new UserInputException('structure');
+               }
+               
+               $sql = "SELECT  menuItemID
+                       FROM    wcf".WCF_N."_user_profile_menu_item";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute();
+               $menuItemIDs = [];
+               while ($menuItemID = $statement->fetchColumn()) {
+                       $menuItemIDs[$menuItemID] = $menuItemID;
+               }
+               
+               $this->parameters['data']['structure'][0] = ArrayUtil::toIntegerArray($this->parameters['data']['structure'][0]);
+               foreach ($this->parameters['data']['structure'][0] as $menuItemID) {
+                       if (!isset($menuItemIDs[$menuItemID])) {
+                               throw new UserInputException('structure');
+                       }
+                       
+                       unset($menuItemIDs[$menuItemID]);
+               }
+               
+               if (!empty($menuItemIDs)) {
+                       throw new UserInputException('structure');
+               }
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function updatePosition() {
+               $sql = "UPDATE  wcf".WCF_N."_user_profile_menu_item
+                       SET     showOrder = ?
+                       WHERE   menuItemID = ?";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               
+               WCF::getDB()->beginTransaction();
+               for ($i = 0, $length = count($this->parameters['data']['structure'][0]); $i < $length; $i++) {
+                       $statement->execute([
+                               $i,
+                               $this->parameters['data']['structure'][0][$i]
+                       ]);
+               }
+               WCF::getDB()->commitTransaction();
+               
+               // reset cache
+               UserProfileMenuCacheBuilder::getInstance()->reset();
+       }
 }