diff edit_section_db.php @ 1:1f9d2bfe1d13

handle new section created by user, update sections_index table
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Thu, 19 Mar 2015 18:36:21 +0100
parents 723a162b6627
children bd46d26a7b18
line wrap: on
line diff
--- a/edit_section_db.php	Thu Mar 19 15:06:34 2015 +0100
+++ b/edit_section_db.php	Thu Mar 19 18:36:21 2015 +0100
@@ -36,7 +36,8 @@
 		$condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 ";
 		$versionId=$versionInfo['id'];
 	}else{
-		$table="sections";
+		// $table="sections";
+		$table="sections_index";
 		$condition="";
 	}
 	$query="SELECT *
@@ -71,7 +72,8 @@
 			$condition="AND deleted=0 ";
 			$condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 ";
 		}else{
-			$table="sections";
+			// $table="sections";
+			$table="sections_index";
 			$condition="";
 		}
 
@@ -97,7 +99,8 @@
         if($versionInfo['version']!=0){
                 $query="SELECT id AS revisions_id, name,books_id,section_after,start_page,end_page,level,split_from,sections_id AS id, deleted FROM sections_revisions WHERE versions_id=".$versionInfo['id']." ORDER BY revisions_id ASC";
         }else{
-                $query="SELECT * FROM sections WHERE books_id=".$bookId." ORDER BY id ASC";
+                //$query="SELECT * FROM sections WHERE books_id=".$bookId." ORDER BY id ASC";
+                $query="SELECT * FROM sections_index WHERE books_id=".$bookId." ORDER BY id ASC";
         }
         $result=mysql_query($query);
 
@@ -127,15 +130,70 @@
         return $sectionArray;
 }
 function updateSectionArray($bookId,$sectionArray,$version,$editor){
+	
+	// TODO: ---transaction ---
+
+
 	$query="INSERT INTO sections_versions (version,editor,date,books_id) VALUES (".($version+1).",'".$editor."',NOW(),'".$bookId."')";	
 	$result=mysql_query($query);
 	if(!$result){
 		echo json_encode("Failed during inserting sections_version records."); // .mysql_error();
-		return;
+		//return;
 	}
 	$versionId=mysql_insert_id();
 	$query="INSERT INTO sections_revisions (name,books_id,section_after,start_page,end_page,level,split_from,sections_id,versions_id,deleted) VALUES ";
 	foreach($sectionArray as $idx=>$row){
+		// get the section_id
+		$section_id = $row['id'];
+		$name = $row['name'];
+		$books_id = sprintf("%05d",$row['booksId']);
+		$section_after = $row['sectionAfter'];
+		$start_page = $row['startPage'];
+		$end_page = $row['endPage'];
+		$level = $row['level'];
+		$split_from = $row['splitFrom'];
+		$deleted = $row['deleted'];
+
+		// update to section_index table, which is the up-to-date section table
+		if ($section_id == 0) { // section_id = 0 means it is a new record created by user
+			// insert new row to section_index table
+			$qry = "INSERT INTO sections_index (name,books_id,section_after,start_page,end_page,level,split_from) VALUES 
+				('".$name."','".$books_id."','".$section_after."',".$start_page.
+				",".$end_page.",".$level.",".$split_from.")";
+			$rst = mysql_query($qry);
+			if (!$rst) {
+				echo json_encode(mysql_error());
+			}
+			$section_id = mysql_insert_id();
+
+		} else {
+			// update
+			$qry = "UPDATE sections_index SET name='".$name."',books_id='".$books_id."',section_after='".$section_after.
+				"',start_page=".$start_page.",end_page=".$end_page.",level=".$level.",split_from=".$split_from." WHERE id=".$section_id;
+			$rst = mysql_query($qry);
+			if (!$rst){
+				echo json_encode(mysql_error());
+		    }
+		}
+
+		$str="(";
+		$str.="'".$name."',";
+		$str.="'".$books_id."',";
+		$str.="'".$section_after."',";
+		$str.=$start_page.",";
+		$str.=$end_page.",";
+		$str.=$level.",";
+		$str.=$split_from.",";
+		$str.=$section_id.",";	// sections_id should get from section_index table
+		$str.=$versionId.",";
+		$str.=$deleted;
+		$str.=")";
+		if($idx!=sizeof($sectionArray)-1){
+			$str.=", ";
+		}
+
+
+		/*
 		$str="(";
 		$str.="'".$row['name']."',";
 		$str.="'".sprintf("%05d",$row['booksId'])."',";
@@ -144,13 +202,14 @@
 		$str.=$row['endPage'].",";
 		$str.=$row['level'].",";
 		$str.=$row['splitFrom'].",";
-		$str.=$row['id'].",";
+		$str.=$section_id.",";	// sections_id should get from section_index table
 		$str.=$versionId.",";
 		$str.=$row['deleted'];
 		$str.=")";
 		if($idx!=sizeof($sectionArray)-1){
 			$str.=", ";
 		}
+		*/
 		$query.=$str;	
 	}
 	$result=mysql_query($query);
@@ -168,6 +227,8 @@
 	}else{
 		echo json_encode("Succeeded.");
 	}
+
+
 }
 
 
@@ -193,7 +254,8 @@
 
 
 	//$query="SELECT id FROM books WHERE id NOT IN (SELECT DISTINCT (books_id) FROM sections)"; 
-	$query="SELECT books.id, books.name FROM books LEFT JOIN sections ON books.id=sections.books_id WHERE sections.books_id IS NULL";
+	// $query="SELECT books.id, books.name FROM books LEFT JOIN sections ON books.id=sections.books_id WHERE sections.books_id IS NULL";
+	$query="SELECT books.id, books.name FROM books LEFT JOIN sections_index ON books.id=sections_index.books_id WHERE sections_index.books_id IS NULL";
 	
 	$result=mysql_query($query);
 	if(!$result){
@@ -246,7 +308,8 @@
 	*/
 
 	// id should be autoincremated 
-	$query="INSERT INTO sections (name,books_id,section_after) VALUES ";
+	// $query="INSERT INTO sections (name,books_id,section_after) VALUES ";
+	$query="INSERT INTO sections_index (name,books_id,section_after) VALUES ";
 
 	$str="(";
 	$str.="'".$name."','";