Added the RootTab, RandomSnippet, RandomImage, CanEditText, If, Spacer, SpecificDropMenu, TopDropMenu, LastModified, PreviousDropMenu, and SI (scaled image) macros.

This commit is contained in:
JT Smith 2003-04-25 03:38:30 +00:00
parent ff6a6e45ea
commit 6d234e929d
15 changed files with 564 additions and 6 deletions

View file

@ -0,0 +1,31 @@
package WebGUI::Macro::CanEditText;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Macro;
use WebGUI::Session;
use WebGUI::Privilege;
#-------------------------------------------------------------------
sub process {
my @param = WebGUI::Macro::getParams($_[0]);
if (WebGUI::Privilege::canEditPage()) {
return $param[0];
} else {
return "";
}
}
1;

35
lib/WebGUI/Macro/If.pm Normal file
View file

@ -0,0 +1,35 @@
package WebGUI::Macro::If;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black Software.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use Safe;
use WebGUI::Macro;
sub process {
my ($expression, $true, $false) = WebGUI::Macro::getParams(shift);
my $output = $false;
# Workaround to "Safely" eval $expression
my $compartment = new Safe;
my $return = $compartment->reval($expression);
return "<p><b>If Macro failed:</b> $@ <p>Expression: $expression
<br>Display if true: $true<br>Display if false: $false" if ($@);
$output = $true if ($return);
return $output;
}
1;

View file

@ -0,0 +1,37 @@
package WebGUI::Macro::LastModified;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::DateTime;
use WebGUI::Macro;
use WebGUI::Session;
use WebGUI::SQL;
#-------------------------------------------------------------------
sub process {
my ($label, $format, $time, $output);
($label, $format) = WebGUI::Macro::getParams(shift);
$format = '%z' if ($format eq "");
$output = "";
($time) = WebGUI::SQL->quickArray("SELECT max(lastEdited) FROM wobject where pageId=$session{page}{pageId}");
if ($time) {
$output = $label.epochToHuman($time,$format);
}
return $output;
}
1;

View file

@ -0,0 +1,64 @@
package WebGUI::Macro::PreviousDropMenu;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Macro;
use WebGUI::Navigation;
use WebGUI::Session;
use WebGUI::SQL;
#-------------------------------------------------------------------
sub _draw {
my ($output, $i, $padding, $pageId);
my ($tree, $indent, $maxDepth, $currentDepth) = @_;
unless ($currentDepth >= $maxDepth) {
for ($i=1;$i<=$indent;$i++) {
$padding .= "&nbsp;&nbsp;";
}
foreach $pageId (keys %{$tree}) {
$output .= '<option value="'.$tree->{$pageId}{url}.'">';
$output .= $padding."- ".$tree->{$pageId}{title};
$output .= '</option>';
$output .= _draw($tree->{$pageId}{sub}, ($indent+1), $maxDepth, ($currentDepth+1));
}
}
return $output;
}
#-------------------------------------------------------------------
sub process {
my ($temp, @param, $tree);
@param = WebGUI::Macro::getParams($_[0]);
$param[2] = 99 unless ($param[2]);
if ($param[0] ne "") {
$tree = WebGUI::Navigation::tree($session{page}{parentId},$param[0]);
} else {
$tree = WebGUI::Navigation::tree($session{page}{parentId},1);
}
$temp = '<script language="JavaScript" type="text/javascript">
function go(formObj){
if (formObj.chooser.options[formObj.chooser.selectedIndex].value != "none") {
location = formObj.chooser.options[formObj.chooser.selectedIndex].value
}
}
</script>';
$temp .= '<form><select name="chooser" size=1 onChange="go(this.form)">';
$temp .= '<option value=none>Where do you want to go?';
$temp .= _draw($tree,0,$param[1]);
$temp .= '</select></form>';
return $temp;
}
1;

View file

@ -20,8 +20,15 @@ use WebGUI::SQL;
#-------------------------------------------------------------------
sub process {
my @param = WebGUI::Macro::getParams($_[0]);
my ($collateralFolderId) = WebGUI::SQL->quickArray("select collateralFolderId from collateralFolder where name=".quote($param[0]));
my @images = WebGUI::SQL->buildArray("select collateralId from collateral where collateralType='image' and collateralFolderId=".$collateralFolderId);
my $collateralFolderId;
if ($param[0] ne "") {
($collateralFolderId) = WebGUI::SQL->quickArray("select collateralFolderId from collateralFolder
where name=".quote($param[0]));
} else {
$collateralFolderId = 0; #Root
}
my @images = WebGUI::SQL->buildArray("select collateralId from collateral
where collateralType='image' and collateralFolderId=".$collateralFolderId);
my $collateral = WebGUI::Collateral->new($images[rand($#images+1)]);
return '<img src="'.$collateral->getURL.'" '.$collateral->get("parameters").' />';
}

View file

@ -20,8 +20,15 @@ use WebGUI::SQL;
#-------------------------------------------------------------------
sub process {
my @param = WebGUI::Macro::getParams($_[0]);
my ($collateralFolderId) = WebGUI::SQL->quickArray("select collateralFolderId from collateralFolder where name=".quote($param[0]));
my @snippets = WebGUI::SQL->buildArray("select collateralId from collateral where collateralType='snippet' and collateralFolderId=".$collateralFolderId);
my $collateralFolderId;
if ($param[0] ne "") {
($collateralFolderId) = WebGUI::SQL->quickArray("select collateralFolderId from collateralFolder
where name=".quote($param[0]));
} else {
$collateralFolderId = 0; #Root
}
my @snippets = WebGUI::SQL->buildArray("select collateralId from collateral
where collateralType='snippet' and collateralFolderId=".$collateralFolderId);
my $collateral = WebGUI::Collateral->new($snippets[rand($#snippets+1)]);
return $collateral->get("parameters");
}

View file

@ -0,0 +1,59 @@
package WebGUI::Macro::RootTab;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2002 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Navigation;
use WebGUI::Session;
use WebGUI::SQL;
#-------------------------------------------------------------------
sub _draw {
my ($tree, $root) = @_;
#my $output;# = '<table class="rootTab"><tr>';
my $output;# = '<div class="rootTabs">';
foreach my $key (keys %{$tree}) {
$output .= ' <span class="';
if ($root == $key) {
$output .= "rootTabOn";
} else {
$output .= "rootTabOff";
}
$output .= '">';
$output .= '<a href="'.$tree->{$key}{url}.'">'.$tree->{$key}{title}.'</a>';
$output .= '</span> ';
}
#$output .= '</div>';
#$output .= '</tr></table>';
return $output;
}
#-------------------------------------------------------------------
sub _findRoot {
my ($pageId,$parentId) = WebGUI::SQL->quickArray("select pageId,parentId from page where pageId=$_[0]");
if ($parentId == 0) {
return $pageId;
} else {
return _findRoot($parentId);
}
}
#-------------------------------------------------------------------
sub process {
my $root = _findRoot($session{page}{pageId});
my $tree = WebGUI::Navigation::tree(0,1);
return _draw($tree,$root);
}
1;

View file

@ -0,0 +1,124 @@
package WebGUI::Macro::SI_scaledImage;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Collateral;
use WebGUI::Macro;
use WebGUI::ErrorHandler;
use WebGUI::Session;
# test for Image::Magick
# (Would be nice if the results of this test were availiable somewhere
# central)
my $hasImageMagick=1;
eval " use Image::Magick; "; $hasImageMagick=0 if $@;
#-------------------------------------------------------------------
sub _getImage {
my ($collateral) = @_;
return undef unless ($hasImageMagick);
my $image = Image::Magick->new();
if (my $error = $image->Read($collateral->getPath)) {
WebGUI::ErrorHandler::warn("Couldn't read image for resizing: ".$error);
return undef;
}
return $image;
}
#-------------------------------------------------------------------
sub process {
my ($collateralIdent,$width,$height,$parameters) = WebGUI::Macro::getParams($_[0]);
my ($collateral,$url);
if ($collateralIdent =~ /^\d+$/) {
$collateral = WebGUI::Collateral->new($collateralIdent);
}
else {
$collateral = WebGUI::Collateral->find($collateralIdent);
}
unless ($collateral) {
WebGUI::ErrorHandler::warn("collateral not found: $collateralIdent");
return '';
}
unless ($collateral->isImage()) {
WebGUI::ErrorHandler::warn("Bad image type: $collateralIdent");
return '';
}
if ($width || $height) {
$url = GuideGuide::scaleImage(
collateral => $collateral,
width => $width,
height => $height
);
}
else {
WebGUI::ErrorHandler::warn("width or heigth must be specified");
}
$url ||= $collateral->getURL;
return qq!<img src="$url" $parameters/>!;
}
#-------------------------------------------------------------------
sub scaleImage {
my (%p) = @_;
my ($collateral,$width,$height) = @p{qw(collateral width height)};
# paranoia
return undef unless ($height || $width);
my $filename = "SIThumb_".($width || 'r')."x".($height || 'r')."_".$collateral->getFilename();
$filename .= '.png' if (isIn($collateral->getType(), qw(tif tiff bmp)));
my $pathName = $collateral->{_node}->getPath().$session{os}{slash}.$filename;
unless (-e $pathName) {
my $image = _getImage($collateral);
return undef unless $image;
my ($newWidth,$newHeight);
if ($width && $height) {
($newWidth,$newHeight) = ($width,$height);
}
else {
my ($x, $y) = $image->Get('width','height');
my $ratio = $x / $y;
$newWidth = $width ? $width : $height * $ratio;
$newHeight = $height ? $height : $width / $ratio;
}
my $max = $session{setting}{maxImageSize};
if ($newHeight > $max || $newWidth > $max) {
WebGUI::ErrorHandler::warn(
"Image too large ($newWidth,$newHeight) :".$collateral->get('name')
);
return undef;
}
$image->Scale(width => $newWidth, height => $newHeight);
if (my $error = $image->Write($pathName)) {
WebGUI::ErrorHandler::warn("Couldn't resize image: ".$error);
}
}
return $collateral->{_node}->getURL."/$filename";
}
1;

View file

@ -0,0 +1,29 @@
package WebGUI::Macro::Spacer;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Session;
use WebGUI::Macro;
#-------------------------------------------------------------------
sub process {
my ($output, @param, $width, $height);
@param = WebGUI::Macro::getParams($_[0]);
$width = $param[0] if defined $param[0];
$height = $param[1] if defined $param[1];
$output = '<img src="'.$session{config}{extrasURL}.'/spacer.gif"'.(defined $width?' width="'.$width.'"':'').(defined $height?' height="'.$height.'"':'').' border="0">';
return $output;
}
1;

View file

@ -0,0 +1,69 @@
package WebGUI::Macro::SpecificDropMenu;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Macro;
use WebGUI::Navigation;
use WebGUI::Session;
use WebGUI::SQL;
#-------------------------------------------------------------------
sub _draw {
my ($output, $i, $padding, $pageId);
my ($tree, $indent, $maxDepth, $currentDepth) = @_;
unless ($currentDepth >= $maxDepth) {
for ($i=1;$i<=$indent;$i++) {
$padding .= "&nbsp;&nbsp;";
}
foreach $pageId (keys %{$tree}) {
$output .= '<option value="'.$tree->{$pageId}{url}.'">';
$output .= $padding."- ".$tree->{$pageId}{title};
$output .= '</option>';
$output .= _draw($tree->{$pageId}{sub}, ($indent+1), $maxDepth, ($currentDepth+1));
}
}
return $output;
}
#-------------------------------------------------------------------
sub process {
my ($temp, @param, $pageId, $tree);
@param = WebGUI::Macro::getParams($_[0]);
($pageId) = WebGUI::SQL->quickArray("select pageId from page where urlizedTitle='$param[0]'");
if (defined $pageId) {
$param[2] = 99 unless ($param[2]);
if ($param[1] ne "") {
$tree = WebGUI::Navigation::tree($pageId,$param[1]);
} else {
$tree = WebGUI::Navigation::tree($pageId,1);
}
$temp = '<script language="JavaScript" type="text/javascript">
function go(formObj){
if (formObj.chooser.options[formObj.chooser.selectedIndex].value != "none") {
location = formObj.chooser.options[formObj.chooser.selectedIndex].value
}
}
</script>';
$temp .= '<form><select name="chooser" size=1 onChange="go(this.form)">';
$temp .= '<option value=none>Where do you want to go?';
$temp .= _draw($tree,0,$param[2]);
$temp .= '</select></form>';
} else {
$temp = "No page specified.";
}
return $temp;
}
1;

View file

@ -0,0 +1,40 @@
package WebGUI::Macro::TopDropMenu;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Navigation;
use WebGUI::Session;
#-------------------------------------------------------------------
sub process {
my ($temp, $tree, $pageId);
$tree = WebGUI::Navigation::tree(1,1);
$temp = '<script language="JavaScript" type="text/javascript">
function go(formObj){
if (formObj.chooser.options[formObj.chooser.selectedIndex].value != "none") {
location = formObj.chooser.options[formObj.chooser.selectedIndex].value
}
}
</script>';
$temp .= '<form><select name="chooser" size=1 onChange="go(this.form)">';
$temp .= '<option value=none>Where do you want to go?';
foreach $pageId (keys %{$tree}) {
$temp .= '<option value="'.$tree->{$pageId}{url}.'">'.$tree->{$pageId}{title};
}
$temp .= '</select></form>';
return $temp;
}
1;