WikiCategory.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Define a category.
  4. * @author Valentin CARRUESCO
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. require_once(__DIR__.SLASH.'WikiPage.class.php');
  9. class WikiCategory extends Entity{
  10. public $id,$label,$icon,$color,$state,$path,$slug,$sort=0;
  11. protected $TABLE_NAME = 'wiki_category';
  12. public $fields =
  13. array(
  14. 'id' => 'key',
  15. 'label' => 'string',
  16. 'slug' => 'string',
  17. 'icon' => 'string',
  18. 'color' => 'string',
  19. 'state' => 'string',
  20. 'sort' => 'int',
  21. 'path' => 'string'
  22. );
  23. const DEFAULT_CATEGORY = 'Non classé';
  24. public function __construct(){
  25. $this->color = self::color();
  26. parent::__construct();
  27. }
  28. public static function synchronize(){
  29. $workspace = WikiPage::workspace();
  30. $defaultCategory = $workspace.SLASH.wiki_os_encode(self::DEFAULT_CATEGORY);
  31. if(!file_exists($defaultCategory)) mkdir($defaultCategory,0755,true);
  32. $existing = array();
  33. //MAJ de la base pour les dossiers supprimés
  34. foreach(self::loadAll() as $category){
  35. if(!file_exists($workspace.SLASH.wiki_os_encode($category->path)))
  36. self::deleteById($category->id);
  37. $existing[$category->path] = $category;
  38. }
  39. //MAJ de la base pour les nouveaux dossiers
  40. foreach(glob($workspace.SLASH.'*') as $i=>$folder){
  41. $path = str_replace($workspace.SLASH,'',wiki_os_decode($folder));
  42. if(isset($existing[$path])) continue;
  43. if(is_dir($folder)){
  44. $category = new self();
  45. $category->label = wiki_os_decode(basename($folder));
  46. $category->slug = slugify($category->label);
  47. $category->path = $path;
  48. $category->sort = $i;
  49. $category->icon = 'far fa-bookmark';
  50. $category->save();
  51. } else {
  52. rename($folder, $defaultCategory.SLASH.basename($folder));
  53. }
  54. }
  55. }
  56. public function pages(){
  57. $pages = array();
  58. $workspace = WikiPage::workspace();
  59. $existing = array();
  60. foreach(WikiPage::loadAll(array('category'=>$this->id)) as $page){
  61. if(!file_exists($workspace.SLASH.wiki_os_encode($page->path))){
  62. WikiPage::deleteById($page->id);
  63. continue;
  64. }
  65. $existing[$page->path] = $page;
  66. }
  67. foreach(glob($workspace.SLASH.wiki_os_encode($this->path).SLASH.'*.md') as $i=>$file){
  68. $path = str_replace($workspace.SLASH,'',wiki_os_decode($file));
  69. if(!is_file($file) || isset($existing[$path]) || !file_exists($file)) continue;
  70. $page = new WikiPage();
  71. $page->label = preg_replace('|\.md|i','',wiki_os_decode(basename($file)));
  72. $page->slug = slugify($page->label);
  73. $page->path = $path;
  74. $page->content = file_get_contents(wiki_os_decode($file));
  75. $page->category = $this->id;
  76. $page->sort = $i;
  77. $page->state = WikiPage::PUBLISHED;
  78. $page->save();
  79. }
  80. $pages = WikiPage::loadAll(array('category'=>$this->id),array('sort','label'));
  81. return $pages;
  82. }
  83. public static function deleteById($id){
  84. $category = self::getById($id);
  85. parent::deleteById($id);
  86. $workspace = WikiPage::workspace();
  87. $path = $workspace.SLASH.wiki_os_encode($category->path);
  88. if(file_exists($path) && is_dir($path)) self::remove_directory($path);
  89. }
  90. public static function remove_directory($dir){
  91. if(!is_dir($dir)) return;
  92. foreach (glob($dir.SLASH.'*') as $file) {
  93. if(is_dir($file)){
  94. self::remove_directory($file);
  95. } else {
  96. unlink($file);
  97. }
  98. }
  99. rmdir($dir);
  100. }
  101. public static function color(){
  102. $colors = array(
  103. '#4E5C6E',
  104. '#19B7FF',
  105. '#19B7FF',
  106. '#7F6BFF',
  107. '#FC7419',
  108. '#FC2D2D',
  109. '#FFE100',
  110. '#14CF9F',
  111. '#EE84F0',
  112. '#2F362F'
  113. );
  114. return $colors[array_rand($colors)];
  115. }
  116. public function author(){
  117. return User::byLogin($this->creator)->fullName();
  118. }
  119. public function created(){
  120. return relative_time($this->created);
  121. }
  122. public function updater(){
  123. return User::byLogin($this->updater)->fullName();
  124. }
  125. public function updated(){
  126. return relative_time($this->updated);
  127. }
  128. }
  129. ?>