<?php

/**
 * Created by Sevio®.
 * All rights reserved to Sevio Solutions® SRL
 */
require_once("module_config.inc");
class class_campaigns
{
    protected $unique, $unique_user, $unique_admin, $unique_websites, $unique_countries, $unique_agents, $name, $campaign_cost, $budget, $budget_remain, $bid, $ad_type, $ad_destination, $ad_content, $html_content, $valid, $date,$limit,$limit_remain,$capping,$default;
    public function class_campaigns($unique)
    {
        $sql = mysql_query("SELECT * FROM `app_campaigns` WHERE `unique`='" . $unique . "'");
        $data = mysql_fetch_array($sql);
        if ($data) {
            $this->unique=$data['unique'];
            $this->unique_user = $data['unique_user'];
			
            $this->unique_admin = $data['unique_admin'];
            $this->unique_websites = $data['unique_tags'];
            $this->unique_countries = $data['unique_countries'];
            $this->unique_agents = $data['unique_agents'];
            $this->name = $data['name'];
            $this->campaign_cost = $data['campaign_cost'];
            $this->budget = $data['budget'];
            $this->budget_remain = $data['budget_remain'];
            $this->bid = $data['bid'];
            $this->ad_type = $data['ad_type'];
            $this->ad_destination = $data['ad_destination'];
            $this->ad_content = $data['ad_content'];
            $this->html_content = $data['html_content'];
            $this->valid = $data['valid'];
            $this->date = $data['date'];
            $this->limit=$data['limit'];
            $this->capping=$data['capping'];
            $this->default=$data['default'];
        }
    }
    static public function add($unique_user, $unique_websites, $unique_countries, $unique_agents, $name, $campaign_cost, $budget,$bid, $ad_type, $ad_destination, $ad_content, $html_content,$limit,$capping)
    {

        $key = rand(1,999).uniqid().rand(1,999);
        $user=new class_users($unique_user);
        if(!$html_content)
        {
			$target_dir = '../ads_data/';
            $extension=pathinfo($ad_content["name"],PATHINFO_EXTENSION);
            $target_dir .= $key.'.'.$extension;
            
                if (move_uploaded_file($ad_content["tmp_name"], $target_dir))
                    $ad_content=$key.'.'.$extension;
                   else return 0;

        }
        $sql = mysql_query("INSERT INTO `app_campaigns` (`unique`,`unique_user`,`unique_tags`,`unique_countries`,`unique_agents`,`name`,`ad_type`,`ad_destination`,`ad_content`,`html_content`,`valid`,`date`) VALUES ('".$key."','".$unique_user."','".$unique_websites."','".$unique_countries."','".$unique_agents."','".$name."','".$ad_type."','".$ad_destination."','".$ad_content."','".$html_content."','1','".date('Y-m-d')."')");
        return $key;
    }

	public function get_user_email()
    {
		$sql = mysql_query("SELECT `email` FROM `app_users` WHERE `unique`='" . $this->unique_user . "'");
        $data = mysql_fetch_array($sql);
        return $data[email];
    }

    public function get_unique()
    {
        return $this->unique;
    }
    public function get_unique_user()
    {
        return $this->unique_user;
    }

    public function get_unique_websites()
    {
        return $this->unique_websites;
    }
    public function get_unique_countries()
    {
        return $this->unique_countries;
    }
    public function get_unique_agents()
    {
        return $this->unique_agents;
    }
    public function get_name()
    {
        return $this->name;
    }
    public function get_ad_type()
    {
        return $this->ad_type;
    }
    public function get_ad_destination()
    {
        return $this->ad_destination;
    }
    public function get_ad_content()
    {
        return $this->ad_content;
    }
    public function get_html_content()
    {
        return $this->html_content;
    }
    public function get_valid()
    {
        return $this->valid;
    }
    public function get_date()
    {
        return $this->date;
    }
    public function set_unique_user($value)
    {
        $this->unique_user = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `unique_user`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }

    public function set_unique_admin($value)
    {
        $this->unique_admin = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `unique_admin`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_unique_websites($value)
    {
        $this->unique_websites = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `unique_tags`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_unique_countries($value)
    {
        $this->unique_countries = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `unique_countries`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_unique_agents($value)
    {
        $this->unique_agents = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `unique_agents`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_name($value)
    {
        $this->name = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `name`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }

    public function set_ad_type($value)
    {
        $this->ad_type = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `ad_type`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_ad_destination($value)
    {
        $this->ad_destination = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `ad_destination`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_ad_content($value)
    {
        if(!$this->html_content)
        {
			if($value["name"])
			{
				$target_dir = '../ads_data/';
            	$extension=pathinfo($value["name"],PATHINFO_EXTENSION);
            	$target_dir .= $this->unique.'.'.$extension;
              	  if (move_uploaded_file($value["tmp_name"], $target_dir))
                  	  $this->ad_content = $this->unique.'.'.$extension;
            	else return 0;
			}

			
        }
		else
		$this->ad_content = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `ad_content`='" . $this->ad_content . "' WHERE `unique`='" . $this->unique . "'");
        return 1;
    }
    public function set_html_content($value)
    {
        $this->html_content = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `html_content`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_valid($value,$unique_admin)
    {
        $this->valid = $value;
        $this->unique_admin=$unique_admin;
        $sql = mysql_query("UPDATE `app_campaigns` SET `valid`='" . $value . "',`unique_admin`='".$unique_admin."' WHERE `unique`='" . $this->unique . "'");
        $mail=new module_mail();
        if($value==3)
            $mail->send_email($this->unique_user,"Your campaign ".$this->name." has been denied!","Status updated on '".$this->name."' ","mail_notification_admin");
        elseif($value==1)
            $mail->send_email($this->unique_user,"Your campaign ".$this->name." has been approved!","Status updated on '".$this->name."' ","mail_notification_admin");
    }
    public function get_status()
    {
        if($this->valid==0)
            return "pending";
        elseif($this->valid==1)
            return "valid";
        elseif($this->valid==2)
            return "hold";
        elseif($this->valid==3)
            return "deny";
    }
    public function hold()
    {
        if($this->valid==2)
            $sql = mysql_query("UPDATE `app_campaigns` SET `valid`='1' WHERE `unique`='" . $this->unique . "'");
        elseif($this->valid==1)
            $sql = mysql_query("UPDATE `app_campaigns` SET `valid`='2' WHERE `unique`='" . $this->unique . "'");
        else return 0;
    }
    public function set_date($value)
    {
        $this->date = $value;
        $sql = mysql_query("UPDATE `app_campaigns` SET `date`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function del()
    {
        mysql_query("DELETE FROM `app_campaigns` WHERE `unique`='" . $this->unique . "' ");
    }

    public function plus_bid()
    {
        $this->bid=$this->bid+0.01;
        $sql = mysql_query("UPDATE `app_campaigns` SET `bid`='" . $this->bid . "' WHERE `unique`='" . $this->unique . "'");
        return $this->bid;
    }

    public function get_websites_name()
    {
        $array=array();
        if($this->unique_websites=="All")
        {
            $sql=mysql_query("SELECT `unique`,`website`,`name` FROM `app_tags` WHERE `ad_type` = '".$this->ad_type."'");
            while($data=mysql_fetch_array($sql))
                $array[$data['unique']]=$data['name'] ." - ".$data['website'];
            return $array;
        }else
        {
           $aux_cat=explode(',',$this->unique_websites);
            foreach($aux_cat as $websites_unique)
            {
                $sql=mysql_query("SELECT `website`,`name` FROM `app_tags` WHERE `unique`='".$websites_unique."'");
                while($data=mysql_fetch_array($sql))
                    $array[$websites_unique]=$data['name'] ." - ". $data['website'];
            }
            return $array;
        }
    }
}
?>