<?php
/**
 * Class represents records from table aff_lead
 * {autogenerated}
 * @property int $lead_id PK
 * @property int|null $aff_id
 * @property int $user_id FK user
 * @property datetime $time
 * @property int|null $banner_id FK aff3_banner
 * @property string|null $remote_addr
 * @property string|null $referer
 * @property datetime|null $first_visited
 * @property int|null $keyword_id FK aff_keyword
 * {/autogenerated}
 */
class AffLead extends Am_Record 
{
}

/** @extends Am_Table<AffLead> */
class AffLeadTable extends Am_Table 
{
    protected $_key = 'lead_id';
    protected $_table = '?_aff_lead';
    
    public function insert(array $values, $returnInserted = false)
    {
        if (empty($values['time']))
            $values['time'] = $this->getDi()->sqlDateTime;
        return parent::insert($values, $returnInserted);
    }
    
    function log($aff_id, $banner_id, $user_id, $aff_click_id = null)
    {
        $click = $this->getDi()->affClickTable->load($aff_click_id, false);
        if ($click)
        {
            $referer = $click->referer;
            $first_visited = $click->time;
            $keyword_id = $click->keyword_id;
        } else {
            $referer = null;
            $first_visited = null;
            $keyword_id = null;
        }
        $this->_db->query("INSERT INTO $this->_table
            SET aff_id=?d, 
            user_id=?, time=?, banner_id=?,
            remote_addr=?, referer=?, first_visited=?, keyword_id=?
            ", $aff_id, $user_id, 
            $this->getDi()->sqlDateTime,
            $banner_id, 
            $_SERVER['REMOTE_ADDR'], $referer, $first_visited, $keyword_id);
    }
}
