MenuItem.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Define a menuitem.
  4. * @author Valentin CARRUESCO
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. class MenuItem extends Entity{
  9. public $id,$icon,$label,$url,$classes,$sort,$parent,$user,$target,$editable,$slug,$childs,$visibility,$menu;
  10. public $TABLE_NAME = 'menuitem';
  11. public $fields =
  12. array(
  13. 'id' => 'key',
  14. 'icon' => 'string',
  15. 'label' => 'string',
  16. 'slug' => 'string',
  17. 'url' => 'longstring',
  18. 'visibility' => 'longstring',
  19. 'classes' => 'string',
  20. 'sort' => 'int',
  21. 'user' => 'string',
  22. 'target' => 'string',
  23. 'editable' => 'int',
  24. 'parent' => 'int',
  25. 'menu' => 'int'
  26. );
  27. const MENU_SHORTCUT = 'shortcut-menu';
  28. public $indexes = array('slug','menu');
  29. function __construct(){
  30. parent::__construct();
  31. $this->editable = true;
  32. $this->menu = 0;
  33. $this->user = '';
  34. }
  35. public static function target($slug=null){
  36. $targets = array(
  37. 'redirect' => array('label'=>'Redirection','slug'=>'redirect','icon'=>'fas fa-link'),
  38. '_blank' => array('label'=>'Redirection (nouvel onglet)','slug'=>'_blank','icon'=>'fas fa-link'),
  39. 'iframe' => array('label'=>'IFrame','slug'=>'iframe','icon'=>'fas fa-crop'),
  40. );
  41. $undefined = array('label'=>'Non défini','slug'=>'','icon'=>'');
  42. if(!isset($slug)) return $targets;
  43. return isset($targets[$slug]) ? $targets[$slug] : $undefined;
  44. }
  45. public function visibleFor($user){
  46. if(!isset($this->visibility) || $this->visibility=='' || $user->superadmin) return true;
  47. $visibility = explode(',', $this->visibility);
  48. foreach($visibility as $entity){
  49. if(is_numeric($entity)){
  50. if($user->hasRank($entity)) return true;
  51. }else{
  52. if($user->login==$entity) return true;
  53. }
  54. }
  55. return false;
  56. }
  57. public static function bySlug($menuSlug,$user=''){
  58. $menuItems = array();
  59. $mainMenu = array();
  60. foreach(self::staticQuery('SELECT it.* FROM {{table}} it LEFT JOIN {{table}} me ON it.menu=me.id WHERE me.slug=? and me.user=? ORDER BY sort',array($menuSlug,$user),true) as $dbItem){
  61. $mainMenu[] = $dbItem->toArray();
  62. }
  63. uasort ($mainMenu , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
  64. foreach ($mainMenu as $i=>$item) {
  65. if(isset($item['parent'])) continue;
  66. $menuItems[$item['id']] = $item;
  67. $menuItems[$item['id']]['childs'] = array();
  68. unset($mainMenu[$i]);
  69. }
  70. foreach ($mainMenu as $item) {
  71. $menuItems[$item['parent']]['childs'][] = $item;
  72. }
  73. return $menuItems;
  74. }
  75. }
  76. ?>