Index: img_assist.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist.info,v retrieving revision 1.5 diff -u -p -r1.5 img_assist.info --- img_assist.info 18 Jul 2008 22:58:09 -0000 1.5 +++ img_assist.info 12 Aug 2009 06:24:38 -0000 @@ -2,6 +2,7 @@ name = Image assist description = This module allows users to upload and insert inline images into posts. It automatically generates an Add image link under the textarea fields of your choice. dependencies[] = image +dependencies[] = inline dependencies[] = views package = Image core = 6.x Index: img_assist.inline.inc =================================================================== RCS file: img_assist.inline.inc diff -N img_assist.inline.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ img_assist.inline.inc 12 Aug 2009 07:12:07 -0000 @@ -0,0 +1,83 @@ + array( + '#title' => t('Node id'), + '#description' => t('A node id containing an image file to render.'), + '#type' => 'int', + ), + 'fid' => array( + '#title' => t('File id'), + '#description' => t('A file id of an image file to render.'), + '#type' => 'int', + ), + ); + return $args; + + case 'presave': + // Context may be passed via inline_nodeapi(); update our mapping table. + if (isset($macro->params['nid']) && !empty($macro->context['node'])) { + static $shutdown_added; + if (!isset($shutdown_added)) { + register_shutdown_function('img_assist_inline_map_update'); + $shutdown_added = TRUE; + } + img_assist_inline_map_update($macro->context['node']->nid, $macro->params['nid']); + } + return; + + case 'validate': + if (!isset($macro->params['nid']) && !isset($macro->params['fid'])) { + return FALSE; + } + return TRUE; + + case 'render': + $output = img_assist_render_image($macro->params); + return $output; + } +} + +/** + * @todo This was only invoked when at least 1 macro was processed, but only + * appended once to the entire filtered text (not each macro). + */ +function theme_img_assist_filter($text) { + // The div tag added to the end of each node is necessary to clear the + // floating properties of inline images immediately after a node's content. + return $text . '
'; +} + +/** + * Update img_assist_map image reference table. + */ +function img_assist_inline_map_update($nid, $iid) { + static $updates = array(); + + if (isset($nid) && isset($iid)) { + $updates[$nid][] = $iid; + } + // Final shutdown. + else { + foreach ($updates as $nid => $iids) { + db_query('DELETE FROM {img_assist_map} WHERE nid = %d', $nid); + $iids = array_unique($iids); + foreach ($iids as $iid) { + db_query('INSERT INTO {img_assist_map} (nid, iid) VALUES (%d, %d)', $nid, $iid); + } + } + } +} + Index: img_assist.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/img_assist.module,v retrieving revision 1.107 diff -u -p -r1.107 img_assist.module --- img_assist.module 13 Aug 2009 19:32:44 -0000 1.107 +++ img_assist.module 13 Aug 2009 19:33:15 -0000 @@ -33,6 +33,7 @@ function img_assist_theme() { ), 'img_assist_filter' => array( 'arguments' => array('text' => NULL), + 'file' => 'img_assist.inline.inc', ), 'img_assist_popup' => array( 'arguments' => array('content' => NULL, 'attributes' => NULL), @@ -96,7 +97,6 @@ function img_assist_menu() { ); // Insert callback (only for inserting HTML, not filter tag). $items['img_assist/insert_html'] = array( - 'title' => 'Insert callback', 'page callback' => 'img_assist_insert_html', 'access arguments' => array('access img_assist'), 'type' => MENU_CALLBACK, @@ -159,7 +159,7 @@ function img_assist_textarea($element) { } $link = variable_get('img_assist_link', 'icon'); if ($link == 'icon' || $link == 'text') { - if (_img_assist_textarea_match($element['#id']) && _img_assist_page_match() && !strstr($_GET['q'], 'img_assist')) { + if (!strstr($_GET['q'], 'img_assist')) { if (!$initialized) { // Add settings. $settings['link'] = $link; @@ -197,11 +197,9 @@ function img_assist_block($op = 'list', else if ($op == 'view') { switch ($delta) { case 0: - // Since blocks aren't passed node objects (which makes sense) we need - // to determine if we are viewing a node and grab its nid. - if (arg(0) == 'node' && is_numeric(arg(1))) { + if ($node = menu_get_object()) { $block['subject'] = t('This image appears in...'); - $block['content'] = img_assist_get_references(arg(1)); + $block['content'] = img_assist_get_references($node->nid); return $block; } break; @@ -210,61 +208,21 @@ function img_assist_block($op = 'list', } /** - * Implementation of hook_filter(). + * Implementation of hook_inline_info(). */ -function img_assist_filter($op, $delta = 0, $format = -1, $text = '') { - switch ($op) { - case 'list': - return array(0 => t('Inline images')); - - case 'description': - return t('Add images to your posts with Image assist.'); - -// case 'no cache': -// return TRUE; - - case 'process': - $processed = FALSE; - foreach (img_assist_get_macros($text) as $unexpanded_macro => $macro) { - $expanded_macro = img_assist_render_image($macro); - $text = str_replace($unexpanded_macro, $expanded_macro, $text); - $processed = TRUE; - } - return $processed ? theme('img_assist_filter', $text) : $text; - - default: - return $text; - } -} - -/** - * Implementation of hook_filter_tips(). - */ -function img_assist_filter_tips($delta, $format, $long = FALSE) { - return t('Images can be added to this post.'); +function img_assist_inline_info() { + $info['img_assist'] = array( + 'file' => 'img_assist.inline.inc', + 'callback' => 'img_assist_inline', + ); + return $info; } /** * Implementation of hook_nodeapi(). - * - * - Clear input filter cache. - * - Keep track of where images are used. - * - Catch nids of recently uploaded images. */ function img_assist_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { switch ($op) { - case 'update': - if ($node->type == 'image') { - // Clear the input filter cache to force all node content to be rebuilt. - // This is to make sure all image paths are up to date. - cache_clear_all(NULL, 'cache_filter'); - } - // break is intentionally left out. - case 'insert': - // Update the image map. - img_assist_map_save($node); - break; - case 'delete': img_assist_map_delete($node); break; @@ -1285,45 +1243,6 @@ function img_assist_popup() { */ /** - * Update the map table - * - * Look for any images linked in this content and keep a reference of them. - */ -function img_assist_map_save($node) { - $content = $node->body; - // If CCK is used, image macros can be found in fields other than the body. - // Get all the fields that use text filtering and extract their content: - if (function_exists('content_types')) { - $type = content_types($node->type); - if (!empty($type['fields'])) { - foreach ($type['fields'] as $field) { - // Distinguish between plain text fields and filtered fields: - if (!empty($field['text_processing'])) { - if (count($node->{$field['field_name']})) { - foreach ($node->{$field['field_name']} as $field_instance) { - $content .= $field_instance['value']; - } - } - } - } - } - } - - // Get all the macros from the content: - $macros = (array)img_assist_get_macros($content); - - // Save the image references: - db_query('DELETE FROM {img_assist_map} WHERE nid = %d', $node->nid); - $nids = array(); - foreach ($macros as $m) { - if (!isset($nids[$m['nid']]) && is_numeric($m['nid'])) { - db_query('INSERT INTO {img_assist_map} (nid, iid) VALUES(%d, %d)', $node->nid, $m['nid']); - $nids[$m['nid']] = $m['nid']; - } - } -} - -/** * Delete references to a non-existant node. * * If a node is being deleted update the map table. The node can either be an @@ -1335,30 +1254,17 @@ function img_assist_map_delete($node) { } /** - * Load the image map for a given nid. - */ -function img_assist_map_load($nid) { - $imagemap = array(); - $result = db_query('SELECT * FROM {image} i INNER JOIN {img_assist_map} iam ON i.nid = iam.iid WHERE i.nid = %d', $nid); - while ($data = db_fetch_object($result)) { - $imagemap[] = $data->nid; - } - - return $imagemap; -} - -/** * Return a list of node links for a given nid. */ function img_assist_get_references($nid, $limit = 10) { $and_clause = array(); - $images = img_assist_map_load($nid); - foreach ($images as $id) { - $and_clause[] = 'n.nid = '. $id; + $images = array(); + $result = db_query('SELECT * FROM {image} i INNER JOIN {img_assist_map} iam ON i.nid = iam.iid WHERE i.nid = %d', $nid); + while ($data = db_fetch_object($result)) { + $and_clause[] = 'n.nid = ' . $data->nid; } - $and_clause = implode(' OR ', $and_clause); - - if ($images) { + if ($and_clause) { + $and_clause = implode(' OR ', $and_clause); return node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.status = 1 AND $and_clause ORDER BY n.nid DESC"), 0, (int) $limit)); } } @@ -1368,94 +1274,6 @@ function img_assist_get_references($nid, */ /** - * @defgroup img_assist_macro Image Assist Filter macro parsing - * @{ - */ - -/** - * Return all img_assist macros as an array. - */ -function img_assist_get_macros($text) { - $m = array(); - preg_match_all('/ \[ ( [^\[\]]+ )* \] /x', $text, $matches); - // Don't process duplicates. - $tag_match = (array) array_unique($matches[1]); - - foreach ($tag_match as $macro) { - $current_macro = '['. $macro .']'; - $param = array_map('trim', explode('|', $macro)); - // The first macro param is assumed to be the function name. - $func_name = array_shift($param); - if ($func_name == 'img_assist') { - $vars = array(); - foreach ($param as $p) { - $pos = strpos($p, '='); - $varname = trim(substr($p, 0, $pos)); - $varvalue = substr($p, $pos + 1); - $vars[$varname] = trim($varvalue); - } - // The full unaltered filter string is the key for the array of filter - // attributes. - $m[$current_macro] = $vars; - } - } - - return $m; -} - -/** - * Determine if img_assist can render the current page. - * @see block_list(). - * - * @return - * TRUE if can render, FALSE if not allowed. - */ -function _img_assist_page_match() { - $must_match = variable_get('img_assist_paths_type', 2); - if ($must_match == 2) { - return TRUE; - } - else { - $paths = variable_get('img_assist_paths', "node/*\ncomment/*"); - $path = drupal_get_path_alias($_GET['q']); - $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\