diff models/extractapp.php @ 95:9a29e9d28ece extractapp

new: create new topic
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Wed, 11 Nov 2015 14:12:55 +0100
parents b0cecc104639
children c1bb174a22f3
line wrap: on
line diff
--- a/models/extractapp.php	Mon Nov 09 12:00:24 2015 +0100
+++ b/models/extractapp.php	Wed Nov 11 14:12:55 2015 +0100
@@ -188,9 +188,10 @@
         if (isset($_postdata['userId'])) {
             $this->user_id = $_postdata['userId'];
         }
-        if (isset($_postdata['topic_id'])) {
-            $this->topic = $_postdata['topic_id'];
+        if (isset($_postdata['topic'])) {
+            $this->topic = $_postdata['topic'];
         }
+        
         if (isset($_postdata['sectionName'])) {
             $this->section_name = $_postdata['sectionName'];
         }
@@ -954,12 +955,87 @@
     }
 
     // === for config topic ===
-    public function ConfigTagsInTopic($postdata) {
+    public function CreateNewTopic($_postdata) {    
+        $new_topic_id = "2";  // default topic_id=1 for 'person' topic
+
+        // get user input data from postdata
+        $new_topic_name_en = $_postdata['new_topic_name_en'];
+        $new_topic_name_ch = $_postdata['new_topic_name_ch'];
+        $new_topic_name_pi = $_postdata['new_topic_name_pi'];
+        $new_topic_tag = $_postdata['new_topic_tag'];
+        $new_topic_tag_name = $_postdata['new_topic_tag_name'];
+
+        // -- Start Transaction -- 
+        mysql_query("BEGIN");
+
+        // create records for the new topic in database
+        /* In table:
+            taglist: name, tag, color (default, user can change later in the editing page)
+            topics: tag, name_en, name_ch, name_pinyin
+            topic_tag_relation: topic_id (new), tag_id (new)
+
+        - need transaction for the following actions:
+            taglist.name: topic_tag name in chinese 
+            taglist.tag: topic_tag name in eng
+            topics.tag: (the same a taglist.tag)
+            topics.name_en:
+            topics.name_ch: 
+            topics.name_pinyin: 
+
+             
+            topic_tag_relation.topic_id = new_topic_id
+            topic_tag_relation.tag_id = new_tag_id 
+                which are the new record that just been created in the 'taglist' and 'topics' table
+        */
+
+        // new record in topics table           
+        $query="INSERT INTO topics (tag, name_en, name_ch, name_pinyin) VALUES ('".$new_topic_tag."','".$new_topic_name_en."','".$new_topic_name_ch."','".$new_topic_name_pi."')";  
+        $result_topics=mysql_query($query);
+        if($result_topics){
+            $new_topic_id = mysql_insert_id();
+        } else {
+            //echo mysql_error();
+        }
+
+        // new record in taglist table
+        $query="INSERT INTO taglist (name, tag, color, systemName) VALUES ('".$new_topic_tag_name."','".$new_topic_tag."','rgb(255, 0, 174)','interface')";  
+        $result_taglist=mysql_query($query);
+        if($result_taglist){
+            $new_tag_id = mysql_insert_id();
+        } else {
+            //echo mysql_error();
+        }
+
+        // new record in topic_tag_relation
+        $query="INSERT INTO topic_tag_relation (topic_id, tag_id) VALUES ('".$new_topic_id."','".$new_tag_id."')";  
+        $result_topic_tag_relation=mysql_query($query);
+        if($result_topic_tag_relation){
+            
+        } else {
+            //echo mysql_error();
+        }
+
+ 
+        // --- End Transaction ---
+        if ($result_taglist && $result_topics && $result_topic_tag_relation) {
+            mysql_query("COMMIT");  
+            
+        } else {
+            mysql_query("ROLLBACK");
+            return "error";
+        }
+        
+
+        $_postdata['topic'] = $new_topic_id;
+        return $_postdata;
+    }
+
+    public function ConfigTagsInTopic($_postdata) {
         /**
         * 
         */
 
-        $topic = $postdata['topic'];
+        $topic = $_postdata['topic'];
         $result = $this->GetTopicById($topic);
         $row = mysql_fetch_assoc($result);
         $topic_name_en = $row['name_en'];
@@ -1046,10 +1122,7 @@
         return $data;
     }
 
-    public function CreateNewTopic($_postdata) {
 
-        
-    }
 
     public function UpdateTagsInTopic($_postdata) {
         $topic_id = $_postdata['topic_id'];