<?php
    
    //error_reporting(-1);
    //ini_set('display_errors', 'On');

    include_once '../scripts/bd.php';
    include('../scripts/bdd.php');
    
    $track  =   $_GET["track"];
    $query  =   bd::query("SELECT * FROM redirect WHERE track like '$track'");
    $row    =   mysql_fetch_array($query);
    
    if($row!=null)
    {
        
        if($row['id_campaign']!="0" && $_GET["id_user"]!="0")
        {
            $date_click =   date('Y-m-d h:i:s');
            $ip         =   $_SERVER['REMOTE_ADDR'];
            bd::query("INSERT INTO track_click VALUES(NULL,'{$row['id']}','{$_GET["id_campaign"]}','{$_GET["id_list"]}','{$_GET["id_user"]}','$ip','0','$date_click')");
        
            
            if($row['tag']=='[LandingPage]')
            {
                if( ($_GET["id_list"] > 0) && ($_GET["id_list"] != 9) && ($_GET["id_user"] > 0) && ($row['id_offer']>0) )
                {
                    $id_list    =   $_GET["id_list"];
                    $id_user    =   $_GET["id_user"];
                    $id_offer   =   $row['id_offer'];
                    
                    
                     echo    'le id du destinataire vaut : '.$id_user."<br/>";


                    //1- Get List Infos :
                    $sqlTableInfos  =
                    "
                        SELECT  data_table 
                        FROM    data_list 
                        WHERE   id  =   ?
                    ";

                    $cmdTableInfos  =   $bdd->prepare($sqlTableInfos);
                    $cmdTableInfos->execute(array($id_list));
                    $res            =   $cmdTableInfos->fetch();
                    $tableName      =   $res['data_table'];
                    $cmdTableInfos->closeCursor();
                        
                     echo    'Tablename vaut : '.$tableName."<br/>";
                
                    //2- Get Email Infos :
                    $sqlEmailInfos  =
                    "
                        SELECT  *
                        FROM    $tableName
                        WHERE   id  =   ?
                    ";
                    $cmdEmailInfos  =   $bdd->prepare($sqlEmailInfos);
                    $cmdEmailInfos->execute(array($id_user));
                    $user           =   $cmdEmailInfos->fetch();
                    $cmdEmailInfos->closeCursor();
                    
                
                    
                      
                    
                    //3- Get the vertical click table name :
                    $sqlVerticalClickTable      =
                    "
                        SELECT  name_click_table
                        FROM    vertical
                        WHERE   id_vertical     =   (select id_vertical from offer where id = ?)
                    ";
                    $cmdVerticalClickTable      =   $bdd->prepare($sqlVerticalClickTable);
                    $cmdVerticalClickTable->execute(array($id_offer));
                    $vertical_row               =   $cmdVerticalClickTable->fetch();
                    $verticalClickTableName     =   $vertical_row['name_click_table'];
                    $cmdVerticalClickTable->closeCursor();
                    echo "le nom de la table du click est : $verticalClickTableName";
                   
                    
                    
                //4- Save in the vertical table :
                if($user && !empty($verticalClickTableName))
                {
                    $uniq_id        =   $user['unique_id'];
                    $email_address  =   $user['email_address'];
                    $email_md5      =   $user['email_md5'];

                    $tE = $bdd->prepare("SELECT email_address FROM ? WHERE email_address = ?");
                    $tE->execute(array($verticalClickTableName,$email_address));
                    if(!$tE->rowCount()) {
                        $sqlInsertUser  =
                        "
                            INSERT  INTO    $verticalClickTableName     VALUES (NULL,?,?,?);
                        ";
                        $cmdInsertUser  =   $bdd->prepare($sqlInsertUser);
                        $cmdInsertUser->execute(array($uniq_id,$email_address,$email_md5));
                        $cmdInsertUser->closeCursor();
                    }
                    
                }
                    
                }
            }
        }
        header("Location: {$row['link']} ");
    }
    else
    {
        header("Location: 404.html");
    }
    

    


