<?php
    include_once '../account/session.php';
    include_once '../../scripts/bd.php';
    
    if(isset($_GET['action']))
    {
        $action =   $_GET['action'];
        if($action=='add')add();
        if($action=='show')show();
        if($action=='get')get();
        if($action=='set')set();
        if($action=='set_active_on')set_active_on();
        if($action=='set_active_off')set_active_off();
        if($action=='get_sponsors')get_sponsors();
        
        if($action=='get_verticals')
            get_verticals();
        
    }
   
    function verify_sid($id,$sid,$id_sponsor)
    {
        $query = bd::query("SELECT * FROM offer where sid='$sid' && id_sponsor='$id_sponsor' && id!='$id'");
        if(mysql_num_rows($query)>0){
            return true;
        }
        return false;
    }
   
    function add()
    { 
        include_once '../../scripts/bdd.php';

        $id_sponsor     =   $_POST["id_sponsor"];
        $id_vertical    =   $_POST["id_vertical"];
        $sid            =   $_POST["sid"];
        $country            =   $_POST["country"];
        $fname            =   $_POST["fname"];
        $subject            =   $_POST["subject"];
        $name           =   addslashes($_POST["name"]);
        $page_link      =   $_POST["page_link"];
        $unsub_link     =   $_POST["unsub_link"];
        $active         =   0;
        ini_set("date.timezone", "Africa/Casablanca");
        $date_add       =   date('Y-m-d H:i:s');
        $date_set       =   date('Y-m-d H:i:s');

        $ok=0;
        if(!verify_sid('0',$sid,$id_sponsor))
        {
            //Creation de l'offre :
            $rep = $bdd->prepare("INSERT INTO offer(id_sponsor,id_vertical,sid,country,fname,subject,name,page_link,unsub_link,suppression_status,active,date_add,date_set) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
            $rep->execute(array($id_sponsor,$id_vertical,$sid,$country,$fname,$subject,$name,$page_link,$unsub_link,'',$active,$date_add,$date_set));
            $id_offer = $bdd->lastInsertId();
            
            //Creation du dossier de Supression :
            $cmd='mkdir /home/suppression/offer/'.$id_offer;
            exec($cmd,$output,$report);
            $cmd='chmod a+rwxt /home/suppression/offer/'.$id_offer;
            exec($cmd,$output,$report);
            if(!$rep)
            {
                $ok=2; 
            }
        }
        else
        {
            $ok=1; 
        }
        echo $ok;
    }
   
    function show()
    {
        update_suppression();
        $list   =   array();
        unset($_GET['action']);
        $sql = "SELECT id,sid,name,suppression_status,active FROM offer ";
        $i = 0;
        foreach ($_GET as $key => $value) {
            if(empty($value)) {
                unset($_GET[$key]);
            }
        }
        foreach ($_GET as $key => $value) {
            if($i > 0) {
                $where = null;
                $and = " AND ";
            } else {
                $where = "WHERE ";
                $and = null;
            }
            $sql .= $where.$and.$key."='".$value."'";
            $i++;
        }
        $data = bd::query($sql);
        while ($row = mysql_fetch_object($data)) 
        {
            $list[]=$row;
        }
        echo json_encode($list);
    }

    function update_suppression(){
        $data= bd::query("SELECT id FROM offer");
        $date_now=date('d F Y');
        while ($row = mysql_fetch_array($data)) {
            $filename = '/home/suppression/offer/'.$row[id].'/list';
            if (file_exists($filename)) {
                $date_suppression=date ('d F Y', filemtime($filename));
                $str = strtotime($date_now) - (strtotime($date_suppression));
                $days= floor($str/3600/24);
                $diff=10-$days;
                if($days<=5){
                    $value=  addslashes("<span class='badge badge-success'>".$date_suppression."</span> ");
                }
                if($days>5 && $days<=10){
                    $value=  addslashes("<span class='badge badge-warning'>".$date_suppression."</span> ");
                }
                if($days>10){
                    $value=  addslashes("<span class='badge badge-important'>".$date_suppression."</span> ");
                }
                $req= bd::query("UPDATE offer SET suppression_status='$value' WHERE id={$row[id]}");
            }else{
                $value=  addslashes("<span class='badge badge-inverse'>No Suppression</span> ");
                $req= bd::query("UPDATE offer SET suppression_status='$value' WHERE id={$row[id]}");
            }
        }
    }


    function get()
    { 
       $data = bd::query("SELECT * FROM offer WHERE id='{$_GET["id_offer"]}'");
       $row = mysql_fetch_array($data);
       return $row;
    }
   
    function set()
    { 
        include_once '../../scripts/bdd.php';
        $id_sponsor =   $_POST["id_sponsor"];
        $id_vertical=   $_POST["id_vertical"];
        $sid        =   $_POST["sid"];
        $country        =   $_POST["country"];
        $fname        =   $_POST["fname"];
        $subject        =   $_POST["subject"];
        $name       =   addslashes($_POST["name"]);
        $page_link  =   $_POST["page_link"];
        $unsub_link =   $_POST["unsub_link"];
        ini_set("date.timezone", "Africa/Casablanca");
        $date_set   =   date('Y-m-d h:i:s');
        
        $ok=0;

        if(!verify_sid($_POST["id"],$sid,$id_sponsor))
        {


            $req = $bdd->prepare("UPDATE offer SET id_sponsor = ?, id_vertical = ?, sid = ?, country = ?, fname = ?, subject = ?, name = ?, page_link = ?, unsub_link = ?, date_set = ? WHERE id = ?");

            $req->execute(array(
                $id_sponsor,
                $id_vertical,
                $sid,
                $country,
                $fname,
                $subject,
                $name,
                $page_link,
                $unsub_link,
                $date_set,
                $_POST["id"]
            ));

            if(!$req)
            {
               $ok=2; 
            }
        }
        else
        {
            $ok=1; 
        }
        echo $ok;
    }
   
    function set_active_on(){ 
        bd::query("UPDATE offer SET active= '1' WHERE id='{$_GET["id"]}'");
    }


   
    function set_active_off(){ 
        bd::query("UPDATE offer SET active= '0' WHERE id='{$_GET["id"]}'");
    }
   
    function get_sponsors(){
       $data=array();
       $query = bd::query("SELECT id,name FROM sponsor");
       while ($row = mysql_fetch_object($query)) {
           $data[]=$row;
       }
       echo json_encode($data);
    }
    
    
    
    function get_verticals()
    {
        include('../../scripts/bdd.php');
        $sqlGetVerticals    =   'SELECT id_vertical,name_vertical FROM vertical order by name_vertical ASC';

        $cmdGetVerticals    =   $bdd->prepare($sqlGetVerticals);
        $cmdGetVerticals->execute(array());
        $data               =   array();
        while ($row = $cmdGetVerticals->fetch()) 
        {
                $data[]     =   $row;
        }
        echo json_encode($data);
    }
    

