<commit_before><?php
/*
 Base Fw Model class

 Part of PHP osa framework  www.osalabs.com/osafw/php
 (c) 2009-2019 <NAME> www.osalabs.com
*/

abstract class FwModel {
    public $fw;  //current app/framework object
    public $table_name; //must be defined in inherited classes

    public $CACHE_PREFIX = 'fwmodel.one.'; #TODO - ability to cleanup all, but this model-only cache items

    public $field_id='id';                  #default primary key name
    public $field_iname='iname';
    # default field names. If you override it and make empty - automatic processing disabled
    public $field_status='status';
    public $field_add_users_id='add_users_id';
    public $field_upd_users_id='upd_users_id';
    public $field_upd_time='upd_time';

    protected $db;

    #alternative of fw::model(Model)->method() is Model::i()->method()
    #7 chars shorter :]
    static function i(){
        return fw::model(get_called_class());
    }

    public function __construct($param_fw=null) {
        if ( is_null($param_fw) ){
            $this->fw = fw::i();
        }else{
            $this->fw = $param_fw;
        }
        $this->db = $this->fw->db;
    }

    //cached, pass $is_force=true to force read from db
    public function one($id, $is_force=false) {
        $cache_key = $this->CACHE_PREFIX.$this->table_name.'*'.$id;
        if (!$is_force) {
            $row = FwCache::getValue($cache_key);
        }
        if ($is_force || is_null($row)){
            $row = $this->db->row("SELECT * FROM ".$this->table_name." WHERE ".$this->db->quote_ident($this->field_id)."=".$this->db->quote($id));
            FwCache::setValue($cache_key, $row);
        }else{
            #logger('CACHE HIT!');
        }
        return $row;
    }

    public function oneByIname($iname) {
        return $this->db->row("SELECT * FROM ".$this->table_name." WHERE ".$this->db->quote_ident($this->field_iname)."=".$this->db->quote($iname));
    }

    public function listFields(){
        $result=array();

        $rows = $this->db->arr("explain ".dbq_ident($this->table_name));
        foreach ($rows as $key => $row) {
            #add standard id/name fields
            $rows[$key]['id']=$row['Field'];
            $rows[$key]['iname']=$row['Field'];
        }

        return $result;
    }

    public function iname($id) {
        $row = $this->one($id);
        return $row[$this->field_iname];
    }

    public function getFullName($id) {
        $id+=0;
        $result = '';

        if ($id){
            $item = $this->one($id);
            $result = $item[$this->field_iname];
        }

        return $result;
    }

    //add new record
    public function add($item) {
        if (!empty($this->field_add_users_id) && !isset($item[$this->field_add_users_id])) $item[$this->field_add_users_id]=Utils::me();
        $id=$this->db->insert($this->table_name, $item);

        $this->removeCache($id);

        $this->fw->model('FwEvents')->logFields($this->table_name.'_add', $id, $item);
        return $id;
    }

    //update record
    public function update($id, $item) {
        if (!empty($this->field_upd_users_id) && !isset($item[$this->field_upd_users_id])) $item[$this->field_upd_users_id]=Utils::me();
        if (!empty($this->field_upd_time) && !isset($item[$this->field_upd_time])) $item[$this->field_upd_time]='~!now()';
        $this->db->update($this->table_name, $item, $id);

        $this->removeCache($id);

        $this->fw->model('FwEvents')->logFields($this->table_name.'_upd', $id, $item);
        return $id;
    }

    #quickly add new record just with iname
    #if such iname exists - just id returned
    #RETURN id - for new or existing record
    public function findOrAddByIname($iname, &$is_added=false){
        $result=0;
        $iname=trim($iname);
        if (!strlen($iname)) return 0;

        $item = $this->oneByIname($iname);
        if ($item){
            #exists
            $result = $item[$this->field_id];
        }else{
            $item=array(
                $this->field_iname => $iname,
            );
            $result = $this->add($item);
            $is_added=true;
        }<commit_msg>FwModel update fix<commit_after><?php
/*
 Base Fw Model class

 Part of PHP osa framework  www.osalabs.com/osafw/php
 (c) 2009-2019 <NAME> www.osalabs.com
*/

abstract class FwModel {
    public $fw;  //current app/framework object
    public $table_name; //must be defined in inherited classes

    public $CACHE_PREFIX = 'fwmodel.one.'; #TODO - ability to cleanup all, but this model-only cache items

    public $field_id='id';                  #default primary key name
    public $field_iname='iname';
    # default field names. If you override it and make empty - automatic processing disabled
    public $field_status='status';
    public $field_add_users_id='add_users_id';
    public $field_upd_users_id='upd_users_id';
    public $field_upd_time='upd_time';

    protected $db;

    #alternative of fw::model(Model)->method() is Model::i()->method()
    #7 chars shorter :]
    static function i(){
        return fw::model(get_called_class());
    }

    public function __construct($param_fw=null) {
        if ( is_null($param_fw) ){
            $this->fw = fw::i();
        }else{
            $this->fw = $param_fw;
        }
        $this->db = $this->fw->db;
    }

    //cached, pass $is_force=true to force read from db
    public function one($id, $is_force=false) {
        $cache_key = $this->CACHE_PREFIX.$this->table_name.'*'.$id;
        if (!$is_force) {
            $row = FwCache::getValue($cache_key);
        }
        if ($is_force || is_null($row)){
            $row = $this->db->row("SELECT * FROM ".$this->table_name." WHERE ".$this->db->quote_ident($this->field_id)."=".$this->db->quote($id));
            FwCache::setValue($cache_key, $row);
        }else{
            #logger('CACHE HIT!');
        }
        return $row;
    }

    public function oneByIname($iname) {
        return $this->db->row("SELECT * FROM ".$this->table_name." WHERE ".$this->db->quote_ident($this->field_iname)."=".$this->db->quote($iname));
    }

    public function listFields(){
        $result=array();

        $rows = $this->db->arr("explain ".dbq_ident($this->table_name));
        foreach ($rows as $key => $row) {
            #add standard id/name fields
            $rows[$key]['id']=$row['Field'];
            $rows[$key]['iname']=$row['Field'];
        }

        return $result;
    }

    public function iname($id) {
        $row = $this->one($id);
        return $row[$this->field_iname];
    }

    public function getFullName($id) {
        $id+=0;
        $result = '';

        if ($id){
            $item = $this->one($id);
            $result = $item[$this->field_iname];
        }

        return $result;
    }

    //add new record
    public function add($item) {
        if (!empty($this->field_add_users_id) && !isset($item[$this->field_add_users_id])) $item[$this->field_add_users_id]=Utils::me();
        $id=$this->db->insert($this->table_name, $item);

        $this->removeCache($id);

        $this->fw->model('FwEvents')->logFields($this->table_name.'_add', $id, $item);
        return $id;
    }

    //update record
    public function update($id, $item) {
        if (!empty($this->field_upd_users_id) && !isset($item[$this->field_upd_users_id])) $item[$this->field_upd_users_id]=Utils::me();
        if (!empty($this->field_upd_time) && !isset($item[$this->field_upd_time])) $item[$this->field_upd_time]='~!now()';
        $this->db->update($this->table_name, $item, $id, $this->field_id);

        $this->removeCache($id);

        $this->fw->model('FwEvents')->logFields($this->table_name.'_upd', $id, $item);
        return $id;
    }

    #quickly add new record just with iname
    #if such iname exists - just id returned
    #RETURN id - for new or existing record
    public function findOrAddByIname($iname, &$is_added=false){
        $result=0;
        $iname=trim($iname);
        if (!strlen($iname)) return 0;

        $item = $this->oneByIname($iname);
        if ($item){
            #exists
            $result = $item[$this->field_id];
        }else{
            $item=array(
                $this->field_iname => $iname,
            );
            $result = $this->add($item);
            $is_added=true;
        }
{'fraction_non_alphanumeric': 0.1746526114039291, 'fraction_numerical': 0.0028749401054144704, 'mean_word_length': 2.4831038798498124, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 132, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3063717', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2690, 'n_words': 541}