PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/wkp_Menu.php

https://github.com/pastak/jichikai_web
PHP | 121 lines | 70 code | 32 blank | 19 comment | 19 complexity | 2b5625b7234ed7eb32d2594f167dcd93 MD5 | raw file
  1. <?php
  2. /*
  3. * Menu plugin for LionWiki, (c) 2009 Adam Zivner, adam.zivner@gmail.com
  4. *
  5. * This plugin provides simple way to create one level menus, use it like this:
  6. *
  7. * {menu(default.html) [Hauptseite|Main page] [Home]}
  8. *
  9. * or
  10. *
  11. * {menu [Hauptseite|Main page] [Home]}
  12. *
  13. * Parameter "default.html" is a template file for menu (located in plugins/Menu/),
  14. * it's content is probably self explanatory.
  15. *
  16. * If you don't explicitly write a template, "default.html" is used, which is
  17. * in default distribution.
  18. */
  19. class Menu
  20. {
  21. var $desc = array(
  22. array("Menu", "provides syntax for simple one level menus.")
  23. );
  24. var $template_dir;
  25. var $default_template = "default.html";
  26. var $menus = array();
  27. function Menu()
  28. {
  29. $this->template_dir = $GLOBALS["PLUGINS_DIR"] . "Menu/";
  30. }
  31. function formatBegin()
  32. {
  33. global $CON;
  34. // First we need to save it, otherwise main parsing algorithm would mess with it
  35. preg_match_all("/\{menu(\(([^)]*)\))?(\s+parent=\[([^\]]+)\])?\s+([^\}]*)\}/s", $CON, $this->menus, PREG_SET_ORDER);
  36. foreach($this->menus as $menu)
  37. $CON = str_replace($menu[0], "{MENU}", $CON);
  38. }
  39. // $str is something like [Link] or [Title|Link] or [Title|http://hskdjfhjks]
  40. function getLink($str)
  41. {
  42. $parts = explode("|", $str);
  43. if(empty($parts[1]))
  44. $parts[1] = $parts[0];
  45. list($name, $link) = $parts;
  46. if(substr($link, 0, 4) != "http" && substr($link, 0, 4) != "http" && substr($link, 0, 2) != "./" && $link[0] != "/")
  47. $link = $GLOBALS["self"] . "?page=" . u($link);
  48. return array($link, $parts[0]);
  49. }
  50. function formatEnd()
  51. {
  52. global $CON;
  53. foreach($this->menus as $m) {
  54. $template_file = $m[2];
  55. list($parent_link, $parent_name) = $this->getLink($m[4]);
  56. $item_string = $m[5];
  57. $template_file = clear_path($template_file);
  58. if(empty($template_file) || !file_exists($this->template_dir . $template_file))
  59. $template_file = $this->default_template; // use default.html template if none is provided or does not exist
  60. $tmpl = file_get_contents($this->template_dir . $template_file);
  61. $item_tmpl = "";
  62. if(preg_match("/\{item\}(.*)\{\/item\}/Us", $tmpl, $m))
  63. $item_tmpl = $m[1];
  64. $items = array();
  65. if(preg_match_all("/\[([^\]]+)\]/U", $item_string, $matches))
  66. $items = $matches[1];
  67. $items_str = "";
  68. for($i = 0, $c = count($items); $i < $c; $i++) {
  69. $class = "";
  70. if($i == 0)
  71. $class = "first";
  72. if($i == $c - 1)
  73. $class .= " last";
  74. list($link, $name) = $this->getLink($items[$i]);
  75. $items_str .= strtr($item_tmpl, array(
  76. "{class}" => $class,
  77. "{name}" => h($name),
  78. "{link}" => $link
  79. ));
  80. }
  81. $menu_str = strtr($tmpl, array(
  82. "{parent_name}" => $parent_name,
  83. "{parent_link}" => $parent_link
  84. ));
  85. $menu_str = preg_replace("/\{item\}.*\{\/item\}/Us", $items_str, $menu_str);
  86. $CON = preg_replace("/\{MENU\}/", $menu_str, $CON, 1);
  87. }
  88. }
  89. }