root/trunk/plugins/altarchive_onemonth.php

Revision 689, 9.0 KB (checked in by michiel, 13 months ago)

stop showing 'go to previous month' links in the singlemonth
alternative montharchive sidebar when we are showing the month
of the oldest post in the database.

Closes #171

Line 
1<?php
2Class altarchive_onemonth extends MvBlog_plugin implements MvBlog_pluginiface {
3    /* variables */
4    public $name    = "altarchive_onemonth";
5    public $author  = "Michiel van Baak";
6    public $license = "GPL";
7    public $website = "http://www.mvblog.org";
8    public $description = "Replaces the normal month archive menu with a nice calendar.";
9
10    private $_mvblog;
11
12    private $_count    = array();
13    private $_settings = array(
14        "altarchive_onemonth_noitems_bg"   => "#FFFFFF",
15        "altarchive_onemonth_items_bg"     => "#8bb7b5",
16        "altarchive_onemonth_noitems_text" => "#000000",
17        "altarchive_onemonth_items_text"   => "#FFFFFF"
18    );
19
20    /* methods */
21    /* __construct {{{ */
22    public function __construct(&$mvblog) {
23        $this->addHook("menu_archive_output", "genaltarchive_onemonth");
24        $this->addHook("css_output", "altarchive_onemonthCSS");
25        $this->_mvblog =& $mvblog;
26    }
27    /* }}} */
28    /* activate {{{ */
29    public function activate() {
30        /* get the months where we have articles */
31        $sql = "SELECT date FROM articles WHERE active=1 AND public=1 ORDER BY date DESC";
32        $res = $this->_mvblog->db->query($sql);
33        while ($row = $res->fetchRow()) {
34            $day   = date("d", $row[0]);
35            $month = date("m", $row[0]);
36            $year  = date("Y", $row[0]);
37            $key   = $day.$month.$year;
38            if (array_key_exists($key, $this->_count)) {
39                $this->_count[$key]++;
40            } else {
41                $this->_count[$key] = 1;
42            }
43        }
44        /* populate some settings */
45        $sql = "SELECT * FROM settings WHERE settingname LIKE 'altarchive_onemonth%'";
46        $res = $this->_mvblog->db->query($sql);
47        while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
48            $this->_settings[$row["settingname"]] = $row["settingvalue"];
49        }
50    }
51    /* }}} */
52    /* deactivate {{{ */
53    public function deactivate() {
54        /* clear local variables */
55        unset($this->_count);
56    }
57    /* }}} */
58    /* genaltarchive_onemonth {{{ */
59    public function genaltarchive_onemonth($defaultmenu) {
60        /* find out what the oldest post date is */
61        $q = "SELECT date FROM articles WHERE active = 1 AND public = 1 ORDER BY date LIMIT 1";
62        $res = $this->_mvblog->db->query($q);
63        $row = $res->fetchRow(MDB2_FETCHMODE_ASSOC);
64        $oldest = $row["date"];
65
66        $output   = "";
67        $url_next = "";
68        $url_prev = "";
69        /* create url schema */
70        if ($this->_mvblog->settings["cleanurl"])
71            $url = "archive/";
72        else
73            $url = "index.php?action=archive&m=";
74        /* read url params so we know what to show */
75        if (array_key_exists("action", $_REQUEST) && $_REQUEST["action"] == "archive") {
76            if (array_key_exists("m", $_REQUEST)) {
77                if (strlen($_REQUEST["m"]) == 6) {
78                    /* month */
79                    $_month = substr($_REQUEST["m"], 0, 2);
80                    $_year  = substr($_REQUEST["m"], 2, 4);
81                } elseif (strlen($_REQUEST["m"]) == 8) {
82                    /* day */
83                    $_month = substr($_REQUEST["m"], 2, 2);
84                    $_year  = substr($_REQUEST["m"], 4, 4);
85                }
86            } else {
87                $_month = date("m");
88                $_year  = date("Y");
89            }
90        } else {
91            $_month = date("m");
92            $_year  = date("Y");
93        }
94        $url_next = $url.date("m", mktime(0, 0, 0, $_month+1, 1, $_year)).date("Y", mktime(0, 0, 0, $_month+1, 1, $_year));
95        $url_prev = $url.date("m", mktime(0, 0, 0, $_month-1, 1, $_year)).date("Y", mktime(0, 0, 0, $_month-1, 1, $_year));
96        $output .= "<b>".strftime("%B %Y", mktime(0, 0, 0, $_month, 1, $_year))."</b><br />\n";
97        $output .= "<table><tr>\n";
98        $output .= "<td class=\"altarchive_onemonth_month_noitems\">s</td>\n";
99        $output .= "<td class=\"altarchive_onemonth_month_noitems\">m</td>\n";
100        $output .= "<td class=\"altarchive_onemonth_month_noitems\">t</td>\n";
101        $output .= "<td class=\"altarchive_onemonth_month_noitems\">w</td>\n";
102        $output .= "<td class=\"altarchive_onemonth_month_noitems\">t</td>\n";
103        $output .= "<td class=\"altarchive_onemonth_month_noitems\">f</td>\n";
104        $output .= "<td class=\"altarchive_onemonth_month_noitems\">s</td>\n";
105        $output .= "</tr><tr>";
106        /* calendar start on sunday, calculate what day this month started */
107        $skipdays = date("w", mktime(0, 0, 0, $_month, 1, $_year));
108        $dow = $skipdays;
109        /* skip the days before this month started */
110        for ($i=0;$i!=$skipdays;$i++)
111            $output .= "<td class=\"altarchive_onemonth_month_noitems\">&nbsp;</td>\n";
112        for ($i=0; $i!=date("t",mktime(0,0,0,$_month,1,$_year)); $i++) {
113            /* look if we have a post today */
114            $today = date("dmY", mktime(0, 0, 0, $_month, $i+1, $_year));
115            if (array_key_exists($today, $this->_count) && $this->_count[$today] > 0)
116                $output .= "<td class=\"altarchive_onemonth_month_items\"><a href=\"".$url.$today."\">".($i+1)."</a></td>\n";
117            else
118                $output .= "<td class=\"altarchive_onemonth_month_noitems\">".($i+1)."</td>\n";
119            $dow++;
120            /* start new row every week */
121            if ($dow == 7) {
122                $output .= "</tr><tr>\n";
123                $dow = 0;
124            }
125        }
126        /* if last day of month is not saturday, complete the table */
127        if ($dow) {
128            for ($i=0;$i!=7-$dow;$i++)
129                $output .= "<td class=\"altarchive_onemonth_month_noitems\">&nbsp;</td>\n";
130        }
131        $output .= "</tr><tr>\n";
132        if ($oldest < mktime(0, 0, 0, $_month, 1, $_year))
133            $output .= "<td class=\"altarchive_onemonth_month_noitems\" colspan=\"3\"><a href=\"$url_prev\"><< ".strftime("%b", mktime(0, 0, 0, $_month-1, 1, $_year))."</a></td>\n";
134        else
135            $output .= "<td class=\"altarchive_onemonth_month_noitems\" colspan=\"3\">&nbsp;</td>\n";
136        $output .= "<td class=\"altarchive_onemonth_month_noitems\">&nbsp;</td>\n";
137        if (date("m") == $_month)
138            $output .= "<td class=\"altarchive_onemonth_month_noitems\" colspan=\"3\">&nbsp;</td>\n";
139        else
140            $output .= "<td class=\"altarchive_onemonth_month_noitems\" colspan=\"3\"><a href=\"$url_next\">".strftime("%b", mktime(0, 0, 0, $_month+1, 1, $_year))." >></a></td>\n";
141        $output .= "</tr></table>\n";
142        return $output;
143    }
144    /* }}} */
145    /* altarchiveCSS {{{ */
146    public function altarchive_onemonthCSS($data) {
147        if (array_key_exists("altarchive_onemonth_showdefaultcss", $this->_settings) && $this->_settings["altarchive_onemonth_showdefaultcss"]) {
148            $output  = $data."\n\t<style type=\"text/css\">\n";
149            $output .= "\t\t.altarchive_onemonth_month_noitems {\n";
150            $output .= "\t\t\ttext-align: center;\n";
151            $output .= "\t\t\tbackground-color: ".$this->_settings["altarchive_onemonth_noitems_bg"].";\n";
152            $output .= "\t\t\tcolor: ".$this->_settings["altarchive_onemonth_noitems_text"].";\n";
153            $output .= "\t\t\tborder: 1px solid #b0b0b0;\n";
154            $output .= "\t\t}\n";
155            $output .= "\t\t.altarchive_onemonth_month_items {\n";
156            $output .= "\t\t\ttext-align: center;\n";
157            $output .= "\t\t\tbackground-color: ".$this->_settings["altarchive_onemonth_items_bg"].";\n";
158            $output .= "\t\t\tborder: 1px solid #b0b0b0;\n";
159            $output .= "\t\t}\n";
160            $output .= "\t\t.altarchive_onemonth_month_items a {\n";
161            $output .= "\t\t\tcolor: ".$this->_settings["altarchive_onemonth_items_text"].";\n";
162            $output .= "\t\t}\n";
163            $output .= "\t</style>\n";
164        } else {
165            $output = $data;
166        }
167        return $output;
168    }
169    /* }}} */
170    /* show_settings {{{ */
171    public function show_settings() {
172        $output  = "<form name=\"acronym\" method=\"post\" action=\"index.php\">";
173        $output .= "<input type=\"hidden\" name=\"action\" value=\"save_plugin_setting\" />";
174        $output .= "<input type=\"hidden\" name=\"plugin\" value=\"altarchive_onemonth\" />";
175        $output .= "<table style=\"width: 220px;\"><tr>";
176        $output .= "<td>".gettext("use default stylesheet?")."</td>";
177        $output .= "<td><select name=\"altarchive_onemonth_showdefaultcss\">";
178        $output .= "<option value=\"1\"";
179        if (array_key_exists("altarchive_onemonth_showdefaultcss", $this->_settings) && $this->_settings["altarchive_onemonth_showdefaultcss"] == 1)
180            $output .= " selected=\"selected\"";
181        $output .= ">".gettext("yes")."</option>";
182        $output .= "<option value=\"0\"";
183        if (!array_key_exists("altarchive_onemonth_showdefaultcss", $this->_settings) ||
184            array_key_exists("altarchive_onemonth_showdefaultcss", $this->_settings) && $this->_settings["altarchive_onemonth_showdefaultcss"] == 0)
185            $output .= " selected=\"selected\"";
186        $output .= ">".gettext("no")."</option>";
187        $output .= "</select></td>\n";
188        $output .= "</tr><tr>";
189        $output .= "<td colspan=\"2\"><input type=\"submit\" value=\"".gettext("Save")."\" /></td>";
190        $output .= "</tr></table>";
191
192        $output .= "</form>";
193        echo $output;
194    }
195    /* }}} */
196    /* save_setting {{{ */
197    public function save_setting($requestdata) {
198        /* first look if the setting is already there */
199        $sql = "SELECT COUNT(*) FROM settings WHERE settingname = 'altarchive_onemonth_showdefaultcss'";
200        $res = $this->_mvblog->db->query($sql);
201        $row = $res->fetchRow();
202        if ($row[0]) {
203            /* yes, so update */
204            $sql = sprintf("UPDATE settings SET settingvalue='%d' WHERE settingname='altarchive_onemonth_showdefaultcss'", $requestdata["altarchive_onemonth_showdefaultcss"]);
205        } else {
206            /* no, so insert */
207            $sql = sprintf("INSERT INTO settings (settingname, settingvalue) VALUES ('altarchive_onemonth_showdefaultcss', '%d')", $requestdata["altarchive_onemonth_showdefaultcss"]);
208        }
209        $res = $this->_mvblog->db->exec($sql);
210        $this->_settings["altarchive_onemonth_showdefaultcss"] = sprintf("%d", $requestdata["altarchive_onemonth_showdefaultcss"]);
211        $this->show_settings();
212    }
213    /* }}} */
214}
215?>
Note: See TracBrowser for help on using the browser.