<commit_before><?php
namespace MapasCulturais\Controllers;

use \MapasCulturais\App;

/**
 * User Panel Controller
 *
 * By default this controller is registered with the id 'panel'.
 *
 */
class Panel extends \MapasCulturais\Controller {

    /**
     * Render the user panel.
     *
     * This method requires authentication and renders the template 'panel/index'
     *
     * <code>
     * // creates the url to this action
     * $url = $app->createUrl('panel');
     * </code>
     *
     */
    function GET_index(){
        $this->requireAuthentication();

        $app = App::i();

        $count = new \stdClass();
        $count->agents = 0;
        $count->spaces = 0;
        $count->events = 0;
        $count->projects = 0;

        foreach($count as $entity=>$c){
            $n = 0;
            foreach($app->user->$entity as $e) {
                if($e->status >= 0){
                    $n++;
                }
            }
            $count->$entity = str_pad($n,2,'0', STR_PAD_LEFT);
        }

        $this->render('index', ['count'=>$count]);
    }

    protected function _getUser(){
        $app = App::i();
        $user = null;
        if($app->user->is('admin') && key_exists('userId', $this->data)){
            $user = $app->repo('User')->find($this->data['userId']);


        }elseif($app->user->is('admin') && key_exists('agentId', $this->data)){
            $agent = $app->repo('Agent')->find($this->data['agentId']);
            $user = $agent->user;
        }
        if(!$user)
            $user = $app->user;

        return $user;
    }

    function GET_requireAuth(){
        $this->requireAuthentication();
        $this->render('require-authentication');
    }

    /**
     * Render the agent list of the user panel.
     *
     * This method requires authentication and renders the template 'panel/agents'
     *
     * <code><commit_msg>Panel counts using better queries<commit_after><?php
namespace MapasCulturais\Controllers;

use \MapasCulturais\App;

/**
 * User Panel Controller
 *
 * By default this controller is registered with the id 'panel'.
 *
 */
class Panel extends \MapasCulturais\Controller {

    /**
     * Render the user panel.
     *
     * This method requires authentication and renders the template 'panel/index'
     *
     * <code>
     * // creates the url to this action
     * $url = $app->createUrl('panel');
     * </code>
     *
     */
    function GET_index(){
        $this->requireAuthentication();

        $app = App::i();

        $count = new \stdClass();
        $count->spaces = $this->countEntity('Space');
        $count->events = $this->countEntity('Event');
        $count->projects = $this->countEntity('Project');

        $count->agents = $app->em->createQuery("SELECT COUNT(a.id) FROM \MapasCulturais\Entities\Agent a JOIN a.user u
            WHERE u = :user AND a.status >= 0")->setParameter('user', $app->user)->getSingleScalarResult();
        $count->agents = str_pad($count->agents, 2, '0', STR_PAD_LEFT);

        $this->render('index', ['count'=>$count]);
    }

    protected function countEntity($entityName){
        $app = App::i();
        $entityClass = '\\MapasCulturais\\Entities\\' . $entityName;
        $dql = "SELECT COUNT(e.id) FROM $entityClass e JOIN e.owner o WHERE o.user = :user AND e.status >= 0";
        $query = $app->em->createQuery($dql);
        $query->setParameter('user', $app->user);
        $count = $query->getSingleScalarResult();
        $padded = str_pad($count, 2, '0', STR_PAD_LEFT);
        return $padded;
    }

    protected function _getUser(){
        $app = App::i();
        $user = null;
        if($app->user->is('admin') && key_exists('userId', $this->data)){
            $user = $app->repo('User')->find($this->data['userId']);


        }elseif($app->user->is('admin') && key_exists('agentId', $this->data)){
            $agent = $app->repo('Agent')->find($this->data['agentId']);
            $user = $agent->user;
        }
        if(!$user)
            $user = $app->user;

        return $user;
    }

    function GET_requireAuth(){
        $this->requireAuthentication();
        $this->render('require-authentication');
    }

    /**
     * Render the agent list of the user panel.
     *
     * This method requires authentication and renders the template 'panel/agents'
     *
     * <code>
{'fraction_non_alphanumeric': 0.1721763085399449, 'fraction_numerical': 0.0032139577594123047, 'mean_word_length': 2.234595397178916, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 80, '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': '138855', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1297, 'n_words': 290}