Mercurial > hg > extraction-interface
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b12c99b7c3f0 |
---|---|
1 <?php | |
2 include_once('Lib_mb_utf8.php'); | |
3 include_once('config.php'); | |
4 set_time_limit(0); | |
5 ini_set('memory_limit', '-1'); | |
6 | |
7 $link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password); | |
8 mysql_query("SET NAMES utf8"); | |
9 | |
10 if (!$link_mysql) { | |
11 die('Could not connect: ' . mysql_error()); | |
12 } | |
13 | |
14 $db_selected = mysql_select_db($mysql_database, $link_mysql); | |
15 if (!$db_selected) { | |
16 die ('Can\'t use foo : ' . mysql_error()); | |
17 } | |
18 function checkSectionInfo($bookId){ | |
19 $query="SELECT * FROM sections_versions WHERE books_id=".$bookId." ORDER BY version DESC"; | |
20 $result=mysql_query($query); | |
21 if(mysql_num_rows($result)==0){ | |
22 $row['version']=0; | |
23 $row['editor']=""; | |
24 $row['date']=""; | |
25 return $row; | |
26 }else{ | |
27 $row=mysql_fetch_assoc($result); | |
28 return $row; | |
29 } | |
30 } | |
31 function checkMissingPage($bookInfo,$versionInfo){ | |
32 $bookId=$bookInfo['id']; | |
33 $bookPage=$bookInfo['line']; | |
34 if($versionInfo['version']!=0){ | |
35 $table="sections_revisions"; | |
36 $condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 "; | |
37 $versionId=$versionInfo['id']; | |
38 }else{ | |
39 $table="sections"; | |
40 $condition=""; | |
41 } | |
42 $query="SELECT * | |
43 FROM ".$table." | |
44 WHERE books_id=".$bookId." ".$condition." | |
45 GROUP BY books_id,start_page,end_page | |
46 ORDER BY books_id,start_page,level,end_page"; | |
47 $result=mysql_query($query); | |
48 $lastPage=1; | |
49 $i=0; | |
50 $pageArray=array(); | |
51 while($row=mysql_fetch_assoc($result)){ | |
52 if($row['start_page']-1>$lastPage){ | |
53 $pageArray[$i]['start_page']=$lastPage; | |
54 $pageArray[$i]['end_page']=$row['start_page']; | |
55 //echo $pageArray[$i]['start_page']." ".$pageArray[$i]['end_page']."<br>"; | |
56 $i++; | |
57 } | |
58 $lastPage=$row['end_page']; | |
59 } | |
60 if($bookPage>$lastPage){ | |
61 $pageArray[$i]['start_page']=$lastPage; | |
62 $pageArray[$i]['end_page']=$bookPage; | |
63 } | |
64 return $pageArray; | |
65 } | |
66 function getSectionArray($bookId,$count,$versionInfo){ | |
67 $str=""; | |
68 if($versionInfo['version']!=0){ | |
69 $table="sections_revisions"; | |
70 $condition="AND deleted=0 "; | |
71 $condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 "; | |
72 }else{ | |
73 $table="sections"; | |
74 $condition=""; | |
75 } | |
76 $query="SELECT * | |
77 FROM ".$table." | |
78 WHERE books_id=".$bookId." ".$condition." | |
79 GROUP BY books_id, start_page, end_page | |
80 HAVING COUNT( * ) >=".$count." | |
81 ORDER BY id ASC"; | |
82 /*ORDER BY start_page ASC, level ASC, id ASC,end_page DESC";*/ | |
83 $result=mysql_query($query); | |
84 $i=0; | |
85 $pageArray=array(); | |
86 while($row=mysql_fetch_assoc($result)){ | |
87 $pageArray[$i]['start_page']=$row['start_page']; | |
88 $pageArray[$i]['end_page']=$row['end_page']; | |
89 $i++; | |
90 } | |
91 if($versionInfo['version']!=0){ | |
92 $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"; | |
93 }else{ | |
94 $query="SELECT * FROM sections WHERE books_id=".$bookId." ORDER BY id ASC"; | |
95 } | |
96 $result=mysql_query($query); | |
97 $i=0; | |
98 $sectionArray=array(); | |
99 while($row=mysql_fetch_assoc($result)){ | |
100 $flag=""; | |
101 if($i!=sizeof($pageArray) && $pageArray[$i]['start_page']==$row['start_page'] && $pageArray[$i]['end_page']==$row['end_page']){ | |
102 $flag="overlapper"; | |
103 $i++; | |
104 } | |
105 if($i!=0 && $pageArray[$i-1]['start_page']==$row['start_page'] && $pageArray[$i-1]['end_page']==$row['end_page']){ | |
106 $flag="overlapper"; | |
107 } | |
108 $row['flag']=$flag; | |
109 if(!isset($row['deleted'])){ | |
110 $row['deleted']=""; | |
111 }else{ | |
112 if($row['deleted']==0){ | |
113 $row['deleted']=""; | |
114 }else{ | |
115 $row['deleted']="deleted"; | |
116 } | |
117 } | |
118 $sectionArray[]=$row; | |
119 } | |
120 return $sectionArray; | |
121 } | |
122 function updateSectionArray($bookId,$sectionArray,$version,$editor){ | |
123 $query="INSERT INTO sections_versions (version,editor,date,books_id) VALUES (".($version+1).",'".$editor."',NOW(),'".$bookId."')"; | |
124 $result=mysql_query($query); | |
125 if(!$result){ | |
126 echo json_encode("Failed during inserting version records."); | |
127 return; | |
128 } | |
129 $versionId=mysql_insert_id(); | |
130 $query="INSERT INTO sections_revisions (name,books_id,section_after,start_page,end_page,level,split_from,sections_id,versions_id,deleted) VALUES "; | |
131 foreach($sectionArray as $idx=>$row){ | |
132 $str="("; | |
133 $str.="'".$row['name']."',"; | |
134 $str.="'".sprintf("%05d",$row['booksId'])."',"; | |
135 $str.="'".$row['sectionAfter']."',"; | |
136 $str.=$row['startPage'].","; | |
137 $str.=$row['endPage'].","; | |
138 $str.=$row['level'].","; | |
139 $str.=$row['splitFrom'].","; | |
140 $str.=$row['id'].","; | |
141 $str.=$versionId.","; | |
142 $str.=$row['deleted']; | |
143 $str.=")"; | |
144 if($idx!=sizeof($sectionArray)-1){ | |
145 $str.=", "; | |
146 } | |
147 $query.=$str; | |
148 } | |
149 $result=mysql_query($query); | |
150 if(!$result){ | |
151 echo json_encode("Failed during inserting section records."); | |
152 return; | |
153 }else{ | |
154 echo json_encode("Succeeded."); | |
155 } | |
156 } | |
157 if(isset($_POST['command']) && $_POST['command']!=''){ | |
158 $command=$_POST['command']; | |
159 if($command=="write"){ | |
160 if(isset($_POST['bookId'])&&isset($_POST['sectionArray'])&&isset($_POST['version'])&&isset($_POST['editor'])&& | |
161 is_numeric($_POST['bookId']) && sizeof($_POST['sectionArray'])!=0 && is_numeric($_POST['version']) && $_POST['editor']!=""){ | |
162 $bookId=$_POST['bookId']; | |
163 $sectionArray=$_POST['sectionArray']; | |
164 $version=$_POST['version']; | |
165 $editor=$_POST['editor']; | |
166 updateSectionArray($bookId,$sectionArray,$version,$editor); | |
167 } | |
168 } | |
169 } | |
170 | |
171 ?> |