<?
if ( is_array( $this -> pages ) and !empty( $this -> pages ) ) {
  $settings = ( new \Domain\Settings\SettingsRepository( $GLOBALS['mdb'] ) )->allSettings();

  echo '<ul class="level-' . $this -> level . ' clear" level="' . $this -> level . '">';

  foreach ( $this -> pages as $page ) {
    $url = "";
    
    if ( $page['page_type'] == 3 ) {
      $page['language']['link'] ? $url = $page['language']['link'] : $url = '#';      
    } else if ( $page['page_type'] == 5 ) {
      $page['category_id'] ? $url = ( new \Domain\Category\CategoryRepository( $GLOBALS['mdb'] ) )->categoryUrl( (int)$page['category_id'], $GLOBALS['lang_id'] ) : $url = '#';
    } else {
      $page['language']['seo_link'] ? $url = '/' . $page['language']['seo_link'] : $url = '/s-' . $page['id'] . '-' . \Shared\Helpers\Helpers::seo( $page['language']['title'] );
    }
    $children = [];
    $has_children = !empty( $page['pages'] ) and is_array( $page['pages'] );

    if ( $has_children ): foreach ( $page['pages'] as $page_tmp ):
        $children[] = $page_tmp['id'];
      endforeach;
    endif;

    echo '<li id="link-' . $page['id'] . '" class="';

    if ( $page['id'] == $this -> current_page )
      echo ' active';

    if ( $has_children and in_array( $this -> current_page, $children ) )
      echo ' open';

    if ( $has_children )
      echo ' parent';

    echo '">';
    echo '<a href="';
    if ( \Shared\Helpers\Helpers::get_session( 'current-lang' ) != ( new \Domain\Languages\LanguagesRepository( $GLOBALS['mdb'] ) )->defaultLanguage() and $url != '#' and $page['page_type'] != 3 and $page['page_type'] != 5 )
      echo '/' . \Shared\Helpers\Helpers::get_session( 'current-lang' );
    echo $url . '"';
    if ( $page['language']['noindex'] )
      echo 'rel="nofollow"';
    echo ' title="' . $page['language']['title'] . '"';
    if ( $has_children )
      echo "class='menu-trigger'";
    echo '>';
    echo $page['language']['title'];
    echo '</a>';
    if ( $has_children )
      echo '<i class="fa fa-chevron-down menu-toggle" menu-id="link-' . $page['id'] . '"></i>';
    if ( $has_children )
      echo \front\Views\Menu::pages( $page['pages'], $this -> level + 1, $this -> current_page );
    echo '</li>';
  }
  echo '</ul>';
}
?>
