<commit_before><?php

/**
 * Class Posts
 *
 * Please note:
 * Don't use the same name for class and method, as this might trigger an (unintended) __construct of the class.
 * This is really weird behaviour, but documented here: http://php.net/manual/en/language.oop5.decon.php
 *
 */
class Posts extends Controller {

    /**
     * The number of posts to show on one page in the vistor view.
     */
    private $page_count = 10;

    /**
     * The number of posts to show in the admin page.
     */
    private $admin_page_count = 20;

    function __construct (){
        parent::__construct();
        require_once APP . 'model/post.php';
        $this->model = new Post($this->db);
    }

    /**
     * PAGE: page 1
     * A method wrapper to the first page of the blog
     */
    public function index() {
        $this->page(1);
    }

    /**
     * PAGE: posts display page
     * Display a page of blog posts.
     *
     * Can be accessed by anyone
     *
     * @param   int     $page   The page number to display
     */
    public function page($page) {
        $page = (int) $page;
        $total_pages = (int) ceil($this->model->count() / $this->page_count);

        $posts = $this->model->list_paged($page, $this->page_count);


        require APP . 'view/_templates/header.php';
        require APP . 'view/posts/index.php';
        require APP . 'view/_templates/footer.php';
    }

    /**
     * PAGE: Posts admin page
     * Displays the admin page, or the dashboard
     *
     * Requires login.
     *
     * @param   int     $page   The page number to display
     */
    public function admin($page) {
        if(isset($_SESSION['user'])){
            $posts = $this->model->list_paged($page, $this->admin_page_count);
            
            require APP . 'view/_templates/header.php';
            require APP . 'view/posts/admin.php';
            require APP . 'view/_templates/footer.php';   
        } else{
            header('Location: ' . URL . 'error/unauthorized');
        }
    }

    /**
     * PAGE: Blog post show page
     * Displays the blog post on it's own.
     * 
     * Does not require login.
     * 
     * Comments should be handled from this page.
     * 
     * @param   int     $id     The ID of the post to show.
     */<commit_msg>Add an indicies array to control pagination bar<commit_after><?php

/**
 * Class Posts
 *
 * Please note:
 * Don't use the same name for class and method, as this might trigger an (unintended) __construct of the class.
 * This is really weird behaviour, but documented here: http://php.net/manual/en/language.oop5.decon.php
 *
 */
class Posts extends Controller {

    /**
     * The number of posts to show on one page in the vistor view.
     */
    private $page_count = 10;

    /**
     * The number of posts to show in the admin page.
     */
    private $admin_page_count = 20;

    private $admin_pagination_preview = 2;

    function __construct (){
        parent::__construct();
        require_once APP . 'model/post.php';
        $this->model = new Post($this->db);
    }

    /**
     * PAGE: page 1
     * A method wrapper to the first page of the blog
     */
    public function index() {
        $this->page(1);
    }

    /**
     * PAGE: posts display page
     * Display a page of blog posts.
     *
     * Can be accessed by anyone
     *
     * @param   int     $page   The page number to display
     */
    public function page($page) {
        $page = (int) $page;
        $total_pages = (int) ceil($this->model->count() / $this->page_count);

        $posts = $this->model->list_paged($page, $this->page_count);


        require APP . 'view/_templates/header.php';
        require APP . 'view/posts/index.php';
        require APP . 'view/_templates/footer.php';
    }

    /**
     * PAGE: Posts admin page
     * Displays the admin page, or the dashboard
     *
     * Requires login.
     *
     * @param   int     $page   The page number to display
     */
    public function admin($page) {
        if(isset($_SESSION['user'])){

            $page = (int) $page;
            $total_pages = (int) ceil($this->model->count() / $this->admin_page_count);

            //We set an array of pagination indices, with -1 as ellipsis
            //We can ignore empty cases or invalid indices, since the entire pagination bar will be hidden
            $indices = array();

            //First index
            array_push($indices, 1);
            
            //Ellipsis if page number is larger enough
            if($page > $this->admin_pagination_preview + 2){
                array_push($indices, -1);
            }

            //Previews before and after the page number
            for ($i = $page - $this->admin_pagination_preview; $i <= $page + $this->admin_pagination_preview; $i++) { 
                if($i > 1 && $i < $total_pages){
                    array_push($indices, $i);
                }
            }

            //Ellipsis if page number is small enough
            if($page + $this->admin_pagination_preview < $total_pages - 1){
                array_push($indices, -1);
            }

            //Last page
            if($total_pages > 1){
                array_push($indices, $total_pages);
            }

            $posts = $this->model->list_paged($page, $this->admin_page_count);
            
            require APP . 'view/_templates/header.php';
            require APP . 'view/posts/admin.php';
            require APP . 'view/_templates/footer.php';   
        } else{
            header('Location: ' . URL . 'error/unauthorized');
        }
    }

    /**
     * PAGE: Blog post show page
     * Displays the blog post on it's own.
     * 
     * Does not require login.
     * 
     * Comments should be handled from this page.
     * 
     * @param   int     $id     The ID of the post to show.
     */
{'fraction_non_alphanumeric': 0.11954297407912688, 'fraction_numerical': 0.003922237380627558, 'mean_word_length': 1.7849002849002849, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 37, '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': '1064890', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1666, 'n_words': 524}