<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */

 /**
  * Backup
  * 
  * Yii module to backup, restore databse
  * 
  * @version 1.0
  * @author Shiv Charan Panjeta <shiv@toxsl.com> <shivcharan.panjeta@outlook.com>
  */
 class BackupController extends Controller
     {

     /**
      * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
      * using two-column layout. See 'protected/views/layouts/column2.php'.
      */
     public $layout = '//layouts/column2';


     /**
      * @return array action filters
      */
     public function filters()
     {
         return array(
             'accessControl', // perform access control for CRUD operations
         );
     }


     /**
      * Specifies the access control rules.
      * This method is used by the 'accessControl' filter.
      * @return array access control rules
      */
     public function accessRules()
     {
         return array(
             array('allow', // allow all users to perform 'index' and 'view' actions
                 'actions' => array('index', 'delete', 'download'),
                 'roles' => array('System Admin', 'Super Admin'),
             ),
         );
     }

     public $tables = array();
     public $fp;
     public $file_name;
     public $_path = null;
     public $back_temp_file = 'db_backup_';


     public function actionDelete($file = null)
     {
         $settings_array = Setting::getSettingsArray();
         $path = (isset($settings_array["backup_file_path"]) && ($settings_array["backup_file_path"] != null) && ($settings_array["backup_file_path"] != '')) ? $settings_array["backup_file_path"] . '/' : './protected/backup/';

         if (isset($file))
         {
             $sqlFile = $path . $file;
             if (file_exists($sqlFile))
                 unlink($sqlFile);
         }
         else
             throw new CHttpException(404, Yii::t('app', 'File not found'));
         $this->actionIndex();
     }


     public function actionIndex()
     {
         $settings_array = Setting::getSettingsArray();
         $path = (isset($settings_array["backup_file_path"]) && ($settings_array["backup_file_path"] != null) && ($settings_array["backup_file_path"] != '')) ? $settings_array["backup_file_path"] . '/' : './protected/backup/';
         $list = glob($path . '*.sql');
         $list = array_map('basename', $list);
         sort($list);

         $dataArray = array();
         foreach ($list as $id => $filename)
         {
             $columns = array();
             $columns['id'] = $id;
             $columns['name'] = basename($filename);
             $columns['size'] = floor(filesize($path . $filename) / 1024) . ' KB';
             $columns['create_time'] = date(DATE_RFC822, filectime($path . $filename));
             $dataArray[] = $columns;
         }
         $dataProvider = new CArrayDataProvider($dataArray);
         $this->render('index', array(
             'dataProvider' => $dataProvider,
         ));
     }


     public function actionDownload($file = null)
     {
         $settings_array = Setting::getSettingsArray();
         $path = (isset($settings_array["backup_file_path"]) && ($settings_array["backup_file_path"] != null) && ($settings_array["backup_file_path"] != '')) ? $settings_array["backup_file_path"] . '/' : './protected/backup/';

         if (isset($file))
         {
             $sqlFile = $path . $file;
             if (file_exists($sqlFile))
             {
                 $request = Yii::app()->getRequest();
                 $request->sendFile(basename($sqlFile), file_get_contents($sqlFile));
             }
         }
         throw new CHttpException(404, Yii::t('app', 'File not found'));
     }
     }