WikiCategory.class.php 3.6 KB

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