<commit_before><?php
/*
 * This file is part of the Goteo Package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the README.md
 * and LICENSE files that was distributed with this source code.
 */

namespace Goteo\Controller;

use Goteo\Application\App;
use Goteo\Application\Config;
use Goteo\Application\Exception\ControllerAccessDeniedException;
use Goteo\Application\Exception\ControllerException;
use Goteo\Application\Message;
use Goteo\Application\Session;
use Goteo\Application\View;
use Goteo\Library\Feed;
use Goteo\Library\Text;
use Goteo\Model\Node;
use Goteo\Model\User;
use Goteo\Model\Log;
use Goteo\Controller\Admin\AdminControllerInterface;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;

class AdminController extends \Goteo\Core\Controller {

    private static $subcontrollers = array();
    private static $context_vars = array();
    private static $groups = [
        'consultants' => ['text' => 'admin-consultants', 'icon' => '<i class="fa fa-2x fa-fax"></i>', 'position' => 10],
        'projects' => ['text' => 'admin-projects', 'icon' => '<i class="icon icon-2x icon-projects"></i>', 'position' => 20],
        'main' => ['text' => 'admin-main', 'icon' => '<i class="fa fa-2x fa-home"></i>', 'position' => 100],
        'legacy' => ['text' => 'admin-legacy', 'icon' => '<i class="fa fa-2x fa-folder"></i>', 'position' => 110]
    ];

    public function __construct() {
        // changing to a responsive theme here
        View::setTheme('responsive');
    }

    /**
     * Controller for route /admin
     */
    public function indexAction(Request $request) {
        $user = self::getCurrentUser($request);
        static::createAdminSidebar($user);
        return $this->viewResponse('admin/index');
    }
    /**
     * Controller for any route under /admin/{route}

     */
    public function routingAction($id, $uri = '', Request $request) {

        $user = self::getCurrentUser($request);
        $uri = "/$uri";

        if($module = self::getSubController($id)) {

            // Log this entry
            Log::append(['scope' => 'admin', 'target_type' => 'admin_module', 'target_id' => $id]);

            if(in_array('Goteo\Controller\Admin\AdminControllerInterface', class_implements($module))) {
                static::createAdminSidebar($user, $id, $request->getPathInfo());

                // Add the admin routes
                $module_routes = $routes = $module::getRoutes();

                if( $routes instanceOf Route) {
                    $module_routes = new RouteCollection();
                    $module_routes->add("admin-submodule-$id", $routes);
                }
                elseif( is_array($routes)) {
                    $module_routes = new RouteCollection();
                    foreach($routes as $key => $route) {
                        $module_routes->add("admin-submodule-$id-$key", $route);
                    }
                }

                // Manual matching routes
                if($module_routes instanceOf RouteCollection) {
                    $context = new RequestContext('/');
                    $matcher = new UrlMatcher($module_routes, $context);

                    try {
                        $parameters = $matcher->match($uri);
                        // print_r($parameters);die;
                        // Check permissions for this route
                        if(!$module::isAllowed($user, $uri)) {
                            throw new ControllerAccessDeniedException("User [{$user->id}] has no privileges on URI [$uri] in module [$module]");
                        }

                        // Add vars to the view
                        $this->contextVars([
                            'icon' => $module::getLabel('icon'),
                            'module_id' => $id,
                            'module_label' => $module::getLabel('text')
                        ]);
                        // Forward the subcontroller
                        return $this->forward($parameters['_controller'], $parameters);

                    } catch(ResourceNotFoundException $e) {
                        throw new NotFoundHttpException("Route [$uri] is not defined in module [$module]");
                    }

                } else {
                    throw new ControllerException("Error: [$module::getRoutes()] must return a valid instance of Symfony\Component\Routing\Route (or an array of several Routes) or Symfony\Component\Routing\RouteCollection");
                }
            } else {
                // OLD admin modules
                // Do not create sidebar here
                list($action, $sid, $subaction) = explode('/', $uri);
                return $this->optionAction($id, $action ? $action : 'list', $sid, $subaction, $request);
            }
        }
        throw new NotFoundHttpException("Admin module [$id] not found");
    }

    /**
     * Creates the sidebar in admin
     * @param  User   $user [description]
     * @param  string $zone [description]
     */
    public static function createAdminSidebar (User $user, $module = '', $uri = '') {

        $prefix = '/admin';
        $modules = [];
        $zone = preg_replace('|^/admin|', '', $uri);
        // die("[$uri]");

        foreach (static::$subcontrollers as $id => $class) {

            if(in_array('Goteo\Controller\Admin\AdminControllerInterface', class_implements($class))) {<commit_msg>fix admin legacy compatibility<commit_after><?php
/*
 * This file is part of the Goteo Package.
 *
 * (c) Platoniq y Fundación Goteo <<EMAIL>>
 *
 * For the full copyright and license information, please view the README.md
 * and LICENSE files that was distributed with this source code.
 */

namespace Goteo\Controller;

use Goteo\Application\App;
use Goteo\Application\Config;
use Goteo\Application\Exception\ControllerAccessDeniedException;
use Goteo\Application\Exception\ControllerException;
use Goteo\Application\Message;
use Goteo\Application\Session;
use Goteo\Application\View;
use Goteo\Library\Feed;
use Goteo\Library\Text;
use Goteo\Model\Node;
use Goteo\Model\User;
use Goteo\Model\Log;
use Goteo\Controller\Admin\AdminControllerInterface;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;

class AdminController extends \Goteo\Core\Controller {

    private static $subcontrollers = array();
    private static $context_vars = array();
    private static $groups = [
        'consultants' => ['text' => 'admin-consultants', 'icon' => '<i class="fa fa-2x fa-fax"></i>', 'position' => 10],
        'projects' => ['text' => 'admin-projects', 'icon' => '<i class="icon icon-2x icon-projects"></i>', 'position' => 20],
        'main' => ['text' => 'admin-main', 'icon' => '<i class="fa fa-2x fa-home"></i>', 'position' => 100],
        'legacy' => ['text' => 'admin-legacy', 'icon' => '<i class="fa fa-2x fa-folder"></i>', 'position' => 110]
    ];

    public function __construct() {
        // changing to a responsive theme here
        View::setTheme('responsive');
    }

    /**
     * Controller for route /admin
     */
    public function indexAction(Request $request) {
        $user = self::getCurrentUser($request);
        static::createAdminSidebar($user);
        return $this->viewResponse('admin/index');
    }
    /**
     * Controller for any route under /admin/{route}

     */
    public function routingAction($id, $uri = '', Request $request) {

        $user = self::getCurrentUser($request);
        $uri = "/$uri";

        if($module = self::getSubController($id)) {

            // Log this entry
            Log::append(['scope' => 'admin', 'target_type' => 'admin_module', 'target_id' => $id]);

            if(in_array('Goteo\Controller\Admin\AdminControllerInterface', class_implements($module))) {
                static::createAdminSidebar($user, $id, $request->getPathInfo());

                // Add the admin routes
                $module_routes = $routes = $module::getRoutes();

                if( $routes instanceOf Route) {
                    $module_routes = new RouteCollection();
                    $module_routes->add("admin-submodule-$id", $routes);
                }
                elseif( is_array($routes)) {
                    $module_routes = new RouteCollection();
                    foreach($routes as $key => $route) {
                        $module_routes->add("admin-submodule-$id-$key", $route);
                    }
                }

                // Manual matching routes
                if($module_routes instanceOf RouteCollection) {
                    $context = new RequestContext('/');
                    $matcher = new UrlMatcher($module_routes, $context);

                    try {
                        $parameters = $matcher->match($uri);
                        // print_r($parameters);die;
                        // Check permissions for this route
                        if(!$module::isAllowed($user, $uri)) {
                            throw new ControllerAccessDeniedException("User [{$user->id}] has no privileges on URI [$uri] in module [$module]");
                        }

                        // Add vars to the view
                        $this->contextVars([
                            'icon' => $module::getLabel('icon'),
                            'module_id' => $id,
                            'module_label' => $module::getLabel('text')
                        ]);
                        // Forward the subcontroller
                        return $this->forward($parameters['_controller'], $parameters);

                    } catch(ResourceNotFoundException $e) {
                        throw new NotFoundHttpException("Route [$uri] is not defined in module [$module]");
                    }

                } else {
                    throw new ControllerException("Error: [$module::getRoutes()] must return a valid instance of Symfony\Component\Routing\Route (or an array of several Routes) or Symfony\Component\Routing\RouteCollection");
                }
            } else {
                // OLD admin modules
                // Do not create sidebar here
                list($empty,$action, $sid, $subaction) = explode('/', $uri);

                return $this->optionAction($id, $action ? $action : 'list', $sid, $subaction, $request);
            }
        }
        throw new NotFoundHttpException("Admin module [$id] not found");
    }

    /**
     * Creates the sidebar in admin
     * @param  User   $user [description]
     * @param  string $zone [description]
     */
    public static function createAdminSidebar (User $user, $module = '', $uri = '') {

        $prefix = '/admin';
        $modules = [];
        $zone = preg_replace('|^/admin|', '', $uri);
        // die("[$uri]");

        foreach (static::$subcontrollers as $id => $class) {

            if(in_array('Goteo\Controller\Admin\AdminControllerInterface', class_implements($class))) {
{'fraction_non_alphanumeric': 0.13711543405604593, 'fraction_numerical': 0.0023995200959808036, 'mean_word_length': 2.1660336408030387, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 90, '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': '6600569', 'n_tokens_mistral': 3152, 'n_tokens_neox': 3051, 'n_words': 736}