<?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 ] );

      return $layout;
    
  }
  
  public static function layout_save( $layout_id, $name, $status, $pages, $html, $css, $js, $m_html, $m_css, $m_js )
  { 
    global $mdb;
      if ( !$layout_id )
      {
        if ( $status == 'on' )
          $mdb -> update( 'pp_layouts', [ 'status' => 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
        ] );

        $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
            ] );
          }

          \S::delete_cache();

          return $id;
        }
      }
      else
      {
        if ( $status == 'on' )
          $mdb -> update( 'pp_layouts', [ 'status' => 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
              ], [
                '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
          ] );
        }

        \S::delete_cache();

        return $layout_id;
      }
      return false;
    
  }
  
  public static function menus_list()
  {
    global $mdb;
      $results = $mdb -> select( 'pp_menus', 'id', [ 'ORDER' => [ 'name' => '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' ] ] );
    }
  
}
?>