<commit_before><?php
namespace RogerioLino\Controller;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use RogerioLino\Form\EntityValidator;
use RogerioLino\Util\Arrays;
use Slim\Slim;

/**
 * CrudController
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class CrudController extends SlimController {
        
    protected $name;
    protected $entityName;
    protected $maxResults = 20;
    protected $readonly = false;
    
    public function __construct(Slim $app, $entityName, $title = "", $subtitle = "") {
        parent::__construct($app);
        $this->entityName = $entityName;
        // resolving controller name (class prefix)
        $this->name = strtolower(str_replace('Controller', '', @end(@explode('\\', get_class($this)))));
        
        $this->app()->view()->set('title', $title);
        $this->app()->view()->set('subtitle', $subtitle);
    }
    
    /**
     * @return EntityManager
     */
    public final function em() {
        return $this->app()->em;
    }
    
    public function index() {
        $this->postIndex();
        $page = (int) Arrays::value($_GET, 'page', 0);
        $search = Arrays::value($_GET, 's', '');
        $query = $this->searchQuery("%$search%", $this->maxResults, $page * $this->maxResults);
        $paginator = new Paginator($query, true);
        $total = count($paginator);

        $this->app()->view()->set('controllerName', $this->name);
        
        $this->app()->view()->set('search', $search);
        $this->app()->view()->set('items', $paginator);
        $this->app()->view()->set('total', $total);
        $this->app()->view()->set('page', $page);
        $this->app()->view()->set('maxResults', $this->maxResults);
        $this->app()->view()->set('pages', ceil($total / $this->maxResults));
    }
    
    protected function searchQuery($searchValue, $maxResults, $first = 0) {
        $dql = "SELECT e FROM {$this->entityName} e";
        $query = $this->em()->createQuery($dql)
                    ->setFirstResult($first)
                    ->setMaxResults($maxResults);
        return $query;
    }
    
    public function edit($id = 0) {
        $model = null;
        $id = (int) $id;
        if ($id > 0) {
            $model = $this->em()->find($this->entityName, $id);
        }
        if (!$model) {
            $model = new $this->entityName;
        }
        
        $this->preEdit($model);
        
        if (!$this->readonly) {
            $form = new EntityValidator($model);
            if ($this->app()->request()->isPost()) {
                $form->hydrate($this->app()->request()->post('data'));
                try {
                    if ($form->isValid()) {
                        $this->preSave($model);
                        $redirectUrl = $_SERVER['HTTP_REFERER'];
                        if ($model->getId() > 0) {
                            $this->em()->merge($model);
                            $message = 'Registro atualizado com sucesso';
                        } else {
                            $this->em()->persist($model);
                            $message = 'Registro adicionado com sucesso';
                            if ($redirectUrl[strlen($redirectUrl) - 1] !== '/') {
                                $redirectUrl .= '/';
                            }
                        }
                        $this->em()->flush();
                        $this->postSave($model);
                        $this->app()->flash('success', $message);
                        if ($redirectUrl[strlen($redirectUrl) - 1] === '/') {
                            $redirectUrl .= $model->getId();
                        }
                        $this->app()->redirect($redirectUrl);
                    }
                } catch (Exception $e) {
                    //echo $e->getFile() . ':' . $e->getLine();
                    $this->app()->view()->set('error', $e->getMessage());
                }
            }
        }
        $this->postEdit($model);
        
        $this->app()->view()->set('controllerName', $this->name);
        $this->app()->view()->set('model', $model);
    }
    
    protected function postIndex() {}
    protected function preEdit($model) {}
    protected function postEdit($model) {}
    protected function preSave($model) {}
    protected function postSave($model) {}

    
}
<commit_msg>readonly check<commit_after><?php
namespace RogerioLino\Controller;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Exception;
use RogerioLino\Form\EntityValidator;
use RogerioLino\Util\Arrays;
use Slim\Slim;

/**
 * CrudController
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class CrudController extends SlimController {
        
    protected $name;
    protected $entityName;
    protected $maxResults = 20;
    protected $readonly = false;
    
    public function __construct(Slim $app, $entityName, $title = "", $subtitle = "") {
        parent::__construct($app);
        $this->entityName = $entityName;
        // resolving controller name (class prefix)
        $this->name = strtolower(str_replace('Controller', '', @end(@explode('\\', get_class($this)))));
        
        $this->app()->view()->set('title', $title);
        $this->app()->view()->set('subtitle', $subtitle);
    }
    
    /**
     * @return EntityManager
     */
    public final function em() {
        return $this->app()->em;
    }
    
    public function index() {
        $this->postIndex();
        $page = (int) Arrays::value($_GET, 'page', 0);
        $search = Arrays::value($_GET, 's', '');
        $query = $this->searchQuery("%$search%", $this->maxResults, $page * $this->maxResults);
        $paginator = new Paginator($query, true);
        $total = count($paginator);

        $this->app()->view()->set('controllerName', $this->name);
        
        $this->app()->view()->set('search', $search);
        $this->app()->view()->set('items', $paginator);
        $this->app()->view()->set('total', $total);
        $this->app()->view()->set('page', $page);
        $this->app()->view()->set('maxResults', $this->maxResults);
        $this->app()->view()->set('pages', ceil($total / $this->maxResults));
    }
    
    protected function searchQuery($searchValue, $maxResults, $first = 0) {
        $dql = "SELECT e FROM {$this->entityName} e";
        $query = $this->em()->createQuery($dql)
                    ->setFirstResult($first)
                    ->setMaxResults($maxResults);
        return $query;
    }
    
    public function edit($id = 0) {
        $model = null;
        $id = (int) $id;
        if ($id > 0) {
            $model = $this->em()->find($this->entityName, $id);
        }
        if (!$model) {
            $model = new $this->entityName;
        }
        
        $this->preEdit($model);
        
        // form submit
        if ($this->app()->request()->isPost()) {
            if (!$this->readonly) {
                $form = new EntityValidator($model);
                $form->hydrate($this->app()->request()->post('data'));
                try {
                    if ($form->isValid()) {
                        $this->preSave($model);
                        $redirectUrl = $_SERVER['HTTP_REFERER'];
                        if ($model->getId() > 0) {
                            $this->em()->merge($model);
                            $message = 'Registro atualizado com sucesso';
                        } else {
                            $this->em()->persist($model);
                            $message = 'Registro adicionado com sucesso';
                            if ($redirectUrl[strlen($redirectUrl) - 1] !== '/') {
                                $redirectUrl .= '/';
                            }
                        }
                        $this->em()->flush();
                        $this->postSave($model);
                        $this->app()->flash('success', $message);
                        if ($redirectUrl[strlen($redirectUrl) - 1] === '/') {
                            $redirectUrl .= $model->getId();
                        }
                        $this->app()->redirect($redirectUrl);
                    }
                } catch (Exception $e) {
                    //echo $e->getFile() . ':' . $e->getLine();
                    $this->app()->view()->set('error', $e->getMessage());
                }
            }
        }
        $this->postEdit($model);
        
        $this->app()->view()->set('controllerName', $this->name);
        $this->app()->view()->set('model', $model);
        $this->app()->view()->set('readonly', $this->readonly);
    }
    
    protected function postIndex() {}
    protected function preEdit($model) {}
    protected function postEdit($model) {}
    protected function preSave($model) {}
    protected function postSave($model) {}

    
}

{'fraction_non_alphanumeric': 0.18433490884537473, 'fraction_numerical': 0.002025658338960162, 'mean_word_length': 1.7069753274444106, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 187, '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': '968982', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2416, 'n_words': 433}