<?php
namespace front\factory;

class Layouts
{
  public static function layout_details( $layout_id )
  {
    global $mdb, $cache;
    
    if ( !$layout = \Cache::fetch( "layout_details:$layout_id" ) )
    {
      $layout = $mdb -> get( 'pp_layouts', '*', [ 'id' => (int)$layout_id ] );
      
      if ( !$layout )
        $layout = $mdb -> get( 'pp_layouts', '*', [ 'status' => 1 ] );
      
      \Cache::store( "layout_details:$layout_id", $layout );
    }
    return $layout;
  }
  
  public static function article_layout( $article_id )
  {
    global $mdb, $cache;
    
    if ( !$layout = \Cache::fetch( "article_layout:$article_id" ) )
    {
      $layout = $mdb -> get( 'pp_layouts', [ '[><]pp_articles' => [ 'id' => 'layout_id' ] ], '*', [ 'pp_articles.id' => (int)$article_id ] );
      
      if ( !$layout )
        $layout = $mdb -> get( 'pp_layouts', '*', [ 'status' => 1 ] );
      
      \Cache::store( "article_layout:$article_id", $layout );
    }
    return $layout;
  }
  
  public static function active_layout( $page_id )
  {
    global $mdb, $cache;
    
    if ( !$layout = \Cache::fetch( "active_layouts:$page_id" ) )
    {
      $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 ] );
      
      \Cache::store( "active_layouts:$page_id", $layout );
    }
    return $layout;
  }
}
