MenuItem.class.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public $indexes = array('slug','menu');
  28. function __construct(){
  29. parent::__construct();
  30. $this->editable = true;
  31. $this->menu = 0;
  32. $this->user = '';
  33. }
  34. public static function target($slug=null){
  35. $targets = array(
  36. 'redirect' => array('label'=>'Redirection','slug'=>'redirect','icon'=>'fas fa-link'),
  37. '_blank' => array('label'=>'Redirection (nouvel onglet)','slug'=>'_blank','icon'=>'fas fa-link'),
  38. 'iframe' => array('label'=>'IFrame','slug'=>'iframe','icon'=>'fas fa-crop'),
  39. );
  40. $undefined = array('label'=>'Non définis','slug'=>'','icon'=>'');
  41. if(!isset($slug)) return $targets;
  42. return isset($targets[$slug]) ? $targets[$slug] : $undefined;
  43. }
  44. public function visibleFor($user){
  45. if(!isset($this->visibility) || $this->visibility=='' || $user->superadmin) return true;
  46. $visibility = explode(',', $this->visibility);
  47. foreach($visibility as $entity){
  48. if(is_numeric($entity)){
  49. if($user->hasRank($entity)) return true;
  50. }else{
  51. if($user->login==$entity) return true;
  52. }
  53. }
  54. return false;
  55. }
  56. public static function bySlug($menuSlug,$user=''){
  57. $menuItems = array();
  58. $mainMenu = array();
  59. 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){
  60. $mainMenu[] = $dbItem->toArray();
  61. }
  62. uasort ($mainMenu , function($a,$b){return $a['sort']>$b['sort']?1:-1;});
  63. foreach ($mainMenu as $i=>$item) {
  64. if(isset($item['parent'])) continue;
  65. $menuItems[$item['id']] = $item;
  66. $menuItems[$item['id']]['childs'] = array();
  67. unset($mainMenu[$i]);
  68. }
  69. foreach ($mainMenu as $item) {
  70. $menuItems[$item['parent']]['childs'][] = $item;
  71. }
  72. return $menuItems;
  73. }
  74. }
  75. ?>