diff develop/models/extractapp.php @ 19:f60969462226

save tag modification of topic configration into topic_tag_relation table
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 23 Feb 2015 12:46:03 +0100
parents 82f8c6bd2b69
children 04db1dd9d10d
line wrap: on
line diff
--- a/develop/models/extractapp.php	Mon Feb 23 10:55:28 2015 +0100
+++ b/develop/models/extractapp.php	Mon Feb 23 12:46:03 2015 +0100
@@ -532,7 +532,7 @@
 
             // add it to topic_tag_relation table
             $topic_id = $postdata['topic_id'];
-            
+
             // tag_id is $id;
             $query = sprintf("INSERT INTO `topic_tag_relation` (`topic_id`,`tag_id`) VALUES (%s,%s)", $topic_id, $id);
             $result = mysql_query($query);
@@ -547,13 +547,13 @@
         if ($postdata['id']){   
             list($id, $name, $tag, $color) = $this->_GetTag($postdata);
             
-            $queryInsert = sprintf("UPDATE taglist SET `name`=%s, `tag`=%s, `color`=%s WHERE `id`=%s",
+            $query = sprintf("UPDATE taglist SET `name`=%s, `tag`=%s, `color`=%s WHERE `id`=%s",
                                 $this->GetSQLValueString($name, "text"),
                                 $this->GetSQLValueString($tag, "text"),
                                 $this->GetSQLValueString($color, "text"),
                                 $this->GetSQLValueString($id, "int"));
-            $resultInsert = mysql_query($queryInsert);
-          
+            $result = mysql_query($query);
+                     
         }
 
     }
@@ -591,6 +591,47 @@
 
         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";
+        $result = mysql_query($query);
+        if (!$result) {
+            echo json_encode("error when select from topic_tag_relation");
+        }
+        $topic_tag = array();
+        while ($row = mysql_fetch_assoc($result)) {
+            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) {
+                if ($value['tag_id'] == $tag_id) {
+                    //update its topic_id to $topic_id
+                    $queryUpdate = "UPDATE topic_tag_relation SET topic_id=".$topic_id." WHERE tag_id=".$tag_id;
+                    $resultUpdate = mysql_query($queryUpdate); 
+                    if (!$resultUpdate) {
+                        return json_encode("error when update topic_tag_relation table");
+                    }   
+                    $flag = true;
+                    break;
+                }
+            }
+            if (!$flag && $value['topic_id'] == $topic_id) {
+                // set its topic_id to 0, indicating unsigned
+                $queryUpdate = "UPDATE topic_tag_relation SET topic_id=0 WHERE tag_id=".$value['tag_id'];
+                $resultUpdate = mysql_query($queryUpdate); 
+                if (!$resultUpdate) {
+                    return json_encode("error when update topic_tag_relation table");
+                }   
+            }
+        }
+
+    }
+
 
     private function GetTaglistByTopicID($topic_id) {
         $taglistArray = array();
@@ -655,6 +696,7 @@
 
     }
 
+
     // =========================== 
 
     // === for manage wordlist ===