<?php
    include_once("../lib/symfony/vendor/phing/lib/Zip.php");
/**
 * log actions.
 *
 * @package    tvtl
 * @subpackage log
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class logActions extends autologActions
{
    public  function executeDel() {
        $id_list = $this->getRequestParameter('idList');
        $page = $this->getRequestParameter('page');
        $id_array = explode(",", $id_list);
        
        $c = new  Criteria();
        $c->add(TbllogPeer::LOG_ID , $id_array, Criteria::IN );
        if(TbllogPeer::doDelete($c)) {
            $this->redirect("log/list?page=$page");
            return sfView::NONE ;
        }
        else {
            $this->getRequest()->setError('delete', "System error: count not delete");
            return $this->forward('log', 'list');
        }
    }
    
    /**
     * Backup form database to file
     *
     * @return unknown
     */
    public  function executeSaveToFile() {
        if ($this->getRequest()->getMethod() == sfRequest::POST){
            
            //write from database to file
            $c = new Criteria();
            $logs = TbllogPeer::doSelect($c);
            if($fileHandle = fopen("log.txt", "w")) {
                $buffer = "";
                foreach ($logs as $log) {
                    $buffer.=$log->getLogId()."\t".$log->getLogType()."\t".$log->getLogDate()."\t".$log->getLogUser()."\t".$log->getLogLink().chr(13).chr(10);
                }
                if($fileHandle) {
                    fwrite($fileHandle, $buffer);
                    fclose($fileHandle);
                }
                if(!is_dir("backup"))
                    mkdir("backup");
                if(!is_dir("backup/log"))
                    mkdir("backup/log");
                $time = date("m_d_Y_H_i_s", time());
                $filebakup = "log".$time.".zip";
                $zip = new Archive_Zip("backup/log/$filebakup");
                $zip->add("log.txt");
                unlink("log.txt");
                
                $backup_type = $this->getRequestParameter('backup-type');
                if ($backup_type == 1) { // save and delete database
                	if(!TbllogPeer::doDeleteAll()) {
                	   $this->getRequest()->setError('delete', "System error: count not delete database");
                       
                	}
                }

                //var_dump(sfDatabase::getParameter('database'));
                $this->setFlash('notice', 'Your modifications have been saved');
            }
            else {
                
                $this->getRequest()->setError('delete', "System error: count not delete database");
            }
            //return $this->forward('log', 'list');  
            //return  sfView::NONE ;
        }//if
        else {
            
        }//else
    }
}
