<?
namespace admin\factory;

class Pages
{
  public static $_page_types = [ 0 => 'pełne artykuły', 1 => 'wprowadzenia', 2 => 'miniaturki', 3 => 'link', 4 => 'kontakt', 5 => 'kategoria sklepu' ];
  public static $_sort_types = [
                  0 => 'data dodania - najstarsze na początku',
                  1 => 'data dodania - najnowsze na początku',
                  2 => 'data modyfikacji - rosnąco',
                  3 => 'data mofyfikacji - malejąco',
                  4 => 'ręczne',
                  5 => 'alfabetycznie - A - Z',
                  6 => 'alfabetycznie - Z - A'
                ];

  public static function save_articles_order( $page_id, $articles )
  {
    global $mdb;

    if ( is_array( $articles ) )
    {
      $mdb -> update( 'pp_articles_pages', [ 'o' => 0 ], [ 'page_id' => (int)$page_id ] );

      for ( $i = 0; $i < count( $articles ); $i++ )
      {
        if ( $articles[$i]['item_id'] )
        {
          $x++;
          $mdb -> update( 'pp_articles_pages', [ 'o' => $x ], [ 'AND' => [ 'page_id' => (int)$page_id, 'article_id' => $articles[ $i ]['item_id'] ] ] );
        }
      }
    }
    return true;
  }

  public static function page_articles( $page_id )
  {
    global $mdb;

    $results = $mdb -> query( 'SELECT '
                                  . 'article_id, o, status '
                                . 'FROM '
                                  . 'pp_articles_pages AS ap '
                                    . 'INNER JOIN pp_articles AS a ON a.id = ap.article_id '
                                . 'WHERE '
                                  . 'page_id = ' . (int)$page_id . ' AND status != -1 '
                                . 'ORDER BY '
                                  . 'o ASC' ) -> fetchAll();
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $row['title'] = \admin\factory\Articles::article_title( $row['article_id'] );
      $articles[] = $row;
    }
    return $articles;
  }

  public static function menus_list()
  {
    global $mdb;
    return $mdb -> select( 'pp_menus', '*', [ 'ORDER' => [ 'id' => 'ASC' ] ] );
  }

  public static function save_pages_order( $menu_id, $pages )
  {
    global $mdb;

    if ( is_array( $pages ) )
    {
      $mdb -> update( 'pp_pages', [ 'o' => 0 ], [ 'menu_id' => (int)$menu_id ] );

      for ( $i = 0; $i < count( $pages ); $i++ )
      {
        if ( $pages[$i]['item_id'] )
        {
          $pages[$i]['parent_id'] ? $parent_id = $pages[$i]['parent_id'] : $parent_id = 0;

          if ( $pages[$i]['item_id'] && $pages[$i]['depth'] > 1 )
          {
            if ( $pages[$i]['depth'] == 2 )
              $parent_id = null;

            $x++;

            $mdb -> update( 'pp_pages', [ 'o' => $x, 'parent_id' => $parent_id ], [ 'id' => (int)$pages[ $i ]['item_id'] ] );
          }
        }
      }
    }

    \S::delete_dir( '../temp/' );

    return true;
  }

  public static function page_delete( $page_id )
  {
		global $mdb;

    if ( $mdb -> count( 'pp_pages', [ 'parent_id' => (int)$page_id ] ) )
      return false;

    return $mdb -> delete( 'pp_pages', [ 'id' => (int)$page_id ] );
  }

  public static function max_order()
  {
    global $mdb;
    return $mdb -> max( 'pp_pages', 'o' );
  }

  public static function page_save(
          $page_id, $title, $seo_link, $meta_title, $meta_description, $meta_keywords, $menu_id, $parent_id, $page_type, $sort_type, $layout_id, $articles_limit, $show_title, $status,
          $link, $noindex, $start, $page_title, $canonical, $category_id
        )
  {
    global $mdb;

    if ( !$parent_id )
      $parent_id = null;

    if ( !$page_id )
    {
      $order = self::max_order() + 1;

      $mdb -> insert( 'pp_pages', [
              'menu_id'         => (int)$menu_id,
              'page_type'       => $page_type,
              'sort_type'       => $sort_type,
              'articles_limit'  => $articles_limit,
              'show_title'      => $show_title == 'on' ? 1 : 0,
              'status'          => $status == 'on' ? 1 : 0,
              'o'               => (int)$order,
              'parent_id'       => $parent_id,
              'start'           => $start == 'on' ? 1 : 0,
              'category_id' => ( $category_id and $page_type == 5 ) ? $category_id : null
      ] );

      $id = $mdb -> id();

      if ( $id )
      {
        if ( $start )
          $mdb -> update( 'pp_pages', [ 'start' => 0 ], [ 'id[!]' => (int)$id ] );

        if ( $layout_id )
          $mdb -> insert( 'pp_layouts_pages', [ 'page_id' => (int)$id, 'layout_id' => (int)$layout_id ] );

        foreach ( $title as $key => $val )
        {
          $mdb -> insert( 'pp_pages_langs', [
            'page_id' => (int)$id,
            'lang_id' => $key,
            'title' => $title[$key] != '' ? $title[$key] : null,
            'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
            'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
            'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
            'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
            'noindex' => $noindex[$key],
            'page_title' => $page_title[$key] != '' ? $page_title[$key] : null,
            'link' => ( $link[$key] and $page_type == 3 ) ? $link[$key] : null,
            'canonical' => $canonical[$key] != '' ? $canonical[$key] : null
          ] );
        }

        \S::htacces();
        \S::delete_dir( '../temp/' );
        return $id;
      }
    }
    else
    {
      $mdb -> update( 'pp_pages', [
        'menu_id'         => (int)$menu_id,
        'page_type'       => $page_type,
        'sort_type'       => $sort_type,
        'articles_limit'  => $articles_limit,
        'show_title'      => $show_title == 'on' ? 1 : 0,
        'status'          => $status == 'on' ? 1 : 0,
        'parent_id'       => $parent_id,
        'start'           => $start == 'on' ? 1 : 0,
        'category_id' => ( $category_id and $page_type == 5 ) ? $category_id : null
      ], [
        'id' => (int)$page_id
      ] );

      if ( $layout_id )
      {
        $mdb -> delete( 'pp_layouts_pages', [ 'page_id' => (int)$page_id ] );
        $mdb -> insert( 'pp_layouts_pages', [ 'layout_id' => (int)$layout_id, 'page_id' => (int)$page_id ] );
      }

      if ( $start )
        $mdb -> update( 'pp_pages', [ 'start' => 0 ], [ 'id[!]' => (int)$page_id ] );

      foreach ( $title as $key => $val )
      {
        if ( $translation_id = $mdb -> get( 'pp_pages_langs', 'id', [ 'AND' => [ 'page_id' => $page_id, 'lang_id' => $key ] ] ) )
          $mdb -> update( 'pp_pages_langs', [
            'lang_id' => $key,
            'title' => $title[$key] != '' ? $title[$key] : null,
            'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
            'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
            'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
            'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
            'noindex' => $noindex[$key],
            'page_title' => $page_title[$key] != '' ? $page_title[$key] : null,
            'link' => ( $link[$key] and $page_type == 3 ) ? $link[$key] : null,
            'canonical' => $canonical[$key] != '' ? $canonical[$key] : null
          ], [
            'id' => $translation_id
          ] );
        else
          $mdb -> insert( 'pp_pages_langs', [
            'page_id' => (int)$page_id,
            'lang_id' => $key,
            'title' => $title[$key] != '' ? $title[$key] : null,
            'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
            'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
            'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
            'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
            'noindex' => $noindex[$key],
            'page_title' => $page_title[$key] != '' ? $page_title[$key] : null,
            'link' => ( $link[$key] and $page_type == 3 ) ? $link[$key] : null,
            'canonical' => $canonical[$key] != '' ? $canonical[$key] : null
          ] );
      }

      self::update_supages_menu_id( $page_id, $menu_id );

      \S::htacces();
      \S::delete_dir( '../temp/' );

      return $page_id;
    }
    return false;
  }

  public static function update_supages_menu_id( $parent_id, $menu_id )
  {
    global $mdb;

    $mdb -> update( 'pp_pages', [ 'menu_id' => (int)$menu_id ], [ 'parent_id' => $parent_id ] );

    $results = $mdb -> select( 'pp_pages', [ 'id' ], [ 'parent_id' => $parent_id ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
      self::update_supages_menu_id( $row['id'], $menu_id );
  }

  public static function generate_seo_link( $title, $page_id, $article_id, $category_id )
  {
    global $mdb;

    $seo_link = \S::seo( $title );

    while ( !$seo_link_check )
    {
      if ( $mdb -> count( 'pp_pages_langs', [ 'AND' => [ 'seo_link' => $seo_link, 'page_id[!]' => (int)$page_id ] ] ) )
        $seo_link = \S::seo( $title ) . '-' . ( ++$i );
      else
        $seo_link_check = true;
    }

    $seo_link_check = false;

    while ( !$seo_link_check )
    {
      if ( $mdb -> count( 'pp_articles_langs', [ 'AND' => [ 'seo_link' => $seo_link, 'article_id[!]' => (int)$article_id ] ] ) )
        $seo_link = \S::seo( $title ) . '-' . ( ++$i );
      else
        $seo_link_check = true;
    }

    $seo_link_check = false;

    while ( !$seo_link_check )
    {
      if ( $mdb -> count( 'pp_shop_categories_langs', [ 'AND' => [ 'seo_link' => $seo_link, 'category_id[!]' => (int)$category_id ] ] ) )
        $seo_link = \S::seo( $title ) . '-' . ( ++$i );
      else
        $seo_link_check = true;
    }

    return $seo_link;
  }

  public static function menu_delete( $menu_id )
  {
		global $mdb;

    if ( $mdb -> count( 'pp_pages', [ 'menu_id' => (int)$menu_id ] ) )
      return false;

    return $mdb -> delete( 'pp_menus', [ 'id' => (int)$menu_id ] );
  }

  public static function menu_details( $menu_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_menus', '*', [ 'id' => (int)$menu_id ] );
  }

  public static function menu_save( $menu_id, $name, $status )
  {
    global $mdb;

    $status == 'on' ? $status = 1 : $status = 0;

    if ( !$menu_id )
    {
      return $mdb -> insert( 'pp_menus', [
                      'name' => $name,
                      'status' => $status
                    ] );
    }
    else
    {
      $mdb -> update( 'pp_menus', [
                      'name' => $name,
                      'status' => $status
                    ], [
                      'id' => (int)$menu_id
                    ] );

      return true;
    }
    return false;
  }

  public static function menu_lists()
  {
    global $mdb;
    return $mdb -> select( 'pp_menus', '*', [ 'ORDER' => [ 'id' => 'ASC' ] ] );
  }

  public static function page_details( $page_id )
  {
    global $mdb;

    $page = $mdb -> get( 'pp_pages', '*', [ 'id' => (int)$page_id ] );

    $results = $mdb -> select( 'pp_pages_langs', '*', [ 'page_id' => (int)$page_id ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $page['languages'][ $row['lang_id'] ] = $row;

    $page['layout_id'] = $mdb -> get( 'pp_layouts_pages', 'layout_id', [ 'page_id' => (int)$page_id ] );

    return $page;
  }

  public static function page_url( $page_id )
  {
    global $mdb;

    $results = $mdb -> query( "SELECT seo_link FROM pp_pages_langs AS ppl, pp_langs AS pl WHERE lang_id = pl.id AND page_id = " . (int)$page_id . " AND seo_link != '' ORDER BY o ASC LIMIT 1" ) -> fetchAll();
    if ( !$results[0]['seo_link'] )
    {
      $title = self::page_title( $article_id );
      return 's-' . $page_id . '-' . \S::seo( $title );
    }
    else
      return $results[0]['seo_link'];
  }

  public static function page_title( $page_id )
  {
    global $mdb;

    $result = $mdb -> select( 'pp_pages_langs', [
                        '[><]pp_langs' => [ 'lang_id' => 'id' ]
                      ], 'title', [
                        'AND' => [
                          'page_id' => (int)$page_id, 'title[!]' => ''
                        ],
                        'ORDER' => [ 'o' => 'ASC' ],
                        'LIMIT' => 1
                      ] );
    return $result[0];
  }

  public static function page_languages( $page_id )
  {
    global $mdb;
    return $mdb -> select( 'pp_pages_langs', '*',
                    [ 'AND' => [ 'page_id' => (int) $page_id, 'title[!]' => null ] ] );
  }

	public static function menu_pages( $menu_id, $parent_id = null )
  {
    global $mdb;

    $results = $mdb -> select( 'pp_pages',
            [ 'id', 'menu_id', 'status', 'parent_id', 'start' ],
            [ 'AND' => [ 'menu_id' => $menu_id, 'parent_id' => $parent_id ], 'ORDER' => [ 'o' => 'ASC' ] ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $row['title']     = self::page_title( $row['id'] );
      $row['languages'] = self::page_languages( $row['id'] );
      $row['subpages']  = self::menu_pages( $menu_id, $row['id'] );

      $pages[] = $row;
    }

    return $pages;
	}
}
?>