Index: common/db/blog.php
===================================================================
--- common/db/blog.php	(revision 941)
+++ common/db/blog.php	(working copy)
@@ -22,26 +22,14 @@
 }
 
 function blog_get_forum_topic($name) {
-    $query = sprintf("SELECT topic_id FROM ia_blog_forum
-                      WHERE textblock = '%s'", db_escape($name));
+    $query = sprintf("SELECT `forum_topic` FROM ia_textblock
+                      WHERE name = '%s'", db_escape($name));
     $result = db_fetch($query);
-    return $result['topic_id'];
+    return $result['forum_topic'];
 }
 
-function blog_set_forum_topic($name, $value) {
-    $old_id = blog_get_forum_topic($name);
-    if (is_null($old_id)) {
-        $query = sprintf("INSERT INTO ia_blog_forum (textblock, topic_id)
-                          VALUES (%s, %s)", db_quote($name), db_escape($value));
-    } else {
-        $query = sprintf("UPDATE ia_blog_forum SET topic_id = %s 
-                          WHERE textblock = %s", db_escape($value), db_quote($name));
-    }
-    db_query($query);
-}
-
 function blog_get_comment_count($topic_id) {
-    $query = sprintf("SELECT numReplies FROM ia_smf_topics 
+    $query = sprintf("SELECT `numReplies` FROM ia_smf_topics 
                       WHERE ID_TOPIC = %d", db_escape($topic_id));
     $result = db_fetch($query);
     return $result['numReplies'];
Index: common/db/textblock.php
===================================================================
--- common/db/textblock.php	(revision 941)
+++ common/db/textblock.php	(working copy)
@@ -13,8 +13,8 @@
 // Add a new revision
 // FIXME: hash parameter?
 function textblock_add_revision(
-        $name, $title, $content, $user_id, $security = "public", 
-        $timestamp = null, $creation_timestamp = null) {
+        $name, $title, $content, $user_id, $security = "public",
+        $forum_topic = null, $timestamp = null, $creation_timestamp = null) {
     $name = normalize_page_name($name);
     $tb = array(
             'name' => $name,
@@ -22,6 +22,7 @@
             'text' => $content,
             'user_id' => $user_id,
             'security' => $security,
+            'forum_topic' => $forum_topic,
             'timestamp' => $timestamp,
             'creation_timestamp' => $creation_timestamp,
     );
@@ -57,11 +58,13 @@
         log_assert(is_db_date($timestamp), "Invalid timestamp");
     }
     $query = sprintf("INSERT INTO ia_textblock
-                        (name, `text`, `title`, `creation_timestamp`, `timestamp`, `user_id`, `security`)
-                      VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
+                        (name, `text`, `title`, `creation_timestamp`, `timestamp`, `user_id`, `security`, `forum_topic`)
+                      VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %s)",
                      db_escape($name), db_escape($content),
                      db_escape($title), db_escape($creation_timestamp), 
-                     db_escape($timestamp), db_escape($user_id), db_escape($security));
+                     db_escape($timestamp), db_escape($user_id),
+                     db_escape($security),
+                     is_null($forum_topic) ? "NULL" : db_escape($forum_topic));
     return db_query($query);
 }
 
@@ -70,7 +73,7 @@
 {
     //log_print_r($options);
 
-    $field_list = "`name`, `title`, `creation_timestamp`, `timestamp`, `security`, `user_id`";
+    $field_list = "`name`, `title`, `creation_timestamp`, `timestamp`, `security`, `user_id`, `forum_topic`";
 
     // Select content.
     if (getattr($options, 'content', false) == true) {
@@ -199,7 +202,7 @@
 
 // Grep through textblocks. This is mostly a hack needed for macro_grep.php
 function textblock_grep($substr, $page) {
-    $query = sprintf("SELECT `name`, `title`, `creation_timestamp`, `timestamp`, `user_id`, `security`
+    $query = sprintf("SELECT `name`, `title`, `creation_timestamp`, `timestamp`, `user_id`, `security`, `forum_topic`
                       FROM ia_textblock
                       WHERE `name` LIKE '%s' AND
                             (`text` LIKE '%s' OR `title` LIKE '%s')
Index: common/security.php
===================================================================
--- common/security.php	(revision 942)
+++ common/security.php	(working copy)
@@ -108,6 +108,7 @@
         case 'textblock-restore':
         case 'textblock-attach':
         case 'textblock-create':
+        case 'textblock-change-topic':
         case 'simple-rev-edit':
             return 'simple-rev-edit';
 
@@ -138,7 +139,6 @@
         case 'textblock-tag':
         case 'job-reeval':  
         case 'simple-critical':
-        case 'blog-admin':
             return 'simple-critical';
 
         // Special actions fall through
Index: common/task.php
===================================================================
--- common/task.php	(revision 941)
+++ common/task.php	(working copy)
@@ -306,19 +306,15 @@
     return $errors;
 }
 
-//FIXME: this is a hack; we should have a database table for this
 function task_get_topic($task_id) {
     if (!is_task_id($task_id)) {
         log_error("Invalid task id");
     }
 
     // Get task
-    $task = textblock_get_revision("problema/".$task_id);
-    $pattern = '/==\ *smftopic\(\ *topic_id="\ *([0-9]*).*0*\ *"\ *\)\ *==/i';
-    if (preg_match($pattern, $task['text'], $matches)) {
-        return $matches[1];
-    }
-    return null;
+    $task = task_get($task_id);
+    $task_page = textblock_get_revision($task["page_name"]);
+    return $task_page["forum_topic"];
 }
 
 ?>
Index: common/textblock.php
===================================================================
--- common/textblock.php	(revision 941)
+++ common/textblock.php	(working copy)
@@ -37,6 +37,10 @@
         $errors['user_id'] = 'ID de utilizator invalid';
     }
 
+    if (!is_null(getattr($tb, 'forum_topic')) && !is_whole_number(getattr($tb, 'forum_topic'))) {
+        $errors['forum_topic'] = 'Topic forum invalid';
+    }
+
     // NOTE: missing timestamp is OK!!!
     // It stands for 'current moment'.
     if (!is_db_date(getattr($tb, 'timestamp', db_date_format()))) {
@@ -84,7 +88,8 @@
 
         textblock_add_revision($textblock['name'], $textblock['title'],
                 $textblock['text'], $user_id, $textblock['security'],
-                null, $first_textblock['creation_timestamp']);
+                $textblock['forum_topic'], null,
+                $first_textblock['creation_timestamp']);
     }
 }
 
Index: scripts/link-task-forum
===================================================================
--- scripts/link-task-forum	(revision 941)
+++ scripts/link-task-forum	(working copy)
@@ -15,12 +15,8 @@
 }
 
 function link_task($task, $page, $topic, $verbose = false) {
-    $content = $page['text'];
-
-    // do we already have the macro there?
-    $pattern = '/==\ *smftopic\(\ *topic_id="\ *([0-9]*).*0*\ *"\ *\)\ *==/i';
-    if (preg_match($pattern, $content, $matches)) {
-        $topic_id = $matches[1];
+    if (!is_null($page["forum_topic"])) {
+        $topic_id = $page["forum_topic"];
         // WTF, wrong topic?
         if ($topic_id == $topic['ID_TOPIC']) {
             if ($verbose) {
@@ -29,27 +25,16 @@
             return false;
         }
         log_warn($task['id'].': Topic-ul vechi "'.$topic_id.'" este diferit de cel gasit: "'.$topic['ID_TOPIC'].'"!');
-        $replacement = "==SmfTopic(topic_id=\"".$topic['ID_TOPIC']."\")==";
-        $content = preg_replace($pattern, $replacement, $content);
     }
-    else {
-        if (stristr($content, 'SmfTopic')) {
-            log_warn('Eroare de regexp pentru "'.$task['id'].'"!');
-        }
-        $content .= "\n\n==SmfTopic(topic_id=\"".$topic['ID_TOPIC']."\")==\n";
-    }
 
     // Build new page
-    $new_page['name'] = $task['page_name'];
-    $new_page['text'] = $content;
-    $new_page['title'] = $page['title'];
-    $new_page['security'] = $page['security'];
-    $new_page['creation_timestamp'] = $page['creation_timestamp'];
-    $new_page['timestamp'] = null;
-    $new_page['user_id'] = 1; // update as "domino" :)
+    $new_page = $page;
+    $new_page["forum_topic"] = $topic["ID_TOPIC"];
+    $new_page["timestamp"] = null;
+    $new_page["user_id"] = 1; // update as "domino" :)
 
     // It worked
-    if (!textblock_validate($new_page)) {
+    if (count(textblock_validate($new_page)) == 0) {
         textblock_add_revision($new_page['name'], $new_page['title'],
                                $new_page['text'], $new_page['user_id'],
                                $new_page['security'], $new_page['timestamp'],
Index: scripts/textblock-add-topic-id
===================================================================
--- scripts/textblock-add-topic-id	(revision 0)
+++ scripts/textblock-add-topic-id	(revision 0)
@@ -0,0 +1,62 @@
+#! /usr/bin/env php
+<?php
+require_once(dirname($argv[0]) . "/utilities.php");
+require_once(IA_ROOT_DIR . "common/textblock.php");
+
+db_connect();
+db_query("ALTER TABLE `ia_textblock` ADD COLUMN `forum_topic` INT UNSIGNED DEFAULT NULL");
+db_query("ALTER TABLE `ia_textblock_revision` ADD COLUMN `forum_topic` INT UNSIGNED DEFAULT NULL");
+
+$blog_pages = db_fetch_all("SELECT * FROM `ia_blog_forum`");
+
+foreach ($blog_pages as $blog_page) {
+    $page = textblock_get_revision($blog_page["textblock"]);
+
+    $page["forum_topic"] = $blog_page["topic_id"];
+    $page["user_id"] = 1490;               // update as "bogdan2412" :)
+    $page["timestamp"] = null;
+
+    if (count(textblock_validate($page)) == 0) {
+        textblock_add_revision($page['name'], $page['title'], $page['text'],
+                               $page['user_id'], $page['security'],
+                               $page["forum_topic"], $page['timestamp'],
+                               $page['creation_timestamp']);
+    } else {
+        log_error('Eroare la procesarea ia_blog_forum!');
+    }
+}
+
+db_query("DROP TABLE `ia_blog_forum`");
+
+
+$task_pages = db_fetch_all("SELECT `page_name` FROM `ia_task`");
+
+foreach ($task_pages as $task_page) {
+    $page = textblock_get_revision($task_page["page_name"]);
+
+    $pattern = '/==\ *smftopic\(\ *topic_id="\ *([0-9]*).*0*\ *"\ *\)\ *==/i';
+    if (preg_match($pattern, $page['text'], $matches)) {
+        $topic_id = $matches[1];
+
+        $page["text"] = preg_replace($pattern, "", $page["text"]);
+        $page["forum_topic"] = $topic_id;
+        $page["user_id"] = 1490;               // update as "bogdan2412" :)
+        $page["timestamp"] = null;
+
+        if (count(textblock_validate($page)) == 0) {
+            textblock_add_revision($page['name'], $page['title'], $page['text'],
+                                   $page['user_id'], $page['security'],
+                                   $page["forum_topic"], $page['timestamp'],
+                                   $page['creation_timestamp']);
+        } else {
+            log_error('Eroare la procesarea paginii ' . $page["name"] . '!');
+        }
+    } else {
+        if (stristr($page["text"], 'SmfTopic')) {
+            log_warn('Eroare de regexp pentru "'.$task['id'].'"!');
+        }
+        log_warn($page["name"] . " nu are nici un topic asociat");
+    }
+}
+
+?>
Index: www/controllers/blog.php
===================================================================
--- www/controllers/blog.php	(revision 941)
+++ www/controllers/blog.php	(working copy)
@@ -32,48 +32,6 @@
     execute_view_die('views/rss.php', $view);
 }
 
-function controller_blog_admin($blog_post) {
-    if (!is_blog_post($blog_post)) {
-        flash_error('Numele paginii este invalid');
-        redirect(url_blog());
-    }
-    
-    $page = textblock_get_revision($blog_post);
-    if (!$page) {
-        flash_error("Pagina nu exista");
-        redirect(url_blog());
-    }
-
-    // Security check
-    identity_require('blog-admin');
-    
-    // Form stuff.
-    $values = array();
-    $errors = array();
-
-    // Fill in form values, and validate
-    $values['topic_id'] = request('topic_id', blog_get_forum_topic($blog_post));
-    if (!is_whole_number($values['topic_id'])) {
-        $errors['topic_id'] = 'Topic-ul trebuie sa fie un numar intreg!';
-    }
-
-    // Update database
-    if (request_is_post() && !$errors) {
-        blog_set_forum_topic($blog_post, $values['topic_id']);
-        flash("Am actualizat informatiile");
-        redirect(url_textblock($blog_post));
-    }
-
-    $view = array();
-    $view['title'] = 'Administrare '.$page['title'];
-    $view['page'] = $page;
-    $view['form_values'] = $values;
-    $view['form_errors'] = $errors;
-
-    execute_view_die("views/blog_admin.php", $view);
-
-}
-
 function controller_blog_index() {
     // Build view
     $view = array();
@@ -91,15 +49,14 @@
     $view['subpages'] = blog_get_range($view['tag'], $view['options']['first_entry'], $view['options']['display_entries']);
     $view['options']['total_entries'] = blog_count($view['tag']);
 
-
     // Get some extra info for blog posts
     foreach ($view['subpages'] as &$subpage) {
         $first_textblock = textblock_get_revision($subpage['name'], 1, true);
         $subpage['user_name'] = $first_textblock['user_name'];
         $subpage['user_fullname'] = $first_textblock['user_fullname'];
         $subpage['rating_cache'] = $first_textblock['rating_cache'];
-        $subpage['topic_id'] = blog_get_forum_topic($subpage['name']);
-        $subpage['comment_count'] = blog_get_comment_count($subpage['topic_id']);
+        $subpage['forum_topic'] = blog_get_forum_topic($subpage['name']);
+        $subpage['comment_count'] = blog_get_comment_count($subpage['forum_topic']);
         $subpage['tags'] = tag_get_names("textblock", $subpage['name']);
     }
     
@@ -129,7 +86,6 @@
         }
     } else {
         // Missing page.
-        // FIXME: what if the user can't create the page?
         flash_error("Nu exista pagina, dar poti sa o creezi ...");
         redirect(url_textblock_edit($page_name));
     }
@@ -139,12 +95,12 @@
     // Build view.
     $view = array();
     $view['topnav_select'] = 'blog';
-    $view['topic_id'] = blog_get_forum_topic($page['name']);
     $view['title'] = $page['title'];
     $view['revision'] = $rev_num;
     $view['revision_count'] = $rev_count;
     $view['page_name'] = $page['name'];
-    $view['textblock'] = $page;
+    $view['textblock'] = $page; 
+    $view['forum_topic'] = $page['forum_topic'];
     $view['tags'] = tag_get_names("textblock", $page['name']);
     $view['first_textblock'] = textblock_get_revision($page_name, 1, true);
     log_assert($view['textblock']['creation_timestamp'] == $view['first_textblock']['timestamp']);
Index: www/controllers/textblock.php
===================================================================
--- www/controllers/textblock.php	(revision 941)
+++ www/controllers/textblock.php	(working copy)
@@ -44,9 +44,9 @@
     $view['title'] = $page['title'];
     $view['revision'] = $rev_num;
     $view['revision_count'] = $rev_count;
-
     $view['page_name'] = $page['name'];
     $view['textblock'] = $page;
+    $view['forum_topic'] = $page['forum_topic'];
 
     execute_view_die('views/textblock_view.php', $view);
 }
@@ -132,7 +132,8 @@
 
     textblock_add_revision($rev['name'], $rev['title'], $rev['text'],
                            getattr($identity_user, 'id'), $rev['security'],
-                           null, $rev['creation_timestamp']);
+                           $rev['forum_topic'], null,
+                           $rev['creation_timestamp']);
     flash("Pagina a fost inlocuita cu revizia {$rev_num}");
     redirect(url_textblock($page_name));
 }
Index: www/controllers/textblock_edit.php
===================================================================
--- www/controllers/textblock_edit.php	(revision 941)
+++ www/controllers/textblock_edit.php	(working copy)
@@ -22,6 +22,7 @@
                 'text' => "Scrie aici despre " . $page_name,
                 'security' => $security,
                 'user_id' => identity_get_user_id(),
+                'forum_topic' => null,
         );
         identity_require('textblock-create', $page);
         $big_title = "Creare " . $page_name;
@@ -33,6 +34,7 @@
     $values['text'] = request('text', $page['text']);
     $values['title'] = request('title', $page['title']);
     $values['security'] = request('security', $page['security']);
+    $values['forum_topic'] = request('forum_topic', $page['forum_topic']);
     $values['tags'] = request('tags', tag_build_list("textblock", $page_name));
     $values['creation_timestamp'] = getattr($page, 'creation_timestamp');
     $values['timestamp'] = null;
@@ -43,9 +45,13 @@
         $new_page['text'] = $values['text'];
         $new_page['title'] = $values['title'];
         $new_page['security'] = $values['security'];
+        $new_page['forum_topic'] = $values['forum_topic'];
         $new_page['creation_timestamp'] = $values['creation_timestamp'];
         $new_page['timestamp'] = $values['timestamp'];
         $new_page['user_id'] = identity_get_user_id();
+        if ($new_page['forum_topic'] === "") {
+            $new_page['forum_topic'] = null;
+        }
 
         // Validate new page
         $errors = textblock_validate($new_page);
@@ -69,7 +75,9 @@
         if (!$errors) {
             textblock_add_revision($new_page['name'], $new_page['title'],
                                    $new_page['text'], $new_page['user_id'],
-                                   $new_page['security'], $new_page['timestamp'],
+                                   $new_page['security'],
+                                   $new_page['forum_topic'],
+                                   $new_page['timestamp'],
                                    $new_page['creation_timestamp']);
             if (identity_can('textblock-tag', $new_page)) {
                 tag_update("textblock", $new_page['name'], $values['tags']);
@@ -84,6 +92,9 @@
     if (!identity_can('textblock-change-security', $page)) {
         unset($values['security']);
     }
+    if (!identity_can('textblock-change-topic', $page)) {
+        unset($values['forum_topic']);
+    }
 
     // Create view.
     $view = array();
Index: www/index.php
===================================================================
--- www/index.php	(revision 941)
+++ www/index.php	(working copy)
@@ -69,13 +69,6 @@
     controller_account(getattr($pagepath, 1));
 }
 
-// Blog admin
-else if ($urlstart == 'admin' && getattr($pagepath, 1) == 'blog') {
-    $obj_id = implode("/", array_slice($pagepath, 1));
-    require_once(IA_ROOT_DIR.'www/controllers/blog.php');
-    controller_blog_admin($obj_id);
-}
-
 // Task creator
 else if ($page == 'admin/problema-noua') {
     require_once(IA_ROOT_DIR.'www/controllers/task.php');
Index: www/url.php
===================================================================
--- www/url.php	(revision 941)
+++ www/url.php	(working copy)
@@ -92,11 +92,6 @@
     return url_complex('blog');
 }
 
-function url_blog_admin($blog_post) {
-    log_assert(is_blog_post($blog_post));
-    return url_complex("admin/blog/$blog_post");
-}
-
 function url_textblock_diff($page_name, $revfrom, $revto) {
     return url_complex($page_name, array(
             'action' => 'diff',
Index: www/views/blog_admin.php
===================================================================
--- www/views/blog_admin.php	(revision 941)
+++ www/views/blog_admin.php	(working copy)
@@ -1,21 +0,0 @@
-<?php include('views/header.php'); ?>
-<h1>Administrare <?= format_link(url_textblock($page['name']), $page['title']) ?></h1>
-<form action="<?= html_escape(getattr($view, 'action')) ?>" method="post">
- <fieldset>
-  <legend>Informatii generale</legend>
-  <ul class="form">
-   <?= view_form_field_li(array('name' => 'Topic forum', 'default' => $form_values['topic_id'], 'type' => 'string'), 'topic_id') ?>
-  </ul>
- </fieldset>
- <div class="submit">
-  <ul class="form">
-   <li id="field_submit">
-    <input type="submit"
-           value="Salveaza"
-           id="form_submit"
-           class="button important" />
-   </li>
-  </ul>
- </div>
-</form>
-<?php include('footer.php'); ?>
Index: www/views/blog_view.php
===================================================================
--- www/views/blog_view.php	(revision 941)
+++ www/views/blog_view.php	(working copy)
@@ -34,8 +34,8 @@
 echo '</div>';
 // blog comments
 echo '<div id="comentarii">';
-if (getattr($view, 'topic_id')) {
-    echo macro_remotebox(array('url' => IA_SMF_URL.'/ia_comments.php?topic_id='.$view['topic_id']), true);
+if (getattr($view, 'forum_topic')) {
+    echo macro_remotebox(array('url' => IA_SMF_URL.'/ia_comments.php?topic_id='.$view['forum_topic']), true);
 }
 echo '</div></div>';
 echo '</div>';
Index: www/views/textblock_edit.php
===================================================================
--- www/views/textblock_edit.php	(revision 941)
+++ www/views/textblock_edit.php	(working copy)
@@ -35,6 +35,14 @@
         <?= format_link(url_textblock('documentatie/wiki'), "Cum formatez text?") ?>
     </li>
 
+    <?php if (array_key_exists('forum_topic', $form_values)) { ?>
+    <li id="field_forum_topic">
+        <label for="form_forum_topic">Forum Topic</label>
+        <input type="text" name="forum_topic" value="<?= fval('forum_topic') ?>" id="form_forum_topic" />
+        <?= ferr_span('forum_topic') ?>
+    </li>
+    <?php } ?>
+
     <?php if (identity_can('textblock-tag', $view['page'])) { ?>
        <?= tag_format_input_box(fval('tags')) ?>
     <?php } ?>
@@ -43,7 +51,7 @@
     <li id="field_security">
         <label for="form_security">Nivel de securitate al paginii
         <a href="<?= html_escape(url_textblock('documentatie/securitate')) ?>">(?)</a></label> 
-        <input type="text" name="security" value="<?= fval('security') ?>" id="form_security"/>
+        <input type="text" name="security" value="<?= fval('security') ?>" id="form_security" />
         <?= ferr_span('security') ?>
     </li>
     <?php } ?>
Index: www/views/textblock_view.php
===================================================================
--- www/views/textblock_view.php	(revision 941)
+++ www/views/textblock_view.php	(working copy)
@@ -18,6 +18,13 @@
 echo wiki_process_textblock($textblock);
 echo '</div>';
 
+if (getattr($view, "forum_topic")) {
+    require_once(IA_ROOT_DIR.'www/macros/macro_smftopic.php');
+    echo '<div id="forum_box">';
+    echo macro_smftopic(array('topic_id' => $forum_topic));
+    echo '</div>';
+}
+
 // site footer
 include('footer.php');
 
