public function reorderPages()
    {
        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($page) {
            if ($page['parent_id'] === 'null') {
                $page['parent_id'] = null;
            }
            return $page;
        });

        $pages = $this->Pages->patchEntities($this->Pages->find('all'), $this->request->data, ['associated' => false]);

        /** @var Connection $connection */
        $connection = $this->Pages->connection();
        $connection->begin();
        foreach ($pages as $page) {
            $this->Pages->behaviors()->unload('Tree');
            if (!$this->Pages->save($page)) {
                $connection->rollback();
                break;
            }
        }
        if ($connection->inTransaction()) {
            $connection->commit();
            $status = 'success';
            $flashMessage = __d('wasabi_cms', 'The page positions have been updated.');
        } else {
            $status = 'error';
            $flashMessage = $this->dbErrorMessage;
        }

        $this->set([
            'status' => $status,
            'flashMessage' => $flashMessage,
            '_serialize' => ['status', 'flashMessage']
        ]);

        $this->RequestHandler->renderAs($this, 'json');
    }