mostly filebased Content Presentation System
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

toc.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. case 'publication':
  67. case 'publications':
  68. $this->load('\Modules\CPublication');
  69. break;
  70. }
  71. }
  72. function prepare_filters() {
  73. // Filters on Key value pairs
  74. $pattern = "/(.+)=(.+)/";
  75. foreach ($this->body as $k=>$v) {
  76. if(preg_match($pattern,$v,$matches)) {
  77. unset($this->body[$k]);
  78. if(strncmp("!",$matches[1],1)) {
  79. $this->filters['must_have'][]=array($matches[1]=>$matches[2]);
  80. } else {
  81. $this->filters['must_not_have'][]=array(substr($matches[1],1)=>$matches[2]);
  82. }
  83. }
  84. }
  85. }
  86. function apply_filters() {
  87. foreach($this->elements as $k=>$v) {
  88. $show = true;
  89. if (array_key_exists('must_have', $this->filters) && count($this->filters['must_have'])) {
  90. $show = false;
  91. foreach ($this->filters['must_have'] as $l=>$w) {
  92. $ke = array_keys($w);
  93. $key = $ke[0];
  94. if (array_key_exists($key,$v)) {
  95. if (is_object($v[$key]) && !strncmp("@",$w[$key],1)) {
  96. $show = (in_array(substr($w[$key],1), $v[$key]->ids))
  97. ? true
  98. : $show
  99. ;
  100. } else {
  101. $show = (trim($v[$key])==trim($w[$key]))
  102. ? true
  103. : $show
  104. ;
  105. }
  106. }
  107. }
  108. }
  109. if (array_key_exists('must_not_have', $this->filters) && count($this->filters['must_not_have'])) {
  110. foreach ($this->filters['must_not_have'] as $l=>$w) {
  111. $ke = array_keys($w);
  112. $key = $ke[0];
  113. if (array_key_exists($key,$v)) {
  114. $show = (trim($v[$key])==trim($w[$key])) ? false : $show;
  115. }
  116. }
  117. }
  118. switch ($show) {
  119. case true:
  120. break;
  121. case false:
  122. unset($this->elements[$k]);
  123. break;
  124. }
  125. }
  126. }
  127. function prepare_elements() {
  128. if(is_dir($this->folder)) {
  129. $ls=scandir($this->folder);
  130. if ($this->body) {
  131. //var_dump($this->body);
  132. foreach ($this->body as $f) {
  133. $f = trim($f);
  134. if (in_array($f,$ls)
  135. && is_dir($this->folder.$f)) {
  136. $this->elements[$this->folder.$f."/"] = true;
  137. }
  138. }
  139. } else {
  140. foreach($ls as $k=>$f) {
  141. if (!strncmp($f,'.',1)) continue;
  142. if (!is_dir($this->folder.$f)) continue;
  143. $this->elements[$this->folder.$f."/"] = true;
  144. }
  145. }
  146. }
  147. foreach ($this->elements as $key=>$value) {
  148. $folder = new FilesInFolders($key);
  149. $folder->prepare_files();
  150. $this->elements[$key] = $folder->read_config();
  151. }
  152. // can be moves before
  153. $this->apply_filters();
  154. }
  155. function string_to_key($in) {
  156. return md5($in);
  157. }
  158. function load($class) {
  159. foreach ($this->elements as $k=>$v) {
  160. $config=array('path'=>$k);
  161. $this->entries[$k] = new $class($v,$config);
  162. if ($this->layout) {
  163. if ($this->layout != 'collect') {
  164. //var_dump($this->entries[$k]);
  165. $this->entries[$k]->set_layout($this->layout);
  166. }
  167. }
  168. }
  169. if ($this->layout == 'collect' && $this->key != "") {
  170. $new = array();
  171. foreach ($this->entries as $k=>$en) {
  172. if (array_key_exists($this->key,$en->keys)) {
  173. $key_orig = $en->keys[$this->key];
  174. }
  175. $key_new = self::string_to_key($en->values[$key_orig]);
  176. if (!array_key_exists($key_new, $new)) {
  177. $new[$key_new] = array();
  178. }
  179. $new[$key_new][] = $en;
  180. }
  181. //var_dump($new);
  182. foreach ($new as $k=>$collection) {
  183. $obj = new CMultiple($collection);
  184. $obj->set_first(clone $obj->elements[0],'collected_header');
  185. $obj->set_layout('collected_entry');
  186. $new[$k] = $obj;
  187. }
  188. //var_dump($new);
  189. $this->entries = $new;
  190. }
  191. //var_dump($this->entries);
  192. }
  193. }