<?php
/*
Plog Plugin - DigestView
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."class/dao/article.class.php");


class PluginDigestView extends PluginBase
{
  var $_resMax = -1;
  var $_userId = 0;
  function PluginDigestView()
  {
    $this->PluginBase();

    $this->id = "digestview";
    $this->desc = "Allows a template designer to access quick post summaries".
            " of categories and specific users whenever he/she wants";
    $this->author = "woodzy";
    $this->locale = Array();
  } 
  function setResultMax($maxReturn)
  {
    if (is_numeric($maxReturn))
      $this->_resMax = $maxReturn;
  }
  function setUserId($userId)
  {
    if (is_numeric($userId))
      $this->_userId = $userId;
  }
  function generateDigestByCategory($categoryId)
  {
    if (!is_numeric($maxReturn))
      $maxReturn = 4;
    if ($this->_resMax > 0)
      $maxReturn = $this->_resMax;
    
    /* replicating some code here... but only because its much faster than
     * instantiating a billion plog classes just to do this... 
     */
    $db =& Db::getDb();
    $prefix = Db::getPrefix();
    $blogInfo = $this->blogInfo;
    
    $sql="select a.id as id, l.category_id as category_id, ".
            "a.user_id as user_id, a.blog_id as blog_id, t.text as text, ".
            "t.topic as topic, a.slug as slug ".
         "from ". $prefix ."articles a, ". 
            "". $prefix ."article_categories_link l, ". 
            "". $prefix ."articles_text t ".
         "where ".
           "a.blog_id='". Db::qstr($blogInfo->getId()) ."' and ".
           "a.user_id='". Db::qstr($this->_userId) ."' and ".
           "l.category_id='". Db::qstr($categoryId) ."' and ".
           "a.id=l.article_id and a.id=t.id and a.status=1 ".
         "order by date DESC limit $maxReturn";
    $res = $db->Execute($sql);
    $articles = Array();
    if (!$res){return $articles; }
    while ($row = $res->FetchRow())
    {
      $tmp = new Article( $row['topic'],
                          $row['text'],
                          0,
                          $row['user_id'],
                          $row['blog_id'],
                          0,
                          0,
                          false,
                          $row['slug'],
                          $row['id']);
      $articles[] = $tmp;
    }
    return $articles;
  }
}

/* vim:ts=2:cin:expandtab:
*/
?>
