<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[48]!=1)
  jump("jump.php?id=25&jump=main.php");
if(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "deljuan")
{
  if($_POST['volume']=="0")
    jump("jump.php?id=2&jump=book_chapter.php?id=".$_GET['bid']);
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['bid']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->table="book_sql";
    $phpcolor->field="*";
    $phpcolor->where="did='".$book['sqldata']."'";
    $booksql=$phpcolor->exist(1);
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  }
  $phpcolor->table="book_volume";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $volume=$phpcolor->exist(1);
  $phpcolor->delete();
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="volume='".$_POST['volume']."'";
  $phpcolor->where="volume = '".$_GET['id']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=book_chapter.php?id=".$volume['belong']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "addjuan")
{
  if($_POST['volumename']=="")
    jump("jump.php?id=2&jump=book_chapter.php?id=".$_GET['id']);
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->table="book_sql";
    $phpcolor->field="*";
    $phpcolor->where="did='".$book['sqldata']."'";
    $booksql=$phpcolor->exist(1);
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  }
  $phpcolor->table="book_volume";
  $phpcolor->field="*";
  $phpcolor->where="belong = '".$_GET['id']."'";
  $queue=$phpcolor->exist(2);
  $queue++;
  $phpcolor->field="volumename, belong, queue";
  $phpcolor->where="'".$_POST['volumename']."', '".$_GET['id']."', '".$queue."'";
  $phpcolor->write();
  jump("jump.php?id=24&jump=book_chapter.php?id=".$_GET['id']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "editjuan")
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['bid']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->table="book_sql";
    $phpcolor->field="*";
    $phpcolor->where="did='".$book['sqldata']."'";
    $booksql=$phpcolor->exist(1);
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  }
  $phpcolor->table="book_volume";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $volume=$phpcolor->exist(1);
  $phpcolor->field="volumename='".$_POST['volumename']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=book_chapter.php?id=".$volume['belong']);
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "del")
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['bid']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->table="book_sql";
    $phpcolor->field="*";
    $phpcolor->where="did='".$book['sqldata']."'";
    $booksql=$phpcolor->exist(1);
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  }
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $chapter=$phpcolor->exist(1);
  $phpcolor->delete();
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="id,theme";
  $phpcolor->where="belong = '".$chapter['belong']."' order by queue desc limit 1";
  $chapters=$phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->close();
    $phpcolor = new mysql();
    $phpcolor->open();
  }
  $phpcolor->table="book";
  $phpcolor->field="words=words-".$chapter['words'].",cid='".$chapters['id']."',ctheme = '".$chapters['theme']."'";
  $phpcolor->where="id = '".$chapter['belong']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=book_chapter.php?id=".$chapter['belong']);
}
elseif(isset($_GET['action']) and $_GET['action'] == "add")
{
  $phpcolor->table="book";
  $phpcolor->field="id,bookname,cid,ctheme";
  $phpcolor->where="id = '".$_GET['id']."'";
  if(!$book=$phpcolor->exist(1))
    jump("jump.php?id=28&jump=book.php");
  $phpcolor->table="book_volume";
  $phpcolor->field="*";
  $phpcolor->where="belong='".$_GET['id']."' order by queue";
  $volume=$phpcolor->assignment();
  $tpl -> assign("book", $book);
  $tpl -> assign("volume", $volume);
  $tpl -> display("book_chapter_add.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "adddo")
{
  if($_POST['theme']=="" || $_POST['content']=="")
    jump("jump.php?id=2&jump=book_chapter.php?action=add=and=id=".$_GET['id']);
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->table="book_sql";
    $phpcolor->field="*";
    $phpcolor->where="did='".$book['sqldata']."'";
    $booksql=$phpcolor->exist(1);
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  }
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="id";
  $phpcolor->where="belong = '".$_GET['id']."'";
  $queue=$phpcolor->exist(2);
  $queue++;
  $words=strlen($_POST['content'])/2;
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $_POST['content']=nl2br($_POST['content']);
  $phpcolor->field="theme, content, words, uptime, belong, volume, status, queue, rule, ruleid";
  $phpcolor->where="'".$_POST['theme']."', '".$_POST['content']."', '".$words."', '".$time."', '".$_GET['id']."', '".$_POST['volume']."', '".$_POST['status']."', '".$queue."', '".$_POST['rule']."', '".$_POST['ruleid']."'";
  $phpcolor->write();
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="id,theme";
  $phpcolor->where="belong = '".$_GET['id']."' order by queue desc limit 1";
  $chapter=$phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->close();
    $phpcolor = new mysql();
    $phpcolor->open();
  }
  $phpcolor->table="book";
  $phpcolor->field="words=words+".$words.", uptime='".$time."',cid='".$chapter['id']."',ctheme = '".$chapter['theme']."'";
  $phpcolor->where="id = '".$_GET['id']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=book_chapter.php?id=".$_GET['id']."=and=action=add");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "edit")
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['bid']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->table="book_sql";
    $phpcolor->field="*";
    $phpcolor->where="did='".$book['sqldata']."'";
    $booksql=$phpcolor->exist(1);
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  }
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $chapter = $phpcolor->exist(1);
  $phpcolor->table="book_volume";
  $phpcolor->field="*";
  $phpcolor->where="belong='".$chapter['belong']."' order by queue";
  $volume=$phpcolor->assignment();
  $chapter['belongn'] = $book['bookname'];
  $chapter['content'] = str_replace("<br />","",$chapter['content']);
  $tpl -> assign("volume", $volume);
  $tpl -> assign("chapter", $chapter);
  $tpl -> display("book_chapter_edit.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "editdo")
{
  if($_POST['theme']=="" || $_POST['content']=="")
    jump("jump.php?id=2&jump=book_chapter.php?action=edit=and=id=".$_GET['id']);
  else
  {
	$phpcolor->table="book";
    $phpcolor->field="*";
    $phpcolor->where="id = '".$_GET['bid']."'";
    $book = $phpcolor->exist(1);
    if($book['sqldata']!=1)
    {
      $phpcolor->table="book_sql";
      $phpcolor->field="*";
      $phpcolor->where="did='".$book['sqldata']."'";
      $booksql=$phpcolor->exist(1);
	  $phpcolor->close();
	  $phpcolor = new mysql();
      $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
    }
	$words=strlen($_POST['content'])/2;
	$_POST['content']=nl2br($_POST['content']);
    $phpcolor->table="book_chapter_".$book['sqltable'];
    $phpcolor->field="belong,words";
	$phpcolor->where="id = '".$_GET['id']."'";
	$chapter = $phpcolor->exist(1);
    $phpcolor->field="theme='".$_POST['theme']."', content='".$_POST['content']."', words='".$words."',volume='".$_POST['volume']."' , status='".$_POST['status']."'";
    $phpcolor->upgrade();
	$phpcolor->table="book_chapter_".$book['sqltable'];
    $phpcolor->field="id,theme";
    $phpcolor->where="belong = '".$_GET['bid']."' order by queue desc limit 1";
    $chapters=$phpcolor->exist(1);
	if($book['sqldata']!=1)
    {
	  $phpcolor->close();
      $phpcolor = new mysql();
      $phpcolor->open();
    }
    $phpcolor->table="book";
    $phpcolor->field="words=words-".$chapter['words']."+".$words.",cid='".$chapters['id']."',ctheme = '".$chapters['theme']."'";
    $phpcolor->where="id = '".$_GET['bid']."'";
    $phpcolor->upgrade();
    jump("jump.php?id=24&jump=book_chapter.php?id=".$_GET['bid']);
  }
}
else
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $book=$phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
    $phpcolor->table="book_sql";
    $phpcolor->field="*";
    $phpcolor->where="did='".$book['sqldata']."'";
    $booksql=$phpcolor->exist(1);
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  }
  $phpcolor->table="book_volume";
  $phpcolor->field="*";
  $phpcolor->where="belong = '".$_GET['id']."' order by queue";
  $volume=$phpcolor->assignment();
  $chapters=0;
  for ($count=0;$count<count($volume);$count++)
  {
	$phpcolor->table="book_chapter_".$book['sqltable'];
    $phpcolor->field="*";
    $phpcolor->where="belong='".$_GET['id']."' and volume='".$volume[$count]['id']."' order by queue";
    $chapter[$count]=$phpcolor->assignment();
	$chapters=$chapters+count($chapter[$count]);
  }
  $tpl -> assign("book", $book);
  $tpl -> assign("chapter", $chapter);
  $tpl -> assign("volume", $volume);
  $tpl -> assign("chapters", $chapters);
  $tpl -> display("book_chapter.htm");
}
?>