mostly filebased Content Presentation System

230 lines
7.7KB

  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(\Base $f3) {
  26. self::index($f3, array('page'=>'home'));
  27. }
  28. function secondLevel(\Base $f3,$params) {
  29. self::index($f3, array('page'=>$params['level1']."/".$params['level2']));
  30. }
  31. function thirdLevel(\Base $f3,$params) {
  32. self::index($f3, array('page'=>$params['level1']."/".$params['level2']."/".$params['level3']));
  33. }
  34. function fourthLevel(\Base $f3,$params) {
  35. self::index($f3, array('page'=>$params['level1']."/".$params['level2']."/".$params['level3']."/".$params['level4']));
  36. }
  37. //////////////////
  38. // main program //
  39. //////////////////
  40. function folder2() {
  41. $f3 = \Base::instance();
  42. $page = $f3->get('page');
  43. $f3->set('current_page_folder', $f3->get('CONTENT').$page."/");
  44. $folder = new \Modules\FilesInFolders(
  45. $f3->get('current_page_folder'),
  46. array(
  47. 'content'=>array(
  48. 'secondary'=>'secondary',
  49. 'zzz'=>'hidden',
  50. 'unpublish'=>'hidden'
  51. ),
  52. 'keyfiles'=>array(
  53. 'banner'=>array(
  54. 'until'=>$f3->get("CONTENT"),
  55. 'type'=>'pic'
  56. ),
  57. 'background'=>array(
  58. 'until'=>$f3->get("CONTENT"),
  59. 'type'=>'pic'
  60. ),
  61. 'colors'=>array(
  62. 'until'=>$f3->get("CONTENT"),
  63. 'type'=>'txt'
  64. )
  65. )
  66. )
  67. );
  68. $folder->prepare_files();
  69. foreach($f3->get('customVars') as $key=>$value) {
  70. $f3->set($key,$value);
  71. $this->register_key($key,$folder);
  72. }
  73. $folder->fill_content();
  74. foreach($f3->get('siteColors') as $k=>$v){
  75. $f3->set($k,
  76. array_key_exists($k,$folder->config)
  77. ? $folder->config[$k]
  78. : $v
  79. );
  80. }
  81. $f3->set('hasBanner',
  82. array_key_exists('includeBanner',$folder->config)
  83. ? $folder->config['includeBanner']
  84. : $f3->get('templateVars.includeBanner')
  85. );
  86. if ($f3->get('hasBanner')) {
  87. //$banner = new \Modules\CachedImage($folder->extras['banner']);
  88. $f3->set('banner',$folder->extras['banner']);//$banner->get_src(2000));
  89. }
  90. if ($folder->extras['background']
  91. && !$folder->config['supressBackground']) {
  92. //$background = new \Modules\CachedImage($folder->extras['background']);
  93. //$f3->set('backgroundImage',$background->get_src(3000));
  94. $background = $folder->extras['background'];
  95. $f3->set('backgroundImage',$background);
  96. }
  97. // $f3->set('background',$background);
  98. $f3->set('content', implode("\n", $folder->content['default']));
  99. $f3->set('secondary_content', implode("\n", $folder->content['secondary']));
  100. }
  101. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  103. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  107. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  110. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  111. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. ////////////////
  113. // recursions //
  114. ////////////////
  115. function search_up($key,$paths,$ext) {
  116. $return = "";
  117. if(count($paths) == 2) {
  118. $current = $paths[0];
  119. $last_try = $paths[1];
  120. $ls=scandir($current);
  121. foreach($ls as $f) {
  122. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  123. $ex=explode(".", $f);
  124. if(in_array(strtolower(end($ex)),$ext)) {
  125. if($ex[0]==$key) {
  126. $return = $current.$f;
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. if ($return) {
  133. return $return;
  134. } elseif($current == $last_try) {
  135. return false;
  136. } else {
  137. $p = explode('/',$current);
  138. array_pop($p);
  139. array_pop($p);
  140. return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
  141. }
  142. }
  143. ///////////////////////
  144. // Utility functions //
  145. ///////////////////////
  146. function register_key($name, &$ff) {
  147. $f3 = \Base::instance();
  148. if (array_key_exists($name,$ff->config)) {
  149. $f3->set($name,$ff->config[$name]);
  150. }
  151. }
  152. function linkify($string) {
  153. $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
  154. $count = 0;
  155. $new = preg_replace_callback
  156. ($pattern,
  157. function($m){
  158. $f3 = \Base::instance();
  159. return $f3->get('SITE_URL')
  160. .$f3->alias($m[1],self::$keyword."=".$m[2])
  161. ;},
  162. $string);
  163. return $new;
  164. }
  165. function get_config_from_content($string) {
  166. $f3 = \Base::instance();
  167. $f = 0;
  168. $pattern = "/#\+(\w+):\s?(.*)/";
  169. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  170. foreach($matches[0] as $match) {
  171. $string = str_replace($match,"",$string);
  172. }
  173. foreach($matches[1] as $key => $match) {
  174. $f3->set('content_config_'.$match,$matches[2][$key]);
  175. }
  176. return $string;
  177. }
  178. public static function check_folder_for_backgroundimage($folder){
  179. $EXT=array(
  180. 'pic'=>array( 'jpg', 'jpeg', 'png' ),
  181. 'tpl'=>array( 'html', 'htm', 'tpl' ),
  182. 'txt'=>array( 'txt', 'text', 'md' )
  183. );
  184. $ls = scandir($folder);
  185. $background = false;
  186. foreach ($ls as $key=>$f) {
  187. if(!strncmp($f,'.',1)) continue;
  188. $ex=explode(".", $f);
  189. $ext=array_pop($ex);
  190. if(in_array(strtolower($ext), $EXT['pic'])) {
  191. if($ex[0]=='background') {
  192. $background=pic_cache($folder.$f,836);
  193. break;
  194. }
  195. }
  196. }
  197. return $background;
  198. }
  199. }