<commit_before><?php
/**
 * TO-Do application for AmidaMVC Demonstration. 
 */

$_ctrl->prependComponent( array(
    array( 'appTodo',  'app' ),
    array( 'viewTodo', 'view' ),
));


class appTodo extends AmidaMVC\Component\Model
{
    static $todo_file = '';
    static $id_max = 0;
    // +-------------------------------------------------------------+
    static function _init() {
        static::$todo_file = __DIR__ . '/_todo/_todo.csv';
        if( !is_dir( dirname( static::$todo_file ) ) ) {
            mkdir( dirname( static::$todo_file ), 0777 );
        }
        if( !file_exists( static::$todo_file ) ) {
            $todo = "1,9,\"run todo demo.\"\n2,1,\"finish the first todo.\"";
            file_put_contents( static::$todo_file, $todo );
        }
    }
    // +-------------------------------------------------------------+
    static function putTodo( $todo ) {
        $data = '';
        foreach( $todo as $do ) {
            $line = '"' . implode( '","', $do ) . '"';
            $data .= $line . "\n";
        }
        file_put_contents( static::$todo_file, $data );
    }
    // +-------------------------------------------------------------+
    static function getTodo( $raw=FALSE ) {
        $list = trim( file_get_contents( static::$todo_file ) );
        if( $raw ) {
            return $list;
        }
        if( !$list ) return array();
        $list = explode( "\n", $list );
        $todo = array();
        $id_max = 0;
        foreach( $list as $line ) {
            $do = explode( ",", trim( $line ) );
            foreach( $do as &$col ) {
                if( substr( $col, 0, 1 ) == '"' && substr( $col, -1 ) == '"' ) {
                    $col = substr( $col, 1, -1 );
                }
            }
            if( $do[0] > $id_max ) {
                $id_max = $do[0];
            }
            $todo[] = $do;
        }
        static::$id_max = $id_max;
        return $todo;
    }
    // +-------------------------------------------------------------+
    // ACTIONS
    // +-------------------------------------------------------------+
    static function actionDefault(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  )
    {

        // default is page not found error. 
        $ctrl->setAction( '_pageNotFound' );
    }
    // +-------------------------------------------------------------+
    static function actionList(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  ) 
    {
        self::_init();
        $todo = self::getTodo();
        return $todo;
    }
    // +-------------------------------------------------------------+
    static function actionPost(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  ) 
    {
        self::_init();
        // add new to do item.
        $what = addslashes( $_POST[ 'what'] );
        if( $what ) {
            $todo = self::getTodo();
            $new  = array( static::$id_max+1, 1, $what );
            $todo[] = $new;
            self::putTodo( $todo );
        }
        $ctrl->redirect( '/todo/list' );
    }
    // +-------------------------------------------------------------+
    static function actionReset(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  )
    {
        self::_init();
        if( file_exists( static::$todo_file ) ) {
            unlink( static::$todo_file );
        }
        self::_init();
        $ctrl->redirect( '/todo/list' );
    }
    // +-------------------------------------------------------------+
    static function actionToggle(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  )
    {
        self::_init();
        $loadInfo = $siteObj->get( 'loadInfo' );
        $id = $loadInfo->id;
        if( $id ) {
            $todo = self::getTodo();
            foreach( $todo as &$do ) {
                if( $do[0] == $id ) {
                    if( $do[1] == '1' ) { // active -> done
                        $do[1] = '9';
                    }
                    else {                // done -> active
                        $do[1] = '1';
                    }
                }
            }
            self::putTodo( $todo );
        }
        $ctrl->redirect( '/todo/list' );
    }<commit_msg>slightly efficient loop by break if id is found.<commit_after><?php
/**
 * TO-Do application for AmidaMVC Demonstration. 
 */

$_ctrl->prependComponent( array(
    array( 'appTodo',  'app' ),
    array( 'viewTodo', 'view' ),
));


class appTodo extends AmidaMVC\Component\Model
{
    static $todo_file = '';
    static $id_max = 0;
    // +-------------------------------------------------------------+
    static function _init() {
        static::$todo_file = __DIR__ . '/_todo/_todo.csv';
        if( !is_dir( dirname( static::$todo_file ) ) ) {
            mkdir( dirname( static::$todo_file ), 0777 );
        }
        if( !file_exists( static::$todo_file ) ) {
            $todo = "1,9,\"run todo demo.\"\n2,1,\"finish the first todo.\"";
            file_put_contents( static::$todo_file, $todo );
        }
    }
    // +-------------------------------------------------------------+
    static function putTodo( $todo ) {
        $data = '';
        foreach( $todo as $do ) {
            $line = '"' . implode( '","', $do ) . '"';
            $data .= $line . "\n";
        }
        file_put_contents( static::$todo_file, $data );
    }
    // +-------------------------------------------------------------+
    static function getTodo( $raw=FALSE ) {
        $list = trim( file_get_contents( static::$todo_file ) );
        if( $raw ) {
            return $list;
        }
        if( !$list ) return array();
        $list = explode( "\n", $list );
        $todo = array();
        $id_max = 0;
        foreach( $list as $line ) {
            $do = explode( ",", trim( $line ) );
            foreach( $do as &$col ) {
                if( substr( $col, 0, 1 ) == '"' && substr( $col, -1 ) == '"' ) {
                    $col = substr( $col, 1, -1 );
                }
            }
            if( $do[0] > $id_max ) {
                $id_max = $do[0];
            }
            $todo[] = $do;
        }
        static::$id_max = $id_max;
        return $todo;
    }
    // +-------------------------------------------------------------+
    // ACTIONS
    // +-------------------------------------------------------------+
    static function actionDefault(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  )
    {

        // default is page not found error. 
        $ctrl->setAction( '_pageNotFound' );
    }
    // +-------------------------------------------------------------+
    static function actionList(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  ) 
    {
        self::_init();
        $todo = self::getTodo();
        return $todo;
    }
    // +-------------------------------------------------------------+
    static function actionPost(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  ) 
    {
        self::_init();
        // add new to do item.
        $what = addslashes( $_POST[ 'what'] );
        if( $what ) {
            $todo = self::getTodo();
            $new  = array( static::$id_max+1, 1, $what );
            $todo[] = $new;
            self::putTodo( $todo );
        }
        $ctrl->redirect( '/todo/list' );
    }
    // +-------------------------------------------------------------+
    static function actionReset(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  )
    {
        self::_init();
        if( file_exists( static::$todo_file ) ) {
            unlink( static::$todo_file );
        }
        self::_init();
        $ctrl->redirect( '/todo/list' );
    }
    // +-------------------------------------------------------------+
    static function actionToggle(
        \AmidaMVC\Framework\Controller $ctrl,
        \AmidaMVC\Component\SiteObj &$siteObj  )
    {
        self::_init();
        $loadInfo = $siteObj->get( 'loadInfo' );
        $id = $loadInfo->id;
        if( $id ) {
            $todo = self::getTodo();
            foreach( $todo as &$do ) {
                if( $do[0] == $id ) {
                    if( $do[1] == '1' ) { // active -> done
                        $do[1] = '9';
                    }
                    else {                // done -> active
                        $do[1] = '1';
                    }
                    break;
                }
            }
            self::putTodo( $todo );
        }
        $ctrl->redirect( '/todo/list' );
    }
{'fraction_non_alphanumeric': 0.27140585964115377, 'fraction_numerical': 0.0059050647285941405, 'mean_word_length': 1.0514791521080828, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3903203', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2548, 'n_words': 497}