//=-- DateEnumPlugin.cpp - Enumerate and link to pages by date ---------------=
//
//  PLUGIN DESCRIPTION:
//    The DateEnumPlugin is useful on pages that are monthly or yearly pages.
//    it links to all of the pages that exist for the days or months in the
//    scope of the time period.
//
//  PLUGIN AUTHOR:
//    Chris Lattner, Author of Magicstats  (sabre@magic.hurrah.com)
//
//  PLUGIN HOMEPAGE:
//     http://magic.hurrah.com/~sabre/MagicStats/
//
//  PLUGIN VERSION / COMPATIBILITY:
//     DateEnumPlugin v1.0 / MagicStats 2.0+
//
//=---------------------------------------------------------------------------=
//  This file is copyright (c) 1997-1998 Chris Lattner
//=---------------------------------------------------------------------------=

#include <fstream.h>          // We need file streams...
#include "PagePlugin.h"
#include "Options.h"
#include "Table.h"
#include "VarTable.h"

class DateEnumPlugin : public PluginTemplate<PagePlugin,DateEnumPlugin> {
public:
  inline static unsigned int GetCurrentVersion() {
    return 100;                       // Version 1.00
  }

  inline static const char *GetPluginName() {
    return "DateEnum";
  }

  DateEnumPlugin(int &CE) {
    Flags = FIgnoreErrors | FNoAccesses;
    CE = 0;
  }

  // The full name consists of all of the data settings that alter the data 
  // that is kept track of and stored.
  //
  void GetFullName(VarTable &Params, String &fullName, int updateFreq) {
    fullName = String::IntToStr(updateFreq) + Name;
  }
  
  void OutputHTML(ostream &O, VarTable &Params) {
    MSFilename PageLink = Params["PageLink"]->GetStringValue();
    MSFilename PageURL  = Params["PageURL"]->GetStringValue();
    String TempStr;
    int i, j;
    Date D;
    
    switch (UpdateFreq) {
    case Plugin::UpdateAll:
    case Plugin::UpdateDaily:
    default:
      O << "<b>Error! DateEnumPlugin works only with Weekly, Monthly, and "
           "Yearly UpdateFrequencies!</b>\n";
      return;
      
      // UpdateWeekly - List the days of the week and link to the appropriate 
      //   pages if they are available
      //
    case Plugin::UpdateWeekly:
      D.strftime(TempStr, "%w");
      D = D - TempStr.atoi();
      
      for (i = 0; i < 7; i++) {
        MSFilename TempLink = PageLink.EvaluateTokens(D);
        String     TempURL  = PageURL .EvaluateTokens(D);
        
        if (TempLink.Exists())
          O << "<a href=\"" << TempURL << "\">" << TempStr << "</a>\n";
        else
          O << TempStr << " ";

        D = D + 1;
      }
      break;
      
      // UpdateMonthly - List the days of the month and link to them if 
      // available
      //
    case Plugin::UpdateMonthly:
      D = MSOptions::CurDate;
      j = D.GetDaysInMonth();       // Number of days in the current month
      for (i = 0; i < j; i++) {
        D.SetDay(i+1);
        MSFilename TempLink = PageLink.EvaluateTokens(D);
        String     TempURL  = PageURL .EvaluateTokens(D);

        if (TempLink.Exists())
          O << "<a href=\"" << TempURL << "\">" << i+1 << "</a>\n";
        else 
          O << i+1 << " ";
      }
      break;
      
      // UpdateYearly - List the months of the year and link to them if 
      // available...
      //
/*    case Plugin::UpdateYearly:
      break;*/
    }
  }
};

INIT_PLUGIN(DateEnumPlugin);

