Commit 06e78013 authored by walkah's avatar walkah
Browse files

g2_filter.module from Michelle (closes #40465).

needs updating for HEAD
parent 9fe08b6a
This module would not exist without image_filter by Eric Scouten (http://drupal.org/node/9707). I started with that module and transformed it bit by bit into this module. While there's not a lot of original code left, I wouldn't have been able
to do this without using image_filter as a template.
This module is dependent on Drupal (of course), Gallery 2, and the gallery.module (http://drupal.org/project/gallery) so thanks to everyone for all the work on those. Without them, this module would be useless. :)
\ No newline at end of file
1) Unpack the g2_filter module into your /modules directory.
2) Enable the module under admin/modules.
3) Add the filter to at least one of your input types at /admin/filters
3) Configure the filter. This is important because there are some path settings it needs to know.
This module creates a filter that lets you embed items from your embedded Gallery 2 install.
Requirements:
* Gallery 2 - http://gallery.menalto.com/
* gallery.module - http://drupal.org/project/gallery
Syntax:
[G2:item_id class=name size=number frame=name]
* item_id (required) *
This is the item ID from G2. If you look at the URL of the item, this is the last number.
* class *
The block that G2 returns is wrapped in a DIV so additional styling can be done. The classes for this DIV are located in g2_filter.css. Included with the module are "left", "right", and "nowrap". These position the image block to the left or right or on a line all its own with the text not wrapping. You can also add your own class(es) to the CSS file and they will automatically be available.
* size *
The length of the longest side for the thumbnail. The other side is determined automatically to keep the same aspect ratio.
* frame *
G2 comes with several item/album frames and allows you to add more. You can use any of these frames for the embedded thumbnail just by specifying a name. Frames included with the default install are: Bamboo, Book, Branded Wood, Dot Apple, Dots, Flicking, Gold, Gold 2, Polaroid, Polaroids, Shadow, Shells, Slide, Solid, Spiral Notebook, Wood.
Send comments to mcox@charter.net
Left to do as of 2005/Oct/21:
* Add arguments for the rest of the "show" items
* Add arguments for the rest of the "block" items
* Improve the CSS for the div class.
* Investigate the float clearing brought up by kiz
Keizo's notes:
/******issues that remain
*should we keep n as an option in the config settings? see comments in get_settings_form()
*should n be required to solve that issue?
*how should the 'show' param be implemented. I've put it in the settings, which works good for defaults.
*How should the syntax be? it could be caption=yes/no title=yes/no date=yes/no etc.
*is that optimal? how about about show=title,caption,date ..this one seems easier interms of usability, yes?
*are the syntax standard enough? it would be a pain to change them later
*since it would break all instances of the filters use.
*Should we make the default tag [image:id] instead of G2? like the image filter? or may [img:id]? or just lower case [g2:id] for easyness?
*what should we use as the default for 'type', right now it's viewedImage. recentImage might be more useful?
*do we need to bother making different types per block? I don't think so, people could just make a second block
*still need to implement link target
*does the 'frame=' work? its not working for me in this version, i didn't look too closely as to why
***features wish list
*I would like it so there was an 'see all photos' link when an album is displayed. selecting an item_id of an album with n=1 does this.
*any possible to make the block display horizontally?
*and ofcourse an auto javescript thing to auto insert the [G2] tags
*/
Oct 21, 2005
* Changed class names internally to add "giImageBlock." to make them unique. Classes are still refered to as "left, right, nowrap" in the filter call.
* Moved CSS file links to the HEAD via theme_add_style()
Oct 7, 2005
* Add ability to specify "none" for class and not have block wrapped in div (not added to docs)
* Removed redundant / from frame CSS link
div.giImageBlock.left{
float: left;
margin: 1em;
}
div.giImageBlock.right{
float: right;
margin: 1em;
}
div.giImageBlock.nowrap {
float: none;
margin: 1em
}
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Allows adding gallery blocks into nodes using a filter --> [G2:item_id]
*
*/
/******************************* Help sections ************************************/
function g2_filter_short_tip_translated() {
return t('You may link to G2 items on this site <a href="%explanation-url">using a special syntax</a>',
array('%explanation-url' => url('filter/tips', NULL, 'filter-g2_filter-0')));
}
function g2_filter_long_tip_translated() {
$prefix = variable_get("g2_filter_prefix", "G2");
$output = "";
$output .= '<strong>G2 Filter:</strong><p>You can link to items in your ';
$output .= 'embedded Gallery 2 using a special code. ';
$output .= 'This code will be replaced by a thumbnail image that is ';
$output .= 'linked to the actual item in your Gallery. ';
$output .= '<em>Syntax:</em><br />';
$output .= '<blockquote><code>';
$output .= '['.$prefix.':<em>item_id</em> n=<em>number</em> type=<em>type</em> size=<em>number</em> class=<em>name</em> frame=<em>name</em> album_frame=<em>name</em> item_frame=<em>name</em>]';
$output .= '</code></blockquote>';
$output .= '<ul><li>item_id (required): This is the item ID from G2. ';
$output .= 'If you look at the URL of the item, this is the last number. '
.'<em>Note that if the item_id is a single photo, n must be 1</em></li> ';
$output .= '<li>n (suggested): This is the number of photos you want the block to show. ';
$output .= 'It will override whatever is set in the defaults (initially 1). '
.' Note: this will change past instances where you did not set n -- the reason for its suggested use.</li> ';
$output .= '<li>type: The default type of gallery block. Any of the following may be used: ';
$output .= '<em>randomImage, recentImage, viewedImage, randomAlbum, recentAlbum, viewedAlbum, dailyImage, '
.'weeklyImage, monthlyImage, dailyAlbum, weeklyAlbum, monthlyAlbum, specificItem . </em> '
.'Note that for n=1, selectedItem is automatically chosen regardless of this parameter.</li> ';
$output .= '<li>class: The block that G2 returns is wrapped in a DIV so additional styling can be done. ';
$output .= 'The classes for this DIV are located in g2_filter.css. Included with the module ';
$output .= 'are "left", "right", and "nowrap". These position the image block to the left or ';
$output .= 'right or on a line all its own with the text not wrapping. You can also add your ';
$output .= 'own class(es) to the CSS file and they will automatically be available.</li> ';
$output .= '<li>size: The length of the longest side for the thumbnail. ';
$output .= 'The other side is determined automatically to keep the same aspect ratio.</li> ';
$output .= '<li>frame/album_frame/item_frame: You can use just "frame" to assign a frame ';
$output .= 'to the thumbnail regardless of whether it\'s for an album or a single item. ';
$output .= 'Using aframe will only affect albums and iframe will only affect single items. ';
$output .= 'Frames included with the default Gallery 2 install are: ';
$output .= 'bamboo, book, brand , dots, flicking, gold, gold2, polaroid, polaroids, shadow, ';
$output .= 'shells, slide, solid, notebook, wood.</li>';
return t($output);
}
function g2_filter_help($section = 'admin/help#image_filter') {
$output = '';
switch ($section) {
case 'admin/help#g2_filter':
$output = t('<p>Used to add image blocks from your embedded Gallery 2 to a node
like a blog entry or a story. To enable this feature and learn the proper syntax,
visit the <a href="%filters">filters configuration screen</a>.</p>',
array('%filters' => url('admin/filters')));
break;
case 'admin/modules#description':
$output = t("Allow users to reference G2 items from nodes.");
break;
case 'filter#short-tip':
return g2_filter_short_tip_translated();
case 'filter#long-tip':
return g2_filter_long_tip_translated();
}
return $output;
}
/******************************* Configuration ************************************/
function g2_filter_get_settings_form() {
$typeMap = array('randomImage' => t('Random image'),
'recentImage' => t('Recent image'),
'viewedImage' => t('Viewed image'),
'randomAlbum' => t('Random album'),
'recentAlbum' => t('Recent album'),
'viewedAlbum' => t('Viewed album'),
'dailyImage' => t('Daily image'),
'weeklyImage' => t('Weekly image'),
'monthlyImage' => t('Monthly image'),
'dailyAlbum' => t('Daily album'),
'weeklyAlbum' => t('Weekly album'),
'monthlyAlbum' => t('Monthly album'));
//we don't include specificItem, since it can be selected automatically if n=1
$output .= form_textfield(t("Filter prefix"), "g2_filter_prefix",
variable_get("g2_filter_prefix", "G2"), 10, 10,
t("Prefix to use with filter. Example: 'G2' means you use [G2: 999]."));
$output .= form_select(t('Image Block Type'), 'g2_filter_default_block_type',
variable_get('g2_filter_default_block_type', 'viewedImage'), $typeMap,
t('Pick default type of image block you\'d like to use, default is Recent image. Viewed image is by most clicks.'));
$output .= form_textfield(t("Default Number of Images"), "g2_filter_n_images",
variable_get("g2_filter_n_images", 1), 3, 3,
t("How many images you want the default block to show. Best to keep at 1 and use the n parameter."));
//////*********in fact, should we remove this option all together? because the way it is now, if you change this to n>1
//then instances where n was not specified and item_id is a specific item, the block breaks and nothing is shown.
$output .= form_checkboxes(
t('Default Image Block Settings'),
'g2_filter_default_show',
variable_get('g2_filter_default_show', 'none'),
array('title' => 'Title (the caption)',
'date' => 'Date',
'views' => 'View Count',
'owner' => 'Item owner',
'heading' => 'Heading',
'fullSize' => 'Full Size'),
t('Choose the item metadata you\'d like to display by default. This will change all instances where show parameter was not specified.'));
$output .= form_textfield(t("Default thumbnail size"), "g2_filter_default_size",
variable_get("g2_filter_default_size", "150"), 3, 3,
t('If no size is specified when calling the filter, this size will be used. '
.'Note that the display size will be limited by the size of the item_id in Gallery.'));
$output .= form_textfield(t("Default class"), "g2_filter_default_div_class",
variable_get("g2_filter_default_div_class", "nowrap"), 20, 20,
t("left, right, or nowrap. (See g2_filter.css to add more or modify these.)"));
$output .= form_textfield(t("Default album frame"), "g2_filter_default_album_frame",
variable_get("g2_filter_default_album_frame", "none"), 20, 20,
t("Enter a frame name like notebook, polaroid, shadow, slide, wood, etc. See your G2 install for a complete list. Use 'none' or blank for no frame. "));
$output .= form_textfield(t("Default item frame"), "g2_filter_default_item_frame",
variable_get("g2_filter_default_item_frame", "none"), 20, 20,
t("Enter a frame name like notebook, polaroid, shadow, slide, wood, etc. See your G2 install for a complete list. Use 'none' or blank for no frame. "));
return $output;
}
// Hook which handles filtering.
function g2_filter_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(0 => t('G2 filter'));
case 'description':
return t('Allow users to easily reference G2 items from nodes.');
case 'process':
return g2_filter_process($text);
case 'settings':
$group = g2_filter_get_settings_form();
return form_group(t("Gallery 2 Filter"), $group);
default:
return $text;
}
}
function g2_filter_filter_tips($delta = 0, $format = -1, $long = false) {
if ($long) {
return g2_filter_long_tip_translated();
}
else {
return g2_filter_short_tip_translated();
}
}
// ***************** The Filter in Action ***********************
define("G2_FILTER_WORD", 1);
define("G2_FILTER_INTEGER", 2);
define("G2_FILTER_STRING", 3);
function g2_filter_attr_value($text, $value_type = G2_FILTER_WORD) {
// Strip off initial and final quotes.
$first = substr($text, 0, 1);
if ($first == "\"" || $first == "\'") {
if (substr($text, -1, 1) == $first) {
$text = substr($text, 1, -1);
}
}
switch ($value_type) {
case G2_FILTER_WORD:
return preg_replace("/\W/", '', $text);
case G2_FILTER_INTEGER:
return preg_replace("/\D/", '', $text);
default:
return check_plain($text);
}
}
// Execute filter on given text.
function g2_filter_process($text) {
// Find all the image codes and loop over them, replacing each with the G2 image block
$prefix = variable_get("g2_filter_prefix", "G2");
$matchetxt = "/\[".trim($prefix).":(\d+)(\s*,)?\s*(.*?)\]/i";
preg_match_all($matchetxt, $text, $matches, PREG_SET_ORDER);
// If we have at least one match, set everything up
if (count($matches) > 0) {
// Set the default and path variables based on module settings
$default_size = variable_get("g2_filter_default_size", 150);
$default_div_class = variable_get("g2_filter_default_div_class", "nowrap");
$default_album_frame = variable_get("g2_filter_default_album_frame", "");
$default_item_frame = variable_get("g2_filter_default_item_frame", "");
$default_block_type = variable_get('g2_filter_default_block_type', 'recentImage');
$default_n_images = variable_get("g2_filter_n_images", 1);
$default_show = variable_get('g2_filter_default_show', 'none');
if ($default_album_frame == 'none') {
$default_album_frame = '';
}
if ($default_item_frame == 'none') {
$default_item_frame = '';
}
// This will hold the list of frames used for images so we can add the CSS link(s) at the end
$frame_list = array() ;
/* this is all done by _gallery_init(true) function
// Set up the pathing
$relative_G2_Path = variable_get("gallery_dir", "gallery2");
$embed_uri = url('gallery');
$embed_php_path = $relative_G2_Path . 'embed.php';
//the above line needs work, when the user sets the relative G2 path without a trailing / this module breaks
// The tolerant base_url patch in http://drupal.org/node/32389 seems to work well, but not all of it is needed.
// The http or https part is not required.
// BUT, does this work for all cases (Apache, IIS?)
if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
$embedPath = "/$dir";
} else {
$embedPath = '/';
}
// Link to the embedding code
require_once($embed_php_path);
// Grab the user information from Drupal
global $user;
*/
// This sets up the embedding
list ($success, $ret) = _gallery_init(true);
//_gallery_init() is from gallery.module, any reason not to use it?
//just remember if they change that function this module breaks
if (!$success) {
gallery_error(t('Unable to initialize embedded Gallery'), $ret);
return;
} /* since i commented this out, and use galler_init, i can probably get rid of some of the init code above
$ret = GalleryEmbed::init(array('embedUri' => $embed_uri,
'embedPath' => $embed_path,
'relativeG2Path' => $relative_G2_Path,
'loginRedirect' => '',
'fullInit' => true,
'activeUserId' => $user->uid)); */
}
foreach ($matches as $match) {
// Pull out the arguments into the $args array
$args = array();
preg_match_all("/(\w+)\=(\"[^\"]*\"|\S*)/", $match[3], $a, PREG_SET_ORDER);
foreach ($a as $arg) {
$args[strtolower($arg[1])] = $arg[2];
}
// Set number of images to show
$n_images = g2_filter_attr_value($args['n'], G2_FILTER_INTEGER);
if ($n_images == 0) {
// No size specified; use the default
$n_images = $default_n_images;
}
// Set the block type
$block_type = g2_filter_attr_value($args['type'], G2_FILTER_WORD);
if (empty($block_type)) {
// No size specified; use the default
$block_type = $default_block_type;
}
if ($n_images <= 1) $block_type = 'specificItem'; //so it shows something if n=1 and an album is selected
// Set the size of the thumbnail
$size = g2_filter_attr_value($args['size'], G2_FILTER_INTEGER);
if ($size == 0) {
// No size specified; use the default
$size = $default_size;
}
// Set the class of the div
$div_class = g2_filter_attr_value($args['class'], G2_FILTER_WORD);
if (empty($div_class)) {
// No class specified; use the default
$div_class = $default_div_class;
}
// Set the overriding, album, and item frames
$frame = g2_filter_attr_value($args['frame'], G2_FILTER_WORD);
$album_frame = g2_filter_attr_value($args['aframe'], G2_FILTER_WORD);
$item_frame = g2_filter_attr_value($args['iframe'], G2_FILTER_WORD);
if (empty($frame)) {
// No overriding frame given; check for album_frame and item_frame
if (empty($album_frame)) {
// No album frame specified; use the default one
$album_frame = $default_album_frame;
}
if (empty($item_frame)) {
// No item frame specified; use the default one
$item_frame = $default_item_frame;
}
} else {
// Overriding frame given; use it
$album_frame = $frame;
$item_frame = $frame;
}
// Add the requested frames to the array so we can get the CSS later. Don't worry about
// dupes at this point; they will be filtered out later.
array_push($frame_list,$frame);
array_push($frame_list,$album_frame);
array_push($frame_list,$item_frame);
// This part actually fetches the image block. It uses the same paramaters as the code
// found under "Image Block" in site admin in G2. Copied and slightly modified here for reference.
// blocks Pipe(|) separate list chosen from: randomImage, recentImage,
// viewedImage, randomAlbum, recentAlbum, viewedAlbum, dailyImage,
// weeklyImage, monthlyImage, dailyAlbum, weeklyAlbum, monthlyAlbum,
// specificItem; default is randomImage
// show Pipe(|) separated list chosen from: title, date, views, owner, heading,
// fullSize; the value can also be: none
// NOTE: If you want your size to be bigger than the thumbnail size for that image as
// defined in your G2, you must use 'show' => 'fullSize'
// itemId Limit the item selection to the subtree of the gallery under the
// album with the given id; or the id of the item to display when used with
// specificItem block type
// maxSize Scale images to this maximum size
// linkTarget Add a link target (for example, to open links in a new browser window)
// itemFrame Image frame to use around images
// albumFrame Image frame to use around albums
//$blocks = 'specificItem'; //not used
$show = $default_show;
// Not customized yet:
$link_target = '';
///////keizo
// Allow for multiple image types
$param_blocks_array = array_fill(0,$n_images,$block_type);
$params['itemId'] = $match[1];
$params['blocks'] = is_array($param_blocks_array) ? implode('|', $param_blocks_array) : "";
$param_show_array = $show;
$params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : "";
$params['maxSize'] = $size;
// Add frames and link target using g2_filter code from MichelleC
$params['albumFrame'] = $album_frame;
$params['itemFrame'] = $item_frame;
$params['linkTarget'] = $link_target;
$block = array();
list($ret, $imageBlockHtml) = GalleryEmbed::getImageBlock($params);
if ($ret->isError()) {
gallery_error(t('Unable to get Gallery image block'), $ret);
return;
} else {
//if ($imageBlockHtml) { this line is redundant isn't it?
// Add a div around the table for styling
if ($div_class != 'none') {
$imageBlockHtml = '<div class ="giImageBlock ' . $div_class . '">' .
$imageBlockHtml . '</div>';
}
// This puts the image block HTML back into the rest of the text
$text = str_replace($match[0], $imageBlockHtml, $text);
//}
}
//end keizo
/* list ($ret, $imageBlockHtml) =
GalleryEmbed::getImageBlock(array(
'blocks' => $blocks,
'show' => $show ,
'itemId' => $match[1],
'maxSize' => $size,
'linkTarget' => $link_target,
'albumFrame' => $album_frame,
'itemFrame' => $item_frame));
// Add a div around the table for styling
$imageBlockHtml = '<div class ="' . $div_class . '">' . $imageBlockHtml . '</div>';
// This puts the image block HTML back into the rest of the text
$text = str_replace($match[0], $imageBlockHtml, $text);
*/
} // end of for loop through matches
// If we had at least one match, finish up
if (count($matches) > 0) {
GalleryEmbed::done();
// Add the CSS link(s)
$css_links = '';
// In order to use the album/image frames, we need to link to the style sheet(s)
$frame_list = array_unique($frame_list);
foreach ($frame_list as $frame) {
$css_links .= '<link rel="stylesheet" href="' . $relative_G2_Path .
'/main.php?g2_controller=imageblock.ExternalCSS&amp;g2_frames=' .$frame.'"/> ' ;
}
// Link to the CSS that controls the styling of the wrapping div
$div_css = drupal_get_path('module', 'g2_filter') . "/g2_filter.css";
$css_links .= '<link rel="stylesheet" href="' . $div_css . '"/> ' ;
$text = $css_links .= $text ;
}
return $text;
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment