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.

218 satır
6.5KB

  1. <?php
  2. namespace Modules;
  3. // class TOC
  4. class TOC {
  5. private $selector;
  6. private $folder;
  7. private $layout;
  8. private $caller_dir;
  9. private $body;
  10. private $key = "";
  11. private $elements = array();
  12. public $entries = array();
  13. private $filters = array();
  14. // $conf: array(
  15. // $selector,
  16. // $folder,
  17. // ...
  18. // )
  19. function __construct($conf,$caller_dir,$body) {
  20. $f3 = \Base::instance();
  21. //var_dump($conf);
  22. // read configuration
  23. if (is_array($conf)) {
  24. $this->selector = array_shift($conf);
  25. $folder = trim(array_shift($conf));
  26. $this->folder = strncmp("/", $folder, 1)
  27. ? $caller_dir.$folder
  28. : $f3->get('CONTENT').substr($folder,1);
  29. if (substr_compare($this->folder, "/", -1, 1)) {
  30. $this->folder .= "/";
  31. }
  32. if (count($conf)) {
  33. $this->layout = array_shift($conf);
  34. }
  35. if (count($conf)) {
  36. $this->key = array_shift($conf);
  37. }
  38. }
  39. $this->caller_dir = $caller_dir;
  40. $this->body = is_array($body) ? $body : array($body);
  41. $this->prepare_filters();
  42. $this->prepare_elements();
  43. //echo $this->folder."<br>";
  44. }
  45. function __toString() {
  46. return implode("\n",$this->entries);
  47. }
  48. function dispatch() {
  49. switch ($this->selector) {
  50. case 'event':
  51. case 'events':
  52. $this->load('\Modules\CEvent');
  53. break;
  54. case 'location':
  55. case 'locations':
  56. $this->load('\Modules\CLocation');
  57. break;
  58. case 'member':
  59. case 'members':
  60. $this->load('\Modules\CMember');
  61. break;
  62. case 'concert':
  63. case 'concerts':
  64. $this->load('\Modules\CConcert');
  65. break;
  66. }
  67. }
  68. function prepare_filters() {
  69. // Filters on Key value pairs
  70. $pattern = "/(.+)=(.+)/";
  71. foreach ($this->body as $k=>$v) {
  72. if(preg_match($pattern,$v,$matches)) {
  73. unset($this->body[$k]);
  74. if(strncmp("!",$matches[1],1)) {
  75. $this->filters['must_have'][]=array($matches[1]=>$matches[2]);
  76. } else {
  77. $this->filters['must_not_have'][]=array(substr($matches[1],1)=>$matches[2]);
  78. }
  79. }
  80. }
  81. }
  82. function apply_filters() {
  83. foreach($this->elements as $k=>$v) {
  84. $show = true;
  85. if (count($this->filters['must_have'])) {
  86. $show = false;
  87. foreach ($this->filters['must_have'] as $l=>$w) {
  88. $ke = array_keys($w);
  89. $key = $ke[0];
  90. if (array_key_exists($key,$v)) {
  91. if (is_object($v[$key]) && !strncmp("@",$w[$key],1)) {
  92. $show = (in_array(substr($w[$key],1), $v[$key]->ids))
  93. ? true
  94. : $show
  95. ;
  96. } else {
  97. $show = (trim($v[$key])==trim($w[$key]))
  98. ? true
  99. : $show
  100. ;
  101. }
  102. }
  103. }
  104. }
  105. if (count($this->filters['must_not_have'])) {
  106. foreach ($this->filters['must_not_have'] as $l=>$w) {
  107. $ke = array_keys($w);
  108. $key = $ke[0];
  109. if (array_key_exists($key,$v)) {
  110. $show = (trim($v[$key])==trim($w[$key])) ? false : $show;
  111. }
  112. }
  113. }
  114. switch ($show) {
  115. case true:
  116. break;
  117. case false:
  118. unset($this->elements[$k]);
  119. break;
  120. }
  121. }
  122. }
  123. function prepare_elements() {
  124. if(is_dir($this->folder)) {
  125. $ls=scandir($this->folder);
  126. if ($this->body) {
  127. //var_dump($this->body);
  128. foreach ($this->body as $f) {
  129. $f = trim($f);
  130. if (in_array($f,$ls)
  131. && is_dir($this->folder.$f)) {
  132. $this->elements[$this->folder.$f."/"] = true;
  133. }
  134. }
  135. } else {
  136. foreach($ls as $k=>$f) {
  137. if (!strncmp($f,'.',1)) continue;
  138. if (!is_dir($this->folder.$f)) continue;
  139. $this->elements[$this->folder.$f."/"] = true;
  140. }
  141. }
  142. }
  143. foreach ($this->elements as $key=>$value) {
  144. $folder = new FilesInFolders($key);
  145. $folder->prepare_files();
  146. $this->elements[$key] = $folder->read_config();
  147. }
  148. // can be moves before
  149. $this->apply_filters();
  150. }
  151. function string_to_key($in) {
  152. return md5($in);
  153. }
  154. function load($class) {
  155. foreach ($this->elements as $k=>$v) {
  156. $config=array('path'=>$k);
  157. $this->entries[$k] = new $class($v,$config);
  158. if ($this->layout) {
  159. if ($this->layout != 'collect') {
  160. //var_dump($this->entries[$k]);
  161. $this->entries[$k]->set_layout($this->layout);
  162. }
  163. }
  164. }
  165. if ($this->layout == 'collect' && $this->key != "") {
  166. $new = array();
  167. foreach ($this->entries as $k=>$en) {
  168. if (array_key_exists($this->key,$en->keys)) {
  169. $key_orig = $en->keys[$this->key];
  170. }
  171. $key_new = self::string_to_key($en->values[$key_orig]);
  172. if (!array_key_exists($key_new, $new)) {
  173. $new[$key_new] = array();
  174. }
  175. $new[$key_new][] = $en;
  176. }
  177. //var_dump($new);
  178. foreach ($new as $k=>$collection) {
  179. $obj = new CMultiple($collection);
  180. $obj->set_first(clone $obj->elements[0],'collected_header');
  181. $obj->set_layout('collected_entry');
  182. $new[$k] = $obj;
  183. }
  184. //var_dump($new);
  185. $this->entries = $new;
  186. }
  187. //var_dump($this->entries);
  188. }
  189. }