<?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/plugin/pluginbase.class.php");
include_once(PLOG_CLASS_PATH.
    "plugins/plogbirthdays/class/dao/plogbirthdays.class.php");

class PluginPlogBirthdays extends PluginBase
{
  function PluginPlogBirthdays()
  {
    $this->PluginBase();

    $this->id = "PlogBirthdays";
    $this->desc = "Allows plog users to display users birthdays in a cute display";
    $this->author = "woodzy";
    $this->locale = Array();

    $this->addMenuEntry("/menu/Manage","PlogBirthdays","");
    $this->addMenuEntry("/menu/Manage/PlogBirthdays","ViewBirthdayList",
        "?op=plogbirthdaysView");
    $this->addMenuEntry("/menu/Manage/PlogBirthdays","NewBirthdayEntry",
        "?op=plogbirthdaysNewEntryView");

    $this->registerAdminAction("plogbirthdaysView",
        "AdminPlogBirthdaysViewAction");
    $this->registerAdminAction("plogbirthdaysSearch",
        "AdminPlogBirthdaysSearchAction");
    $this->registerAdminAction('plogbirthdaysNewEntryView',
        "AdminPlogBirthdaysNewEntryViewAction");
    $this->registerAdminAction("plogbirthdaysNewEntry",
        "AdminPlogBirthdaysNewEntryAction");
    $this->registerAdminAction("plogbirthdaysDeleteEntry",
        "AdminPlogBirthdaysDeleteEntryAction");

    $this->_verifyTableStructure();
  }

  function _verifyTableStructure()
  {
    /* L = smallint in adodb dd speak */
    $fields = "
      bdid I4 NOTNULL PRIMARY AUTOINCREMENT,
      bdperson C(50) NOTNULL KEY,
      month L NOTNULL KEY,
      day L NOTNULL KEY,
      year I4 NOTNULL KEY,
      bdmessage C(200)";

    $db =& Db::getDb();
    $dbPrefix = Db::getPrefix();

    $tableName = $dbPrefix."plogbirthdays";

    $dict = NewPDbDataDictionary($db);

    $sqlAry = $dict->ChangeTableSQL($tableName,$fields);
    $result = $dict->ExecuteSQLArray($sqlAry);
    if (!$result)
      die("There was an error creating plogbirthday plugin tables!");
    
    return true;
  }
  function getTodaysBirthdays()
  {
    /* database stores month & day in mmdd format */
    $bdmodel = new PlogBirthdays();
    $bdms = $bdmodel->getTodaysBirthdays();
    
    $sentences = array();
    $curyear = date("Y");
    foreach($bdms as $bday)
    {
      $tmp = "Happy";
      if ($bday->getYear() != 0)
      {
        $age = $curyear - $bday->getYear();
        $tmp .= " $age";
      
        if ($age%10=='1')
          $tmp .="st";
        else if ($age%10=='2')
          $tmp .="nd";
        else if ($age%10=='3')
          $tmp .="rd";
        else
          $tmp .="th";
      }

      $tmp .= " Birthday to ". $bday->getName();
      if ($bday->getMessage() != "")
        $tmp .= " from ". $bday->getMessage();
      
      $sentences[] = $tmp .'!';
    }
    return $sentences;
  }
}

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