<?php
// auto-generated by sfPropelCrud
// date: 2007/06/13 11:50:03
include_once("../lib/symfony/vendor/phing/lib/Zip.php");
require_once("web/lib.backend/mysql_dump.inc.php");
?>
<?php

/**
 * system actions.
 *
 * @package    tvtl
 * @subpackage system
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 3335 2007-01-23 16:19:56Z fabien $
 */
class systemActions extends sfActions
{
    public function executeIndex()
    {
    return $this->forward('system', 'list');
    }
    
    public function executeList()
    {
    }
    
    /**
     * Backup data and sql
     *
     */
    public function executeBackup() {
        if ($this->getRequest()->getMethod() == sfRequest::POST){
            $backup_type = $this->getRequestParameter('backup_type');
          
            if(!is_dir("backup"))
                mkdir("backup");
            if(!is_dir("backup/resources")){
                mkdir("backup/resources");
            }
            
            $time = date("m_d_Y_H_i_s", time());
            $filebakup = "data".$time.".zip";
            $zip = new Archive_Zip("backup/resources/$filebakup");
            $zip->add("uploads", array('no_compression' => true));
            
            //bakup database
            $dbhost = "localhost";
            $dbuser = "root";
            $dbpwd  = "";
            $dbname = "tvtl";
            /**
            @params : [$database name] You can give database name ,names seperated by coma or array of names
            						   By default it will take all the database   
            		: [$database name] You can give table name ,names seperated by coma or array of names
            						   By default it will take all the tables into a database
            		 [OPTIONS] :- 
            			HAR_LOCK_TABLE  Put Lock table command into sql file 
            			HAR_FULL_SYNTAX FULL Insert Syntax
            			HAR_DROP_TABLE  Include Drop Table Statement
            			HAR_NO_STRUCT   No Create Statement will include
            			HAR_NO_DATA     No Insert Statement
            			HAR_ALL_OPTIONS (HAR_LOCK_TABLE | HAR_FULL_SYNTAX | HAR_DROP_TABLE) Use All Options 
            */
            $mysql_dump = new MYSQL_DUMP($dbhost,$dbuser,$dbpwd);
            //$sql = $mysql_dump->dumpDB(); //Takes all database backups
            //$sql = $mysql_dump->dumpDB("database_name",HAR_ALL_TABLES, HAR_ALL_OPTIONS | HAR_NO_DATA ) It will take only back up of table structures only
            $sql = $mysql_dump->dumpDB($dbname); //Takes backup of particular database
            if($sql==false)
            	echo $mysql_dump->error();
            else { //save sql to file
                $mysql_dump->save_sql($sql);
                $filename = $dbname.".sql";
                $zip->add($filename);
                unlink($filename);
            }
            $this->setFlash('notice', 'Backup success!.');
            //To force sql to download
            // To save sql 
            /*@param : $sql sql commands comes from MYSQL_DUMP::dumpDB() function
            		   [$sqlfilename]	Sql file name
            */
            //$mysql_dump->download_sql($sql,"dump.sql");
            
            // Print SQL
            //echo nl2br($sql);
            
            //To restore sqlfile
            /*if($mysql_dump->restoreDB("dump.sql")==false)
            	echo $mysql_dump->error();*/
            
      }
      else {
          
      }
    }
    
    /**
     * Restore data and database
     *
     */
    public  function executeRestore() {
        if ($this->getRequest()->getMethod() == sfRequest::POST){
            
            $fileRestore = $this->getRequestParameter('data');
            
            //restore data;
            if(file_exists("backup/resources/$fileRestore")) {
                $zip = new Archive_Zip("backup/resources/$fileRestore");
                $zip->extract();
            
                //restore database
                $dbhost = "localhost";
                $dbuser = "root";
                $dbpwd = "";
                $dbname = "tvtl";
                $mysql_dump = new MYSQL_DUMP($dbhost,$dbuser,$dbpwd);
                $mysql_dump->dumpDB($dbname);
                $fileSql = $dbname.".sql";
                if(file_exists($fileSql)) {
                    if($mysql_dump->restoreDB($fileSql) == false)
                        echo $mysql_dump->error();
                    else {
                        unlink($fileSql);
                    }
                }
            }
            //echo 'backup success';
            $this->setFlash('notice', 'Restore success');
        }
        else {
            
        }
    }
    
    /**
     * Empty data in database.
     *
     */
    function executeEmptyData(){
        if ($this->getRequest()->getMethod() == sfRequest::POST){
                        
            //bakup struct database
            $dbhost = "localhost";
            $dbuser = "root";
            $dbpwd  = "";
            $dbname = "tvtl";
            /**
            @params : [$database name] You can give database name ,names seperated by coma or array of names
            						   By default it will take all the database   
            		: [$database name] You can give table name ,names seperated by coma or array of names
            						   By default it will take all the tables into a database
            		 [OPTIONS] :- 
            			HAR_LOCK_TABLE  Put Lock table command into sql file 
            			HAR_FULL_SYNTAX FULL Insert Syntax
            			HAR_DROP_TABLE  Include Drop Table Statement
            			HAR_NO_STRUCT   No Create Statement will include
            			HAR_NO_DATA     No Insert Statement
            			HAR_ALL_OPTIONS (HAR_LOCK_TABLE | HAR_FULL_SYNTAX | HAR_DROP_TABLE) Use All Options 
            */
            $adminInfo = TbluserPeer::retrieveByPK(1);
            
            $mysql_dump = new MYSQL_DUMP($dbhost,$dbuser,$dbpwd);
            //$sql = $mysql_dump->dumpDB(); //Takes all database backups
            $sql = $mysql_dump->dumpDB($dbname,HAR_ALL_TABLES, HAR_ALL_OPTIONS | HAR_NO_DATA );// It will take only back up of table structures only
            if($sql==false)
            	echo $mysql_dump->error();
            else { //save sql to file
                $mysql_dump->save_sql($sql);
                $filename = $dbname.".sql";
            }
            
            //empty data
            if(file_exists($filename)) {
                
                //restore database
                if($mysql_dump->restoreDB($filename) == false)
                    echo $mysql_dump->error();
                else {
                    //insert account admin
                    $user = new Tbluser();
                    $user->setUsId($adminInfo->getUsId());
                    $user->setUsUsername($adminInfo->getUsUsername());
                    $user->setUsPassword($adminInfo->getUsPassword());
                    $user->setUsEmail($adminInfo->getUsEmail());
                    $user->setUsLastname($adminInfo->getUsLastname());
                    $user->setUsFirstname($adminInfo->getUsFirstname());
                    $user->setUsAclgroup($adminInfo->getUsAclgroup());
                    $user->setUsStatus($adminInfo->getUsStatus());
                    $user->save();
                    
                    unlink($filename);
                    $this->deldir("uploads/resources/");
                }
            }
            
            
            $this->setFlash('notice', 'Empty success!.');
            //To force sql to download
            // To save sql 
            /*@param : $sql sql commands comes from MYSQL_DUMP::dumpDB() function
            		   [$sqlfilename]	Sql file name
            */
            //$mysql_dump->download_sql($sql,"dump.sql");
            
            // Print SQL
            //echo nl2br($sql);
            
            //To restore sqlfile
            /*if($mysql_dump->restoreDB("dump.sql")==false)
            	echo $mysql_dump->error();*/
            
      }
      else {
          
      }
    }
    /**
     * test ajax in symfony
     *
     */
    // function
      function deldir( $dir ) {
      	$current_dir = opendir( $dir );
      	$old_umask = umask(0);
      	while ($entryname = readdir( $current_dir )) {
      		if ($entryname != '.' and $entryname != '..') {
      			if (is_dir( $dir . $entryname )) {
      				$this->deldir( "$dir$entryname/" );
      			} else {
                      @chmod($dir . $entryname, 0777);
      				unlink( $dir . $entryname );
      			}
      		}
      	}
      	umask($old_umask);
      	closedir( $current_dir );
      	return rmdir( $dir );
      }

}
