<?php
/*
PluginAdRotate
Copyright (C) 2005 Matthew Wood

This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
*/

include_once( PLOG_CLASS_PATH."class/dao/model.class.php");
include_once(PLOG_CLASS_PATH.
    "plugins/adrotate/class/dao/adrotateadstats.class.php");

class AdRotateAdsStats extends Model
{
  function AdRotateAdsStats()
  {
    $this->Model();
  }

  /**
   * stick a new ad into the db
   */
  function addAd(&$ad)
  {
    $prefix = $this->getPrefix();
    $q = "insert into {$prefix}adrotate_statistics ".
      "(imageurl,redirecturl,clickmd5,alttext,type,dateadded) values (".
      "'". Db::qstr($ad->getImageUrl()) ."',".
      "'". Db::qstr($ad->getRedirectUrl()) ."',".
      "'". md5(mt_rand()*7 . $ad->getRedirectUrl() . mt_rand()*7) ."',".
      "'". Db::qstr($ad->getAltText()) ."',".
      "'". Db::qstr($ad->getType()) ."',".
      "'". time() ."')";
    return $this->Execute($q);
  }


  /* append */
  function getAdByMD5($md5)
  {
    $prefix = $this->getPrefix();
    $q = "select * from {$prefix}adrotate_statistics ".
      "where clickmd5 = '". Db::qstr($md5)."'";

    $result  = $this->Execute( $q );

    if (!$result)
      return false;

    $row = $result->FetchRow();
    return $this->_generateAd($row);
  }
 	
	function incImpressions($adId)
	{
	  $prefix = $this->getPrefix();
		$q = "update {$prefix}adrotate_statistics ".
			"set impressions=impressions+1 where ".
			"id = '". Db::qstr($adId) ."'";
		$result = $this->Execute($q);
		if (!$result)
		  return false;
		else 
		  return true;
	}

	function incClicks($adId)
	{
	  $prefix = $this->getPrefix();
		$q = "update {$prefix}adrotate_statistics ".
			"set clicks=clicks+1 where ".
			"id = '". Db::qstr($adId) ."'";
		$result = $this->Execute($q);
		if (!$result)
      return false;
		else
      return true;
	}
	
  /* append */
  function getAd($adId)
  {
    $prefix = $this->getPrefix();
    $q = "select * from {$prefix}adrotate_statistics ".
      "where id = '". Db::qstr($adId)."'";

    $result  = $this->Execute( $q );

    if (!$result)
      return false;

    $row = $result->FetchRow();
    return $this->_generateAd($row);
  }
  /* delete */
  function deleteAd($adId)
  {
    $prefix = $this->getPrefix();
    $q = "delete from {$prefix}adrotate_statistics ".
      "where id='".Db::qstr($adId)."'";

    $result = $this->Execute($q);

    if ($result && 1 == $this->_db->Affected_Rows())
      return true;
    else
      return false;
  }

  /**
   * returns all the ads needed for statistics viewing
   */
  function getAllAds()
  {
    $prefix = $this->getPrefix();
    $qry = "select * from {$prefix}adrotate_statistics ".
      "order by impressions DESC";
    $res = $this->Execute($qry);
    if (!$res)
      return Array();
    $ads = Array();
    while ($row = $res->FetchRow())
    {
      $ads[] = $this->_generateAd($row);
    }
    return $ads;
  }

  /**
   * used to get a random ad on request
   */
  function getRandomAd()
  {
    $prefix = $this->getPrefix();
		$q = "select count(*) as total from {$prefix}adrotate_statistics";
		$res = $this->Execute($q);
		if (!$res)
      return Array();
		
		$row = $res->FetchRow();
		if (!$row)
      return Array();
		
		$rand = mt_rand(0,$row['total']-1);
		
    $q = "select * from {$prefix}adrotate_statistics ".
      "limit $rand,1";
    $res = $this->Execute($q);
    if (!$res)
      return Array();
    else
      return $this->_generateAd($res->FetchRow());
  }

  /* generates a new adstats class from the database row */
  function _generateAd($sqlRow)
  {
    $ad = new AdRotateAdStats($sqlRow['id'],
                              $sqlRow['impressions'],
                              $sqlRow['clicks'],
                              $sqlRow['imageurl'],
                              $sqlRow['redirecturl'],
                              $sqlRow['clickmd5'],
                              $sqlRow['alttext'],
                              $sqlRow['type'],
                              $sqlRow['dateadded']);
    return $ad;
  }
}
/* vim:ts=2:cin:expandtab:ai:
*/
?>
