<?php
namespace front\factory;
class Layouts
{
  static public function category_default_layout()
  {
    global $mdb;
    return $mdb -> get( 'pp_layouts', 'id', [ 'categories_default' => 1 ] );
  }

  static public function default_layout()
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\Layouts::default_layout";

    $objectData = $cacheHandler -> get( $cacheKey );
    if ( $objectData )
    {
      $cachedLayout = @unserialize( $objectData );
      if ( is_array( $cachedLayout ) and !empty( $cachedLayout ) )
        return $cachedLayout;

      $cacheHandler -> delete( $cacheKey );
    }

    $layout = $mdb -> get( 'pp_layouts', '*', [ 'status' => 1 ] );
    $cacheHandler -> set( $cacheKey, $layout );

    return $layout;
  }

  static public function product_layout( $product_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\Layouts::product_layout:$product_id";

    $objectData = $cacheHandler -> get( $cacheKey );
    if ( $objectData )
    {
      $cachedLayout = @unserialize( $objectData );
      if ( is_array( $cachedLayout ) and !empty( $cachedLayout ) )
        return $cachedLayout;

      $cacheHandler -> delete( $cacheKey );
    }

    $layoutRows = $mdb -> query(
      "SELECT pp_layouts.* 
       FROM pp_layouts 
       JOIN pp_shop_products ON pp_layouts.id = pp_shop_products.layout_id 
       WHERE pp_shop_products.id = " . (int)$product_id . " 
       ORDER BY pp_layouts.id DESC"
    ) -> fetchAll( \PDO::FETCH_ASSOC );

    if ( is_array( $layoutRows ) and isset( $layoutRows[0] ) )
      $layout = $layoutRows[0];
    else
    {
      $layoutRows = $mdb -> query(
        "SELECT pp_layouts.* 
         FROM pp_layouts 
         JOIN pp_layouts_categories ON pp_layouts.id = pp_layouts_categories.layout_id 
         JOIN pp_shop_products_categories ON pp_shop_products_categories.category_id = pp_layouts_categories.category_id 
         WHERE pp_shop_products_categories.product_id = " . (int)$product_id . " 
         ORDER BY pp_shop_products_categories.o ASC, pp_layouts.id DESC"
      ) -> fetchAll( \PDO::FETCH_ASSOC );

      if ( is_array( $layoutRows ) and isset( $layoutRows[0] ) )
        $layout = $layoutRows[0];
      else
        $layout = $mdb -> get( 'pp_layouts', '*', [ 'categories_default' => 1 ] );
    }

    if ( !$layout )
      $layout = $mdb -> get( 'pp_layouts', '*', [ 'status' => 1 ] );

    $cacheHandler -> set( $cacheKey, $layout );

    return $layout;
  }

  static public function article_layout( $article_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\Layouts::article_layout:$article_id";

    $objectData = $cacheHandler -> get( $cacheKey );

    if ( !$objectData )
    {
      $layout = $mdb -> get( 'pp_layouts', [ '[><]pp_articles' => [ 'id' => 'layout_id' ] ], '*', [ 'pp_articles.id' => (int)$article_id ] );

      $cacheHandler -> set( $cacheKey, $layout );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $layout;
  }

  static public function category_layout( $category_id )
  {
    global $mdb;

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\Layouts::category_layout:$category_id";

    $objectData = $cacheHandler -> get( $cacheKey );
    if ( $objectData )
    {
      $cachedLayout = @unserialize( $objectData );
      if ( is_array( $cachedLayout ) and !empty( $cachedLayout ) )
        return $cachedLayout;

      $cacheHandler -> delete( $cacheKey );
    }

    $layoutRows = $mdb -> query(
      "SELECT pp_layouts.* 
       FROM pp_layouts 
       JOIN pp_layouts_categories ON pp_layouts.id = pp_layouts_categories.layout_id 
       WHERE pp_layouts_categories.category_id = " . (int)$category_id . " 
       ORDER BY pp_layouts.id DESC"
    ) -> fetchAll( \PDO::FETCH_ASSOC );

    if ( is_array( $layoutRows ) and isset( $layoutRows[0] ) )
      $layout = $layoutRows[0];
    else
      $layout = $mdb -> get( 'pp_layouts', '*', [ 'categories_default' => 1 ] );

    if ( !$layout )
      $layout = $mdb -> get( 'pp_layouts', '*', [ 'status' => 1 ] );

    $cacheHandler -> set( $cacheKey, $layout );

    return $layout;
  }

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

    $cacheHandler = new \Shared\Cache\CacheHandler();
    $cacheKey = "\front\factory\Layouts::active_layout:$page_id";

    $objectData = $cacheHandler -> get( $cacheKey );

    if ( !$objectData )
    {
      $layout = $mdb -> get( 'pp_layouts', [ '[><]pp_layouts_pages' => [ 'id' => 'layout_id' ] ], '*', [ 'page_id' => (int)$page_id ] );

      if ( !$layout )
        $layout = $mdb -> get( 'pp_layouts', '*', [ 'status' => 1 ] );

      $cacheHandler -> set( $cacheKey, $layout );
    }
    else
    {
      return unserialize( $objectData );
    }

    return $layout;
  }
}
