| 1 | <?php |
|---|
| 2 | Class altarchive extends MvBlog_plugin implements MvBlog_pluginiface { |
|---|
| 3 | |
|---|
| 4 | public $name = "altarchive"; |
|---|
| 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_noitems_bg" => "#FFFFFF", |
|---|
| 15 | "altarchive_items_bg" => "#8bb7b5", |
|---|
| 16 | "altarchive_noitems_text" => "#000000", |
|---|
| 17 | "altarchive_items_text" => "#FFFFFF" |
|---|
| 18 | ); |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | public function __construct(&$mvblog) { |
|---|
| 23 | |
|---|
| 24 | $this->addHook("menu_archive_output", "genaltarchive"); |
|---|
| 25 | $this->addHook("css_output", "altarchiveCSS"); |
|---|
| 26 | $this->_mvblog =& $mvblog; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | public function activate() { |
|---|
| 31 | |
|---|
| 32 | $sql = "SELECT date FROM articles WHERE active=1 AND public=1 ORDER BY date DESC"; |
|---|
| 33 | $res = $this->_mvblog->db->query($sql); |
|---|
| 34 | while ($row = $res->fetchRow()) { |
|---|
| 35 | $month = date("m", $row[0]); |
|---|
| 36 | $year = date("Y", $row[0]); |
|---|
| 37 | $key = $month.$year; |
|---|
| 38 | if (array_key_exists($key, $this->_count)) { |
|---|
| 39 | $this->_count[$key]++; |
|---|
| 40 | } else { |
|---|
| 41 | $this->_count[$key] = 1; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | $sql = "SELECT * FROM settings WHERE settingname LIKE 'altarchive_%'"; |
|---|
| 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 | |
|---|
| 53 | public function deactivate() { |
|---|
| 54 | |
|---|
| 55 | unset($this->_count); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | public function genaltarchive($defaultmenu) { |
|---|
| 61 | |
|---|
| 62 | if ($this->_mvblog->settings["cleanurl"]) |
|---|
| 63 | $url = "archive/"; |
|---|
| 64 | else |
|---|
| 65 | $url = "index.php?action=archive&m="; |
|---|
| 66 | |
|---|
| 67 | if (array_key_exists("altarchive_showyears", $this->_settings)) |
|---|
| 68 | $years = $this->_settings["altarchive_showyears"]; |
|---|
| 69 | else |
|---|
| 70 | $years = 3; |
|---|
| 71 | $output = ""; |
|---|
| 72 | for ($i=date("Y"); $i>date("Y")-$years; $i--) { |
|---|
| 73 | $output .= "<b>".$i."</b><br />\n"; |
|---|
| 74 | $output .= "<table><tr>\n"; |
|---|
| 75 | $newline = 0; |
|---|
| 76 | for ($m = 1; $m <= 12; $m++) { |
|---|
| 77 | $newline++; |
|---|
| 78 | if ($newline > 3) { |
|---|
| 79 | $output .= "</tr><tr>\n"; |
|---|
| 80 | $newline = 1; |
|---|
| 81 | } |
|---|
| 82 | if ($m < 10) $m = "0".$m; |
|---|
| 83 | $output .= "\t<td "; |
|---|
| 84 | if (array_key_exists($m.$i, $this->_count)) { |
|---|
| 85 | $output .= "class=\"altarchive_month_items\">"; |
|---|
| 86 | $output .= "<a href=\"$url".$m.$i."\" title=\"".$this->_count[$m.$i]." ".gettext("articles in this month.")."\">".strftime("%B", mktime(0,0,0,$m,1,$i))."</td>\n"; |
|---|
| 87 | } else { |
|---|
| 88 | $output .= "class=\"altarchive_month_noitems\">"; |
|---|
| 89 | $output .= strftime("%B", mktime(0,0,0,$m,1,$i))."</td>\n"; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | $output .= "</tr></table>\n"; |
|---|
| 93 | } |
|---|
| 94 | return $output; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | public function altarchiveCSS($data) { |
|---|
| 99 | if (array_key_exists("altarchive_showdefaultcss", $this->_settings) && $this->_settings["altarchive_showdefaultcss"]) { |
|---|
| 100 | $output = $data."\n\t<style type=\"text/css\">\n"; |
|---|
| 101 | $output .= "\t\t.altarchive_month_noitems {\n"; |
|---|
| 102 | $output .= "\t\t\ttext-align: center;\n"; |
|---|
| 103 | $output .= "\t\t\tbackground-color: ".$this->_settings["altarchive_noitems_bg"].";\n"; |
|---|
| 104 | $output .= "\t\t\tcolor: ".$this->_settings["altarchive_noitems_text"].";\n"; |
|---|
| 105 | $output .= "\t\t\tborder: 1px solid #b0b0b0;\n"; |
|---|
| 106 | $output .= "\t\t}\n"; |
|---|
| 107 | $output .= "\t\t.altarchive_month_items {\n"; |
|---|
| 108 | $output .= "\t\t\ttext-align: center;\n"; |
|---|
| 109 | $output .= "\t\t\tbackground-color: ".$this->_settings["altarchive_items_bg"].";\n"; |
|---|
| 110 | $output .= "\t\t\tborder: 1px solid #b0b0b0;\n"; |
|---|
| 111 | $output .= "\t\t}\n"; |
|---|
| 112 | $output .= "\t\t.altarchive_month_items a {\n"; |
|---|
| 113 | $output .= "\t\t\tcolor: ".$this->_settings["altarchive_items_text"].";\n"; |
|---|
| 114 | $output .= "\t\t}\n"; |
|---|
| 115 | $output .= "\t</style>\n"; |
|---|
| 116 | } else { |
|---|
| 117 | $output = $data; |
|---|
| 118 | } |
|---|
| 119 | return $output; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | public function show_settings() { |
|---|
| 124 | $output = "<form name=\"altarchive_settings\" method=\"post\" action=\"index.php\">"; |
|---|
| 125 | $output .= "<input type=\"hidden\" name=\"action\" value=\"save_plugin_setting\" />"; |
|---|
| 126 | $output .= "<input type=\"hidden\" name=\"plugin\" value=\"altarchive\" />"; |
|---|
| 127 | $output .= "<table style=\"width: 220px;\"><tr>"; |
|---|
| 128 | $output .= "<td>".gettext("use default stylesheet?")."</td>"; |
|---|
| 129 | $output .= "<td><select name=\"altarchive_showdefaultcss\">"; |
|---|
| 130 | $output .= "<option value=\"1\""; |
|---|
| 131 | if (array_key_exists("altarchive_showdefaultcss", $this->_settings) && $this->_settings["altarchive_showdefaultcss"] == 1) |
|---|
| 132 | $output .= " selected=\"selected\""; |
|---|
| 133 | $output .= ">".gettext("yes")."</option>"; |
|---|
| 134 | $output .= "<option value=\"0\""; |
|---|
| 135 | if (!array_key_exists("altarchive_showdefaultcss", $this->_settings) || array_key_exists("altarchive_showdefaultcss", $this->_settings) && $this->_settings["altarchive_showdefaultcss"] == 0) |
|---|
| 136 | $output .= " selected=\"selected\""; |
|---|
| 137 | $output .= ">".gettext("no")."</option>"; |
|---|
| 138 | $output .= "</select></td>\n"; |
|---|
| 139 | $output .= "</tr><tr>"; |
|---|
| 140 | $output .= "<td>".gettext("years to show")."</td>"; |
|---|
| 141 | $output .= "<td><select name=\"altarchive_showyears\">"; |
|---|
| 142 | for ($i=1;$i<10;$i++) { |
|---|
| 143 | $output .= "<option value=\"$i\""; |
|---|
| 144 | if (array_key_exists("altarchive_showyears", $this->_settings) && $this->_settings["altarchive_showyears"] == $i) |
|---|
| 145 | $output .= " selected=\"selected\""; |
|---|
| 146 | $output .= ">$i</option>"; |
|---|
| 147 | } |
|---|
| 148 | $output .= "</select></td>"; |
|---|
| 149 | $output .= "</tr><tr>"; |
|---|
| 150 | $output .= "<td colspan=\"2\"><input type=\"submit\" value=\"".gettext("Save")."\" /></td>"; |
|---|
| 151 | $output .= "</tr></table>"; |
|---|
| 152 | |
|---|
| 153 | $output .= "</form>"; |
|---|
| 154 | echo $output; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | public function save_setting($requestdata) { |
|---|
| 159 | |
|---|
| 160 | $sql = "SELECT COUNT(*) FROM settings WHERE settingname = 'altarchive_showdefaultcss'"; |
|---|
| 161 | $res = $this->_mvblog->db->query($sql); |
|---|
| 162 | $row = $res->fetchRow(); |
|---|
| 163 | if ($row[0]) { |
|---|
| 164 | |
|---|
| 165 | $sql = sprintf("UPDATE settings SET settingvalue='%d' WHERE settingname='altarchive_showdefaultcss'", $requestdata["altarchive_showdefaultcss"]); |
|---|
| 166 | } else { |
|---|
| 167 | |
|---|
| 168 | $sql = sprintf("INSERT INTO settings (settingname, settingvalue) VALUES ('altarchive_showdefaultcss', '%d')", $requestdata["altarchive_showdefaultcss"]); |
|---|
| 169 | } |
|---|
| 170 | $res = $this->_mvblog->db->exec($sql); |
|---|
| 171 | $this->_settings["altarchive_showdefaultcss"] = sprintf("%d", $requestdata["altarchive_showdefaultcss"]); |
|---|
| 172 | |
|---|
| 173 | $sql = "SELECT COUNT(*) FROM settings WHERE settingname = 'altarchive_showyears'"; |
|---|
| 174 | $res = $this->_mvblog->db->query($sql); |
|---|
| 175 | $row = $res->fetchRow(); |
|---|
| 176 | if ($row[0]) { |
|---|
| 177 | |
|---|
| 178 | $sql = sprintf("UPDATE settings SET settingvalue='%d' WHERE settingname='altarchive_showyears'", $requestdata["altarchive_showyears"]); |
|---|
| 179 | } else { |
|---|
| 180 | |
|---|
| 181 | $sql = sprintf("INSERT INTO settings (settingname, settingvalue) VALUES ('altarchive_showyears', '%d')", $requestdata["altarchive_showyears"]); |
|---|
| 182 | } |
|---|
| 183 | $res = $this->_mvblog->db->exec($sql); |
|---|
| 184 | $this->_settings["altarchive_showyears"] = sprintf("%d", $requestdata["altarchive_showyears"]); |
|---|
| 185 | $this->show_settings(); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | } |
|---|
| 189 | ?> |
|---|