<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Version_101 extends CI_Migration
{
    function __construct()
    {
        parent::__construct();
        //check and create folders

    }

    public function up()
    {
      //102
      // Create the tasks attachments folder
        if(!is_dir(TASKS_ATTACHMENTS_FOLDER)){
            mkdir(TASKS_ATTACHMENTS_FOLDER, 0755);
            fopen(TASKS_ATTACHMENTS_FOLDER . '.htaccess', 'w');
            $fp = fopen(TASKS_ATTACHMENTS_FOLDER.'.htaccess','a+');
            if($fp)
            {
                fwrite($fp,'Order Deny,Allow'.PHP_EOL.'Deny from all');
                fclose($fp);
            }
        }

        //104
        if(!is_dir(INVOICE_ATTACHMENTS_FOLDER)){
            mkdir(INVOICE_ATTACHMENTS_FOLDER, 0755);
            fopen(INVOICE_ATTACHMENTS_FOLDER . '.htaccess', 'w');
            $fp = fopen(INVOICE_ATTACHMENTS_FOLDER.'.htaccess','a+');
            if($fp)
            {
                fwrite($fp,'Order Deny,Allow'.PHP_EOL.'Deny from all');
                fclose($fp);
            }
        }

        if(!is_dir(EXPENSE_ATTACHMENTS_FOLDER)){
          mkdir(EXPENSE_ATTACHMENTS_FOLDER, 0755);
          fopen(EXPENSE_ATTACHMENTS_FOLDER . '.htaccess', 'w');
          $fp = fopen(EXPENSE_ATTACHMENTS_FOLDER.'.htaccess','a+');
          if($fp)
          {
            fwrite($fp,'Order Deny,Allow'.PHP_EOL.'Deny from all');
            fclose($fp);
          }
        }

         if(!is_dir(LEAD_ATTACHMENTS_FOLDER)){
          mkdir(LEAD_ATTACHMENTS_FOLDER, 0755);
          fopen(LEAD_ATTACHMENTS_FOLDER . '.htaccess', 'w');
          $fp = fopen(LEAD_ATTACHMENTS_FOLDER.'.htaccess','a+');
          if($fp)
          {
            fwrite($fp,'Order Deny,Allow'.PHP_EOL.'Deny from all');
            fclose($fp);
          }
        }

        // Remove the translator
        if(is_dir(APPPATH . 'views/admin/translator')){
          delete_dir(APPPATH . 'views/admin/translator');
        }
        if(is_file(APPPATH . 'controllers/admin/Translator.php')){
          unlink(APPPATH . 'controllers/admin/Translator.php');
        }
        if(is_file(APPPATH . 'controllers/admin/._translator.php')){
          unlink(APPPATH . 'controllers/admin/._translator.php');
        }
        if(is_file(APPPATH . 'config/translator.php')){
          unlink(APPPATH . 'config/translator.php');
        }

        if(is_file(APPPATH . 'controllers/Reset_demo.php')){
          unlink(APPPATH . 'controllers/Reset_demo.php');
        }

        //107
        if (file_exists(FCPATH . 'media/.htaccess')) {
            @unlink(FCPATH . 'media/.htaccess');
        }

        if (file_exists(FCPATH.'media/')) {
            if (!file_exists(FCPATH.'media/' . 'index.html')) {
                fopen(FCPATH.'media/' . 'index.html', 'w');
            }
        }

        if (!is_dir(CREDIT_NOTES_ATTACHMENTS_FOLDER)) {
            mkdir(CREDIT_NOTES_ATTACHMENTS_FOLDER, 0755);
            fopen(CREDIT_NOTES_ATTACHMENTS_FOLDER . '.htaccess', 'w');
            $fp = fopen(CREDIT_NOTES_ATTACHMENTS_FOLDER.'.htaccess', 'a+');
            if ($fp) {
                fwrite($fp, 'Order Deny,Allow'.PHP_EOL.'Deny from all');
                fclose($fp);
            }
        }

        if(file_exists(FCPATH.'pipe.php')){
             @chmod(FCPATH.'pipe.php', 0755);
         }
         $this->db->insert('activity_log',[
           'description'=>'Migrated to version 1.0.1',
           'date'=>date('Y-m-d H:i:s')
         ]);
    }
    public function down()
    {
      $this->db->insert('activity_log',[
        'description'=>'Downgraded to version 1.0.0',
        'date'=>date('Y-m-d H:i:s')
      ]);
    }
}
