<?php
/*
Plugin - PlogStaticRotator
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/plogstaticrotator/class/dao/plogstaticrotatorcontent.class.php");

class PlogStaticRotatorContents extends Model
{
  var $_prefix;

  function PlogRotatorContents()
  {
    $this->Model();
    $this->_prefix = $this->getPrefix();
  }
  function addContent(&$content)
  {
    $q = "insert into {$this->_prefix}plogstaticrotator_content ".
      "(blogid,title,content) values (".
      "'". Db::qstr($content->getBlogId()) ."',".
      "'". Db::qstr($content->getTitle()) ."',".
      "'". Db::qstr($content->getContent()) ."')";
    return $this->Execute($q);
  }
  function updateContent(&$content)
  {
    $q = "update {$this->_prefix}plogstaticrotator_content set ".
      "title='". Db::qstr($content->getTitle()) ."',".
      "content='". Db::qstr($content->getContent()) ."' ".
      "where id='". Db::qstr($content->getId()) ."' and ".
      "blogid='". Db::qstr($content->getBlogId()) ."'";
    return $this->Execute($q);
  }
  function getRandomContent($blogid)
  {
    $q = "select count(id) as total from ".
      "{$this->_prefix}plogstaticrotator_content where ".
      "blogid='". Db::qstr($blogid) ."'";
    $res = $this->Execute($q);
    if (!$res)
      return false;
    $row = $res->FetchRow();
    $rand = mt_rand(0,$row['total']-1);
    $q = "select id,blogid,title,content from ".
      "{$this->_prefix}plogstaticrotator_content where ".
      "blogid='". Db::qstr($blogid) ."' limit $rand,1";
    $res = $this->Execute($q);
    if (!$res)
      return false;
    $row = $res->FetchRow();
    if (!$row)
      return false;
    return $this->_content_from_sqlrow($row);
  }
  function getAllContent($blogid)
  {
    $q = "select id,blogid,title,content from ".
      "{$this->_prefix}plogstaticrotator_content where ".
      "blogid='". Db::qstr($blogid) ."'";
    $res = $this->Execute($q);
    if (!$res)
      return false;
    $contents = array();
    while ($row = $res->FetchRow())
      $contents[] = $this->_content_from_sqlrow($row);
    return $contents;
  }

  function deleteContent($dlid,$blogid)
  {
    $q = "delete from {$this->_prefix}plogstaticrotator_content where ".
      "blogid='". Db::qstr($blogid) ."' and id='". Db::qstr($dlid) ."'";
    $res = $this->Execute($q);
    
    if (!$res)
      return false;
    else
      return true;
  }
  function _content_from_sqlrow($row)
  {
    return new PlogStaticRotatorContent(
        $row['id'],$row['blogid'],$row['title'],$row['content']);
  }
}

/* vim:sw=2:ai:cin:expandtab:
*/
?>
