<?
namespace admin\factory;

class PageManager
{
  
  public function getAssignedArticles( $id )
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT article_id FROM pcms_article_page WHERE page_id = :page_id ORDER BY o DESC' );
    $query -> bindValue( ':page_id', $id, \PDO::PARAM_INT );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $articles[] = \article\FArticle::loadArticle( $row['article_id'] );
    $query -> closeCursor();
    
    return $articles;
  }
  
  public function getPageParam( $id )
  {
      global $db;

      $query = $db -> prepare( 'SELECT link , enabled , article_number , show_title , id_sort_type , id_page_type , id_menu , o FROM pcms_page WHERE id = :id' );
      $query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
      $query -> execute();
      if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
          return $row;
  }

	public function getPageParamLanguage( $id )
	{
		global $db;

		$query = $db -> prepare( 'SELECT id , name FROM pcms_lang WHERE enabled = :enabled' );
		$query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			$query2 = $db -> prepare( 'SELECT title , meta_description , meta_keywords, seo_link FROM pcms_page_translation WHERE page_id = :page_id AND lang_id = :lang_id' );
			$query2 -> bindValue( ':page_id' , $id        , \PDO::PARAM_INT );
			$query2 -> bindValue( ':lang_id' , $row['id'] , \PDO::PARAM_STR );
			$query2 -> execute();
			if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
			{
				$lg['title']            = $row2['title'];
				$lg['meta_description'] = $row2['meta_description'];
				$lg['meta_keywords']    = $row2['meta_keywords'];
        $lg['seo_link']         = $row2['seo_link'];
			}
			else
			{
				$lg['title']            = '';
				$lg['meta_description'] = '';
				$lg['meta_keywords']    = '';
        $lg['seo_link']         = '';
			}
			$query2 -> closeCursor();
			$lg['id'] 	= $row['id'];
			$lg['name'] 	= $row['name'];
			$language[]	= $lg;
		}
		$query -> closeCursor();

		return $language;
	}

	public function getPages( $menu_id , $from , $lpk )
	{
		global $sys , $db , $lang;

		$lp     = 1;
		$yes_no = \System::getComboYesNo();
    $pages  = '';

		$query = $db -> prepare( 'SELECT id , id_menu , enabled , o , id_page_type , article_number , id_sort_type , show_title FROM pcms_page WHERE id_menu=:id_menu ORDER BY o ASC LIMIT ' . $from . ',' . $lpk );
		$query -> bindValue( ':id_menu'	, $menu_id	, \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			$page['lp']             	= $lp;
			$page['id']             	= $row['id'];
			$page['article_number']   = $row['article_number'];
			$page['show_title']     	= $yes_no[$row['show_title']];
			$page['enabled']        	= $yes_no[$row['enabled']];
			$page['title']          	= \System::getPageTitle( $row['id'] );

			$query2 = $db -> prepare( 'SELECT name FROM pcms_page_type WHERE id=:page_type' );
			$query2 -> bindValue( ':page_type' , $row['id_page_type'] , \PDO::PARAM_STR );
			$query2 -> execute();
			if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
				$page['page_type'] = $lang -> getTrans( $row2['name'] );
			$query2 -> closeCursor();

			$query2 = $db -> prepare( 'SELECT name FROM pcms_page_sort_type WHERE id=:sort_type' );
			$query2 -> bindValue( ':sort_type' , $row['id_sort_type'] , \PDO::PARAM_STR );
			$query2 -> execute();
			if ( $query2 -> rowCount() ) while ( $row2 = $query2 -> fetch() )
				$page['sort_type'] = $lang -> getTrans( $row2['name'] );
			$query2 -> closeCursor();

			$query2 = $db -> prepare( 'SELECT id FROM pcms_page WHERE o>:order AND id_menu=:id_menu ORDER BY o ASC LIMIT 1' );
			$query2 -> bindValue( 'id_menu'	, $menu_id	, \PDO::PARAM_INT );
			$query2 -> bindValue( ':order'	, $row['o']	, \PDO::PARAM_INT );
			$query2 -> execute();
			if ( $query2 -> rowCount() )
				$page['page_down'] = $row['id'];
			else
				$page['page_down'] = '';
			$query2 -> closeCursor();

			$query2 = $db -> prepare( 'SELECT id FROM pcms_page WHERE o<:order AND id_menu=:id_menu ORDER BY o ASC LIMIT 1' );
			$query2 -> bindValue( 'id_menu'	, $menu_id	, \PDO::PARAM_INT );
			$query2 -> bindValue( ':order'	, $row['o']	, \PDO::PARAM_INT );
			$query2 -> execute();
			if ( $query2 -> rowCount() )
				$page['page_up'] = $row['id'];
			else
				$page['page_up'] = '';
			$query2 -> closeCursor();

      $akcja = "function mycallbackform(v,m,f){
                if( v == true )
                  document.location.href='index.php?rw=del&amp;id=" . $row['id'] . "';
              }";
      $akcja .= "$.prompt('" . $lang -> getTrans( 'T_POTWIERDZ_USUNIECIE' ) . "',{ callback: mycallbackform, buttons: { " . $lang -> getTrans( 'T_TAK' ) . ": true, " . $lang -> getTrans( 'T_NIE' ) . ": false }, focus: 1 })";
      $akcja = 'onClick="'.$akcja.'"';;
			$page['action']	= $akcja;

			$pages[] = $page;
			$lp++;
		}

		return $pages;
	}

	public function selectMaxOrder()
	{
		global $db;

		$query = $db -> prepare( 'SELECT MAX(o) FROM pcms_page' );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
			$order = $row[0]+1;
		$query -> closeCursor();

		return $order;
	}

  public function save() 
  {
      global $lang, $db;

      $enabled        = \System::formGet( 'enabled' );
      $link           = \System::formGet( 'link' );
      $show_title     = \System::formGet( 'show_title' );
      $sort_type      = \System::formGet( 'sort_type' );
      $page_type_id   = \System::formGet( 'page_type_id' );
      $menu           = \System::formGet( 'menu' );
      $page_id        = \System::formGetInt( 'id' );
      $article_number	= \System::formGetInt( 'article_number' );

      $query = $db -> prepare( 'UPDATE pcms_page SET link = :link , id_menu = :id_menu , id_page_type = :id_page_type , id_sort_type = :id_sort_type , article_number = :article_number , show_title = :show_title , enabled = :enabled WHERE id = :id ' );
      $query -> bindValue( ':link'            , $link             , \PDO::PARAM_STR );
      $query -> bindValue( ':id_menu'         , $menu             , \PDO::PARAM_INT );
      $query -> bindValue( ':id_page_type'    , $page_type_id     , \PDO::PARAM_INT );
      $query -> bindValue( ':id_sort_type'    , $sort_type        , \PDO::PARAM_INT );
      $query -> bindValue( ':article_number'  , $article_number   , \PDO::PARAM_INT );
      $query -> bindValue( ':show_title'      , $show_title       , \PDO::PARAM_STR );
      $query -> bindValue( ':enabled'         , $enabled          , \PDO::PARAM_STR );
      $query -> bindValue( ':id'              , $page_id          , \PDO::PARAM_INT );
      $query -> execute();
      $query -> closeCursor();

      if ( $page_id )
      {
        $query = $db -> prepare( 'SELECT id FROM pcms_lang WHERE enabled = :enabled' );
        $query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
        $query -> execute();
        if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
        {
          $title            = \System::formGet( 'title_' . $row['id'] );
          $meta_description = \System::formGet( 'meta_description_' . $row['id'] );
          $meta_keywords	 	= \System::formGet( 'meta_keywords_' . $row['id'] );
          $seo_link         = \System::formGet( 'seo_link_' . $row['id'] );

          if ( $title )
          {
            $query2	= $db -> prepare( 'SELECT id FROM pcms_page_translation WHERE page_id=:page_id AND lang_id = :lang_id' );
            $query2 -> bindValue( ':page_id' , $page_id 	, \PDO::PARAM_INT );
            $query2 -> bindValue( ':lang_id' , $row['id'] 	, \PDO::PARAM_INT );
            $query2 -> execute();
            if ( $query2 -> rowCount() )
            {
              $query3 = $db -> prepare( 'UPDATE pcms_page_translation SET seo_link = :seo_link, title = :title, meta_description = :meta_description, meta_keywords = :meta_keywords WHERE page_id = :page_id AND lang_id = :lang_id' );
              $query3 -> bindValue( ':page_id'          , $page_id          , \PDO::PARAM_INT );
              $query3 -> bindValue( ':lang_id'          , $row['id']        , \PDO::PARAM_INT );
              $query3 -> bindValue( ':title'            , $title            , \PDO::PARAM_STR );
              $query3 -> bindValue( ':meta_description'	, $meta_description	, \PDO::PARAM_STR );
              $query3 -> bindValue( ':meta_keywords'		, $meta_keywords    , \PDO::PARAM_STR );
              $query3 -> bindValue( ':seo_link'         , $seo_link         , \PDO::PARAM_STR );
              $query3 -> execute();
              $query3 -> closeCursor();
            }
            else
            {
              $query3 = $db -> prepare( 'INSERT INTO pcms_page_translation ( page_id, lang_id, title, meta_description, meta_keywords, seo_link ) VALUES ( :page_id, :lang_id, :title, :meta_description, :meta_keywords, :seo_link )' );
              $query3 -> bindValue( ':page_id'          , $page_id          , \PDO::PARAM_INT );
              $query3 -> bindValue( ':lang_id'          , $row['id']        , \PDO::PARAM_INT );
              $query3 -> bindValue( ':title'            , $title            , \PDO::PARAM_STR );
              $query3 -> bindValue( ':meta_description'	, $meta_description	, \PDO::PARAM_STR );
              $query3 -> bindValue( ':meta_keywords'		, $meta_keywords    , \PDO::PARAM_STR );
              $query3 -> bindValue( ':seo_link'         , $seo_link         , \PDO::PARAM_STR );
              $query3 -> execute();
              $query3 -> closeCursor();
            }
            $query2 -> closeCursor();
          }
        }
        $query -> closeCursor();
        \System::rewriteHtacces();
        \System::setAlert( $lang -> getTrans( 'T_ZAPISANO_STRONE' ) );
        \System::deleteCacheAdmin();
        \System::deleteCache();
      }
  }

	public function saveNew() 
  {
		global $lang , $db;

		$enabled        = \System::formGet( 'enabled' );
    $link           = \System::formGet( 'link' );
		$show_title			= \System::formGet( 'show_title' );
		$sort_type			= \System::formGet( 'sort_type' );
		$page_type_id		= \System::formGet( 'page_type_id' );
		$menu           = \System::formGet( 'menu' );
		$o              = \System::formGetInt( 'o' );
		$article_number	= \System::formGetInt( 'article_number' );
		$time           = \System::formGetHash( 'time' );

		if ( $time != \System::getSessionVar( 'db_edit_add' ) ) 
    {
			$query = $db -> prepare( 'INSERT INTO pcms_page ( link , id_menu , id_page_type , id_sort_type , article_number , show_title , enabled , o) VALUES ( :link , :id_menu , :id_page_type , :id_sort_type , :article_number , :show_title , :enabled , :o)' );
			$query -> bindValue( ':link'            , $link           , \PDO::PARAM_STR );
      $query -> bindValue( ':id_menu'         , $menu           , \PDO::PARAM_INT );
			$query -> bindValue( ':id_page_type'    , $page_type_id		, \PDO::PARAM_INT );
			$query -> bindValue( ':id_sort_type'    , $sort_type      , \PDO::PARAM_INT );
			$query -> bindValue( ':article_number'	, $article_number , \PDO::PARAM_INT );
			$query -> bindValue( ':show_title'      , $show_title 		, \PDO::PARAM_STR );
			$query -> bindValue( ':enabled'         , $enabled        , \PDO::PARAM_STR );
			$query -> bindValue( ':o'               , $o              , \PDO::PARAM_INT );
			$query -> execute();
			$query -> closeCursor();

			$page_id = $db -> lastInsertId();

			if ( $page_id )
			{
				$query = $db -> prepare( 'SELECT id FROM pcms_lang WHERE enabled = :enabled' );
				$query -> bindValue( ':enabled' , 1 , \PDO::PARAM_STR );
				$query -> execute();
				if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
				{
					$title              = \System::formGet( 'title_' . $row['id'] );
					$meta_description 	= \System::formGet( 'meta_description_' . $row['id'] );
					$meta_keywords      = \System::formGet( 'meta_keywords_' . $row['id'] );
          $seo_link           = \System::formGet( 'seo_link_' . $row['id'] );

					if ( $title )
					{
						$query2 = $db -> prepare( 'INSERT INTO pcms_page_translation ( page_id, lang_id, title, meta_description, meta_keywords, seo_link ) VALUES ( :page_id, :lang_id, :title, :meta_description, :meta_keywords, :seo_link )' );
						$query2 -> bindValue( ':page_id'          , $page_id          , \PDO::PARAM_INT );
						$query2 -> bindValue( ':lang_id'          , $row['id']        , \PDO::PARAM_INT );
						$query2 -> bindValue( ':title'            , $title            , \PDO::PARAM_STR );
						$query2 -> bindValue( ':meta_description'	, $meta_description	, \PDO::PARAM_STR );
						$query2 -> bindValue( ':meta_keywords'		, $meta_keywords    , \PDO::PARAM_STR );
            $query2 -> bindValue( ':seo_link'         , $seo_link         , \PDO::PARAM_STR );
						$query2 -> execute();
						$query2 -> closeCursor();
					}
				}
				$query -> closeCursor();

				\System::setSessionVar( 'db_edit_add' , $time );
				\System::rewriteHtacces();
				\System::setAlert( $lang -> getTrans( 'T_DODANO_STRONE' ) );
			}

      \System::deleteCacheAdmin();
      \System::deleteCache();
		}
	}

	/* ---------- */

	public function delete( $id )
	{
		global $db , $lang, $sys;

		$query = $db -> prepare( 'DELETE FROM pcms_page WHERE id = :id' );
		$query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
		$query -> execute();
		$query -> closeCursor();

		$query = $db -> prepare( 'DELETE FROM pcms_page_translation WHERE page_id = :page_id' );
		$query -> bindValue( ':page_id' , $id , \PDO::PARAM_INT );
		$query -> execute();
		$query -> closeCursor();

		\System::setAlert( $lang -> getTrans( 'T_USNIETO_STRONE' ) );
		\System::rewriteHtacces();

    \System::deleteCacheAdmin();
    \System::deleteCache();
	}

	/* ----------- */

	public function moveDown( $id , $menu_id )
	{
		global $lang , $db , $sys;

		$query = $db -> prepare( 'SELECT o FROM pcms_page WHERE id = :id ');
		$query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
			$o1 = $row['o'];
		$query -> closeCursor();

		$query = $db -> prepare( 'SELECT id, o FROM pcms_page WHERE o > :o1 AND id_menu = :id_menu ORDER BY o ASC LIMIT 1' );
		$query -> bindValue( ':o1'		, $o1		, \PDO::PARAM_INT );
		$query -> bindValue( ':id_menu'	, $menu_id	, \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			$query2 = $db -> prepare( 'UPDATE pcms_page SET o = :o2 WHERE id = :id' );
			$query2 -> bindValue( ':o2' , $row['o']	, \PDO::PARAM_INT );
			$query2 -> bindValue( ':id' , $id		, \PDO::PARAM_INT );
			$query2 -> execute();
			$query2 -> closeCursor();

			$query2 = $db -> prepare( 'UPDATE pcms_page SET o = :o1 WHERE id = :id2' );
			$query2 -> bindValue( ':o1'	 , $o1			, \PDO::PARAM_INT );
			$query2 -> bindValue( ':id2' , $row['id']	, \PDO::PARAM_INT );
			$query2 -> execute();
			$query2 -> closeCursor();

			\System::setAlert( $lang -> getTrans( 'T_PRZESUNIETO_STRONE' ) );
		}
		$query -> closeCursor();

    \System::deleteCacheAdmin();
    \System::deleteCache();
	}

	/* --------- */

	public function moveUp( $id , $menu_id )
	{
		global $sys , $db , $lang;

		$query = $db -> prepare( 'SELECT o FROM pcms_page WHERE id = :id ');
		$query -> bindValue( ':id' , $id , \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
			$o1 = $row['o'];
		$query -> closeCursor();

		$query = $db -> prepare( 'SELECT id, o FROM pcms_page WHERE o < :o1 AND id_menu = :id_menu ORDER BY o DESC LIMIT 1');
		$query -> bindValue( ':o1'		, $o1		, \PDO::PARAM_INT );
		$query -> bindValue( ':id_menu' , $menu_id	, \PDO::PARAM_INT );
		$query -> execute();
		if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
		{
			$query2 = $db -> prepare( 'UPDATE pcms_page SET o = :o2 WHERE id = :id' );
			$query2 -> bindValue( ':o2'	, $row['o']	, \PDO::PARAM_INT );
			$query2 -> bindValue( ':id'	, $id		, \PDO::PARAM_INT );
			$query2 -> execute();
			$query2 -> closeCursor();

			$query2 = $db -> prepare( 'UPDATE pcms_page SET o = :o1 WHERE id = :id2' );
			$query2 -> bindValue( ':o1'	, $o1			, \PDO::PARAM_INT );
			$query2 -> bindValue( ':id2', $row['id']	, \PDO::PARAM_INT );
			$query2 -> execute();
			$query2 -> closeCursor();

			\System::setAlert( $lang -> getTrans( 'T_PRZESUNIETO_STRONE' ) );
		}
		$query -> closeCursor();

    \System::deleteCacheAdmin();
    \System::deleteCache();
	}
}
?>