<?php
namespace admin\factory;

class Layouts
{
  public static function layout_delete( $layout_id )
  {
    global $mdb;
    
    if ( $mdb -> count( 'pp_layouts' ) > 1 )
      return $mdb -> delete( 'pp_layouts', [ 'id' => (int)$layout_id ] );
    return false;
  }
  
  public static function layout_details( $layout_id )
  {
    global $mdb;
   
    $layout = $mdb -> get( 'pp_layouts', '*', [ 'id' => (int)$layout_id ] );
    
    $layout['pages'] = $mdb -> select( 'pp_layouts_pages', 'page_id', [ 'layout_id' => (int)$layout_id ] );    
    $layout['categories'] = $mdb -> select( 'pp_layouts_categories', 'category_id', [ 'layout_id' => (int)$layout_id ] );
    
    return $layout;
  }
  
  public static function layout_save( $layout_id, $name, $status, $pages, $html, $css, $js, $m_html, $m_css, $m_js, $categories, $categories_default )
  {
    global $mdb;
    
    if ( !$layout_id )
    {
      if ( $status == 'on' )
        $mdb -> update( 'pp_layouts', [ 'status' => 0 ] );
      
      if ( $categories_default == 'on' )
        $mdb -> update( 'pp_layouts', [ 'categories_default' => 0 ] );
      
      $mdb -> insert( 'pp_layouts', [
                'name' => $name,
                'html' => $html,
                'css' => $css,
                'js' => $js,
                'm_html' => $m_html,
                'm_css' => $m_css,
                'm_js' => $m_js,
                'status' => $status == 'on' ? 1 : 0,
                'categories_default' => $categories_default == 'on' ? 1 : 0
              ] );
      
      $id = $mdb -> id();
      
      if ( $id )
      {
        if ( is_array( $pages ) ) foreach ( $pages as $page )
        {
          $mdb -> delete( 'pp_layouts_pages', [ 'page_id' => (int)$page ] );
          
          $mdb -> insert( 'pp_layouts_pages', [
                  'layout_id' => (int)$id,
                  'page_id'   => (int)$page
          ] );
        }
        else if ( $pages )
        {
          $mdb -> delete( 'pp_layouts_pages', [ 'page_id' => (int)$pages ] );
          
          $mdb -> insert( 'pp_layouts_pages', [
                  'layout_id' => (int)$id,
                  'page_id'   => (int)$pages
          ] );
        }
        
        if ( is_array( $categories ) ) foreach ( $categories as $category )
        {
          $mdb -> delete( 'pp_layouts_categories', [ 'category_id' => (int)$category ] );
          
          $mdb -> insert( 'pp_layouts_categories', [
                  'layout_id'   => (int)$id,
                  'category_id' => (int)$category
          ] );
        }
        else if ( $categories )
        {
          $mdb -> delete( 'pp_layouts_categories', [ 'category_id' => (int)$categories ] );
          
          $mdb -> insert( 'pp_layouts_categories', [
                  'layout_id'   => (int)$id,
                  'category_id' => (int)$categories
          ] );
        }
      
        \S::delete_dir( '../temp/' );
        
        return $id;
      }
    }
    else
    {
      if ( $status == 'on' )
        $mdb -> update( 'pp_layouts', [ 'status' => 0 ] );
      
      if ( $categories_default == 'on' )
        $mdb -> update( 'pp_layouts', [ 'categories_default' => 0 ] );
      
      $mdb -> update( 'pp_layouts', [
                'name' => $name,
                'html' => $html,
                'css' => $css,
                'js' => $js,
                'm_html' => $m_html,
                'm_css' => $m_css,
                'm_js' => $m_js,
                'status' => $status == 'on' ? 1 : 0,
                'categories_default' => $categories_default == 'on' ? 1 : 0
            ], [
              'id' => $layout_id
            ] );
      
      $mdb -> delete( 'pp_layouts_pages', [ 'layout_id' => (int)$layout_id ] );
      
      if ( is_array( $pages ) ) foreach ( $pages as $page )
      {
        $mdb -> delete( 'pp_layouts_pages', [ 'page_id' => (int)$page ] );
          
        $mdb -> insert( 'pp_layouts_pages', [
                'layout_id' => (int)$layout_id,
                'page_id'   => (int)$page
        ] );
      }
      else if ( $pages )
      {
        $mdb -> delete( 'pp_layouts_pages', [ 'page_id' => (int)$pages ] );
        
        $mdb -> insert( 'pp_layouts_pages', [
                'layout_id' => (int)$layout_id,
                'page_id'   => (int)$pages
        ] );
      }
       
      $mdb -> delete( 'pp_layouts_categories', [ 'layout_id' => (int)$layout_id ] );
      
      if ( is_array( $categories ) ) foreach ( $categories as $category )
      {
        $mdb -> delete( 'pp_layouts_categories', [ 'category_id' => (int)$category ] );

        $mdb -> insert( 'pp_layouts_categories', [
                'layout_id'   => (int)$layout_id,
                'category_id' => (int)$category
        ] );
      }
      else if ( $categories )
      {
        $mdb -> delete( 'pp_layouts_categories', [ 'category_id' => (int)$categories ] );

        $mdb -> insert( 'pp_layouts_categories', [
                'layout_id'   => (int)$layout_id,
                'category_id' => (int)$categories
        ] );
      }
      
      \S::delete_dir( '../temp/' );

      return $layout_id;
    }
    return false;
  }
  
  public static function menus_list()
  {
    global $mdb;
    
    $results = $mdb -> select( 'pp_menus', 'id', [ 'ORDER' => [ 'id' => 'ASC' ] ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $menu = \admin\factory\Pages::menu_details( $row );
      $menu['pages'] = \admin\factory\Pages::menu_pages( $row );
      
      $menus[] = $menu;
    }
    return $menus;
  }
  
  public static function layouts_list()
  {
    global $mdb;
    return $mdb -> select( 'pp_layouts', '*', [ 'ORDER' => [ 'name' => 'ASC' ] ] );
  }
}
?>