From c5657c542c05e4d03a7f9c3ddfefeb98865f946e Mon Sep 17 00:00:00 2001
From: dolar4ik <dolar4ik@gmail.com>
Date: Mon, 14 Mar 2011 09:39:45 +0200
Subject: [PATCH] Issue #902564 by dolarchik: Fixed 'Recoverable fatal error'.

---
 tableofcontents.admin.inc                          |   39 +++-
 tableofcontents.info                               |    2 +-
 tableofcontents.install                            |    2 -
 tableofcontents.js                                 |   12 +-
 tableofcontents.module                             |  214 ++++++++++----------
 tableofcontents.pages.inc                          |   42 +++--
 tableofcontents_block/tableofcontents_block.info   |    2 +-
 tableofcontents_block/tableofcontents_block.module |   93 +++++----
 8 files changed, 224 insertions(+), 182 deletions(-)

diff --git a/tableofcontents.admin.inc b/tableofcontents.admin.inc
index b3dc649..56c00eb 100644
--- a/tableofcontents.admin.inc
+++ b/tableofcontents.admin.inc
@@ -10,10 +10,11 @@
 /**
  * Return the form used for the filter settings.
  *
- * @param $format
- *   The currently selected input format.
+ * @param $format_array
+ *   The currently selected input $format_array.
  */
-function _tableofcontents_settings($format) {
+function _tableofcontents_settings($format_array) {
+  $format = ($format_array == 'block')?$format_array:$format_array['#format']->format;
   $form['tableofcontents'] = array(
     '#type' => 'fieldset',
     '#title' => t('Table of Contents'),
@@ -385,6 +386,19 @@ function _tableofcontents_settings($format) {
 }
 
 /**
+ * Saves the filter settings.
+ */
+function _tableofcontents_settings_submit($form, &$form_state) {
+  $format = $form_state['values']['format'];
+  $filter_toc_settings = $form_state['values']['filters']['filter_toc']['settings']['tableofcontents'];
+  foreach($filter_toc_settings as $fieldset) {
+    foreach($fieldset as $filter_name => $value) {
+      variable_set($filter_name, $value);
+    }
+  }
+}
+
+/**
  * This function makes sure that the table of content is hidden in the teaser
  * if so requested by the user.
  *
@@ -392,44 +406,44 @@ function _tableofcontents_settings($format) {
  */
 function _tableofcontents_hide_in_teaser(&$node) {
   // node got a teaser?
-  if (!$node->teaser) {
+  if (!$node->body[$node->language][0]['summary']) {
     return;
   }
 
   // node uses our filter?
-  $filters = filter_list_format($node->format);
+  $filters = filter_list_format($node->body[$node->language][0]['format']);
   if (!isset($filters['tableofcontents/0'])) {
     return;
   }
 
   // any [toc ...] tag in the teaser?
   $new_teaser = preg_replace('%(<!--\s*tableofcontents([^>]*)-->|\[\[TOC.*?\]\]|(<(div|p)(\s+[^>]*)?>\s*)?\[toc.*?\](\s*</(div|p)>)?)%', '', $node->teaser);
-  if ($node->teaser == $new_teaser) {
+  if ($node->body[$node->language][0]['summary'] == $new_teaser) {
     // okay, no [toc ...], but maybe we need to hide the table of content?
     if (!variable_get('tableofcontents_automatic_' . $node->format, 0)) {
       return;
     }
   }
 
-  if (strpos($node->body, '<!--break-->') !== FALSE) {
+  if (strpos($node->body[$node->language][0]['value'], '<!--break-->') !== FALSE) {
     // We've specified the split, but the teaser is shown in full
     // view. So, we now have to convert it so that the table of
     // content is hidden in the teaser.
-    $body = '<!--break-->' . str_replace('<!--break-->', '', $node->body);
-    if ($body != $node->body) {
-      $node->body = $body;
+    $body = '<!--break-->' . str_replace('<!--break-->', '', $node->body[$node->language][0]['value']);
+    if ($body != $node->body[$node->language][0]['value']) {
+      $node->body[$node->language][0]['value'] = $body;
       drupal_set_message(t("The content you saved contains a table of contents. A separate summary (teaser) has been automatically created without the table of contents at the top. If you make any further changes, be sure to check the summary field to see if your changes apply there as well."));
     }
   }
   else {
     // This is the case where no teaser was specified by the user.
     // We add an explicit split teaser.
-    $node->body = '<!--break-->' . $node->body;
+    $node->body[$node->language][0]['value'] = '<!--break-->' . $node->body[$node->language][0]['value'];
     drupal_set_message(t("The summary (teaser) was automatically split from the body as site settings does not allow table of contents in summaries. If you make any further changes, be sure to check the summary field to see if your changes apply there as well."));
   }
 
   // Remove toc from teasers.
-  $node->teaser = $new_teaser . '<div>[toc hidden:1]</div>';
+  $node->body[$node->language][0]['summary'] = $new_teaser . '<div>[toc hidden:1]</div>';
 }
 
 /**
@@ -475,6 +489,7 @@ function _tableofcontents_nodetype_form_alter(&$form) {
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
     '#description' => t('Defines whether a table of content is automatically created on those pages and whether the node_view() function is used along with the filter.'),
+    '#group' => 'additional_settings',
   );
 
   $options = array(
diff --git a/tableofcontents.info b/tableofcontents.info
index abe214a..b470878 100644
--- a/tableofcontents.info
+++ b/tableofcontents.info
@@ -1,4 +1,4 @@
 name = Table of Contents
 description = "Adds a filter that generates Tables of contents for pages with  '[toc ...]' tags or that have a predetermined number of headers."
 package = Input filters
-core = 6.x
+core = 7.x
diff --git a/tableofcontents.install b/tableofcontents.install
index ac2e7cb..2c2afd5 100644
--- a/tableofcontents.install
+++ b/tableofcontents.install
@@ -35,14 +35,12 @@ function tableofcontents_schema() {
  * Implementation of hook_install()
  */
 function tableofcontents_install() {
-  drupal_install_schema('tableofcontents');
 }
 
 /**
  * Implementation of hook_uninstall()
  */
 function tableofcontents_uninstall() {
-  drupal_uninstall_schema('tableofcontents');
   db_query("DELETE FROM {variable} WHERE name LIKE 'tableofcontents_%'");
 }
 
diff --git a/tableofcontents.js b/tableofcontents.js
index ba49e1f..ce92b46 100644
--- a/tableofcontents.js
+++ b/tableofcontents.js
@@ -1,8 +1,10 @@
+// $Id$
 
-if (Drupal.jsEnabled) {
-  toc_collapse = 0;
-  toc_scroll_back_to_top = 0;
-  $(document).ready( function () {
+toc_collapse = 0;
+toc_scroll_back_to_top = 0;
+
+(function ($) {
+  $(document).ready(function(){
     // setup table
     if (toc_collapse) {
       $('.toc-list').hide();
@@ -31,4 +33,4 @@ if (Drupal.jsEnabled) {
       $('.toc-list, .toc-back-to-top').localScroll({ hash: true });
     }
   });
-}
+ })(jQuery);
diff --git a/tableofcontents.module b/tableofcontents.module
index 4af19ea..8eccedb 100644
--- a/tableofcontents.module
+++ b/tableofcontents.module
@@ -85,57 +85,31 @@ function tableofcontents_filter_tips($delta, $format, $long = FALSE) {
 }
 
 /**
- * Implementation of hook_filter().
+ * Implementation of hook_filter_info().
  */
-function tableofcontents_filter($op, $delta = 0, $format = -1, $text = '') {
-  global $_tableofcontents_block_processing;
-
-  if ($op == 'list') {
-    return array(
-      0 => t('Table of contents'),
-      1 => t('Assign an ID to each header'),
+function tableofcontents_filter_info() {
+  module_load_include('admin.inc', 'tableofcontents');
+  $filters['filter_toc'] = array(
+    'title' => t('Table of contents'),
+    'description' => t('Inserts a table of contents in place of [toc ...] tags.'),
+    'process callback' => '_tableofcontents_process',
+    'prepare callback' => '_tableofcontents_prepare',
+    'settings callback' => '_tableofcontents_settings',
+    'cache' => TRUE,
+    'tips callback' => 'tableofcontents_filter_tips',
     );
+  $filters['filter_toc_id'] = array(
+    'title' => t('Assign an ID to each anchors'),
+    'description' => t('Add an ID to all the anchors on the page. May be necessary in case the table of contents block is used.'),
+    'process callback' => '_tableofcontents_process',
+    'prepare callback' => '_tableofcontents_prepare',
+    'settings callback' => '_tableofcontents_settings',
+    'cache' => TRUE,
+    'tips callback' => 'tableofcontents_filter_tips',
+  );
+  return $filters;
   }
 
-  switch ($op) {
-  case 'description':
-    switch ($delta) {
-    default:
-      return t('Inserts a table of contents in place of [toc ...] tags.');
-
-    case 1:
-      return t('Add an ID to all the anchors on the page. May be necessary in case the table of contents block is used.');
-
-    }
-    /*NOTREACHED*/
-    break;
-
-  case 'no cache': // allow caching
-    return FALSE;
-
-  case 'settings':
-    module_load_include('admin.inc', 'tableofcontents');
-    return _tableofcontents_settings($format);
-
-  case 'prepare':
-    if ($delta != 1) {
-      module_load_include('pages.inc', 'tableofcontents');
-      return _tableofcontents_prepare($delta, $format, $text);
-    }
-    return $text;
-
-  case 'process':
-    // NOTE: we cannot test for a [toc:...] and skip if none
-    //       because the user could have the auto-toc turned on.
-    if (!$_tableofcontents_block_processing) {
-      module_load_include('pages.inc', 'tableofcontents');
-      return _tableofcontents_process($delta, $format, $text);
-    }
-    return $text;
-
-  }
-}
-
 /**
  * Save the extra data the TOC adds to nodes.
  */
@@ -144,11 +118,21 @@ function _tableofcontents_save($node) {
   if (!isset($node->tableofcontents_toc_automatic)) {
     $node->tableofcontents_toc_automatic = 0;
   }
-  $sql = "UPDATE {tableofcontents_node_toc} SET toc_automatic = %d WHERE nid = %d";
-  db_query($sql, $node->tableofcontents_toc_automatic, $node->nid);
-  if (db_affected_rows() == 0) {
-    $sql = "INSERT INTO {tableofcontents_node_toc} (nid, toc_automatic) VALUES (%d, %d)";
-    db_query($sql, $node->nid, $node->tableofcontents_toc_automatic);
+
+  $num_updated = db_update('tableofcontents_node_toc') // Table name no longer needs {}
+  ->fields(array(
+    'toc_automatic' => $node->tableofcontents_toc_automatic,
+  ))
+  ->condition('nid', $node->nid)
+  ->execute();
+
+  if ($num_updated == 0) {
+    db_insert('tableofcontents_node_toc') // Table name no longer needs {}
+      ->fields(array(
+        'nid' => $node->nid,
+        'toc_automatic' => $node->tableofcontents_toc_automatic,
+      ))
+      ->execute();
   }
 }
 
@@ -156,10 +140,13 @@ function _tableofcontents_save($node) {
  * Load the extra data for the node from the TOC table.
  */
 function _tableofcontents_load(&$node) {
-  $sql = "SELECT toc_automatic FROM {tableofcontents_node_toc} WHERE nid = %d";
-  $result = db_query($sql, $node->nid);
-  if ($toc = db_fetch_object($result)) {
-    $node->tableofcontents_toc_automatic = $toc->toc_automatic;
+  $result = db_select('tableofcontents_node_toc', 'toc')
+    ->fields('toc', array('toc_automatic'))
+    ->condition('nid', $node->nid)
+    ->execute()
+    ->fetch();
+  if (!empty($result) && $toc = $result->toc_automatic) {
+    $node->tableofcontents_toc_automatic = $toc;
   }
   else {
     $node->tableofcontents_toc_automatic = 0;
@@ -167,67 +154,82 @@ function _tableofcontents_load(&$node) {
 }
 
 /**
- * Implementation of hook_nodeapi
- *
- * We need to clear the cache to cover the case where file attachments have changed, but
- * the body hasn't. This might be a little aggressive, in that we clear the cache for any node
- * with attachments, but since this only occurs during editing or creating the load should be
- * pretty minimal. It also only happens if the node has file attachments.
+ * Implementation of hook_node_prepare
  */
-function tableofcontents_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
-  global $user, $theme_key, $_tableofcontents_block_processing;
-
-  switch ($op) {
-  case 'prepare':
+function tableofcontents_node_prepare($node) {
     if (isset($node->files)) {
       // Remove the cached version if there are attachments on this node
       $cid = $node->format . ':' . md5($node->body);
       cache_clear_all($cid, 'cache_filter');
     }
-    break;
+}
 
-  case 'presave':
-    if (variable_get('tableofcontents_remove_teaser_' . $node->format, TRUE)) {
+/**
+ * Implementation of hook_node_presave
+ */
+function tableofcontents_node_presave($node) {
+  if (variable_get('tableofcontents_remove_teaser_' . $node->body[$node->language][0]['format'], TRUE)) {
       module_load_include('admin.inc', 'tableofcontents');
       _tableofcontents_hide_in_teaser($node);
     }
-    break;
+}
 
-  case 'insert':
-  case 'update':
+/**
+ * Implementation of hook_node_insert
+ */
+function tableofcontents_node_insert($node) {
     _tableofcontents_save($node);
-    break;
+}
 
-  case 'load':
+/**
+ * Implementation of hook_node_update
+ */
+function tableofcontents_node_update($node) {
+  _tableofcontents_save($node);
+}
+
+/**
+ * Implementation of hook_node_load
+ */
+function tableofcontents_node_load($nodes) {
+  foreach ($nodes as $node) {
     _tableofcontents_load($node);
-    break;
+  }
+}
+
+/**
+ * Implementation of hook_node_view
+ */
+function tableofcontents_node_view($node, $view_mode) {
+  global $user, $theme_key, $_tableofcontents_block_processing;
 
-  case 'view':
-    // $a3 represents the $teaser flag
-    // $a4 represents the $page flag
     if (!$_tableofcontents_block_processing) {
       $processed = FALSE;
       if (variable_get('tableofcontents_nodetype_toc_vtoc_' . $node->type, FALSE)) {
         // ugly test to make sure we don't double the TOC (i.e. if automatic is turned
         // on we would apply the TOC twice when [toc] was used and the filter includes
         // the TOC!)
-        if (strpos($node->content['body']['#value'], 'class="toc"') === FALSE) {
-          $node->content['body']['#value'] = str_replace('[vtoc', '[toc', $node->content['body']['#value']);
-          if (!$a4 || ($a3 && variable_get('tableofcontents_nodetype_toc_remove_from_teaser_' . $node->type, TRUE))) {
+      if (strpos($node->content['body'][0]['#markup'], 'class="toc"') === FALSE) {
+        $node->content['body'][0]['#markup'] = str_replace('[vtoc', '[toc', $node->content['body'][0]['#markup']);
+        if ($view_mode == 'teaser' && variable_get('tableofcontents_nodetype_toc_remove_from_teaser_' . $node->type, TRUE)) {
             // remove from teaser or "non-page"
-            $node->content['body']['#value'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $node->content['body']['#value']);
+          $node->content['body'][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $node->content['body'][0]['#markup']);
+
           }
           else {
             // TODO: if $a3 is true, then we should process the body and save the resulting table in the teaser
             //       (in other words, make sure we get the complete table of content instead of only the teaser part!)
             module_load_include('pages.inc', 'tableofcontents');
-            $text = _tableofcontents_process(0, $node->format, $node->content['body']['#value']);
-            if ($node->content['body']['#value'] != $text) {
+          $format = new stdClass();
+          $format->format = $node->content['body']['#items'][0]['format'];
+          $text = _tableofcontents_process($node->content['body'][0]['#markup'], 0, $format);
+          if ($node->content['body'][0]['#markup'] != $text) {
               // if tableofcontents_hide_table_<format> is TRUE, then this is wrong...
-              if (!variable_get('tableofcontents_hide_table_' . $node->format, FALSE)) {
+            if (!variable_get('tableofcontents_hide_table_' . $node->content['body']['#items'][0]['format'], FALSE)) {
                 $processed = TRUE;
               }
-              $node->content['body']['#value'] = $text;
+            $node->content['body'][0]['#markup'] = $text;
+
             }
           }
         }
@@ -235,24 +237,29 @@ function tableofcontents_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
       if (!$processed) {
         // is the table of contents block visible?
         $rids = array_keys($user->roles);
-        $sql = "SELECT b.bid FROM {blocks} b"
-          . " LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta"
-          . " WHERE b.delta = '0' AND b.module = 'tableofcontents_block' AND b.theme = '%s'"
-            . " AND b.status = 1 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL)";
-        $sql = db_rewrite_sql($sql, 'b', 'bid');
-        $result = db_query($sql, array_merge(array($theme_key), $rids));
-        if (db_fetch_array($result)) {
+
+      $query = db_select('block', 'b')
+            ->fields('b', array('bid'));
+      $query->leftJoin('block_role', 'r', 'b.module = r.module AND b.delta = r.delta', array());
+      $query->condition('b.delta', 'tableofcontents_block')
+            ->condition('b.module', 'tableofcontents_block')
+            ->condition('b.theme', array_merge(array($theme_key), $rids))
+            ->condition('b.status', '1')
+            ->condition(db_or()->condition('r.rid', $rids, 'IN')->isNull('r.rid'));
+      $result = $query->execute()
+                  ->fetchAll();
+
+      if ($result) {
           // there is a table of contents block, but the node was node parsed...
           // do that now
           module_load_include('pages.inc', 'tableofcontents');
-          $node->content['body']['#value'] = _tableofcontents_process(1, $node->format, $node->content['body']['#value']);
+        $format = new stdClass();
+        $format->format = $node->content['body']['#items'][0]['format'];
+        $node->content['body'][0]['#markup'] = _tableofcontents_process($node->content['body'][0]['#markup'], 1, $format);
         }
       }
     }
-    break;
-
   }
-}
 
 /**
  * Add a field in nodes so one can mark the node as using a TOC without
@@ -264,10 +271,13 @@ function tableofcontents_form_alter(&$form, $form_state, $form_id) {
       module_load_include('admin.inc', 'tableofcontents');
       _tableofcontents_node_form_alter($form);
     }
-    elseif ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
+    elseif ($form_id == 'node_type_form') {
       module_load_include('admin.inc', 'tableofcontents');
       _tableofcontents_nodetype_form_alter($form);
     }
+    elseif ($form_id == 'filter_admin_format_form') {
+      $form['#submit'][] = '_tableofcontents_settings_submit';
+    }
   }
 }
 
@@ -280,19 +290,19 @@ function tableofcontents_form_alter(&$form, $form_state, $form_id) {
 function tableofcontents_theme() {
   return array(
     'tableofcontents_toc' => array(
-      'arguments' => array(
+      'variables' => array(
         'toc' => NULL,
       ),
       'file' => 'tableofcontents.pages.inc',
     ),
     'tableofcontents_back_to_top' => array(
-      'arguments' => array(
+      'variables' => array(
         'toc' => NULL,
       ),
       'file' => 'tableofcontents.pages.inc',
     ),
     'tableofcontents_number' => array(
-      'arguments' => array(
+      'variables' => array(
         'toc' => NULL,
       ),
       'file' => 'tableofcontents.pages.inc',
diff --git a/tableofcontents.pages.inc b/tableofcontents.pages.inc
index a7d7a93..f8b3b5a 100644
--- a/tableofcontents.pages.inc
+++ b/tableofcontents.pages.inc
@@ -151,7 +151,7 @@ function _tableofcontents_replace_headers($matches) {
       // 5. remove any incompatible character
       // 6. remove digits at the start of the name (we could also add a letter?)
       //
-      // sanitize accents by luron & deviantintegral (e.g. é=>e)
+      // sanitize accents by luron & deviantintegral (e.g. ï¿½=>e)
       // thanks to pathauto module for i18n-ascii.txt file
       if (!isset($toc_translations)) {
         $path = drupal_get_path('module', 'tableofcontents');
@@ -222,7 +222,7 @@ function _tableofcontents_replace_headers($matches) {
     }
   }
 
-  $h->number = theme('tableofcontents_number', $_tableofcontents_toc);
+  $h->number = theme('tableofcontents_number', array('toc' => $_tableofcontents_toc));
   if ($_tableofcontents_toc->number_headers) {
     $number = $h->number;
   }
@@ -302,7 +302,7 @@ function _tableofcontents_headers(&$toc, $format, $text) {
     $toc->back_to_top_link = '';
   }
   else {
-    $toc->back_to_top_link = theme('tableofcontents_back_to_top', $toc);
+    $toc->back_to_top_link = theme('tableofcontents_back_to_top', array('toc' => $toc));
   }
 
   // note that the pattern below assumes that the headers
@@ -504,7 +504,7 @@ function _tableofcontents_replace_toc_tags($matches) {
   global $_tableofcontents_toc;
 
   // we do not want options in this [toc ...] tag to affect the following [toc ...] tags
-  $save_toc = drupal_clone($_tableofcontents_toc);
+  $save_toc = clone($_tableofcontents_toc);
 
   // we always want to check the options because hidden:1 is always accepted!
   $options = isset($matches[1]) ? $matches[1] : '';
@@ -532,7 +532,7 @@ function _tableofcontents_replace_toc_tags($matches) {
     $_tableofcontents_toc->files = $node->files;
   }
 
-  $result = theme('tableofcontents_toc', $_tableofcontents_toc);
+  $result = theme('tableofcontents_toc', array('toc' => $_tableofcontents_toc));
 
   $_tableofcontents_toc = $save_toc;
 
@@ -657,15 +657,17 @@ function _tableofcontents_comments(&$toc, $format) {
  *
  * @return The $text with the [toc ...] replaced.
  */
-function _tableofcontents_replace_toc($delta, $format, $text) {
+function _tableofcontents_replace_toc($text, $filter = 0, $format_object) {
   // we just don't have a way to pass that around our callbacks
   // (PHP is lacking on that one) but it is still a lot faster
   // that way.
   global $_tableofcontents_toc;
   global $_tableofcontents_block_toc;
 
+  $format = $format_object->format;
+
   // this is a special case... do what it takes at once
-  if ($delta == 1) {
+  if (!is_object($filter) && $filter == 1) {
     $text = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $text);
     // note: $_tableofcontents_toc->automatic is only used in this very function and
     //       thus we do not need to define it for _tableofcontents_headers().
@@ -865,7 +867,7 @@ function _tableofcontents_replace_toc($delta, $format, $text) {
  * @param[in] $format The format number
  * @param[in] $text The text to filter
  */
-function _tableofcontents_prepare($delta, $format, $text) {
+function _tableofcontents_prepare($text, $filter, $format) {
   // support for PEAR wiki (http://drupal.org/node/150067#comment-937519)
   $text = str_replace('[[TOC]]', '[toc]', $text);
   return preg_replace_callback(
@@ -888,8 +890,8 @@ function _tableofcontents_prepare($delta, $format, $text) {
  * @param[in] $format The format number
  * @param[in] $text The text to filter
  */
-function _tableofcontents_process($delta, $format, $text) {
-  return _tableofcontents_replace_toc($delta, $format, $text);
+function _tableofcontents_process($text, $filter, $format) {
+  return _tableofcontents_replace_toc($text, $filter, $format);
 }
 
 /**
@@ -902,7 +904,8 @@ function _tableofcontents_process($delta, $format, $text) {
  * @return
  *   Rendered HTML to be displayed.
  */
-function theme_tableofcontents_toc($toc) {
+function theme_tableofcontents_toc($variables) {
+  $toc = $variables['toc'];
   // TODO: at this time, the hideshow works with all TOCs at once
   if ($toc->hideshow && !isset($toc->on_print_pages)) {
     // We add the link with JavaScript so if someone does not have JavaScript, they do
@@ -929,6 +932,7 @@ function theme_tableofcontents_toc($toc) {
   elseif ($hs) {
     $output .= $hs;
   }
+
   $list = $toc->numbering == 4 ? 'ol' : 'ul';
   $output .= "<div class=\"toc-list\">\n"
       . "<" .  $list . ">\n";
@@ -1038,10 +1042,10 @@ function theme_tableofcontents_toc($toc) {
  * http://snipplr.com/view/6314/roman-numerals/
  * http://reusablecode.blogspot.com/search/label/roman%20numerals
  */
- 
+
 // Convert Arabic numerals into Roman numerals.
 function _tableofcontents_roman($arabic) {
-  //static $fractions = Array("", "¿", "¿¿", "¿¿¿", "¿¿¿¿", "¿¿¿¿¿", "S", "S¿", "S¿¿", "S¿¿¿", "S¿¿¿¿", "S¿¿¿¿¿", "I");
+  //static $fractions = Array("", "ï¿½", "ï¿½ï¿½", "ï¿½ï¿½ï¿½", "ï¿½ï¿½ï¿½ï¿½", "ï¿½ï¿½ï¿½ï¿½ï¿½", "S", "Sï¿½", "Sï¿½ï¿½", "Sï¿½ï¿½ï¿½", "Sï¿½ï¿½ï¿½ï¿½", "Sï¿½ï¿½ï¿½ï¿½ï¿½", "I");
   static $ones = Array("", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX");
   static $tens = Array("", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC");
   static $hundreds = Array("", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM");
@@ -1087,7 +1091,7 @@ function _tableofcontents_roman($arabic) {
     return $roman;
   }
 }
- 
+
 // Expand subtractive notation in Roman numerals.
 //function _tableofcontents_roman_expand($roman)
 //{
@@ -1099,7 +1103,7 @@ function _tableofcontents_roman($arabic) {
 //  $roman = str_replace("IV", "IIII", $roman);
 //  return $roman;
 //}
- 
+
 // Compress Roman numerals using subtractive notation.
 //function _tableofcontents_roman_compress($roman)
 //{
@@ -1111,7 +1115,7 @@ function _tableofcontents_roman($arabic) {
 //  $roman = str_replace("IIII", "IV", $roman);
 //  return $roman;
 //}
- 
+
 // Convert Roman numerals into Arabic numerals.
 //function _tableofcontents_arabic($roman)
 //{
@@ -1191,7 +1195,8 @@ function _tableofcontents_convert_number($mode, $number) {
  * @return
  *   Rendered HTML to be displayed.
  */
-function theme_tableofcontents_back_to_top($toc) {
+function theme_tableofcontents_back_to_top($variables) {
+  $toc = $variables['toc'];
   return '<div class="toc-back-to-top"><a href="#' . $toc->back_to_top_anchor . '">'
     . $toc->back_to_top . '</a></div>';
 }
@@ -1205,7 +1210,8 @@ function theme_tableofcontents_back_to_top($toc) {
  * @return
  *   Rendered HTML to be displayed.
  */
-function theme_tableofcontents_number($toc) {
+function theme_tableofcontents_number($variables) {
+  $toc = $variables['toc'];
   $result = '';
   switch ($toc->numbering) {
   case 0: // no numbering
diff --git a/tableofcontents_block/tableofcontents_block.info b/tableofcontents_block/tableofcontents_block.info
index 8e15f0b..16fd882 100644
--- a/tableofcontents_block/tableofcontents_block.info
+++ b/tableofcontents_block/tableofcontents_block.info
@@ -2,4 +2,4 @@ name = Table of Contents Block
 description = "Display the table of contents in a block."
 package = Input filters
 dependencies[] = tableofcontents
-core = 6.x
+core = 7.x
diff --git a/tableofcontents_block/tableofcontents_block.module b/tableofcontents_block/tableofcontents_block.module
index 17d68ec..631e906 100644
--- a/tableofcontents_block/tableofcontents_block.module
+++ b/tableofcontents_block/tableofcontents_block.module
@@ -25,19 +25,17 @@ function _tableofcontents_block_view() {
   $node = node_load(arg(1));
 
   $_tableofcontents_block_processing = TRUE;
-  $text = node_view($node, FALSE, TRUE, FALSE);
+  $text = node_view($node, 'full', $node->language);
   unset($_tableofcontents_block_processing);
 
   module_load_include('pages.inc', 'tableofcontents');
   $_tableofcontents_block_toc = '[toc]';
-  $text = _tableofcontents_process(0, 'block', $text);
+  $text = _tableofcontents_process(0, 'block', drupal_render($text['body']));
+
   unset($_tableofcontents_block_toc);
 
   if ($text) {
-    $block['content'] = $text;
-    $block['subject'] = t('Table of contents');
-    $block['weight'] = 0;
-    return $block;
+    return $text;
   }
   // no block
 }
@@ -72,55 +70,70 @@ function _tableofcontents_block_form() {
 }
 
 function _tableofcontents_block_save($edit) {
-  variable_set('tableofcontents_comments_block',             $edit['tableofcontents_comments_block']);
-  variable_set('tableofcontents_comments_level_block',       $edit['tableofcontents_comments_level_block']);
-  variable_set('tableofcontents_min_limit_block',            $edit['tableofcontents_min_limit_block']);
-  variable_set('tableofcontents_title_block',                $edit['tableofcontents_title_block']);
-  variable_set('tableofcontents_list_type_block',            $edit['tableofcontents_list_type_block']);
-  variable_set('tableofcontents_minlevel_block',             $edit['tableofcontents_minlevel_block']);
-  variable_set('tableofcontents_maxlevel_block',             $edit['tableofcontents_maxlevel_block']);
-  variable_set('tableofcontents_attachments_block',          $edit['tableofcontents_attachments_block']);
-  variable_set('tableofcontents_hide_show_block',            $edit['tableofcontents_hide_show_block']);
-  variable_set('tableofcontents_collapsed_block',            $edit['tableofcontents_collapsed_block']);
-  variable_set('tableofcontents_numbering_block',            $edit['tableofcontents_numbering_block']);
-  variable_set('tableofcontents_number_mode_block',          $edit['tableofcontents_number_mode_block']);
-  variable_set('tableofcontents_number_start_letter_block',  $edit['tableofcontents_number_start_letter_block']);
-  variable_set('tableofcontents_number_separator_block',     $edit['tableofcontents_number_separator_block']);
-  variable_set('tableofcontents_number_end_letter_block',    $edit['tableofcontents_number_end_letter_block']);
+  foreach($edit as $key => $value) {
+    if(substr($key,0,15) == 'tableofcontents') {
+      variable_set($key, $value);
+    }
+  }
 }
 
 /**
- * Implementation of hook_block().
+ * Implements hook_block_info().
  */
-function tableofcontents_block_block($op = 'list', $delta = 0, $edit = array()) {
-  switch ($op) {
-  case 'list':
-    $blocks[0]['info'] = t('Table of contents');
-    return $blocks;
-
-  case 'configure':
-    return _tableofcontents_block_form();
+function tableofcontents_block_block_info() {
+  $block['tableofcontents_block'] = array(
+    'info' =>  t('Table of contents'),
+  );
+  return $block;
+}
 
-  case 'save':
-    _tableofcontents_block_save($edit);
+/**
+ * Implements hook_block_view().
+ */
+function tableofcontents_block_block_view($delta) {
+  $block = array();
+  switch ($delta) {
+    case 'tableofcontents_block':
+      $block['subject'] = t('Table of contents');
+      $block['content'] =  _tableofcontents_block_view();
     break;
+    }
+  return $block;
+}
 
-  case 'view':
-    return _tableofcontents_block_view();
+/**
+ * Implements hook_block_configure().
+ */
+function tableofcontents_block_block_configure($delta) {
+  if ($delta == 'tableofcontents_block') {
+    $form = _tableofcontents_block_form();
+    return $form;
+  }
+}
 
+/**
+ * Implements hook_block_save().
+ */
+function tableofcontents_block_block_save($delta = '', $edit = array()) {
+  if ($delta == 'tableofcontents_block') {
+    _tableofcontents_block_save($edit);
   }
 }
 
 /**
  * Implementation of hook_form_FORM_ID_alter().
  */
-function tableofcontents_block_form_filter_admin_configure_alter(&$form, &$form_state) {
+function tableofcontents_block_form_filter_admin_format_form_alter(&$form, &$form_state, $form_id) {
   // verify that the table of contents is turned in that filter.
-  if (isset($form['tableofcontents'])) {
-    $format = $form['format']['#value'];
-
+  if (isset($form['filters']['settings']['filter_toc']['tableofcontents'])) {
+    $format = $form['#format']->format;
     // add block specific settings to this filter
-    $form['tableofcontents']['tableofcontents_block_only_table_' . $format] = array(
+    $form['filters']['settings']['filter_toc']['tableofcontents']['tableofcontents_block'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Table of Contents block'),
+      '#collapsible' => TRUE,
+    );
+    $form['filters']['settings']['filter_toc']['tableofcontents']['tableofcontents_block']['tableofcontents_block_only_table_' . $format] = array(
       '#title' => t('Block-only table of contents'),
       '#type' => 'checkbox',
       '#default_value' => variable_get('tableofcontents_block_only_table_' . $format, FALSE),
@@ -128,5 +141,3 @@ function tableofcontents_block_form_filter_admin_configure_alter(&$form, &$form_
     );
   }
 }
-
-// vim: ts=2 sw=2 et syntax=php
-- 
1.7.0.4

