<?php
namespace admin\factory;

class SContainers {
  
  public function deleteContainer( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'DELETE FROM pp_static_container_langs WHERE static_container_id = :static_container_id' );
    $query -> bindValue( ':static_container_id', $id, \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();
    
    $query = $db -> prepare( 'DELETE FROM pp_static_container WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() )
      return true;
    $query -> closeCursor();
    return false;
  }
  
  public function getContainer( $id )
  {
    global $db, $lang;
    
    $query = $db -> prepare( 'SELECT * FROM pp_static_container WHERE id = :id' );
    $query -> bindValue( ':id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
    {
      $query2	= $db -> prepare( 'SELECT * FROM pp_static_container_langs WHERE static_container_id = :static_container_id AND lang_id = :lang_id' );
      $query2 -> bindValue( ':static_container_id', $id,                      \PDO::PARAM_INT );
      $query2 -> bindValue( ':lang_id',             $lang -> get_language(),  \PDO::PARAM_INT );
      $query2 -> execute();
      if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
        $row['content'] = $row2['content'];
      return $row;
    }
    $query -> closeCursor();
    return false;
  }
  
  public function saveContainer()
  {    
    global $db;

    $id       = \System::formGetInt( 'id' );
    $name     = \System::formGet( 'name' );
    $enabled  = \System::formGet( 'enabled' );

    $query = $db -> prepare( 'UPDATE pp_static_container SET name = :name, enabled = :enabled WHERE id = :id' );
    $query -> bindValue( ':enabled',  $enabled,   \PDO::PARAM_INT );
    $query -> bindValue( ':name',     $name,      \PDO::PARAM_STR );
    $query -> bindValue( ':id',       $id,        \PDO::PARAM_INT );
    $query -> execute();
    $query -> closeCursor();

    if ( $id )
    {
      $query = $db -> prepare( 'SELECT id FROM pp_langs WHERE enabled = :enabled' );
      $query -> bindValue( ':enabled', 1, \PDO::PARAM_STR );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      {
        $text = \System::formGet( 'text_' . $row['id'] );

        if ( $text )
        {
          $query2	= $db -> prepare( 'SELECT id FROM pp_static_container_langs WHERE static_container_id = :static_container_id AND lang_id = :lang_id' );
          $query2 -> bindValue( ':static_container_id', $id,        \PDO::PARAM_INT );
          $query2 -> bindValue( ':lang_id',             $row['id'], \PDO::PARAM_INT );
          $query2 -> execute();
          if ( $query2 -> rowCount() )
          {
            $query3 = $db -> prepare( 'UPDATE pp_static_container_langs SET content = :content WHERE static_container_id = :static_container_id AND lang_id = :lang_id' );
            $query3 -> bindValue( ':static_container_id', $id,        \PDO::PARAM_INT );
            $query3 -> bindValue( ':lang_id',             $row['id'], \PDO::PARAM_INT );
            $query3 -> bindValue( ':content',             $text,      \PDO::PARAM_STR );
            $query3 -> execute();
            $query3 -> closeCursor();
          }
          else
          {
						$query3 = $db -> prepare( 'INSERT INTO  pp_static_container_langs  ( static_container_id, lang_id, content ) VALUES ( :static_container_id, :lang_id, :content )' );
						$query3 -> bindValue( ':static_container_id', $id,        \PDO::PARAM_INT );
						$query3 -> bindValue( ':lang_id',             $row['id'], \PDO::PARAM_INT );
						$query3 -> bindValue( ':content',             $text,      \PDO::PARAM_STR );
						$query3 -> execute();
						$query3 -> closeCursor();
          }
          $query2 -> closeCursor();
        }
      }
      $query -> closeCursor();
      \System::rewriteHtacces();
      \System::setAlert( 'Strona została zapisana.' );
    }
  }
  
  public function addContainer()
  {    
		global $db;

    $name     = \System::formGet( 'name' );
		$enabled  = \System::formGet( 'enabled' );
    $check    = \System::formGet( 'check' );
    
		if ( \System::getSessionVar( 'check' ) != $check ) 
    {
			$query = $db -> prepare( 'INSERT INTO pp_static_container ( name, enabled ) VALUES ( :name, :enabled )' );
      $query -> bindValue( ':name',     $name,    \PDO::PARAM_STR );
			$query -> bindValue( ':enabled',  $enabled, \PDO::PARAM_INT );
			$query -> execute();
			$query -> closeCursor();

			$container_id = $db -> lastInsertId();

			if ( $container_id )
			{
				$query = $db -> prepare( 'SELECT id FROM pp_langs WHERE enabled = :enabled' );
				$query -> bindValue( ':enabled', 1, \PDO::PARAM_STR );
				$query -> execute();
				if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
				{
					$text = \System::formGet( 'text_' . $row['id'] );

					if ( $text )
					{
						$query2 = $db -> prepare( 'INSERT INTO  pp_static_container_langs  ( static_container_id, lang_id, content ) VALUES ( :static_container_id, :lang_id, :content )' );
						$query2 -> bindValue( ':static_container_id', $container_id,  \PDO::PARAM_INT );
						$query2 -> bindValue( ':lang_id',             $row['id'],     \PDO::PARAM_INT );
						$query2 -> bindValue( ':content',             $text,          \PDO::PARAM_STR );
						$query2 -> execute();
						$query2 -> closeCursor();
					}
				}
				$query -> closeCursor();

				\System::setSessionVar( 'check', $check );
				\System::setAlert( 'Kontener został dodany.' );
			}
		}
  }

  public function getContainerParamLanguage( $id = '' )
  {
    global $db;

		$query = $db -> prepare( 'SELECT id, name FROM pp_langs WHERE enabled = :enabled' );
		$query -> bindValue( ':enabled', 1, \PDO::PARAM_STR );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
      unset( $lg );
      
			$query2 = $db -> prepare( 'SELECT * FROM pp_static_container_langs WHERE static_container_id = :static_container_id AND lang_id = :lang_id' );
			$query2 -> bindValue( ':static_container_id', $id,        \PDO::PARAM_INT );
			$query2 -> bindValue( ':lang_id',             $row['id'], \PDO::PARAM_STR );
			$query2 -> execute();
			if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
				$lg = $row2;
			$query2 -> closeCursor();
      
			$lg['id'] 	= $row['id'];
			$lg['name'] = $row['name'];
			$language[]	= $lg;
		}
		$query -> closeCursor();

		return $language;
  }
}
