diff models/extractapp.php @ 94:b0cecc104639 extractapp

new: modified tags can work on tagged text
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 09 Nov 2015 12:00:24 +0100
parents 3e11a9c5a672
children 9a29e9d28ece
line wrap: on
line diff
--- a/models/extractapp.php	Fri Oct 02 12:18:34 2015 +0200
+++ b/models/extractapp.php	Mon Nov 09 12:00:24 2015 +0100
@@ -250,6 +250,58 @@
     }
     
     // === for tagging ===
+    private function ReplaceLgTextWithLatestTag() {
+        // modify $this->lg_text with the latest tags from database
+        // can do it by getting the tag's id using tag's information in the file, then by the id to get latest tag (in db)
+        $lg_text = $this->lg_text;
+
+        $taglist_indb = $this->GetTaglistByTopicId($this->GetTopic());
+        $taglist_infile = $this->taglist_infile;
+
+        if ($taglist_infile == "") return;
+        
+        $count_tag_indb = count($taglist_indb);
+
+        foreach ($taglist_infile as $t1) {
+            $id = $t1[0];
+            $tag = $t1[2];
+            $latest_tag = "";
+
+            $cnt = 0;
+            foreach ($taglist_indb as $t2) {
+                if ($t2[0] == $id) {
+                    $latest_tag = $t2[2];
+                }
+               
+                if ($tag != $t2[2]) {
+                    // not match with this t2
+                    $cnt += 1;
+                } else {
+                    break;
+                }
+
+            }
+            if ($cnt == $count_tag_indb) {
+                if ($latest_tag == "") {
+                    // tag has been deleted in the db
+                    // delele the tag in $this->lg_text
+                    //echo "tag been deleted";
+
+                    $lg_text = preg_replace("/<\/".$tag.">/u", "", $lg_text);
+                    $lg_text = preg_replace("/<".$tag.">/u", "", $lg_text);
+
+                } else {
+                    // replace all tags $tag into $latest_tag in $this->lg_text
+                    //echo "replace tag: ".$tag." into ".$latest_tag."<br>";
+                    $lg_text = preg_replace("/".$tag."/u", $latest_tag, $lg_text);
+                }
+            }
+        }
+
+
+        $this->lg_text = $lg_text;
+
+    }
     public function StartTagging() {
         /**
          * This is the main method for tagging text. It passes all the information to "views/Extractapp/TaggingText.php" view.
@@ -257,11 +309,9 @@
          */
        
         $section_id = $this->section_id;
-        $stringInput = $this->lg_text;
-
+        
         $data = array();    // data array to be passed to view
 
-        //$taglistArray = $this->GetTaglistArray();
         //for GetTaglistByTopicId: 
         $taglistArray = $this->GetTaglistByTopicId($this->GetTopic());
 
@@ -269,9 +319,18 @@
         // $this->taglist_infile is the most up-to-date taglist decided by user. Should be written into file.
         if( $this->TaglistSubsetIn($this->taglist_infile, $taglistArray) ) {    // TaglistSubsetIn($l1,$l2): $l1 is a subset of $l2 or not
             $this->taglist_infile = "";
-        } 
+            
+        } else {
+            // taglist_infile is out-to-date
+            // replace all the old tag with new ones in database
+            // replace old tags for $this->lg_text with the latest tags from db
+            $this->ReplaceLgTextWithLatestTag();
+
+        }
+        $stringInput = $this->lg_text;
+
+
         $data['taglist_infile'] = $this->taglist_infile;    // if taglist_infile == "" means taglist in file is up-to-date (will use taglist in db), otherwise means the taglist in file
-
         
         // book_meta
         $data['book_meta'] = $this->book_meta;
@@ -424,6 +483,10 @@
         // ---- taglist ---
         // $taglist = $this->taglist_infile;
         // obtain the latest taglist from db
+        // TDOO: possible bug: when taglist is modified by other user, the latest taglist from db of course will be changed.
+        // As the result, here the taglist we get from db will be inconsistent with the tag in file.
+        // which means the information in <tagitem> nodes are inconsistent with the text in <text_content> node.
+
         $taglist = $this->GetTaglistByTopicId($this->GetTopic());
 
         foreach ($taglist as $tagitem) {
@@ -875,10 +938,19 @@
 
     public function DeleteTag($postdata) {
         if ($postdata['id']) {
-            $queryInsert = sprintf("DELETE FROM `taglist` WHERE `id` = %s", stripslashes($postdata['id']));
-            $resultInsert = mysql_query($queryInsert);
+            $tag_id = $postdata['id'];
+
+            // delete record in 'taglist' table
+            $query = sprintf("DELETE FROM `taglist` WHERE `id` = %s", stripslashes($tag_id));
+            $result = mysql_query($query);
+
+            // delete record in 'topic_tag_relation' table
+            $topic_id = $postdata['topic_id'];
+            $query = sprintf("DELETE FROM `topic_tag_relation` WHERE `tag_id` = %s and `topic_id` = %s", stripslashes($tag_id), stripcslashes($topic_id));
+            $result = mysql_query($query);
+
         }
-
+       
     }
 
     // === for config topic ===
@@ -974,6 +1046,10 @@
         return $data;
     }
 
+    public function CreateNewTopic($_postdata) {
+
+        
+    }
 
     public function UpdateTagsInTopic($_postdata) {
         $topic_id = $_postdata['topic_id'];
@@ -1307,7 +1383,7 @@
         }
         */
         // -----
-        // TODO: get section_id, section_name from file
+        // get section_id, section_name from file
         if ($this->section_id == 0 && isset($xml->section)) {
             if ($xml->section) {
                 $this->section_id = (string)$xml->section->id;
@@ -1327,6 +1403,7 @@
         $removed_str = array("<text_content>","</text_content>");
         $new_contentString = str_replace($removed_str, "", $contentString);
 
+
         return $new_contentString;
     }