/**
  * Routes requests and sets return data
  */
 public function __construct()
 {
     if (class_exists('mvc')) {
         mvc::$view = '#moadmin';
     }
     $this->mongo['dbs'] = self::$model->listDbs();
     if (isset($_GET['db'])) {
         if (strpos($_GET['db'], '.') !== false) {
             $_GET['db'] = $_GET['newdb'];
         }
         self::$model->setDb($_GET['db']);
     }
     if (isset($_POST['limit'])) {
         $_SESSION['limit'] = (int) $_POST['limit'];
     } else {
         if (!isset($_SESSION['limit'])) {
             $_SESSION['limit'] = OBJECT_LIMIT;
         }
     }
     if (isset($_FILES['import']) && is_uploaded_file($_FILES['import']['tmp_name']) && isset($_GET['collection'])) {
         $data = json_decode(file_get_contents($_FILES['import']['tmp_name']));
         self::$model->import($_GET['collection'], $data, $_POST['importmethod']);
     }
     $action = isset($_GET['action']) ? $_GET['action'] : 'listCollections';
     if (isset($_POST['object'])) {
         if (self::$model->saveObject($_GET['collection'], $_POST['object'])) {
             return $this->_dumpFormVals();
         } else {
             $action = 'editObject';
             $_POST['errors']['object'] = 'Error: object could not be saved - check your array syntax.';
         }
     } else {
         if ($action == 'createCollection') {
             self::$model->{$action}($_GET['collection']);
         } else {
             if ($action == 'renameCollection' && isset($_POST['collectionto']) && $_POST['collectionto'] != $_POST['collectionfrom']) {
                 self::$model->{$action}($_POST['collectionfrom'], $_POST['collectionto']);
                 $_GET['collection'] = $_POST['collectionto'];
                 $action = 'listRows';
             }
         }
     }
     if (isset($_GET['sort'])) {
         self::$model->sort = array($_GET['sort'] => $_GET['sortdir']);
     }
     $this->mongo['listCollections'] = self::$model->listCollections();
     if ($action == 'editObject') {
         $this->mongo[$action] = isset($_GET['_id']) ? self::$model->{$action}($_GET['collection'], $_GET['_id'], $_GET['idtype']) : '';
         return;
     } else {
         if ($action == 'removeObject') {
             self::$model->{$action}($_GET['collection'], $_GET['_id'], $_GET['idtype']);
             return $this->_dumpFormVals();
         } else {
             if ($action == 'ensureIndex') {
                 foreach ($_GET['index'] as $key => $field) {
                     $indexes[$field] = isset($_GET['isdescending'][$key]) && $_GET['isdescending'][$key] ? -1 : 1;
                 }
                 self::$model->{$action}($_GET['collection'], $indexes, $_GET['unique'] == 'Unique' ? array('unique' => true) : array());
                 $action = 'listCollections';
             } else {
                 if ($action == 'deleteIndex') {
                     self::$model->{$action}($_GET['collection'], unserialize($_GET['index']));
                     return $this->_dumpFormVals();
                 } else {
                     if ($action == 'getStats') {
                         $this->mongo[$action] = self::$model->{$action}();
                         unset($this->mongo['listCollections']);
                     } else {
                         if ($action == 'repairDb' || $action == 'getStats') {
                             $this->mongo[$action] = self::$model->{$action}();
                             $action = 'listCollections';
                         } else {
                             if ($action == 'dropDb') {
                                 self::$model->{$action}();
                                 load::redirect(get::url());
                                 return;
                             }
                         }
                     }
                 }
             }
         }
     }
     if (isset($_GET['collection']) && $action != 'listCollections' && method_exists(self::$model, $action)) {
         $this->mongo[$action] = self::$model->{$action}($_GET['collection']);
         $this->mongo['count'] = self::$model->count;
         $this->mongo['colKeys'] = self::$model->colKeys;
     }
     if ($action == 'listRows') {
         $this->mongo['listIndexes'] = self::$model->listIndexes($_GET['collection']);
     } else {
         if ($action == 'dropCollection') {
             return load::redirect(get::url() . '?db=' . urlencode($_GET['db']));
         }
     }
 }