src/Controller/MenusController.php
public function reorderItems()
    {
        if (!$this->request->isAll(['ajax', 'post'])) {
            throw new MethodNotAllowedException();
        }

        if (empty($this->request->data)) {
            throw new BadRequestException();
        }

        $this->request->data = Hash::map($this->request->data, '{n}', function($item) {
            if ($item['parent_id'] === 'null') {
                $item['parent_id'] = null;
            }
            return $item;
        });

        // save the new language positions
        $menuItems = $this->MenuItems->patchEntities(
            $this->MenuItems->find('threaded'),
            $this->request->data
        );
        $this->MenuItems->connection()->begin();
        foreach ($menuItems as $menuItem) {
            $this->MenuItems->behaviors()->unload('Tree');
            if (!$this->MenuItems->save($menuItem)) {
                $this->MenuItems->connection()->rollback();
                break;
            }
        }
        if ($this->MenuItems->connection()->inTransaction()) {
            $this->MenuItems->connection()->commit();
            $status = 'success';
            $flashMessage = __d('wasabi_core', 'The menu item positions have been updated.');
        } else {
            $status = 'error';
            $flashMessage = $this->dbErrorMessage;
        }

        $this->set([
            'status' => $status,
            'flashMessage' => $flashMessage,
            '_serialize' => ['status', 'flashMessage']
        ]);
    }
public function reorderItems()
    {
        if (!$this->request->isAll(['ajax', 'post'])) {
            throw new MethodNotAllowedException();
        }

        if (empty($this->request->data)) {
            throw new BadRequestException();
        }

        $this->request->data = Hash::map($this->request->data, '{n}', function($item) {
            if ($item['parent_id'] === 'null') {
                $item['parent_id'] = null;
            }
            return $item;
        });

        // save the new language positions
        $menuItems = $this->MenuItems->patchEntities(
            $this->MenuItems->find('threaded'),
            $this->request->data
        );
        $this->MenuItems->connection()->begin();
        foreach ($menuItems as $menuItem) {
            $this->MenuItems->behaviors()->unload('Tree');
            if (!$this->MenuItems->save($menuItem)) {
                $this->MenuItems->connection()->rollback();
                break;
            }
        }
        if ($this->MenuItems->connection()->inTransaction()) {
            $this->MenuItems->connection()->commit();
            $status = 'success';
            $flashMessage = __d('wasabi_core', 'The menu item positions have been updated.');
        } else {
            $status = 'error';
            $flashMessage = $this->dbErrorMessage;
        }

        $this->set([
            'status' => $status,
            'flashMessage' => $flashMessage,
            '_serialize' => ['status', 'flashMessage']
        ]);
    }
public function reorderItems ( ) { if ( ! $ this -> request -> isAll ( [ 'ajax' , 'post' ] ) ) { throw new MethodNotAllowedException ( ) ; } if ( empty ( $ this -> request -> data ) ) { throw new BadRequestException ( ) ; } $ this -> request -> data = Hash :: map ( $ this -> request -> data , '{n}' , function ( $ item ) { if ( $ item [ 'parent_id' ] === 'null' ) { $ item [ 'parent_id' ] = null ; } return $ item ; } ) ; // save the new language positions $ menuItems = $ this -> MenuItems -> patchEntities ( $ this -> MenuItems -> find ( 'threaded' ) , $ this -> request -> data ) ; $ this -> MenuItems -> connection ( ) -> begin ( ) ; foreach ( $ menuItems as $ menuItem ) { $ this -> MenuItems -> behaviors ( ) -> unload ( 'Tree' ) ; if ( ! $ this -> MenuItems -> save ( $ menuItem ) ) { $ this -> MenuItems -> connection ( ) -> rollback ( ) ; break ; } } if ( $ this -> MenuItems -> connection ( ) -> inTransaction ( ) ) { $ this -> MenuItems -> connection ( ) -> commit ( ) ; $ status = 'success' ; $ flashMessage = __d ( 'wasabi_core' , 'The menu item positions have been updated.' ) ; } else { $ status = 'error' ; $ flashMessage = $ this -> dbErrorMessage ; } $ this -> set ( [ 'status' => $ status , 'flashMessage' => $ flashMessage , '_serialize' => [ 'status' , 'flashMessage' ] ] ) ; }
reorderItems action
AJAX POST
reorderItems action AJAX POST