plainblack.com
Username Password
search
Bookmark and Share

Appendix 1: Advanced Install Scripts for Assets

As mentioned in the chapter on writing wobjects, there is an additional step you should add to your install methods that is not included in the _NewWobject.skeleton class, which is importing all of the default templates for your asset. This section details one way to do this which allows your templates to not only be automatically imported, but also updated without the need for manually copying and pasting, or committing.

 

The first step is to define a metadata scheme for your templates. By adding a few properties to the beginning of each template you can tell your code how it should be imported. In the example, each metadata property is prefaced with a pound sign (#). Your templates will look something like this:

 

#title=Default New Asset Template

#namespace=NewAsset/view

#assetId=NewAsset00000000000001



<tmpl_if session.var.adminOn><p><tmpl_var controls></p></tmpl_if>

<tmpl_if displayTitle><h2><tmpl_var title></h2></tmpl_if>

<tmpl_if message>

<div class="error"><tmpl_var message></div>

</tmpl_if>

~~~

<style>
</style>


Store all such templates in a folder with .tmpl extensions.

 

/data/WebGUI/docs/NewAsset/templates/view.tmpl


Now, add some code which reads the directory and parses the template looking for the title, namespace, and assetId of your templates, as well as the body and head tags.


Start by exporting a new method:

 

use base 'Exporter';

our @EXPORT = qw(install uninstall upgrade);

use WebGUI::Session;

 

Now, update the install method to additionally install your templates.

 

sub install {

my $config = $ARGV[0];

my $home = $ARGV[1] || "/data/WebGUI";

my $templateDir = $ARGV[2] || "$home/docs/NewAsset/templates";

unless ($home && $config) {

die "usage: perl -MWebGUI::Asset::Wobject::NewAsset -e install www.example.com.conf\n";

}

 

#Open the session

print "Installing asset.\n";

my $session = WebGUI::Session->open($home, $config);

my $db = $session->db;

 

#Change the user session to the admin

$session->user({ userId=>3 });

 

#Add the asset to the config file

$session->config->addToArray("assets","WebGUI::Asset::Wobject::NewAsset");

 

# Create asset table

$db->write("create table if not exists NewAsset (

assetId varchar(22) binary not null,

revisionDate bigint not null,

primary key (assetId, revisionDate)

)");

 

# Create a folder asset to store the default template

my $importNode = WebGUI::Asset->getImportNode($session);

my $newFolder = $importNode->addChild({

className =>"WebGUI::Asset::Wobject::Folder",

title => "New Asset Folder",

menuTitle => "New Asset Folder",

url => "new_asset_folder",

groupIdView =>"3"

},"NewAssetFolder00000001");

 

#Install the default templates

importTemplates($session,$templateDir,$newFolder);

 

#Commit the version tag containing the folder and all the templates

my $tag = WebGUI::VersionTag->new($session, WebGUI::VersionTag->getWorking($session)->getId);

if (defined $tag) {

print "Committing tag\n";

$tag->set({comments=>"Folder and Asset Templates created by Asset Install Process"});

$tag->requestCommit;

}



#Close the session

$session->var->end;

$session->close;

print "Done. Please restart Apache.\n";

}


 

Note that another property was added that can be passed in, which is the location of the template directory. A default location has also been created for it. Additionally, code is added in the install portion which creates a new folder for which you've hardcoded an assetId. It is hard coded so you can later locate it to add, update, or remove templates.



Finally, a new method is added called importTemplates, which is used to do the template import. Once the import is complete, the folder and all the templates are committed.



The next thing to do is to update the uninstall method:

 

sub uninstall {

my $config = $ARGV[0];

my $home = $ARGV[1] || "/data/WebGUI";

unless ($home && $config) {

die "usage: perl -MWebGUI::Asset::Wobject::NewAsset -e uninstall www.example.com.conf\n" ;

}

 

#Open the session

print "Uninstalling asset.\n";

my $session = WebGUI::Session->open($home, $config);

my $db = $session->db;

 

#Change the user session to the admin

$session->user({userId=>3});

 

#Add the asset to the config file

$session->config->deleteFromArray("assets","WebGUI::Asset::Wobject::NewAsset");

 

#Remove the assets from the site

my $rs = $db->read("select assetId from asset where className='WebGUI::Asset::Wobject::NewAsset'");

while (my ($id) = $rs->array) {

my $asset = WebGUI::Asset->new($session, $id, "WebGUI::Asset::Wobject::NewAsset");

$asset->purge if defined $asset;

}

 

#Remove the templates and the template folder

my @templateIds = $db->buildArray(q|

select distinct assetId from template where namespace in ('NewAsset/view')

|);

push(@templateIds,"NewAssetFolder00000001");

foreach my $templateId (@templateIds) {

my $asset = WebGUI::Asset->new($session, $templateId, "WebGUI::Asset::Wobject::NewAsset");

$asset->purge if defined $asset;

}

 

# Drop asset table

$session->db->write("drop table NewAsset");

 

# Close the session

$session->var->end;

$session->close;

print "Done. Please restart Apache.\n";

}

 

Additional code is added to remove the templates and template folder.



Finally, add a new method which simply updates the templates. This makes it easy to update your templates during development, as well as distribute upgrades to your code.

 

sub upgrade {

my $config = $ARGV[0];

my $home = $ARGV[1] || "/data/WebGUI";

my $templateDir = $ARGV[2] || "$home/docs/NewAsset/templates";

unless ($home && $config) {

die "usage: perl -MWebGUI::Asset::Wobject::NewAsset -e upgrade www.example.com.conf\n";

}

 

#Open the session

print "Updating asset.\n";

my $session = WebGUI::Session->open($home, $config);

$session->user({userId=>3});

 

#Install the default templates

importTemplates($session,$templateDir,WebGUI::Asset->new($session,"NewAssetFolder00000001"));

 

#Commit the version tag containing the templates

my $tag = WebGUI::VersionTag->new($session, WebGUI::VersionTag->getWorking($session)->getId);

if (defined $tag) {

print "Committing tag\n";

$tag->set({comments=>"Template updated by Asset Upgrade Process"});

$tag->requestCommit;

}

 

#Close the session

$session->var->end;

$session->close;

print "Done.\n";

}

 

The additional methods used to import templates are displayed below. Please note that this is only one way you might accomplish this. WebGUI used to use an import system similar to this one, but now imports packages of templates directly, allowing you to make changes directly on the site and distribute them in WebGUI native package format.

 

#-----------------------------------------------------------------

sub importTemplates {

my $session = shift;

my $templateDir = shift;

my $folder = shift;

my $quiet = 0;

 

#Open the template directory

return 0 unless (opendir (DIR,$templateDir));

 

#Set the slash variable depending on the operating system

my $slash;

if ($^O =~ /^Win/i || $^O =~ /^MS/i) {

$slash = "\\";

} else {

$slash = "/";

}

 

#Read the files in the template directory

my @files=readdir(DIR);

closedir(DIR);

 

#For each file in the template directory

foreach my $file (@files) {

#Skip it if the file isn't valid

next if ($file eq "." || $file eq ".." || !isValidTemplateFile($file));

#Create a path to the file and open it

my $pathToFile = $templateDir.$slash.$file;

unless ( open (FILE, $pathToFile) ) {

print "Could not open $pathToFile. Skipping Template $!\n" unless ($quiet);

next;

}

#Set up some defaults

my $title = "Default Template";

my $namespace = q{};

my $assetId = q{};

my $settingId = q{};

my $templateFile = q{};

my $headBlock = q{};

my $head = 0;

#Loop over each line of the template and process meta data, head, and body

while (<FILE>) {

my $line = $_;

my $char = "#";

if($line =~ m/^$char[^=<]+=/){

my $setting = substr($line,1);

my ($key,$value) = split("=",$setting);

$key = lc($key);

 

if($key eq "namespace") {

$namespace = trim($value);

}

elsif($key eq "title") {

$title = trim($value);

}

elsif($key eq "assetid") {

$assetId = trim($value);

}

elsif($key eq "settingid") {

$settingId = trim($value);

}

next;

}

elsif ($line =~ m/^~~~$/) {

$head = 1;

next;

}

elsif ($head) {

$headBlock .= $line;

}

else {

$templateFile .= $line;

}

}

#Check for errors in the metadata

if($assetId eq "") {

print "No Asset Id specified. Skipping Template $file\n" unless ($quiet);

next;

}

 

unless(isValidAssetId($assetId)) {

print "Skipping $file. Invalid assetId\n" unless ($quiet);

next;

}

 

if($namespace eq "") {

print "No Namespace specified. Skipping Template $file\n" unless ($quiet);

next;

}

#Process the templates

print "Processing Asset $assetId\n" unless ($quiet);

 

my $tmpl = WebGUI::Asset::Template->new($session,$assetId);

if($tmpl){

print "Asset $assetId found. Updating ... \n" unless ($quiet);

$tmpl->update({

template => $templateFile,

headBlock => $headBlock,

namespace => $namespace,

title => $title,

menuTitle => $title

});

}

else {

print "Asset $assetId not found. Creating ... \n" unless ($quiet);

$folder->addChild({

className => "WebGUI::Asset::Template",

namespace => $namespace,

title => $title,

menuTitle => $title,

ownerUserId => "3",

groupIdView => "7",

groupIdEdit => "4",

isHidden => 1,

template => $templateFile,

headBlock => $headBlock,

}, $assetId);

}

 

#Set the asset Id in the settings table if set

if($settingId) {

my ($exists) = $session->db->quickArray("select count(*) from settings where name=?",[$settingId]);

if($exists) {

$session->setting->set($settingId,$assetId);

}

else {

$session->setting->add($settingId,$assetId);

}

}

}

return 1;

}



#-----------------------------------------------------------------

sub isValidTemplateFile {

my $filename = $_[0];

my $quiet = 0;

unless ($filename =~ m/(.*)\.(.*?)$/) {

print "Skipping $filename. Invalid File Format\n";

return 0;

}

my $extension = $2;

 

unless (lc($extension) eq "tmpl") {

print "Skipping $filename. Invalid template extension\n" unless ($quiet);

return 0;

}

return 1;

}



#-----------------------------------------------------------------

sub isValidAssetId {

my $assetId = $_[0];

unless (length($assetId) <= 22) {

return 0;

}

return 1;

}



#-----------------------------------------

sub trim {

my $string = $_[0];

$string =~ s/^\s+//;

$string =~ s/\s+$//;

return $string;

}

Keywords:

Search | Most Popular | Recent Changes | Wiki Home
© 2018 Plain Black Corporation | All Rights Reserved