diff interface/edit_section_db.php @ 0:b12c99b7c3f0

commit for previous development
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 19 Jan 2015 17:13:49 +0100
parents
children ef6d0c6a13d7
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/interface/edit_section_db.php	Mon Jan 19 17:13:49 2015 +0100
@@ -0,0 +1,171 @@
+<?php
+include_once('Lib_mb_utf8.php');
+include_once('config.php');
+set_time_limit(0);
+ini_set('memory_limit', '-1');
+
+$link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password);
+mysql_query("SET NAMES utf8");
+
+if (!$link_mysql) {
+    die('Could not connect: ' . mysql_error());
+}
+
+$db_selected = mysql_select_db($mysql_database, $link_mysql);
+if (!$db_selected) {
+    die ('Can\'t use foo : ' . mysql_error());
+}
+function checkSectionInfo($bookId){
+	$query="SELECT * FROM sections_versions WHERE books_id=".$bookId." ORDER BY version DESC";
+	$result=mysql_query($query);
+	if(mysql_num_rows($result)==0){
+		$row['version']=0;
+		$row['editor']="";
+		$row['date']="";
+		return $row;
+	}else{
+		$row=mysql_fetch_assoc($result);
+		return $row;
+	}
+}
+function checkMissingPage($bookInfo,$versionInfo){
+	$bookId=$bookInfo['id'];
+	$bookPage=$bookInfo['line'];
+        if($versionInfo['version']!=0){
+		$table="sections_revisions";
+		$condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 ";
+		$versionId=$versionInfo['id'];
+	}else{
+		$table="sections";
+		$condition="";
+	}
+	$query="SELECT *
+		FROM ".$table." 
+		WHERE books_id=".$bookId." ".$condition." 
+		GROUP BY books_id,start_page,end_page 
+		ORDER BY books_id,start_page,level,end_page";
+	$result=mysql_query($query);
+	$lastPage=1;
+	$i=0;
+	$pageArray=array();
+	while($row=mysql_fetch_assoc($result)){
+		if($row['start_page']-1>$lastPage){
+			$pageArray[$i]['start_page']=$lastPage;
+			$pageArray[$i]['end_page']=$row['start_page'];
+			//echo $pageArray[$i]['start_page']." ".$pageArray[$i]['end_page']."<br>";
+			$i++;
+		}
+		$lastPage=$row['end_page'];
+	}
+	if($bookPage>$lastPage){
+		$pageArray[$i]['start_page']=$lastPage;
+                $pageArray[$i]['end_page']=$bookPage;
+	}
+	return $pageArray;
+}
+function getSectionArray($bookId,$count,$versionInfo){
+        $str="";
+        if($versionInfo['version']!=0){
+		$table="sections_revisions";
+		$condition="AND deleted=0 ";
+		$condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 ";
+	}else{
+		$table="sections";
+		$condition="";
+	}
+        $query="SELECT * 
+                FROM ".$table."
+                WHERE books_id=".$bookId." ".$condition."
+                GROUP BY books_id, start_page, end_page
+                HAVING COUNT( * ) >=".$count."
+                ORDER BY id ASC";
+                /*ORDER BY start_page ASC, level ASC, id ASC,end_page DESC";*/
+        $result=mysql_query($query);
+        $i=0;
+	$pageArray=array();
+        while($row=mysql_fetch_assoc($result)){
+                $pageArray[$i]['start_page']=$row['start_page'];
+                $pageArray[$i]['end_page']=$row['end_page'];
+                $i++;
+        }
+        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";
+        }
+        $result=mysql_query($query);
+        $i=0;
+	$sectionArray=array();
+        while($row=mysql_fetch_assoc($result)){
+                $flag="";
+                if($i!=sizeof($pageArray) && $pageArray[$i]['start_page']==$row['start_page'] && $pageArray[$i]['end_page']==$row['end_page']){
+                        $flag="overlapper";
+                        $i++;
+                }
+                if($i!=0 && $pageArray[$i-1]['start_page']==$row['start_page'] && $pageArray[$i-1]['end_page']==$row['end_page']){
+                        $flag="overlapper";
+                }
+                $row['flag']=$flag;
+		if(!isset($row['deleted'])){
+			$row['deleted']="";
+		}else{
+			if($row['deleted']==0){
+				$row['deleted']="";
+			}else{
+				$row['deleted']="deleted";
+			}
+		}
+                $sectionArray[]=$row;
+        }
+        return $sectionArray;
+}
+function updateSectionArray($bookId,$sectionArray,$version,$editor){
+	$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 version records.");
+		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){
+		$str="(";
+		$str.="'".$row['name']."',";
+		$str.="'".sprintf("%05d",$row['booksId'])."',";
+		$str.="'".$row['sectionAfter']."',";
+		$str.=$row['startPage'].",";
+		$str.=$row['endPage'].",";
+		$str.=$row['level'].",";
+		$str.=$row['splitFrom'].",";
+		$str.=$row['id'].",";
+		$str.=$versionId.",";
+		$str.=$row['deleted'];
+		$str.=")";
+		if($idx!=sizeof($sectionArray)-1){
+			$str.=", ";
+		}
+		$query.=$str;	
+	}
+	$result=mysql_query($query);
+	if(!$result){
+		echo json_encode("Failed during inserting section records.");
+		return;
+	}else{
+		echo json_encode("Succeeded.");
+	}
+}
+if(isset($_POST['command']) && $_POST['command']!=''){
+	$command=$_POST['command'];
+	if($command=="write"){
+		if(isset($_POST['bookId'])&&isset($_POST['sectionArray'])&&isset($_POST['version'])&&isset($_POST['editor'])&&
+			is_numeric($_POST['bookId']) && sizeof($_POST['sectionArray'])!=0 && is_numeric($_POST['version']) && $_POST['editor']!=""){
+			$bookId=$_POST['bookId'];
+			$sectionArray=$_POST['sectionArray'];
+			$version=$_POST['version'];
+			$editor=$_POST['editor'];
+			updateSectionArray($bookId,$sectionArray,$version,$editor);
+		}
+	}
+}
+
+?>