<?php

/* 
 * Objeto para escribir los cambios
 */

class LogCambios {
    //put your code here
    private $file, $month, $year;
    private $dir;
    
    function __construct($ruta = "/log/"){
        $this->month = date("m");
        $this->year = date("Y");
        //$this->dir = $_SERVER['DOCUMENT_ROOT'].$ruta;
        $this->dir = $ruta;
        $this->updateFilename();
    }
    
    function setMes($mes){
        $this->month = $mes;
        $this->updateFilename();
    }
    function setAno($ano){
        $this->year = $ano;
        $this->updateFilename();
    }
    private function updateFilename(){
        $this->file = "cambios_".$this->year."_".$this->month.".log";
    }

    private function cleanLog($text){//remueve || de los textos para no afectar estructura
        return trim(str_ireplace( "||" , "" , $text));
    }
    
    function appendLog($desc){
        $filename = $this->dir.$this->file;
        /*
        if (file_exists($this->dir)){
            $data = date('Y-m-d H:i:s')."||".$this->cleanLog($desc)."\n";
            //echo $filename;
            $res = file_put_contents($filename, $data, FILE_APPEND);
            //echo " result: $res<br>";
        }*/
    }
/*
    function getLog($mes ="", $ano = ""){
        if($mes != "") $this->setMes($mes);
        if($ano != "") $this->setAno($ano);
        $filename = $this->dir.$this->file;
        if (file_exists($filename)){
            return file ($filename , FILE_SKIP_EMPTY_LINES);
        }else{
            return array();
        }
    }*/
}
