public function generateTop($page)
 {
     $res = "<div id='header'>";
     $res .= "    <h1 id='header'>" . $this->config->get("application.title") . "</h1>";
     $res .= "</div>";
     $res .= "<div>";
     $res .= "    <ul id='menu'>";
     $pages_available = $this->config->get("pages.available");
     $res .= $this->generateMenuItem("status", "Status", 0, $page, $pages_available, $this->user);
     $res .= $this->generateMenuItem("sources", "Sources", 0, $page, $pages_available, $this->user);
     if ($this->isAvailableMenuItem($pages_available, "search", $this->config)) {
         $search_rooturl = $this->config->getDefault("search.rooturl", "");
         if ($core_url != "") {
             $key = base64_encode($core_url);
             $search_rooturl .= "?key=" . $key;
         }
         if ($search_rooturl != "") {
             $res .= "<li><a href='" . $search_rooturl . "' target='search'>Search</a></li>";
         }
     }
     if ($this->user->getLevel() == "2") {
         $res .= $this->generateMenuItem("manage", "Manage", 0, $page, $pages_available, $this->user);
         if ($this->isAvailableMenuItem($pages_available, "mongodb", $this->config)) {
             $mongodb_rooturl = $this->config->getDefault("mongodb.rooturl", "");
             if ($mongodb_rooturl != "") {
                 $res .= "<li><a href='" . $mongodb_rooturl . "' target='mongodb'>MongoDB</a></li>";
             }
         }
     } else {
         $res .= $this->generateMenuItem("manage", "My account", 0, $page, $pages_available, $this->user);
     }
     $res .= $this->generateMenuItem("logout", "Logout", 0, $page, $pages_available, $this->user);
     $res .= "    </ul>";
     $res .= "</div>";
     $res .= "<div id='account_selector'><div>";
     $res .= "<form id='account_selector' name ='account_selector' method='POST' action='index.php?page=status'>";
     $res .= "    <b>Logged in as </b> " . $this->user->getName() . " (" . getUserLevelLabel($this->user->getLevel()) . ")";
     if ($this->user->getLevel() == "2" && mg_row_count($this->mg, "accounts", "") > 1) {
         $res .= "&nbsp;-&nbsp;<b>Account </b> ";
         $aAccounts = getAvailableAccounts($this->config);
         if ($aAccounts != null) {
             $res .= "<select id='id_account' name='id_account' style='editInputSelect' onChange='accountOnChange();'>";
             foreach ($aAccounts as $key => $value) {
                 $res .= "<option value='" . $key . "'";
                 if ($key == $this->id_account_current) {
                     $res .= " selected";
                 }
                 $res .= ">" . $value . "</option>";
             }
             $res .= "</select>";
         }
     }
     $res .= "</form>";
     $res .= "</div>";
     $res .= "</div>";
     return $res;
 }
        $res .= mg_row_count($mg, "sources", array('$and' => array(array("deleted" => "0"), array("id_account" => intval($id_account_current)))));
        $res .= "</td></tr>";
        // TODO : pages collection do not exists anymore
        //$res .= "<tr><td class='head'>Number of crawled URLs</td><td>";
        //$res .= mg_row_count($mg, "pages", array("id_account" => $id_account_current));
        //$res .= "</td></tr>";
        $res .= "<tr><td class='head'>Number of enabled sources</td><td>";
        $res .= mg_row_count($mg, "sources", array('$and' => array(array("enabled" => "1"), array("deleted" => "0"), array("id_account" => intval($id_account_current)))));
        $res .= "</td></tr>";
        $res .= "<tr><td class='head'>Number of sources to be crawled</td><td>";
        // TODO: V4 - $stmt->addWhereClause("enabled = 1 and deleted = 0 and (crawl_nexttime is null or crawl_nexttime <= now()) and id_account = " . $id_account_current);
        $res .= mg_row_count($mg, "sources", array('$and' => array(array("enabled" => "1"), array("deleted" => "0"), array("crawl_nexttime" => array('$lte' => new MongoDate())), array("id_account" => intval($id_account_current)))));
        $res .= "</td></tr>";
        $res .= "<tr><td class='head'>Number of sources to be crawled for the first time</td><td>";
        // TODO: V4 - $stmt->addWhereClause("enabled = 1 and deleted = 0 and crawl_nexttime is null and id_account = " . $id_account_current);
        $res .= mg_row_count($mg, "sources", array('$and' => array(array("enabled" => "1"), array("deleted" => "0"), array("crawl_firstcompleted" => "0"), array("id_account" => intval($id_account_current)))));
        $res .= "</td></tr>";
    }
    $res .= "</table></center>";
    print $res;
    exit;
}
if ($action == "showrunning") {
    $mg = mg_connect($config, "", "", "");
    if ($mg) {
        $stmt = new mg_stmt_select($mg, "sources");
        //$query_status = array('$or' => array(array(crawl_process_status => "1"), array(crawl_process_status => "2"), array(crawl_process_status => "3"), array(crawl_process_status => "4")));
        //$query = array('$and' => array(array(enabled => "1"), array(deleted => "0"), array(id_account => $id_account_current), array(id_account => $id_account_current), $query_status));
        //TODO: V4
        /*
         $stmt->setWhereClause("
         $mode = "update";
     }
 }
 if ($match == 'name') {
     $query = array('$and' => array($query, array("name" => (string) $item->name)));
     $count = mg_row_count($mg, "sources", $query);
     if ($count > 1) {
         continue;
     }
     if ($count == 1) {
         $mode = "update";
     }
 }
 if ($match == 'host') {
     $query = array('$and' => array($query, array("url_host" => (string) $item->url_host)));
     $count = mg_row_count($mg, "sources", $query);
     if ($count > 1) {
         continue;
     }
     if ($count == 1) {
         $mode = "update";
     }
 }
 if ($mode == 'insert') {
     $stmt = new mg_stmt_insert($mg, "sources", $mg_source_defaults);
     $stmt->addColumnValue("id_account", intval($id_account_current));
     $stmt->addColumnValueDate("createtime");
     $enabled = $status;
 } else {
     if ($strategy == 'skip') {
         continue;
            }
            $res .= "</select>";
            $res .= "</td>";
            $res .= "</tr>";
        }
        $res .= "</table></center>";
        $res .= "<br/>";
        $res .= "<input type='hidden' id='account_id' name ='account_id' value='" . $rs["id"] . "'>";
        $res .= "<input type='hidden' id='action' name ='action' value='saveaccount'>";
        $res .= "<div class='menu_button_on_right'><span id='account_save_result'></span>";
        $res .= "<a href='#' onClick='cancelAccount();return false;'><img src='images/button_cancel_32.png'></a>&nbsp;&nbsp;";
        $res .= "<a href='#' onClick='saveAccount();return false;'><img src='images/button_ok_32.png'></a>&nbsp;&nbsp;";
        if ($rs["id"] != 1) {
            $count_user = mg_row_count($mg, "users", array("id_account" => intval($rs["id"])));
            $count_target = mg_row_count($mg, "targets", array("id_account" => intval($rs["id"])));
            $count_source = mg_row_count($mg, "sources", array("id_account" => intval($rs["id"])));
            if ($count_user == 0 && $count_target == 0 && $count_source == 0) {
                $res .= "<a href='#' onClick='deleteAccount();return false;'><img src='images/trash_32.png'></a>&nbsp;&nbsp;";
            }
        }
        $res .= "</div></form>";
    }
    print $res;
    exit;
}
if ($action == "display_add_account") {
    $res = "<br /><br /><br />";
    $res .= "<form name='account_add' id='account_add'>";
    $res .= "<center><table border='0' cellspacing='0' cellpadding='0'>";
    $res .= "<tbody>";
    $res .= "<tr>";
            $res .= "<td>" . $rs["name"] . "</td>";
        } else {
            $res .= "<td><input class='editInputText' type='text' name='engine_name' id='engine_name' value='" . $rs["name"] . "'></td>";
        }
        $res .= "</tr>";
        $res .= "</table></center>";
        $res .= "<br/>";
        $res .= "<input type='hidden' id='engine_id' name ='engine_id' value='" . $rs["id"] . "'>";
        $res .= "<input type='hidden' id='action' name ='action' value='saveengine'>";
        $res .= "<div class='menu_button_on_right'><span id='engine_save_result'></span>";
        $res .= "<a href='#' onClick='cancelEngine();return false;'><img src='images/button_cancel_32.png'></a>&nbsp;&nbsp;";
        if ($id != "1") {
            $res .= "<a href='#' onClick='saveEngine();return false;'><img src='images/button_ok_32.png'></a>&nbsp;&nbsp;";
        }
        if ($rs["id"] != 1) {
            $count_account = mg_row_count($mg, "accounts", array("id_engine" => intval($rs["id"])));
            if ($count_account == 0) {
                $res .= "<a href='#' onClick='deleteEngine();return false;'><img src='images/trash_32.png'></a>&nbsp;&nbsp;";
            }
        }
        $res .= "</div></form>";
    }
    print $res;
    exit;
}
if ($action == "display_add_engine") {
    $res = "<br /><br /><br />";
    $res .= "<form name='engine_add' id='engine_add'>";
    $res .= "<center><table border='0' cellspacing='0' cellpadding='0'>";
    $res .= "<tbody>";
    $res .= "<tr>";