changeset 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 115dda3d1728
children 04db1dd9d10d
files develop/controllers/extractapp.php develop/models/extractapp.php develop/views/Extractapp/configtagsintopic.php
diffstat 3 files changed, 75 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/develop/controllers/extractapp.php	Mon Feb 23 10:55:28 2015 +0100
+++ b/develop/controllers/extractapp.php	Mon Feb 23 12:46:03 2015 +0100
@@ -128,12 +128,12 @@
         //$viewmodel = new ExtractappModel();
         $viewmodel = $this->viewmodel;
         switch ($this->postdata['func']) {
-            case 'somefunc':
-                
+            case 'Update':
+                $viewmodel->UpdateTagsInTopic($this->postdata);
                 break;
             default:    
                 $this->ReturnView($viewmodel->ConfigTagsInTopic($this->postdata), true);  
-            break;
+                break;
          } 
     }
 
--- 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 ===
--- a/develop/views/Extractapp/configtagsintopic.php	Mon Feb 23 10:55:28 2015 +0100
+++ b/develop/views/Extractapp/configtagsintopic.php	Mon Feb 23 12:46:03 2015 +0100
@@ -53,15 +53,33 @@
     }
 }
 
-
-	
 function checkTopic(t) {
 	// TODO: save the modification into topic_tag_relation
-	var id = t.name;
-	var val;
-	console.log(id);
+	var ids = [];
+
+	for (var i = 0; i < t.options.length; i++) {
+		ids.push(t.options[i].value);
+		console.log(t.options[i].text);
+	}
+
+	console.log(ids);
+	var ids_json = JSON.stringify(ids);
+	var topic_id = JSON.parse('<?php echo json_encode($topic) ?>');
 
+	$.ajax({
+		url : './ConfigTagsInTopic',
+		async : false,
+		type : 'POST',
+		data : 'func=Update'+'&ids='+ids_json+'&topic_id='+topic_id,
+		error: function (e) {
+			console.log("error when update tags in topic"+ e );
+		},
+		success: function (e) {
+			alert("Updated!");
+		}
+	}).done(function(result) {
 
+	});
 
 
 }
@@ -72,7 +90,7 @@
     <div class="row">	
     	<div class="col-xs-12 col-sm-12 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3">
 
-		<form name="config" autocomplete='off' action="Javascript:checkTopic(this)">
+		<form name="config" autocomplete='off' action="Javascript:checkTopic(document.config.InTopic)">
 			<h3>Topic: <?php echo $topic_name;?> </h3>
 			
 			<table class="table">
@@ -90,7 +108,7 @@
 							<?php 
 								foreach ($taglistArray as $taglistValue) {
 									if ($taglistValue['topic_id'] == $topic) {
-										echo "<option value='".$taglistValue['tag']."' class='text-primary'>".$taglistValue['name']."</option>";
+										echo "<option value='".$taglistValue['id']."' class='text-primary'>".$taglistValue['name']." (".$taglistValue['tag'].") "."</option>";
 									}
 								}
 							?>
@@ -106,7 +124,7 @@
 							<?php 
 								foreach ($taglistArray as $taglistValue) {
 									if ($taglistValue['topic_id'] != $topic) {
-										echo "<option value='".$taglistValue['tag']."' class='text-muted' >".$taglistValue['name']."</option>";
+										echo "<option value='".$taglistValue['id']."' class='text-muted'>".$taglistValue['name']." (".$taglistValue['tag'].") "."</option>";
 									}
 								}
 							?>