blog.php (12078B)
1 <?php 2 // Blog extension, https://github.com/annaesvensson/yellow-blog 3 4 class YellowBlog { 5 const VERSION = "0.8.30"; 6 public $yellow; // access to API 7 8 // Handle initialisation 9 public function onLoad($yellow) { 10 $this->yellow = $yellow; 11 $this->yellow->system->setDefault("blogStartLocation", "auto"); 12 $this->yellow->system->setDefault("blogNewLocation", "@title"); 13 $this->yellow->system->setDefault("blogShortcutEntries", "0"); 14 $this->yellow->system->setDefault("blogPaginationLimit", "5"); 15 } 16 17 // Handle page content of shortcut 18 public function onParseContentShortcut($page, $name, $text, $type) { 19 $output = null; 20 if (substru($name, 0, 4)=="blog" && ($type=="block" || $type=="inline")) { 21 switch($name) { 22 case "blogauthors": $output = $this->getShortcutBlogauthors($page, $name, $text); break; 23 case "blogtags": $output = $this->getShortcutBlogtags($page, $name, $text); break; 24 case "blogyears": $output = $this->getShortcutBlogyears($page, $name, $text); break; 25 case "blogmonths": $output = $this->getShortcutBlogmonths($page, $name, $text); break; 26 case "blogpages": $output = $this->getShortcutBlogpages($page, $name, $text); break; 27 } 28 } 29 return $output; 30 } 31 32 // Return blogauthors shortcut 33 public function getShortcutBlogauthors($page, $name, $text) { 34 $output = null; 35 list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text); 36 if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation"); 37 if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries"); 38 $blogStart = $this->getBlogStart($page, $startLocation); 39 if (!is_null($blogStart)) { 40 $pages = $this->getBlogPages($blogStart); 41 $page->setLastModified($pages->getModified()); 42 $authors = $pages->group("author", false, "count"); 43 if ($shortcutEntries!=0) $authors = array_slice($authors, 0, $shortcutEntries, true); 44 uksort($authors, "strnatcasecmp"); 45 $output = "<div class=\"".htmlspecialchars($name)."\">\n"; 46 $output .= "<ul>\n"; 47 foreach ($authors as $author=>$collection) { 48 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("author:$author")."\">"; 49 $output .= htmlspecialchars($author)."</a></li>\n"; 50 } 51 $output .= "</ul>\n"; 52 $output .= "</div>\n"; 53 } else { 54 $page->error(500, "Blogauthors '$startLocation' does not exist!"); 55 } 56 return $output; 57 } 58 59 // Return blogtags shortcut 60 public function getShortcutBlogtags($page, $name, $text) { 61 $output = null; 62 list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text); 63 if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation"); 64 if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries"); 65 $blogStart = $this->getBlogStart($page, $startLocation); 66 if (!is_null($blogStart)) { 67 $pages = $this->getBlogPages($blogStart); 68 $page->setLastModified($pages->getModified()); 69 $tags = $pages->group("tag", false, "count"); 70 if ($shortcutEntries!=0) $tags = array_slice($tags, 0, $shortcutEntries, true); 71 uksort($tags, "strnatcasecmp"); 72 $output = "<div class=\"".htmlspecialchars($name)."\">\n"; 73 $output .= "<ul>\n"; 74 foreach ($tags as $tag=>$collection) { 75 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("tag:$tag")."\">"; 76 $output .= htmlspecialchars($tag)."</a></li>\n"; 77 } 78 $output .= "</ul>\n"; 79 $output .= "</div>\n"; 80 } else { 81 $page->error(500, "Blogtags '$startLocation' does not exist!"); 82 } 83 return $output; 84 } 85 86 // Return blogyears shortcut 87 public function getShortcutBlogyears($page, $name, $text) { 88 $output = null; 89 list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text); 90 if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation"); 91 if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries"); 92 $blogStart = $this->getBlogStart($page, $startLocation); 93 if (!is_null($blogStart)) { 94 $pages = $this->getBlogPages($blogStart); 95 $page->setLastModified($pages->getModified()); 96 $years = $pages->group("published", false, "Y"); 97 if ($shortcutEntries!=0) $years = array_slice($years, 0, $shortcutEntries, true); 98 $output = "<div class=\"".htmlspecialchars($name)."\">\n"; 99 $output .= "<ul>\n"; 100 foreach ($years as $year=>$collection) { 101 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("published:$year")."\">"; 102 $output .= htmlspecialchars($this->yellow->language->getDateStandard($year))."</a></li>\n"; 103 } 104 $output .= "</ul>\n"; 105 $output .= "</div>\n"; 106 } else { 107 $page->error(500, "Blogyears '$startLocation' does not exist!"); 108 } 109 return $output; 110 } 111 112 // Return blogmonths shortcut 113 public function getShortcutBlogmonths($page, $name, $text) { 114 $output = null; 115 list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text); 116 if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation"); 117 if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries"); 118 $blogStart = $this->getBlogStart($page, $startLocation); 119 if (!is_null($blogStart)) { 120 $pages = $this->getBlogPages($blogStart); 121 $page->setLastModified($pages->getModified()); 122 $months = $pages->group("published", false, "Y-m"); 123 if ($shortcutEntries!=0) $months = array_slice($months, 0, $shortcutEntries, true); 124 $output = "<div class=\"".htmlspecialchars($name)."\">\n"; 125 $output .= "<ul>\n"; 126 foreach ($months as $month=>$collection) { 127 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("published:$month")."\">"; 128 $output .= htmlspecialchars($this->yellow->language->getDateStandard($month))."</a></li>\n"; 129 } 130 $output .= "</ul>\n"; 131 $output .= "</div>\n"; 132 } else { 133 $page->error(500, "Blogmonths '$startLocation' does not exist!"); 134 } 135 return $output; 136 } 137 138 // Return blogpages shortcut 139 public function getShortcutBlogpages($page, $name, $text) { 140 $output = null; 141 list($startLocation, $shortcutEntries, $filterTag) = $this->yellow->toolbox->getTextArguments($text); 142 if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation"); 143 if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries"); 144 $blogStart = $this->getBlogStart($page, $startLocation); 145 if (!is_null($blogStart)) { 146 $pages = $this->getBlogPages($blogStart)->remove($page); 147 $page->setLastModified($pages->getModified()); 148 if (!is_string_empty($filterTag)) $pages->filter("tag", $filterTag); 149 $pages->sort("published", false); 150 if ($shortcutEntries!=0) $pages->limit($shortcutEntries); 151 $output = "<div class=\"".htmlspecialchars($name)."\">\n"; 152 $output .= "<ul>\n"; 153 foreach ($pages as $pageBlog) { 154 $output .= "<li><a".($pageBlog->isExisting("tag") ? " class=\"".$this->getClass($pageBlog)."\"" : ""); 155 $output .=" href=\"".$pageBlog->getLocation(true)."\">".$pageBlog->getHtml("title")."</a></li>\n"; 156 } 157 $output .= "</ul>\n"; 158 $output .= "</div>\n"; 159 } else { 160 $page->error(500, "Blogpages '$startLocation' does not exist!"); 161 } 162 return $output; 163 } 164 165 // Handle page layout 166 public function onParsePageLayout($page, $name) { 167 if ($name=="blog-start") { 168 $pages = $this->getBlogPages($page); 169 $pagesFilter = array(); 170 if ($page->isRequest("tag")) { 171 $pages->filter("tag", $page->getRequest("tag")); 172 array_push($pagesFilter, $pages->getFilter()); 173 } 174 if ($page->isRequest("author")) { 175 $pages->filter("author", $page->getRequest("author")); 176 array_push($pagesFilter, $pages->getFilter()); 177 } 178 if ($page->isRequest("published")) { 179 $pages->filter("published", $page->getRequest("published"), false); 180 array_push($pagesFilter, $this->yellow->language->getDateStandard($pages->getFilter())); 181 } 182 $pages->sort("published", false); 183 if (!is_array_empty($pagesFilter)) { 184 $text = implode(" ", $pagesFilter); 185 $page->set("titleHeader", $text." - ".$page->get("sitename")); 186 $page->set("titleContent", $page->get("title").": ".$text); 187 $page->set("title", $page->get("title").": ".$text); 188 $page->set("blogWithFilter", true); 189 } 190 $page->setPages("blog", $pages); 191 $page->setLastModified($pages->getModified()); 192 $page->setHeader("Cache-Control", "max-age=60"); 193 } 194 if ($name=="blog") { 195 $blogStartLocation = $this->yellow->system->get("blogStartLocation"); 196 if ($blogStartLocation=="auto") { 197 $blogStart = $page->getParent(); 198 } else { 199 $blogStart = $this->yellow->content->find($blogStartLocation); 200 } 201 $page->setPage("blogStart", $blogStart); 202 } 203 } 204 205 // Handle content file editing 206 public function onEditContentFile($page, $action, $email) { 207 if ($page->get("layout")=="blog") $page->set("editNewLocation", $this->yellow->system->get("blogNewLocation")); 208 } 209 210 // Return blog start page, null if not found 211 public function getBlogStart($page, $blogStartLocation) { 212 if ($blogStartLocation=="auto") { 213 $blogStart = null; 214 foreach ($this->yellow->content->top(true, false) as $pageTop) { 215 if ($pageTop->get("layout")=="blog-start") { 216 $blogStart = $pageTop; 217 break; 218 } 219 } 220 if ($page->get("layout")=="blog-start") $blogStart = $page; 221 } else { 222 $blogStart = $this->yellow->content->find($blogStartLocation); 223 } 224 return $blogStart; 225 } 226 227 // Return blog pages for page 228 public function getBlogPages($page) { 229 if ($this->yellow->system->get("blogStartLocation")=="auto") { 230 $pages = $page->getChildren(); 231 } else { 232 $pages = $this->yellow->content->index(); 233 } 234 $pages->filter("layout", "blog"); 235 return $pages; 236 } 237 238 // Return class for page 239 public function getClass($page) { 240 $class = ""; 241 if ($page->isExisting("tag")) { 242 foreach (preg_split("/\s*,\s*/", $page->get("tag")) as $tag) { 243 $class .= " tag-".$this->yellow->lookup->normaliseArguments($tag, false); 244 } 245 } 246 return trim($class); 247 } 248 }