<?php
/*
Plugin - PlogBirthdays
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/plogbirthdays/class/dao/plogbirthday.class.php");

class PlogBirthdays extends Model
{
  var $_prefix;
  
  function PlogBirthdays()
  {
    $this->Model();
    $this->_prefix = $this->getPrefix();
  }
  function addBirthday(&$bday)
  {
    $q = "insert into {$this->_prefix}plogbirthdays ".
      "(bdperson,month,day,year,bdmessage) values (".
      "'". Db::qstr($bday->getName()) ."',".
      "'". Db::qstr($bday->getMonth()) ."',".
      "'". Db::qstr($bday->getDay()) ."',".
      "'". Db::qstr($bday->getYear()) ."',".
      "'". Db::qstr($bday->getMessage()) ."')";
    return $this->Execute($q);
  }

  function deleteBirthday($bid)
  {
    $q = "select * from {$this->_prefix}plogbirthdays ".
      "where bdid='". Db::qstr($bid) ."'";
    $res = $this->Execute($q);

    if (!($row = $res->FetchRow()))
      return false;
    else
    {
      $q = "delete from {$this->_prefix}plogbirthdays ".
        "where bdid='". Db::qstr($bid) ."'";
      $this->Execute($q);
      return true;
    }
  }

  function getTodaysBirthdays()
  {
    /* database stores month & day in mmdd format 
     * for easy sort and keying
     */
    $month = date("m");
    $day = date("d");
    $q = "select * from {$this->_prefix}plogbirthdays ".
      "where day=$day and month=$month";
    $res = $this->Execute($q);
    $bdays = array();
    while ($row = $res->FetchRow())
      $bdays[] = $this->_bday_from_sqlrow($row);
                                                                                
    return $bdays;
  }

  function getBirthdayCount()
  {
    $q = "select count(bdid) as total from {$this->_prefix}plogbirthdays";
    $res = $this->Execute($q);
    if (!$res)
      return false;
    $row = $res->FetchRow();
    return $row['total'];
    
  }

  function getBirthdays($selectnum,$multiplier)
  {
    $multiplier--;
    $q = "select * from {$this->_prefix}plogbirthdays ".
      "order by month,day,year limit ". $multiplier*$selectnum .",". 
      $selectnum;
    $res = $this->Execute($q);
    $bdays = array();
    while ($row = $res->FetchRow())
      $bdays[] = $this->_bday_from_sqlrow($row);
    return $bdays;
  }
  function searchBirthdays($selectnum,$multiplier,$nameTerms,$monthTerms,$dayTerms,$yearTerms,&$totalResults)
  {
    $multiplier--;
    $date = $monthTerms ."". $dayTerms ."". $yearTerms;
    $q = "select * from {$this->_prefix}plogbirthdays ".
      "where ". (($monthTerms != "")?("month=$monthTerms and "):"") .
      (($dayTerms != "")?("day=$dayTerms and "):"") .
      (($yearTerms != "")?("year=$yearTerms and "):"") .
      "bdperson like '%". Db::qstr($nameTerms) ."%' ".
      "order by month,day,year limit ". $multiplier*$selectnum .",".
      $selectnum;

    /* I couldn't figure out how to get the TRUE total count 
     * (b/c of limit clause) without this second query :(
     */
    $q2 = "select count(bdid) as total from {$this->_prefix}plogbirthdays ".
      "where ". (($monthTerms != "")?("month=$monthTerms and "):"") .
      (($dayTerms != "")?("day=$dayTerms and "):"") .
      (($yearTerms != "")?("year=$yearTerms and "):"") .
      "bdperson like '%". Db::qstr($nameTerms) ."%' ".
      "order by month,day,year";


    $res = $this->Execute($q);
    $res2 = $this->Execute($q2);
    
    $bdays = array();
    while ($row = $res->FetchRow())
      $bdays[] = $this->_bday_from_sqlrow($row);

    $total = $res2->FetchRow();

    $totalResults = $total['total'];

    return $bdays;
  }

  function _bday_from_sqlrow($row)
  {
    $bday = new PlogBirthday(
        $row['bdid'],
        $row['bdperson'],
        $row['month'],
        $row['day'],
        $row['year'],
        $row['bdmessage']);
    return $bday;
  }
}

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