mostly filebased Content Presentation System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
12KB

  1. <?php
  2. namespace Controller;
  3. class Page {
  4. public static $keyword = "page"; // variable in URL (query string)
  5. function index(\Base $f3, $params) {
  6. /////////////////////////////
  7. // what page are we watching?
  8. $page = $params[self::$keyword];
  9. $f3->set('page', $page);
  10. $f3->set('bodyClass',"page-$page");
  11. switch ($page) {
  12. case 'home':
  13. default:
  14. $f3->mset(array(
  15. 'template'=>'tpl/index.html',
  16. 'templateContent'=>'maincontent.html'
  17. ));
  18. self::folder2();
  19. break;
  20. }
  21. }
  22. ////////////////
  23. // interfaces //
  24. ////////////////
  25. function home() {
  26. $f3 = \Base::instance();
  27. self::index($f3, array('page'=>'home'));
  28. }
  29. function secondLevel(\Base $f3,$params) {
  30. self::index($f3, array('page'=>$params['level1']."/".$params['level2']));
  31. }
  32. function thirdLevel(\Base $f3,$params) {
  33. self::index($f3, array('page'=>$params['level1']."/".$params['level2']."/".$params['level3']));
  34. }
  35. function fourthLevel(\Base $f3,$params) {
  36. self::index($f3, array('page'=>$params['level1']."/".$params['level2']."/".$params['level3']."/".$params['level4']));
  37. }
  38. //////////////////
  39. // main program //
  40. //////////////////
  41. function folder2() {
  42. $f3 = \Base::instance();
  43. $page = $f3->get('page');
  44. $f3->set('current_page_folder', $f3->get('CONTENT').$page."/");
  45. $folder = new \Modules\FilesInFolders(
  46. $f3->get('current_page_folder'),
  47. array(
  48. 'content'=>array(
  49. 'secondary'=>'secondary',
  50. 'zzz'=>'hidden',
  51. 'unpublish'=>'hidden'
  52. ),
  53. 'keyfiles'=>array(
  54. 'banner'=>array(
  55. 'until'=>$f3->get("CONTENT"),
  56. 'type'=>'pic'
  57. ))
  58. )
  59. );
  60. $folder->prepare_files();
  61. $folder->fill_content();
  62. // set template variables
  63. $banner = new \Modules\CachedImage($folder->extras['banner']);
  64. $f3->set('banner',$banner->get_src(2000));
  65. // $f3->set('background',$background);
  66. $f3->set('content', implode("\n", $folder->content['default']));
  67. $f3->set('secondary_content', implode("\n", $folder->content['secondary']));
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  78. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. function folder() {
  81. $f3 = \Base::instance();
  82. $page = $f3->get('page');
  83. $folder = $f3->get("CONTENT").$page."/";
  84. $EXT=array(
  85. 'pic'=>array( 'jpg', 'jpeg', 'png', 'svg' ),
  86. 'tpl'=>array( 'html', 'htm', 'tpl' ),
  87. 'txt'=>array( 'txt', 'text', 'md' ),
  88. 'csv'=>array( 'csv' )
  89. );
  90. $background = "";
  91. $banner = "";
  92. $c1 = array(
  93. // 'background'=>'',
  94. 'pic'=>array(),
  95. 'txt'=>array(),
  96. 'tpl'=>array(),
  97. 'csv'=>array()
  98. );
  99. $c2 = array(
  100. // 'background'=>'',
  101. 'pic'=>array(),
  102. 'txt'=>array(),
  103. 'tpl'=>array(),
  104. 'csv'=>array()
  105. );
  106. $primary = array();
  107. $secondary = array();
  108. // collect contents of folder into an organised array
  109. if(!is_dir($folder)) $folder=$f3->get("CONTENT")."../404/";
  110. $ls = scandir($folder);
  111. foreach ($ls as $key=>$f) {
  112. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  113. $ex=explode(".", $f);
  114. $ext=end($ex);
  115. if(in_array(strtolower($ext), $EXT['pic'])) {
  116. if($ex[0]=='background') {
  117. $background=pic_cache($folder.$f,836);
  118. } elseif($ex[0]=='banner') {
  119. $banner=$folder.$f;
  120. } elseif($ex[0]=='secondary') {
  121. $c2['pic'][$ex[1]] = $folder.$f;
  122. } else {
  123. $c1['pic'][$ex[0]] = $folder.$f;
  124. }
  125. } elseif(in_array(strtolower($ext), $EXT['txt'])) {
  126. if($ex[0]=='secondary') {
  127. $c2['txt'][$ex[1]] = $folder.$f;
  128. } else {
  129. $c1['txt'][$ex[0]] = $folder.$f;
  130. }
  131. } elseif(in_array(strtolower($ext), $EXT['tpl'])) {
  132. if($ex[0]=='secondary') {
  133. $c2['tpl'][$ex[1]] = $folder.$f;
  134. } else {
  135. $c1['tpl'][$ex[0]] = $folder.$f;
  136. }
  137. } elseif (in_array(strtolower($ext), $EXT['csv'])) {
  138. if($ex[0]=='secondary') {
  139. $c2['csv'][$ex[1]] = $folder.$f;
  140. } else {
  141. $c1['csv'][$ex[0]] = $folder.$f;
  142. }
  143. } else { }
  144. }
  145. if(!$banner) {
  146. $banner = self::search_up("banner", array($folder,$f3->get("CONTENT")), $EXT['pic']);
  147. }
  148. // compose elements based on the organised array
  149. $md = new \Parsedown();
  150. foreach($c1['txt'] as $key=>$file) {
  151. $str = file_get_contents($file);
  152. $str = self::linkify($str);
  153. $str = self::get_config_from_content($str);
  154. $str = $md->text($str);
  155. $str = sprintf("<div class='post'>%s</div>", $str);
  156. $primary[$key] = sprintf("<div class=\"item %s %s\">%s</div>", $page,$key,$str);
  157. }
  158. foreach($c1['tpl'] as $key=>$file) {
  159. $str = file_get_contents($file);
  160. $str = \Template::instance()->render($file);
  161. $str = self::linkify($str);
  162. if(array_key_exists($key,$c1['pic'])) {
  163. $str = sprintf("%s\n%s",
  164. \Modules\Pictures::makeImageLink($c1['pic'][$key]),
  165. $str);
  166. unset($c1['pic'][$key]);
  167. }
  168. $primary[$key] = sprintf("<div class=\"item %s %s\">%s</div>", $page ,$key,$str);
  169. }
  170. foreach($c1['pic'] as $key=>$file) {
  171. $primary[$key] = sprintf("<img src=\"$file\" />");
  172. }
  173. foreach($c1['csv'] as $key=>$file) {
  174. $csv = new \Modules\Ography($file,TRUE);
  175. // $c1sv->filter_field("file");
  176. $str="<table>";
  177. foreach($csv->entries as $entry) {
  178. $tmp="";
  179. foreach($entry as $key=>$value) {
  180. $tmp .= sprintf("<td class=\"%s\">%s</td>", $key, $value);
  181. }
  182. $str .= sprintf("<tr>%s</tr>",$tmp);
  183. }
  184. $str.="</table>";
  185. $primary[$key] = $str;
  186. }
  187. foreach($c2['txt'] as $key=>$file) {
  188. $str = file_get_contents($file);
  189. $str = self::linkify($str);
  190. $str = self::get_config_from_content($str);
  191. $str = $md->text($str);
  192. $str = sprintf("<div class='post'>%s</div>", $str);
  193. $secondary[$key] = sprintf("<div class=\"item %s %s\">%s</div>", $page,$key,$str);
  194. }
  195. foreach($c2['tpl'] as $key=>$file) {
  196. $str = file_get_contents($file);
  197. $str = \Template::instance()->render($file);
  198. $str = self::linkify($str);
  199. if(array_key_exists($key,$c2['pic'])) {
  200. $str = sprintf("%s\n%s",
  201. \Modules\Pictures::makeImageLink($c2['pic'][$key]),
  202. $str);
  203. unset($c2['pic'][$key]);
  204. }
  205. $secondary[$key] = sprintf("<div class=\"item %s %s\">%s</div>", $page ,$key,$str);
  206. }
  207. foreach($c2['pic'] as $key=>$file) {
  208. $secondary[$key] = sprintf("<img src=\"$file\" />");
  209. }
  210. foreach($c2['csv'] as $key=>$file) {
  211. $csv = new \Modules\Ography($file,TRUE);
  212. // $c2sv->filter_field("file");
  213. $str="<table>";
  214. foreach($csv->entries as $entry) {
  215. $tmp="";
  216. foreach($entry as $key=>$value) {
  217. $tmp .= sprintf("<td class=\"%s\">%s</td>", $key, $value);
  218. }
  219. $str .= sprintf("<tr>%s</tr>",$tmp);
  220. }
  221. $str.="</table>";
  222. $secondary[$key] = $str;
  223. }
  224. // set template variables
  225. $f3->set('banner',$banner);
  226. $f3->set('background',$background);
  227. $f3->set('content', implode("\n", $primary));
  228. $f3->set('secondary_content', implode("\n", $secondary));
  229. }
  230. ////////////////
  231. // recursions //
  232. ////////////////
  233. function search_up($key,$paths,$ext) {
  234. $return = "";
  235. if(count($paths) == 2) {
  236. $current = $paths[0];
  237. $last_try = $paths[1];
  238. $ls=scandir($current);
  239. foreach($ls as $f) {
  240. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  241. $ex=explode(".", $f);
  242. if(in_array(strtolower(end($ex)),$ext)) {
  243. if($ex[0]==$key) {
  244. $return = $current.$f;
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. if ($return) {
  251. return $return;
  252. } elseif($current == $last_try) {
  253. return false;
  254. } else {
  255. $p = explode('/',$current);
  256. array_pop($p);
  257. array_pop($p);
  258. return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
  259. }
  260. }
  261. ///////////////////////
  262. // Utility functions //
  263. ///////////////////////
  264. function linkify($string) {
  265. $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
  266. $count = 0;
  267. $new = preg_replace_callback
  268. ($pattern,
  269. function($m){
  270. $f3 = \Base::instance();
  271. return $f3->get('SITE_URL')
  272. .$f3->alias($m[1],self::$keyword."=".$m[2])
  273. ;},
  274. $string);
  275. return $new;
  276. }
  277. function get_config_from_content($string) {
  278. $f3 = \Base::instance();
  279. $f = 0;
  280. $pattern = "/#\+(\w+):\s?(.*)/";
  281. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  282. foreach($matches[0] as $match) {
  283. $string = str_replace($match,"",$string);
  284. }
  285. foreach($matches[1] as $key => $match) {
  286. $f3->set('content_config_'.$match,$matches[2][$key]);
  287. }
  288. return $string;
  289. }
  290. public static function check_folder_for_backgroundimage($folder){
  291. $EXT=array(
  292. 'pic'=>array( 'jpg', 'jpeg', 'png' ),
  293. 'tpl'=>array( 'html', 'htm', 'tpl' ),
  294. 'txt'=>array( 'txt', 'text', 'md' )
  295. );
  296. $ls = scandir($folder);
  297. $background = false;
  298. foreach ($ls as $key=>$f) {
  299. if(!strncmp($f,'.',1)) continue;
  300. $ex=explode(".", $f);
  301. $ext=array_pop($ex);
  302. if(in_array(strtolower($ext), $EXT['pic'])) {
  303. if($ex[0]=='background') {
  304. $background=pic_cache($folder.$f,836);
  305. break;
  306. }
  307. }
  308. }
  309. return $background;
  310. }
  311. }