diff develop/models/extractapp.php @ 27:4a29bccb6c59

modify the SmartRegexSave method to prevent duplicated records in topic_regex_relation table and provide better promting to user to force saving regex file or not
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 03 Mar 2015 11:47:41 +0100
parents 579f03f8bee5
children e6e9bdc4f256
line wrap: on
line diff
--- a/develop/models/extractapp.php	Tue Mar 03 10:33:42 2015 +0100
+++ b/develop/models/extractapp.php	Tue Mar 03 11:47:41 2015 +0100
@@ -196,7 +196,7 @@
                 'branch_id'=>$this->branch_id, 'section_id'=>$this->section_id, 'book_id'=>$this->book_id, 
                 'book_name'=>$this->book_name, 'section_name'=>$this->section_name,
                 'current_fileId'=>$this->current_fileId);
-        
+
         $data['messages'] = $this->messages;
 
         return $data;
@@ -377,6 +377,35 @@
 
     public function SmartRegexSave($_postdata) {
         if ($_postdata['text']){
+
+            // --- update topic_regex_relation table ---
+            $topic_id = $_postdata['topic_id'];
+            $filename = $_postdata['filename'].'.txt';
+            
+            // check if already existing a record for this regex file
+            $query = "SELECT * FROM topic_regex_relation WHERE topic_id=".$topic_id." AND regex_filename='".$filename."'";
+            $result = mysql_query($query);
+            if (!$result) {
+                echo json_encode("ErrorDB");
+            }
+            while ($row = mysql_fetch_assoc($result)) {
+                if ($row && !$_postdata['forcesave']) {
+                    // promt user a comfirm to force saving or not
+                    echo json_encode("ForceSave");
+                    return;
+                }
+            } 
+
+        
+            if (!$_postdata['forcesave']) {
+                $query = "INSERT INTO topic_regex_relation (topic_id, regex_filename) VALUES (".$topic_id.",'".$filename."')";
+                $result = mysql_query($query);
+                if (!$result) {
+                    echo json_encode("ErrorDB");
+                }
+            } 
+            // --- write to filesystem ---
+            
             /*
             $date = date('Y_m_d_H_i_s', time());
             if ( file_exists("regex_files/".$_POST['filename'].".txt") ) {
@@ -394,14 +423,6 @@
 
             file_put_contents( $data_path."regex_files/".$_postdata['filename'].".txt", $require);   
 
-            // update topic_regex_relation table
-            $topic_id = $_postdata['topic_id'];
-            $filename = $_postdata['filename'].'.txt';
-            $query = "INSERT INTO topic_regex_relation (topic_id, regex_filename) VALUES (".$topic_id.",'".$filename."')";
-            $result = mysql_query($query);
-            if (!$result) {
-                echo json_encode("error");
-            }
 
         }
     }