<?php

/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 1.4                                                               |
// +---------------------------------------------------------------------------+
// | directory.php                                                             |
// |                                                                           |
// | Directory of all the stories on a Geeklog site.                           |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2004-2006 by the following authors:                         |
// |                                                                           |
// | Authors: Dirk Haun         - dirk AT haun-online DOT de                   |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | as published by the Free Software Foundation; either version 2            |
// | of the License, or (at your option) any later version.                    |
// |                                                                           |
// | This program is distributed in the hope that it will be useful,           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
// | GNU General Public License for more details.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+
//
// $Id: directory.php,v 1.13 2006/12/11 11:49:17 dhaun Exp $
//@@@@@20060419 update year month display format
//     (DIR_displayMonth,DIR_displayYear MAIN)
require_once ('lib-common.php');

// configuration option:
// List stories for the current month on top of the overview page
// (if set = true)
$conf_list_current_month = false;

// name of this script
define ('THIS_SCRIPT', 'directory.php');

$display = '';

if (empty ($_USER['username']) && (($_CONF['loginrequired'] == 1) ||
                                   ($_CONF['directoryloginrequired'] == 1))) {
    $display = COM_siteHeader ('menu', $LANG_DIR['title']);
    $display .= COM_startBlock ($LANG_LOGIN[1], '',
                                COM_getBlockTemplate ('_msg_block', 'header'));
    $login = new Template ($_CONF['path_layout'] . 'submit');
    $login->set_file (array ('login' => 'submitloginrequired.thtml'));
    $login->set_var ('site_url', $_CONF['site_url']);
    $login->set_var ('layout_url', $_CONF['layout_url']);
    $login->set_var ('login_message', $LANG_LOGIN[2]);
    $login->set_var ('lang_login', $LANG_LOGIN[3]);
    $login->set_var ('lang_newuser', $LANG_LOGIN[4]);
    $login->parse ('output', 'login');
    $display .= $login->finish ($login->get_var ('output'));
    $display .= COM_endBlock (COM_getBlockTemplate ('_msg_block', 'footer'));
    $display .= COM_siteFooter ();

    // Added by Phize <----
    if (function_exists( 'CUSTOM_applyFilter' )) {
        $display = CUSTOM_applyFilter('htmlBeforeRendered', $display);
    }
    // Added by Phize ---->

    echo $display;
    exit;
}

/**
* Helper function: Calculate last day of a given month
*
* @param    int     $month  Month
* @param    int     $year   Year
* @return   int             Number of days in that month
* @bugs     Will fail from 2038 onwards ...
*
* "The last day of any given month can be expressed as the "0" day
* of the next month", http://www.php.net/manual/en/function.mktime.php
*
*/
function DIR_lastDayOfMonth ($month, $year)
{
    $month++;
    if ($month > 12) {
        $month = 1;
        $year++;
    }

    $lastday = mktime (0, 0, 0, $month, 0, $year);

    return intval(strftime('%d', $lastday));
}

/**
* Display a topic selection drop-down menu
*
* @param    string  $topic          current topic
* @param    int     $year           current year
* @param    int     $month          current month
* @param    bool    $standalone     true: don't display form inline
*
*/
function DIR_topicList ($topic = 'all', $year = 0, $month = 0, $standalone = false)
{
    global $_CONF, $LANG21;

    // Added by Phize <----
    global $_TABLES;

    $value_class = 'navigation category navigation-category';
    $value_class_current = 'current';
    $value_class_first = 'first';
    $value_class_last = 'last';
    // Added by Phize ---->

    $retval = '';

    $url = $_CONF['site_url'] . '/' . THIS_SCRIPT;

    // Added by Phize <----
    $retval .= '<ul class="' . $value_class . '">';

    $tmp = str_replace( 'DISTINCT ', '', 'tid,topic' );
    $select_set = explode( ',', $tmp );

    $sql = "SELECT tid,topic FROM {$_TABLES['topics']}";
    $permsql = COM_getPermSQL();
    if( empty( $permsql ))
    {
        $sql .= COM_getLangSQL( 'tid' );
    }
    else
    {
        $sql .= $permsql . COM_getLangSQL( 'tid', 'AND' );
    }
    $sql .=  " ORDER BY $select_set[1]";

    $result = DB_query( $sql );
    $nrows = DB_numRows( $result );

    if ($nrows == 0) {
        $last = ' ' . $value_class_last;
    }

    if ($topic == 'all') {
        $retval .= '<li class="' . $value_class_current . ' ' . $value_class_first . $last . '"><strong>' . $LANG21[7] . '</strong></li>';
    } else {
        $retval .= '<li class="' . $value_class_first . $last . '"><a href="' . $url . '?year=' . $year . '&amp;month=' . $month . '&amp;topic=all">' . $LANG21[7] . '</a></li>';
    }

    for( $i = 0; $i < $nrows; $i++ )
    {
        $A = DB_fetchArray( $result, true );

        $last = '';

        if ($i == ($nrows - 1)) {
            $last = $value_class_last;
        }

        if( $A[0] == $topic )
        {
            $retval .= '<li class="' . $value_class_current . ' ' . $last . '"><strong>' . $A[1] . '</strong></li>';
        } else {
            if ($i == ($nrows - 1)) {
                $retval .= '<li class="' . $last . '">';
            } else {
                $retval .= '<li>';
            }

            $retval .= '<a href="' . $url . '?year=' . $year . '&amp;month=' . $month . '&amp;topic=' . $A[0] . '">' . $A[1] . '</a></li>';
        }
    }

    $retval .= '</ul>';
    // Added by Phize ---->


//    $retval .= '<form action="' . $url . '" method="POST"';
//    if (!$standalone) {
//        $retval .= ' style="display:inline; float:right"' . LB;
//    }
//    $retval .= '>' . LB;
//    $retval .= '<select name="topic" onchange="this.form.submit()">' . LB;
//    $retval .= '<option value="all"';
//    if ($topic == 'all') {
//        $retval .= ' selected="selected"';
//    }
//    $retval .= '>' . $LANG21[7] . '</option>' . LB;
//    $retval .= COM_topicList ('tid,topic', $topic);
//    $retval .= '</select>' . LB;
//    $retval .= '<input type="hidden" name="year" value="' . $year . '">';
//    $retval .= '<input type="hidden" name="month" value="' . $month . '">';
//    $retval .= '</form>' . LB;

    return $retval;
}

/**
* Build link to a month's page
*
* @param    string  $topic  current topic
* @param    int     $year   year to link to
* @param    int     $month  month to link to
* @param    int     $count  number of stories for that month (may be 0)
* @return   string          month name + count, as link or plain text
*
*/
function DIR_monthLink ($topic, $year, $month, $count)
{
    global $_CONF, $LANG_MONTH;

    $retval = '';

    if ($count > 0) {
        $retval .= '<a href="' . COM_buildUrl ($_CONF['site_url'] . '/'
                . THIS_SCRIPT . '?topic=' . urlencode ($topic) . '&amp;year='
                . $year . '&amp;month=' . $month) . '">';
    }

    // Changed by Phize <----
    $retval .= $LANG_MONTH[$month];
    // Changed by Phize ---->

    if ($count > 0) {
        $retval .= '</a>';
    }

    // Changed by Phize <----
    $retval .= '(' . COM_numberFormat ($count) . ')' . LB;
    // Changed by Phize ---->

    return $retval;
}

/**
* Display navigation bar
*
* @param    string  $topic  current topic
* @param    int     $year   current year
* @param    int     $month  current month (or 0 for year view pages)
* @return   string          navigation bar with prev, next, and "up" links
*
*/
function DIR_navBar ($topic, $year, $month = 0)
{
    global $_CONF, $_TABLES, $LANG05, $LANG_DIR;

    // Added by Phize <----
    $value_class = 'navigation page navigation-page';
    $value_class_first = 'first';
    $value_class_last = 'last';
    // Added by Phize ---->

    $retval = '';

    // Changed by Phize <----
    $retval .= '<ul class="' . $value_class . '">';
    // Changed by Phize ---->

    if ($month == 0) {
        $prevyear = $year - 1;
        $nextyear = $year + 1;
    } else {
        $prevyear = $year;
        $prevmonth = $month - 1;
        if ($prevmonth == 0) {
            $prevmonth = 12;
            $prevyear--;
        }
        $nextyear = $year;
        $nextmonth = $month + 1;
        if ($nextmonth > 12) {
            $nextmonth = 1;
            $nextyear++;
        }
    }

    $result = DB_query ("SELECT MIN(YEAR(date)) AS year FROM {$_TABLES['stories']}");
    $A = DB_fetchArray ($result);
    if ($prevyear < $A['year']) {
        $prevyear = 0;
    }

    $currentyear = date ('Y', time ());
    if ($nextyear > $currentyear) {
        $nextyear = 0;
    }

    if ($prevyear > 0) {
        $url = $_CONF['site_url'] . '/' . THIS_SCRIPT . '?topic='
             . urlencode ($topic) . '&amp;year=' . $prevyear;
        if ($month > 0) {
            $url .= '&amp;month=' . $prevmonth;
        }

        // Changed by Phize <----
        $retval .= '<li class="' . $value_class_first . '"><a href="' . COM_buildUrl ($url) . '">' . $LANG05[6]
                . '</a></li>';
        // Changed by Phize ---->

    } else {
        $retval .= $LANG05[6];
    }

    // Changed by Phize <----
    //$retval .= ' | ';
    // Changed by Phize ---->

    $url = $_CONF['site_url'] . '/' . THIS_SCRIPT;
    if ($topic != 'all') {
        $url = COM_buildUrl ($url . '?topic=' . urlencode ($topic));
    }

    // Changed by Phize <----
    $retval .= '<li><a href="' . $url . '">' . $LANG_DIR['nav_top'] . '</a></li>';

    //$retval .= ' | ';
    // Changed by Phize ---->

    if ($nextyear > 0) {
        $url = $_CONF['site_url'] . '/' . THIS_SCRIPT . '?topic='
             . urlencode ($topic) . '&amp;year=' . $nextyear;
        if ($month > 0) {
            $url .= '&amp;month=' . $nextmonth;
        }

        // Changed by Phize <----
        $retval .= '<li class="' . $value_class_last . '"><a href="' . COM_buildUrl ($url) . '">' . $LANG05[5]
                . '</a></li>';
        // Changed by Phize ---->
    } else {
        $retval .= $LANG05[5];
    }

    // Changed by Phize <----
    $retval .= '</ul>' . LB;
    // Changed by Phize ---->

    return $retval;
}

/**
* Display month view
*
* @param    string  $topic  current topic
* @param    int     $year   year to display
* @param    int     $month  month to display
* @param    bool    $main   true: display view on its own page
* @return   string          list of articles for the given month
*
*/
function DIR_displayMonth ($topic, $year, $month, $main = false)
{
    global $_CONF, $_TABLES, $LANG_MONTH, $LANG_DIR;

    $retval = '';

    //@@@@@20060419update----->
    //if ($main) {
    //    $retval .= '<div><h1 style="display:inline">' . $LANG_MONTH[$month]
    //            . ' ' . $year . '</h1> ' . DIR_topicList ($topic, $year, $month)
    //            . '</div>' . LB;
    //} else {
    //    $retval .= '<h1>' . $LANG_MONTH[$month] . ' ' . $year . '</h1>' . LB;
    //}
    $wk=sprintf ($LANG_DIR['title_month_year'],$year,$month);

    // Changed by Phize <----
    if ($main) {
        $retval .= '<div class="section directory section-directory"><h2 class="title section-title directory-title">';
        $retval .= $wk;
        $retval .= '</h2><div class="text section-text directory-text">' . DIR_topicList ($topic, $year, $month) . LB;
    } else {
        $retval .= '<div class="section directory section-directory"><h2 class="title section-title directory-title">';
        $retval .= $wk;
        $retval .= '</h2>' . LB;
    }
    // Changed by Phize ---->
    //@@@@@20060419update<-----

    $start = sprintf ('%04d-%02d-01 00:00:00', $year, $month);
    $lastday = DIR_lastDayOfMonth ($month, $year);
    $end   = sprintf ('%04d-%02d-%02d 23:59:59', $year, $month, $lastday);

    $sql = "SELECT sid,title,UNIX_TIMESTAMP(date) AS day,DATE_FORMAT(date, '%e') AS mday FROM {$_TABLES['stories']} WHERE (date >= '$start') AND (date <= '$end') AND (draft_flag = 0) AND (date <= NOW())";
    if ($topic != 'all') {
        $sql .= " AND (tid = '$topic')";
    }
    $sql .= COM_getTopicSql ('AND') . COM_getPermSql ('AND')
         . COM_getLangSQL ('sid', 'AND') . " ORDER BY date ASC";

    $result = DB_query ($sql);
    $numrows = DB_numRows ($result);

    if ($numrows > 0) {
        // Added by Phize <----
        if (!$main) {
            $retval .= '<div class="text section-text">';
        }
        // Added by Phize ---->

        $entries = array ();
        $mday = 0;

        for ($i = 0; $i < $numrows; $i++) {
            $A = DB_fetchArray ($result);

            if ($mday != $A['mday']) {
                if (sizeof ($entries) > 0) {
                    // Changed by Phize <----
                    $retval .= '<div class="text section-text">' . COM_makeList ($entries) . '</div></div>';
                    // Changed by Phize ---->

                    $entries = array ();
                }

                $curtime = COM_getUserDateTimeFormat ($A['day']);
                $day = $curtime[0];

                $day = strftime ($_CONF['shortdate'], $A['day']);

                // Changed by Phize <----
                $retval .= '<div class="section"><h3 class="title section-title">' . $day . '</h3>' . LB;
                // Changed by Phize ---->

                $mday = $A['mday'];
            }

            $url = COM_buildUrl ($_CONF['site_url'] . '/article.php?story='
                                 . $A['sid']);
            $entries[] = '<a href="' . $url . '">' . stripslashes ($A['title'])
                       . '</a>';
        }

        if (sizeof ($entries) > 0) {
            // Changed by Phize <----
            $retval .= '<div class="text section-text">' . COM_makeList ($entries) . '</div></div></div>';
            // Changed by Phize ---->
        }

    } else {
        // Changed by Phize <----
        if ($main) {
            $retval .= '<p>' . $LANG_DIR['no_articles'] . '</p></div>';
        } else {
            $retval .= '<div class="text directory-text"><p>' . $LANG_DIR['no_articles'] . '</p></div>';
        }
        // Changed by Phize ---->
    }

    // Changed by Phize <----
    $retval .= '</div>' . LB;
    // Changed by Phize ---->

    return $retval;
}

/**
* Display year view
*
* @param    string  $topic  current topic
* @param    int     $year   year to display
* @param    bool    $main   true: display view on its own page
* @return   string          list of months (+ number of stories) for given year
*
*/
function DIR_displayYear ($topic, $year, $main = false) 
    {
    global $_CONF, $_TABLES, $LANG_MONTH, $LANG_DIR;

    $retval = '';

    //@@@@@20060419update----->
    //if ($main) {
    //    $retval .= '<div><h1 style="display:inline">' . $year . '</h1> '
    //            . DIR_topicList ($topic, $year) . '</div>' . LB;
    //} else {
    //    $retval .= '<h2>' . $year . '</h2>' . LB;
    //}

    // Changed by Phize <----
    if ($main) {
        $retval .= '<div class="section directory section-directory"><h2 class="title section-title directory-title">';
        $retval .= sprintf ($LANG_DIR['title_year'], $year);
        $retval .=  '</h2><div class="text section-title directory-text">';
        $retval .=  DIR_topicList ($topic, $year) . LB;
    } else {
        $retval .= '<div class="section"><h3 class="title section-title">';
        $retval .= sprintf ($LANG_DIR['title_year'], $year);
        $retval .= '</h3>' . LB;
    }
    // Changed by Phize ---->
    //@@@@@20060419update<-----

    $currentyear = date ('Y', time ());
    $currentmonth = date ('m', time ());

    $start = sprintf ('%04d-01-01 00:00:00', $year);
    $end   = sprintf ('%04d-12-31 23:59:59', $year);

    $monthsql = array();
    $monthsql['mysql'] = "SELECT DISTINCT MONTH(date) AS month,COUNT(*) AS count FROM {$_TABLES['stories']} WHERE (date >= '$start') AND (date <= '$end') AND (draft_flag = 0) AND (date <= NOW())";
    $monthsql['mssql'] = "SELECT MONTH(date) AS month,COUNT(sid) AS count FROM {$_TABLES['stories']} WHERE (date >= '$start') AND (date <= '$end') AND (draft_flag = 0) AND (date <= NOW())";
    if ($topic != 'all') {
        $monthsql['mysql'] .= " AND (tid = '$topic')";
        $monthsql['mssql'] .= " AND (tid = '$topic')";
    }
    $monthsql['mysql'] .= COM_getTopicSql ('AND') . COM_getPermSql ('AND')
              . COM_getLangSQL ('sid', 'AND');
    $monthsql['mssql'] .= COM_getTopicSql ('AND') . COM_getPermSql ('AND')
              . COM_getLangSQL ('sid', 'AND');

    $msql = array();
    $msql['mysql'] = $monthsql['mysql'] . " GROUP BY MONTH(date) ORDER BY date ASC";
    $msql['mssql'] = $monthsql['mssql'] . " GROUP BY MONTH(date) ORDER BY month(date) ASC";

    $mresult = DB_query ($msql);
    $nummonths = DB_numRows ($mresult);

    if ($nummonths > 0) {
        // Added by Phize <----
        if (!$main) {
            $retval .= '<div class="text section-text">';
        }
        // Added by Phize ---->

        $retval .= '<ul>' . LB;
        $lastm = 1;
        for ($j = 0; $j < $nummonths; $j++) {
            $M = DB_fetchArray ($mresult);

            for (; $lastm < $M['month']; $lastm++) {
                $retval .= '<li>' . DIR_monthLink ($topic, $year, $lastm, 0)
                        . '</li>';
            }
            $lastm = $M['month'] + 1;

            $retval .= '<li>' . DIR_monthLink ($topic, $year, $M['month'],
                                               $M['count']) . '</li>';
        }

        if ($year == $currentyear) {
            $fillm = $currentmonth;
        } else {
            $fillm = 12;
        }

        if ($lastm <= $fillm) {
            for (; $lastm <= $fillm; $lastm++) {
                $retval .= '<li>' . DIR_monthLink ($topic, $year, $lastm, 0)
                        . '</li>';
            }
        }

        // Changed by Phize <----
        $retval .= '</ul></div>' . LB;
        // Changed by Phize ---->
    } else {
        // Changed by Phize <----
        if ($main) {
            $retval .= '<p>' . $LANG_DIR['no_articles'] . '</p></div>';
        } else {
            $retval .= '<div class="text section-text"><p>' . $LANG_DIR['no_articles'] . '</p></div>';
        }
        // Changed by Phize ---->
    }

    // Changed by Phize <----
    $retval .= '</div>' . LB;
    // Changed by Phize ---->

    return $retval;
}

/**
* Display main view (list of years)
*
* Displays an overview of all the years and months, starting with the first
* year for which a story has been posted. Can optionally display a list of
* the stories for the current month at the top of the page.
*
* @param    string  $topic                  current topic
* @param    bool    $list_current_month     true = list stories f. current month
* @return   string                          list of all the years in the db
*
*/
function DIR_displayAll ($topic, $list_current_month = false)
{
    global $_TABLES, $LANG_DIR;

    $retval = '';

    if ($list_current_month) {
        $currentyear = date ('Y', time ());
        $currentmonth = date ('n', time ());

        $retval .= DIR_displayMonth ($topic, $currentyear, $currentmonth);

        // Changed by Phize <----
        $retval .= LB;
        // Changed by Phize ---->
    }

    // Changed by Phize <----
    $retval .= '<div class="section directory section-directory"><h2 class="title section-title directory-title">' . $LANG_DIR['title']
            . '</h2><div class="text section-text directory-text">' . DIR_topicList ($topic) . LB;
    // Changed by Phize ---->

    $yearsql = array();
    $yearsql['mysql'] = "SELECT DISTINCT YEAR(date) AS year,date FROM {$_TABLES['stories']} WHERE (draft_flag = 0) AND (date <= NOW())" . COM_getTopicSql ('AND') . COM_getPermSql ('AND')  . COM_getLangSQL ('sid', 'AND');
    $yearsql['mssql'] = "SELECT YEAR(date) AS year FROM {$_TABLES['stories']} WHERE (draft_flag = 0) AND (date <= NOW())" . COM_getTopicSql ('AND') . COM_getPermSql ('AND')  . COM_getLangSQL ('sid', 'AND');
    $ysql = array();
    $ysql['mysql'] = $yearsql['mysql'] . " GROUP BY YEAR(date) ORDER BY date DESC";
    $ysql['mssql'] = $yearsql['mssql'] . " GROUP BY YEAR(date) ORDER BY YEAR(date) DESC";

    $yresult = DB_query ($ysql);
    $numyears = DB_numRows ($yresult);

    for ($i = 0; $i < $numyears; $i++) {
        $Y = DB_fetchArray ($yresult);

        $retval .= DIR_displayYear ($topic, $Y['year']);
    }

    // Added by Phize <----
    $retval .= '</div></div>';
    // Added by Phize ---->

    return $retval;
}

// MAIN
$display = '';

if (isset ($_POST['topic']) && isset ($_POST['year']) && isset ($_POST['month'])) {
    $topic = $_POST['topic'];
    $year = $_POST['year'];
    $month = $_POST['month'];
} else {
    COM_setArgNames (array ('topic', 'year', 'month'));
    $topic = COM_getArgument ('topic');
    $year = COM_getArgument ('year');
    $month = COM_getArgument ('month');
}

$topic = COM_applyFilter ($topic);
if (empty ($topic)) {
    $topic = 'all';
}
$year = COM_applyFilter ($year, true);
if ($year < 0) {
    $year = 0;
}
$month = COM_applyFilter ($month, true);
if (($month < 1) || ($month > 12)) {
    $month = 0;
}

if (($year != 0) && ($month != 0)) {
    //@@@@@20060419upadate ---->
    //$title = sprintf ($LANG_DIR['title_month_year'],
    //                  $LANG30[$month + 12], $year);
    $title = sprintf ($LANG_DIR['title_month_year'],
                        $year,  $month);
    //@@@@@20060419upadate <----
    $display .= COM_siteHeader ('menu', $title);
    $display .= DIR_displayMonth ($topic, $year, $month, true);
    $display .= DIR_navBar ($topic, $year, $month);
} else if ($year != 0) {
    $title = sprintf ($LANG_DIR['title_year'], $year);
    $display .= COM_siteHeader ('menu', $title);
    $display .= DIR_displayYear ($topic, $year, true);
    $display .= DIR_navBar ($topic, $year);
} else {
    $display .= COM_siteHeader ('menu', $LANG_DIR['title']);
    $display .= DIR_displayAll ($topic, $conf_list_current_month);
}

$display .= COM_siteFooter (true);

// Added by Phize <----
if (function_exists( 'CUSTOM_applyFilter' )) {
    $display = CUSTOM_applyFilter('htmlBeforeRendered', $display);
}
// Added by Phize ---->

echo $display;

?>
