diff develop/models/extractapp.php @ 21:c805470cefee

Remove bootstrap.TaggingText UI layout modification
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 24 Feb 2015 16:09:59 +0100
parents 04db1dd9d10d
children a8ae5cbc3364
line wrap: on
line diff
--- a/develop/models/extractapp.php	Mon Feb 23 14:34:43 2015 +0100
+++ b/develop/models/extractapp.php	Tue Feb 24 16:09:59 2015 +0100
@@ -8,7 +8,7 @@
 		return array("Index Value 1", "Value 2", "Value 3");
 	}
     
-    protected $section_id = 0, $data_path, $file_id = 0, $branch_id = 0, $user_id = 0, $lg_text = "";
+    protected $section_id = 0, $data_path, $file_id = 0, $branch_id = 0, $user_id = 0, $lg_text = "", $topic = 0;
     public $messages = "";
 
     private function Initialize($_urlvalues) {
@@ -84,7 +84,13 @@
         if ($_postdata['userId']) {
             $this->user_id = $_postdata['userId'];
         }
+        if ($_postdata['topic_id']) {
+            $this->topic = $_postdata['topic_id'];
+        }
 
+        $this->messages .= "Info: ";
+        $this->messages .= "file_id=".$this->file_id.", section_id=".$this->section_id;
+        $this->messages .= ", user_id=".$this->user_id.", branch_id=".$this->branch_id.", topic_id=".$this->topic."<br>";
 
     }
     public function InitData($_postdata) {
@@ -365,7 +371,9 @@
 
         $content = $postdata['content'];
         $topic = $postdata['topic'];
-        $section_id = $this->section_id;
+        $section_id = $postdata['sectionId'];
+
+        //$section_id = $this->section_id;
         
 
         // TODO: this should be get from LGServic: sectionName, bookId, bookName
@@ -487,6 +495,9 @@
     // === for manage tag list ===
     public function EditTaglist($_postdata) {
         
+        $query = "SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'taglist'";
+        $largest_id = mysql_query($query);
+
         $topic_id = $_postdata['topic_id'];
         $result = $this->GetTaglistByTopicID($topic_id);
 
@@ -508,6 +519,9 @@
         $data = array();
         $data['taglistArray'] = $taglistArray;
         $data['topic_id'] = $topic_id;
+        $data['largest_id'] = $largest_id;
+
+
         return $data;
        
     }
@@ -599,14 +613,112 @@
         $data['topic'] = $topic;
         $data['topic_name'] = $topic_name;
 
+
+
+
+        // ------
+        $query = "SELECT taglist.*, topic_tag_relation.topic_id FROM taglist LEFT JOIN topic_tag_relation ON taglist.id = topic_tag_relation.tag_id ORDER BY `topic_id`";
+        $result = mysql_query($query);
+        if (!$result) {
+            return json_encode("Failed during selecting/joining taglist and topic_tag_relation table.");
+        }
+
+        $tag_intopic = array();
+        $tag_others = array();
+        $tag_tmp_others = array();
+        while ($row = mysql_fetch_assoc($result)) {
+            if ($row['topic_id'] == $topic) {
+                array_push($tag_intopic, array('id=>'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id']));
+            } else {
+                array_push($tag_tmp_others, array('id=>'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id']));
+            }
+        }
+
+        
+        foreach ($tag_tmp_others as $tmp) {
+            $cnt = 0;
+            foreach ($tag_intopic as $intopic) {
+                if ($tmp['tag'] == $intopic['tag']) {
+                    break;
+                } else {
+                    $cnt ++;
+                }
+            }
+            if ($cnt == count($tag_intopic)) {
+                array_push($tag_others, $tmp);
+            }
+        }
+        
+
+        $data['tag_intopic'] = $tag_intopic;
+        $data['tag_others'] = $tag_others;
+
         return $data;
     }
+
+
     public function UpdateTagsInTopic($_postdata) {
         $topic_id = $_postdata['topic_id'];
         $tag_ids = json_decode(str_replace('\\', '', $_postdata['ids']));
 
         // update topic_tag_relation by tags_ids array as `tag_id` and topic_id as `topic_id`
-        $query = "SELECT * FROM topic_tag_relation";
+        // --- add new topic_tag_relation ---
+        foreach ($tag_ids as $tag_id) {
+            $query = "SELECT * FROM topic_tag_relation WHERE tag_id=".$tag_id;
+            $result = mysql_query($query);
+            if (!$result) {
+                echo json_encode("error when select from topic_tag_relation");
+            }
+            $topic_tag = array();
+            $flag = false;
+            while ($row = mysql_fetch_assoc($result)) {
+                if ($row['topic_id'] == $topic_id) {
+                    $flag = true;
+                    break;
+                }
+            }
+            if (!$flag) {
+                // insert a row into topic_tag_relation table
+                $queryUpdate = "INSERT INTO topic_tag_relation (topic_id, tag_id) VALUES (".$topic_id.",".$tag_id.")";
+                $resultUpdate = mysql_query($queryUpdate); 
+                if (!$resultUpdate) {
+                    return json_encode("error when insert topic_tag_relation table");
+                }   
+            }
+
+        }
+
+        // --- remove tags from this topic ---
+        $query = "SELECT * FROM topic_tag_relation WHERE topic_id=".$topic_id;
+        $result = mysql_query($query);
+        if (!$result) {
+            echo json_encode("error when select from topic_tag_relation");
+        }
+
+        while ($row = mysql_fetch_assoc($result)) {
+            $cnt = 0;
+            foreach ($tag_ids as $tag_id) {
+                if ($row['tag_id'] == $tag_id) {
+                    break;
+                } else {
+                    $cnt ++;
+                }
+            }
+            $_id = $row['id'];
+            if ($cnt == count($tag_ids)) {
+                // delete row with (topic_id, tag_ids)
+                $queryDelete = "DELETE FROM topic_tag_relation WHERE id=".$_id;
+                $resultDelete = mysql_query($queryDelete);
+                if (!$resultDelete) {
+                    echo json_encode("error when delete from topic_tag_relation");
+                }
+            }
+        }
+
+    
+
+        /*
+ยง        $query = "SELECT * FROM topic_tag_relation";
         $result = mysql_query($query);
         if (!$result) {
             echo json_encode("error when select from topic_tag_relation");
@@ -616,6 +728,7 @@
             array_push($topic_tag, array('tag_id'=>$row['tag_id'], 'topic_id'=>$row['topic_id']));
         }
 
+
         foreach ($topic_tag as $value) {
             $flag = false;
             foreach ($tag_ids as $tag_id) {
@@ -639,6 +752,7 @@
                 }   
             }
         }
+        */
 
     }