<NME> Records.php
<BEF> <?php

namespace Bolt\Controller\Async;

use Bolt\Storage\ContentRequest\ListingOptions;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Async controller for record manipulation routes.
 *
 * @author Gawain Lynch <gawain.lynch@gmail.com>
 */
class Records extends AsyncBase
{
    protected function addRoutes(ControllerCollection $c)
    {
        $c->method('POST');

        $c->post('/content/{action}/{contenttypeslug}/{id}', 'modify')
            ->bind('contentaction');
    }
}

<MSG> Add record delete & modify routes and methods to the async controller
<DFF> @@ -21,4 +21,105 @@ class Records extends AsyncBase
         $c->post('/content/{action}/{contenttypeslug}/{id}', 'modify')
             ->bind('contentaction');
     }
+
+    /**
+     * Delete a record.
+     *
+     * @param Request $request         The Symfony Request
+     * @param string  $contenttypeslug
+     * @param integer $id
+     *
+     * @return \Symfony\Component\HttpFoundation\RedirectResponse
+     */
+    public function delete(Request $request, $contenttypeslug, $id)
+    {
+        $ids = explode(',', $id);
+        $contenttype = $this->getContentType($contenttypeslug);
+
+        foreach ($ids as $id) {
+            $content = $this->getContent($contenttype['slug'], ['id' => $id, 'status' => '!undefined']);
+            $title = $content->getTitle();
+
+            if (!$this->isAllowed("contenttype:$contenttypeslug:delete:$id")) {
+                $this->flashes()->error(Trans::__('Permission denied', []));
+            } elseif ($this->checkAntiCSRFToken() && $this->storage()->deleteContent($contenttypeslug, $id)) {
+                $this->flashes()->info(Trans::__("Content '%title%' has been deleted.", ['%title%' => $title]));
+            } else {
+                $this->flashes()->info(Trans::__("Content '%title%' could not be deleted.", ['%title%' => $title]));
+            }
+        }
+
+        // Get the referer's query parameters
+        $queryParams = $this->getRefererQueryParameters($request);
+        $queryParams['contenttypeslug'] = $contenttypeslug;
+
+        return $this->redirectToRoute('overview', $queryParams);
+    }
+
+    /**
+     * Perform an action on a Contenttype record.
+     *
+     * @param Request $request         The Symfony Request
+     * @param string  $contenttypeslug The content type slug
+     * @param integer $id              The content ID
+     *
+     * @return \Symfony\Component\HttpFoundation\RedirectResponse
+     */
+    public function modify(Request $request, $action, $contenttypeslug, $id)
+    {
+        if ($action === 'delete') {
+            return $this->delete($request, $contenttypeslug, $id);
+        }
+
+        // This shouldn't happen
+        if (!$this->getContentType($contenttypeslug)) {
+            $this->flashes()->error(Trans::__('Attempt to modify invalid Contenttype.'));
+
+            return $this->redirectToRoute('dashboard');
+        }
+
+        // Map actions to new statuses
+        $actionStatuses = [
+            'held'    => 'held',
+            'publish' => 'published',
+            'draft'   => 'draft',
+        ];
+        // Map actions to requred permission
+        $actionPermissions = [
+            'publish' => 'publish',
+            'held'    => 'depublish',
+            'draft'   => 'depublish',
+        ];
+        // Get the referer's query parameters
+        $queryParams = $this->getRefererQueryParameters($request);
+        $queryParams['contenttypeslug'] = $contenttypeslug;
+
+        if (!isset($actionStatuses[$action])) {
+            $this->flashes()->error(Trans::__('No such action for content.'));
+
+            return $this->redirectToRoute('overview', $queryParams);
+        }
+
+        $newStatus = $actionStatuses[$action];
+        $repo = $this->storage()->getRepository($contenttypeslug);
+        $record = $repo->find($id);
+        $title = $record->getTitle();
+        $canModify = $this->isAllowed("contenttype:$contenttypeslug:{$actionPermissions[$action]}:$id");
+        $canTransition = $this->users()->isContentStatusTransitionAllowed($record->getStatus(), $newStatus, $contenttypeslug, $id);
+
+        if (!$canModify || !$canTransition) {
+            $this->flashes()->error(Trans::__('You do not have the right privileges to %ACTION% that record.', ['%ACTION%' => $actionPermissions[$action]]));
+
+            return $this->redirectToRoute('overview', $queryParams);
+        }
+
+        $record->setStatus($newStatus);
+        if ($repo->save($record)) {
+            $this->flashes()->info(Trans::__("Content '%title%' has been changed to '%newStatus%'", ['%title%' => $title, '%newStatus%' => $newStatus]));
+        } else {
+            $this->flashes()->info(Trans::__("Content '%title%' could not be modified.", ['%title%' => $title]));
+        }
+
+        return $this->redirectToRoute('overview', $queryParams);
+    }
 }

{'commit': 'diff --git a/src/Controller/Async/Records.php b/src/Controller/Async/Records.php\nindex 63a443c1c..966596198 100644\n--- a/src/Controller/Async/Records.php\n+++ b/src/Controller/Async/Records.php\n@@ -21,4 +21,105 @@ class Records extends AsyncBase\n         $c->post(\'/content/{action}/{contenttypeslug}/{id}\', \'modify\')\n             ->bind(\'contentaction\');\n     }\n+\n+    /**\n+     * Delete a record.\n+     *\n+     * @param Request $request         The Symfony Request\n+     * @param string  $contenttypeslug\n+     * @param integer $id\n+     *\n+     * @return \\Symfony\\Component\\HttpFoundation\\RedirectResponse\n+     */\n+    public function delete(Request $request, $contenttypeslug, $id)\n+    {\n+        $ids = explode(\',\', $id);\n+        $contenttype = $this->getContentType($contenttypeslug);\n+\n+        foreach ($ids as $id) {\n+            $content = $this->getContent($contenttype[\'slug\'], [\'id\' => $id, \'status\' => \'!undefined\']);\n+            $title = $content->getTitle();\n+\n+            if (!$this->isAllowed("contenttype:$contenttypeslug:delete:$id")) {\n+                $this->flashes()->error(Trans::__(\'Permission denied\', []));\n+            } elseif ($this->checkAntiCSRFToken() && $this->storage()->deleteContent($contenttypeslug, $id)) {\n+                $this->flashes()->info(Trans::__("Content \'%title%\' has been deleted.", [\'%title%\' => $title]));\n+            } else {\n+                $this->flashes()->info(Trans::__("Content \'%title%\' could not be deleted.", [\'%title%\' => $title]));\n+            }\n+        }\n+\n+        // Get the referer\'s query parameters\n+        $queryParams = $this->getRefererQueryParameters($request);\n+        $queryParams[\'contenttypeslug\'] = $contenttypeslug;\n+\n+        return $this->redirectToRoute(\'overview\', $queryParams);\n+    }\n+\n+    /**\n+     * Perform an action on a Contenttype record.\n+     *\n+     * @param Request $request         The Symfony Request\n+     * @param string  $contenttypeslug The content type slug\n+     * @param integer $id              The content ID\n+     *\n+     * @return \\Symfony\\Component\\HttpFoundation\\RedirectResponse\n+     */\n+    public function modify(Request $request, $action, $contenttypeslug, $id)\n+    {\n+        if ($action === \'delete\') {\n+            return $this->delete($request, $contenttypeslug, $id);\n+        }\n+\n+        // This shouldn\'t happen\n+        if (!$this->getContentType($contenttypeslug)) {\n+            $this->flashes()->error(Trans::__(\'Attempt to modify invalid Contenttype.\'));\n+\n+            return $this->redirectToRoute(\'dashboard\');\n+        }\n+\n+        // Map actions to new statuses\n+        $actionStatuses = [\n+            \'held\'    => \'held\',\n+            \'publish\' => \'published\',\n+            \'draft\'   => \'draft\',\n+        ];\n+        // Map actions to requred permission\n+        $actionPermissions = [\n+            \'publish\' => \'publish\',\n+            \'held\'    => \'depublish\',\n+            \'draft\'   => \'depublish\',\n+        ];\n+        // Get the referer\'s query parameters\n+        $queryParams = $this->getRefererQueryParameters($request);\n+        $queryParams[\'contenttypeslug\'] = $contenttypeslug;\n+\n+        if (!isset($actionStatuses[$action])) {\n+            $this->flashes()->error(Trans::__(\'No such action for content.\'));\n+\n+            return $this->redirectToRoute(\'overview\', $queryParams);\n+        }\n+\n+        $newStatus = $actionStatuses[$action];\n+        $repo = $this->storage()->getRepository($contenttypeslug);\n+        $record = $repo->find($id);\n+        $title = $record->getTitle();\n+        $canModify = $this->isAllowed("contenttype:$contenttypeslug:{$actionPermissions[$action]}:$id");\n+        $canTransition = $this->users()->isContentStatusTransitionAllowed($record->getStatus(), $newStatus, $contenttypeslug, $id);\n+\n+        if (!$canModify || !$canTransition) {\n+            $this->flashes()->error(Trans::__(\'You do not have the right privileges to %ACTION% that record.\', [\'%ACTION%\' => $actionPermissions[$action]]));\n+\n+            return $this->redirectToRoute(\'overview\', $queryParams);\n+        }\n+\n+        $record->setStatus($newStatus);\n+        if ($repo->save($record)) {\n+            $this->flashes()->info(Trans::__("Content \'%title%\' has been changed to \'%newStatus%\'", [\'%title%\' => $title, \'%newStatus%\' => $newStatus]));\n+        } else {\n+            $this->flashes()->info(Trans::__("Content \'%title%\' could not be modified.", [\'%title%\' => $title]));\n+        }\n+\n+        return $this->redirectToRoute(\'overview\', $queryParams);\n+    }\n }\n', 'commit_subject': 'Add record delete & modify routes and methods to the async controller', 'repo_name': 'bolt/bolt', 'committer': {'name': 'Gawain Lynch', 'email': 'f745ec5336bcd096400627fc09f6ec7139408726@gmail.com', 'time_sec': '1444796920', 'tz_offset': '60', 'date': {'seconds': '1444796920'}}, 'commit_tree': 'e8325485c53ae9ea4af05903105dc88c85337a30', 'commit_message': 'Add record delete & modify routes and methods to the async controller', 'language_name': 'PHP', 'license': 'mit', 'hunks': '@@ -21,4 +21,105 @@ class Records extends AsyncBase\n         $c->post(\'/content/{action}/{contenttypeslug}/{id}\', \'modify\')\n             ->bind(\'contentaction\');\n     }\n+\n+    /**\n+     * Delete a record.\n+     *\n+     * @param Request $request         The Symfony Request\n+     * @param string  $contenttypeslug\n+     * @param integer $id\n+     *\n+     * @return \\Symfony\\Component\\HttpFoundation\\RedirectResponse\n+     */\n+    public function delete(Request $request, $contenttypeslug, $id)\n+    {\n+        $ids = explode(\',\', $id);\n+        $contenttype = $this->getContentType($contenttypeslug);\n+\n+        foreach ($ids as $id) {\n+            $content = $this->getContent($contenttype[\'slug\'], [\'id\' => $id, \'status\' => \'!undefined\']);\n+            $title = $content->getTitle();\n+\n+            if (!$this->isAllowed("contenttype:$contenttypeslug:delete:$id")) {\n+                $this->flashes()->error(Trans::__(\'Permission denied\', []));\n+            } elseif ($this->checkAntiCSRFToken() && $this->storage()->deleteContent($contenttypeslug, $id)) {\n+                $this->flashes()->info(Trans::__("Content \'%title%\' has been deleted.", [\'%title%\' => $title]));\n+            } else {\n+                $this->flashes()->info(Trans::__("Content \'%title%\' could not be deleted.", [\'%title%\' => $title]));\n+            }\n+        }\n+\n+        // Get the referer\'s query parameters\n+        $queryParams = $this->getRefererQueryParameters($request);\n+        $queryParams[\'contenttypeslug\'] = $contenttypeslug;\n+\n+        return $this->redirectToRoute(\'overview\', $queryParams);\n+    }\n+\n+    /**\n+     * Perform an action on a Contenttype record.\n+     *\n+     * @param Request $request         The Symfony Request\n+     * @param string  $contenttypeslug The content type slug\n+     * @param integer $id              The content ID\n+     *\n+     * @return \\Symfony\\Component\\HttpFoundation\\RedirectResponse\n+     */\n+    public function modify(Request $request, $action, $contenttypeslug, $id)\n+    {\n+        if ($action === \'delete\') {\n+            return $this->delete($request, $contenttypeslug, $id);\n+        }\n+\n+        // This shouldn\'t happen\n+        if (!$this->getContentType($contenttypeslug)) {\n+            $this->flashes()->error(Trans::__(\'Attempt to modify invalid Contenttype.\'));\n+\n+            return $this->redirectToRoute(\'dashboard\');\n+        }\n+\n+        // Map actions to new statuses\n+        $actionStatuses = [\n+            \'held\'    => \'held\',\n+            \'publish\' => \'published\',\n+            \'draft\'   => \'draft\',\n+        ];\n+        // Map actions to requred permission\n+        $actionPermissions = [\n+            \'publish\' => \'publish\',\n+            \'held\'    => \'depublish\',\n+            \'draft\'   => \'depublish\',\n+        ];\n+        // Get the referer\'s query parameters\n+        $queryParams = $this->getRefererQueryParameters($request);\n+        $queryParams[\'contenttypeslug\'] = $contenttypeslug;\n+\n+        if (!isset($actionStatuses[$action])) {\n+            $this->flashes()->error(Trans::__(\'No such action for content.\'));\n+\n+            return $this->redirectToRoute(\'overview\', $queryParams);\n+        }\n+\n+        $newStatus = $actionStatuses[$action];\n+        $repo = $this->storage()->getRepository($contenttypeslug);\n+        $record = $repo->find($id);\n+        $title = $record->getTitle();\n+        $canModify = $this->isAllowed("contenttype:$contenttypeslug:{$actionPermissions[$action]}:$id");\n+        $canTransition = $this->users()->isContentStatusTransitionAllowed($record->getStatus(), $newStatus, $contenttypeslug, $id);\n+\n+        if (!$canModify || !$canTransition) {\n+            $this->flashes()->error(Trans::__(\'You do not have the right privileges to %ACTION% that record.\', [\'%ACTION%\' => $actionPermissions[$action]]));\n+\n+            return $this->redirectToRoute(\'overview\', $queryParams);\n+        }\n+\n+        $record->setStatus($newStatus);\n+        if ($repo->save($record)) {\n+            $this->flashes()->info(Trans::__("Content \'%title%\' has been changed to \'%newStatus%\'", [\'%title%\' => $title, \'%newStatus%\' => $newStatus]));\n+        } else {\n+            $this->flashes()->info(Trans::__("Content \'%title%\' could not be modified.", [\'%title%\' => $title]));\n+        }\n+\n+        return $this->redirectToRoute(\'overview\', $queryParams);\n+    }\n }\n', 'addition_count': 101, 'deletion_count': 0, 'src_file': 'a/src/Controller/Async/Records.php', 'tgt_file': 'b/src/Controller/Async/Records.php', 'file_extension': '.php', 'before_file': "<?php\n\nnamespace Bolt\\Controller\\Async;\n\nuse Bolt\\Storage\\ContentRequest\\ListingOptions;\nuse Silex\\ControllerCollection;\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\n/**\n * Async controller for record manipulation routes.\n *\n * @author Gawain Lynch <gawain.lynch@gmail.com>\n */\nclass Records extends AsyncBase\n{\n    protected function addRoutes(ControllerCollection $c)\n    {\n        $c->method('POST');\n\n        $c->post('/content/{action}/{contenttypeslug}/{id}', 'modify')\n            ->bind('contentaction');\n    }\n}\n", 'message': 'Add record delete & modify routes and methods to the async controller', 'author_name': 'Gawain Lynch', 'lang': 'php'}