<?php
    /*
    error_reporting(-1);
    ini_set('display_errors', 'On');
    */
    include_once '../../account/session.php';
    include_once '../../../scripts/bd.php';

    if($_SESSION["type"] == 'it') {
      die("404 not found: <a href='/app/manager/offer/show.php'>Go back</a>");
    }
    
    if(isset($_GET['action']))
    {
        $action =   $_GET['action'];
        
        if($action=='add')
            add();
        
        if($action=='show')
            show();
        
        if($action=='get')
            get();
        
        if($action=='set')
            set();
    }
   
    function verify_name($p_name_vertical,$p_name_open_table,$p_name_click_table)
    {
        $query = bd::query("SELECT * FROM vertical where name_vertical = $p_name_vertical and name_open_table = $p_name_open_table and name_click_table = $p_name_click_table");
        if(mysql_num_rows($query)>0)
        {
           return true;
        }
        
        return false;
    }
   
    function add()
    { 
        $name_vertical      =   $_POST["name_vertical"];
        $name_open_table    =   $_POST["name_open_table"];
        $name_click_table   =   $_POST["name_click_table"];
        $date_add           =   date('Y-m-d h:i:s');
        
        
        $ok=0;
        if(!verify_name($name_vertical,$name_open_table,$name_click_table))
        {
            $rep=bd::query("INSERT INTO vertical VALUES (NULL, '$name_vertical', '$name_open_table', '$name_click_table', '$date_add' , NULL);");
            


           //Create Open & Click Tables Physically :
                include '../../../scripts/bdd.php';
                
                //1- Open Table :
                $sqlCreateOpenTable    =   
                "
                CREATE TABLE IF NOT EXISTS $name_open_table 
                (
                    `id` int(11) NOT NULL AUTO_INCREMENT,
                    `unique_id` varchar(255) NOT NULL,
                    `email_address` varchar(255) NOT NULL,
                    `email_md5` varchar(255) NOT NULL,
                    PRIMARY KEY (`id`),
                    UNIQUE KEY `email_address` (`email_address`),
                    UNIQUE KEY `email_address_2` (`email_md5`)
                )
                ";
                $bdd_data->exec($sqlCreateOpenTable);
                
                
                //2- Click Table :
                $sqlCreateClickTable    =
                "
                CREATE TABLE IF NOT EXISTS $name_click_table 
                (
                    `id` int(11) NOT NULL AUTO_INCREMENT,
                    `unique_id` varchar(255) NOT NULL,
                    `email_address` varchar(255) NOT NULL,
                    `email_md5` varchar(255) NOT NULL,
                    PRIMARY KEY (`id`),
                    UNIQUE KEY `email_address` (`email_address`),
                    UNIQUE KEY `email_address_2` (`email_md5`)
                )
                ";
                $bdd_data->exec($sqlCreateClickTable);
                
                 
            if(!$rep)
            {
               $ok  =   2; 
            }
        }
        else
        {
            $ok=1; 
        }
        echo $ok;
    }
   
    function show()
    { 
        $list   =   array();
        $data   =   bd::query("SELECT id_vertical,name_vertical,name_open_table,name_click_table FROM vertical");
        while ($row = mysql_fetch_object($data)) 
        {
                $list[] =   $row;
        }
        echo json_encode($list);
    }
   
    function get()
    { 
        $data = bd::query("SELECT * FROM vertical WHERE id_vertical='{$_GET["id_vertical"]}'");
        $row  = mysql_fetch_array($data);
      
        return $row;
    }
   
    function set()
    { 
        $id_vertical        =   $_POST['id_vertical'];
        $name_vertical      =   addslashes($_POST["name_vertical"]);
        $name_open_table    =   addslashes($_POST["name_open_table"]);
        $name_click_table   =   addslashes($_POST["name_click_table"]);
        
        $ok                 =   0;
        if(!verify_name($name_vertical,$name_open_table,$name_click_table))
        {
            
            
            //Rename Open & Click Tables :
                include '../../../scripts/bdd.php';
                
                //1- Get old_vertical_open_table_name & old_vertical_click_table_name :
                $sqlGetOpenClickTableName   =   "SELECT * FROM vertical WHERE id_vertical = ?";
                $cmdGetOpenClickTableName   =   $bdd->prepare($sqlGetOpenClickTableName);
                $cmdGetOpenClickTableName->execute(array($id_vertical));
                $verticalInfos              =   $cmdGetOpenClickTableName->fetch();
                $old_open_table_name        =   $verticalInfos['name_open_table'];
                $old_click_table_name       =   $verticalInfos['name_click_table'];
                $cmdGetOpenClickTableName->closeCursor();
                
                //2- Update vertical into table 'vertical' :
                $rep=bd::query("UPDATE vertical SET  name_vertical='$name_vertical', name_open_table='$name_open_table', name_click_table='$name_click_table' WHERE id_vertical = $id_vertical");
                
                
                
                //3- Rename Open Table :
                $sqlRenameOpenTable    =    
                "
                    RENAME TABLE $old_open_table_name TO $name_open_table
                ";
                $bdd_data->exec($sqlRenameOpenTable);
            
            
                //4- Rename Click Table :
                $sqlRenameClickTable    =    
                "
                    RENAME TABLE $old_click_table_name TO $name_click_table
                ";
                $bdd_data->exec($sqlRenameClickTable);
                
                
            if(!$rep)
            {
                $ok=2; 
            }
        }
        else
        {
            $ok=1; 
        }
            
        echo $ok;
    }
   
   
