MenuItem.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Define a menuitem.
  4. * @author Valentin CARRUESCO
  5. * @category Plugin
  6. * @license MIT
  7. */
  8. class MenuItem extends Entity{
  9. public $id,$icon,$label,$url,$classes,$sort,$firm,$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. 'firm' => array('type'=>'firm','label'=>'Etablissement', 'link'=>'class/Firm.class.php'),
  22. 'user' => 'string',
  23. 'target' => 'string',
  24. 'editable' => 'int',
  25. 'parent' => 'int',
  26. 'menu' => 'int'
  27. );
  28. const MENU_SHORTCUT = 'shortcut-menu';
  29. public $indexes = array('slug','menu');
  30. function __construct(){
  31. parent::__construct();
  32. $this->editable = true;
  33. $this->menu = 0;
  34. $this->user = '';
  35. }
  36. public static function target($slug=null){
  37. $targets = array(
  38. 'redirect' => array('label'=>'Redirection','slug'=>'redirect','icon'=>'fas fa-link'),
  39. '_blank' => array('label'=>'Redirection (nouvel onglet)','slug'=>'_blank','icon'=>'fas fa-link'),
  40. 'iframe' => array('label'=>'IFrame','slug'=>'iframe','icon'=>'fas fa-crop'),
  41. );
  42. $undefined = array('label'=>'Non défini','slug'=>'','icon'=>'');
  43. if(!isset($slug)) return $targets;
  44. return isset($targets[$slug]) ? $targets[$slug] : $undefined;
  45. }
  46. public function visibleFor($user){
  47. if(!isset($this->visibility) || empty($this->visibility) || $user->superadmin) return true;
  48. $visibility = explode(',', $this->visibility);
  49. foreach($visibility as $entity){
  50. if(is_numeric($entity)){
  51. if($user->hasRank($entity)) return true;
  52. }else{
  53. if($user->login==$entity) return true;
  54. }
  55. }
  56. return false;
  57. }
  58. public static function bySlug($menuSlug,$user='',$firms = array()){
  59. $menuItems = array();
  60. $mainMenu = array();
  61. $query = 'SELECT it.*,me.slug as parentSlug FROM {{table}} it LEFT JOIN {{table}} me ON it.menu=me.id WHERE me.user=?';
  62. $data = array($user);
  63. if(is_array($menuSlug)){
  64. $query .= ' AND me.slug IN ('.implode(',',array_fill(0,count($menuSlug),'?')).') ';
  65. foreach($menuSlug as $slug) $data[] = $slug;
  66. }else{
  67. $query .= ' AND me.slug=? ';
  68. $data[] = $menuSlug;
  69. }
  70. if(!empty($firms)){
  71. $query .=' AND it.firm IN ('.implode(',',array_fill(0, count($firms), '?')).') ';
  72. foreach($firms as $firm)
  73. $data[] = $firm;
  74. }
  75. $query .= ' ORDER BY sort';
  76. foreach(self::staticQuery($query,$data ,true) as $dbItem){
  77. $item = $dbItem->toArray();
  78. $item['foreign'] = $dbItem->foreign();
  79. $mainMenu[] = $item;
  80. }
  81. uasort ($mainMenu , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
  82. foreach ($mainMenu as $i=>$item) {
  83. if(isset($item['parent'])) continue;
  84. $menuItems[$item['id']] = $item;
  85. $menuItems[$item['id']]['childs'] = array();
  86. unset($mainMenu[$i]);
  87. }
  88. foreach ($mainMenu as $item) {
  89. $menuItems[$item['parent']]['childs'][] = $item;
  90. }
  91. if(is_array($menuSlug)){
  92. //tri par slug
  93. $menuItemsBySlug = array();
  94. foreach($menuSlug as $slug){
  95. $menuItemsBySlug[$slug] = array();
  96. foreach($menuItems as $item){
  97. if($item['foreign']['parentSlug']!=$slug) continue;
  98. $menuItemsBySlug[$slug][] = $item;
  99. }
  100. }
  101. return $menuItemsBySlug;
  102. }else{
  103. return $menuItems;
  104. }
  105. }
  106. }
  107. ?>