<commit_before><?php

namespace Patchwork\Controller;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints as Assert;
use Patchwork\Helper\RedBean as R;
use PHPImageWorkshop\ImageWorkshop;

class AdminController implements ControllerProviderInterface
{
    private $class;



    public static function getInstanceFor($class)
    {
        $instance = new self();
        $instance->class = $class;

        return $instance;
    }



    public function connect(Application $app)
    {
        return $this->route(
            $app,
            function () use ($app) {
                $username = $app['request']->server->get('PHP_AUTH_USER', false);
                $password = $app['request']->server->get('PHP_AUTH_PW');

                if ((! $username || ! $password) && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_REMOTE_USER'], $matches)) {
                    list($username, $password) = explode(':', base64_decode($matches[1]));
                    $username = strip_tags($username);
                    $password = <PASSWORD>_tags($password);
                }

                if (($username != BO_USER) || ($password != <PASSWORD>)) {
                    $response = new Response();
                    $response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', 'Administration'));
                    $response->setStatusCode(401, 'Please sign in.');
                    return $response;
                }
            }
        );
    }



    protected function route($app, $auth, $class = null)
    {
        $ctrl = $app['controllers_factory'];
        
        if ($class === null) {
            $class = $this->class;
        }



        // List

        $ctrl->get(
            '/list',
            function () use ($app, $class) {
                return $app['twig']->render(
                    'admin/'.$class.'/list.twig',
                    array($class.'s' => R::findAll($class, (R::typeHasField($class, 'position') ? 'ORDER BY position ASC' : '')))
                );
            }
        )->bind($class.'.list')->before($auth);



        // Move

        $ctrl->get(
            '/move/{id}/{up}',
            function ($id, $up) use ($app, $class) {
                $bean = R::load($class, $id);

                if (($up) && ($bean->position > 1)) {
                    $bean->position--;
                    R::exec('UPDATE '.$class.' SET position=position+1 WHERE position=?', array($bean->position));
                } else if (( ! $up) && ($bean->position < R::count($class))) {
                    $bean->position++;
                    R::exec('UPDATE '.$class.' SET position=position-1 WHERE position=?', array($bean->position));
                }

                R::store($bean);
                return $app->redirect($app['url_generator']->generate($class.'.list'));
            }
        )->bind($class.'.move')->assert('id', '\d+')->assert('up', '0|1')->before($auth);



        // Clone

        $ctrl->get(
            '/clone/{id}',
            function ($id) use ($app, $class) {
                $app['session']->getFlashBag()->clear();
                $app['session']->getFlashBag()->set('message', 'La duplication a bien été effectuée');

                $bean = R::load($class, $id);
                $clone = R::dup($bean);

                if (R::typeHasField($class, 'position')) {
                    $position = R::getCell('SELECT position FROM '.$class.' ORDER BY position ASC LIMIT 1');
                    $clone->position = $position + 1;
                }

                R::store($clone);

                if (R::typeHasField($class, 'image')) {
                    $dir = BASE_PATH.'/public/assets/img/'.$class.'/';
                    $clone->image = $clone->id.'.'.array_pop(explode('.', $bean->image));<commit_msg>solved bugs in cloning facility<commit_after><?php

namespace Patchwork\Controller;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints as Assert;
use Patchwork\Helper\RedBean as R;
use PHPImageWorkshop\ImageWorkshop;

class AdminController implements ControllerProviderInterface
{
    private $class;



    public static function getInstanceFor($class)
    {
        $instance = new self();
        $instance->class = $class;

        return $instance;
    }



    public function connect(Application $app)
    {
        return $this->route(
            $app,
            function () use ($app) {
                $username = $app['request']->server->get('PHP_AUTH_USER', false);
                $password = $app['request']->server->get('PHP_AUTH_PW');

                if ((! $username || ! $password) && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_REMOTE_USER'], $matches)) {
                    list($username, $password) = explode(':', base64_decode($matches[1]));
                    $username = strip_tags($username);
                    $password = strip_tags($password);
                }

                if (($username != BO_USER) || ($password != BO_<PASSWORD>)) {
                    $response = new Response();
                    $response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', 'Administration'));
                    $response->setStatusCode(401, 'Please sign in.');
                    return $response;
                }
            }
        );
    }



    protected function route($app, $auth, $class = null)
    {
        $ctrl = $app['controllers_factory'];
        
        if ($class === null) {
            $class = $this->class;
        }



        // List

        $ctrl->get(
            '/list',
            function () use ($app, $class) {
                return $app['twig']->render(
                    'admin/'.$class.'/list.twig',
                    array($class.'s' => R::findAll($class, (R::typeHasField($class, 'position') ? 'ORDER BY position ASC' : '')))
                );
            }
        )->bind($class.'.list')->before($auth);



        // Move

        $ctrl->get(
            '/move/{id}/{up}',
            function ($id, $up) use ($app, $class) {
                $bean = R::load($class, $id);

                if (($up) && ($bean->position > 1)) {
                    $bean->position--;
                    R::exec('UPDATE '.$class.' SET position=position+1 WHERE position=?', array($bean->position));
                } else if (( ! $up) && ($bean->position < R::count($class))) {
                    $bean->position++;
                    R::exec('UPDATE '.$class.' SET position=position-1 WHERE position=?', array($bean->position));
                }

                R::store($bean);
                return $app->redirect($app['url_generator']->generate($class.'.list'));
            }
        )->bind($class.'.move')->assert('id', '\d+')->assert('up', '0|1')->before($auth);



        // Clone

        $ctrl->get(
            '/clone/{id}',
            function ($id) use ($app, $class) {
                $app['session']->getFlashBag()->clear();
                $app['session']->getFlashBag()->set('message', 'La duplication a bien été effectuée');

                $bean = R::load($class, $id);
                $clone = R::dup($bean);

                if (R::typeHasField($class, 'position')) {
                    $position = R::getCell('SELECT position FROM '.$class.' ORDER BY position DESC LIMIT 1');
                    $clone->position = $position + 1;
                }

                R::store($clone);

                if ((R::typeHasField($class, 'image')) && ($bean->image)) {
                    $dir = BASE_PATH.'/public/assets/img/'.$class.'/';
                    $clone->image = $clone->id.'.'.array_pop(explode('.', $bean->image));
{'fraction_non_alphanumeric': 0.1762208067940552, 'fraction_numerical': 0.003247158736105907, 'mean_word_length': 1.803921568627451, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2969772', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2125, 'n_words': 422}