<?php
/*
   Plugin - PlogHaiku
   Copyright (C) 2005 Darren Greer
                                                                                   
   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/ploghaiku/class/dao/ploghaikuobject.class.php");

class PlogHaikuDao extends Model
{

  function PlogHaikuDao()
  {
    $this->Model();
    $this->_prefix = $this->getPrefix();
  }

  function _haiku_stats($blogId)
  {
    $q = "select count(*) as count from {$this->_prefix}ploghaiku where blog_id = ".$blogId;
    $res = $this->_db->Execute($q);
  
    if(!res) 
      return false;

    $row = $res->FetchRow();

    return $row;
  }

  function _haiku_stats_from_syl($syl,$blogId)
  {
    $q = "select count(*) as count from {$this->_prefix}ploghaiku ".
      "where syl='".$syl."' and blog_id = ".$blogId;
    $res = $this->_db->Execute($q);
  
    if(!res) 
      return false;

    $row = $res->FetchRow();

    return $row;
  }

  function _haiku_from_syl($syl,$blogId)
  {
    $q = "select * from {$this->_prefix}ploghaiku ".
      "where syl='".$syl."' ".
      "and blog_id = ".$blogId. " ".
      "order by rand() LIMIT 1";
    $res = $this->_db->Execute($q);
  
    if(!res) 
      return false;

    $row = $res->FetchRow();

    return $row;
  }

  function initializeHaiku( $blogId, $ownerId )
  {
    $q1 = "CREATE TABLE `{$this->_prefix}ploghaiku` (".
  "`id` int(11) NOT NULL auto_increment,".
  "`syl` int(11) NOT NULL default '0',".
  "`text` varchar(255) NOT NULL default '',".
  "`date` date NOT NULL default '0000-00-00',".
  "`owner_id` int(11) NOT NULL default '0',".
  "`blog_id` int(11) NOT NULL default '0',".
  "PRIMARY KEY  (`id`,`text`),".
  "UNIQUE KEY `text` (`text`)".
  ") TYPE=MyISAM AUTO_INCREMENT=424";

    $q2 = "insert into {$this->_prefix}ploghaiku ".
      "(syl,text,date,owner_id,blog_id) values (".
      "'5','everyone must start','2005-01-01','{$ownerId}','{$blogId}')";
    $q3 = "insert into {$this->_prefix}ploghaiku ".
      "(syl,text,date,owner_id,blog_id) values (".
      "'7','from a humble beginning','2005-01-01','{$ownerId}','{$blogId}')";
    $q4 = "insert into {$this->_prefix}ploghaiku ".
      "(syl,text,date,owner_id,blog_id) values (".
      "'5','to grow wise and strong','2005-01-01','{$ownerId}','{$blogId}')";
    $this->Execute($q1);
    $this->Execute($q2);
    $this->Execute($q3);
    $this->Execute($q4);
    return true;
  }

  function deleteHaiku(&$haikuId)
  {
    $q = "delete from {$this->_prefix}ploghaiku ".
      "where id = " . $haikuId;

    return $this->Execute($q);
  }

  function addHaiku(&$haikuObject)
  {
    $q = "insert into {$this->_prefix}ploghaiku ".
      "(syl,text,date,owner_id,blog_id) values (".
      "'". Db::qstr($haikuObject->getSyl()) . "',".
      "'". Db::qstr($haikuObject->getText()) . "',".
      "'". Db::qstr($haikuObject->getDate()) . "',".
      "'". Db::qstr($haikuObject->getOwnerId()) . "',".
      "'". Db::qstr($haikuObject->getBlogId()) . "')";
    return $this->Execute($q);
  }

  function getHaikuPermutations($blogId)
  {
    $row1 = $this->_haiku_stats_from_syl('5',$blogId); 
    $this->fiveCount = $row1['count'];
    $row2 = $this->_haiku_stats_from_syl('7',$blogId); 
    $this->sevenCount = $row2['count'];

    $this->permutations = ($this->fiveCount * $this->sevenCount * $this->fiveCount);
    return $this->permutations;
  }

  function getHaikuCount($blogId)
  {
    $row = $this->_haiku_stats($blogId); 
    $this->count = $row['count'];

    return $this->count;
  }

  function getHaikuStats($blogId)
  {
    $row1 = $this->_haiku_stats_from_syl('5',$blogId); 
    $this->fiveCount = $row1['count'];
    $row2 = $this->_haiku_stats_from_syl('7',$blogId); 
    $this->sevenCount = $row2['count'];

    $this->haikuStats = "5 Syllable: " . $this->fiveCount . "<br>\n".
			"7 Syllable: " . $this->sevenCount . "<br>\n";

    return $this->haikuStats;
  }

  function getHaiku($blogId)
  {
    $row1 = $this->_haiku_from_syl('5',$blogId); 
    $this->firstHaikuObject = $this->_haiku_from_sqlrow($row1);
    $row2 = $this->_haiku_from_syl('7',$blogId); 
    $this->secondHaikuObject = $this->_haiku_from_sqlrow($row2);
    $row3 = $this->_haiku_from_syl('5',$blogId); 
    $this->thirdHaikuObject = $this->_haiku_from_sqlrow($row3);

    $this->haiku = $this->firstHaikuObject->getText() . "<br>\n" . $this->secondHaikuObject->getText() . "<br>\n" . $this->thirdHaikuObject->getText();

    return $this->haiku;
  }

  function getHaikuList($selectnum,$multiplier,$blogId)
  {
    $multiplier--;
    $q = "select * from {$this->_prefix}ploghaiku ".
         "where blog_id = ".$blogId." ".
      "order by syl,text limit ". $multiplier*$selectnum .",".
      $selectnum;
    $res = $this->Execute($q);
    $haikuList = array();
    while ($row = $res->FetchRow())
      $haikuList[] = $this->_haiku_from_sqlrow($row);

    return $haikuList;
  }

  function _haiku_from_sqlrow(&$row)
  {
    if (!$row)
      return false;
    
    $haikuObject = new PlogHaikuObject( $row['id'],$row['syl'],$row['text'],
                              $row['date'],
			      $row['owner_id'],$row['blog_id']);
    return $haikuObject;
  }
}

?>
