<?php
// po poprawkach
namespace admin\factory;

class Menu
{
  public static function getMenuType()
  {    
    return array( 0 => 'menu poziome', 1 => 'menu pionowe' );
  }
  
  public static function getMenu()
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pp_menu ORDER BY id ASC' );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $Menu[] = $row;
    $query -> closeCursor();
    return $Menu;
  }
  
  public static function getMenuName( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT name FROM pp_menu WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      return $row['name'];
    $query -> closeCursor();
    return false;
  }
  
	public static function deleteMenu( $id )
	{
		global $db;

		$flag	= true;

		$query = $db -> prepare( 'SELECT count(id) FROM pp_pages WHERE id_menu=:id_menu' );
		$query -> bindValue( ':id_menu', $id, \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			if ( $row[0] > 0 )
			$flag = false;
		}
		$query -> closeCursor();

		if ( $flag )
		{
			$query 	= $db -> prepare( 'DELETE FROM pp_menu WHERE id = :id' );
			$query -> bindValue( ':id', $id, \PDO::PARAM_INT );
			$query -> execute();
			$query -> closeCursor();
			\System::setAlert( 'Menu został usunięte.' );
		}
		else
			\System::setAlert( 'Nie można usunąć wybranego menu z powodu przypisanych podstron.' );
	}
}
?>