The Wobject Cookbook is intended for developers as a cookbook guide to writing a custom asset. It includes miscellaneous recipes on how to do various tasks within your custom asset.
Definition
When designing a Wobject, define what attributes you will need for your Wobject in the definition method. These attributes are directly related to the Wobject's asset table.
Collateral
When you need to associate a file or secondary data to your Wobject, you'll need to associate Collateral via a secondary table. Use the setCollateral method in Wobject.pm to update your Wobject's collateral table.
An example, a user uploads an image or file via the use of your Wobject.
- Add the creation and deletion of the table to Wobject's install and uninstall respectively.
- Create a form with a file upload form control via your view method in the Wobject.
- Process the form (including upload) and call setCollateral to update the Wobject's collateral table.
Upload a File From a Form Post
Creates a new file asset in storage from a form post.
my $storage = WebGUI::Storage->create($self->session);
my $filename = $storage->addFileFromFormPost('file_param_name_formId', 1);
Create a Wobject w/Custom View Using Applied Style
Create a custom view with a custom form, using the applied Style template, by calling processTemplate.
sub view {
my $self = shift;
my $session = $self->session;
#This automatically creates template variables for all of your wobject's properties.
my %var = %{$self->get};
my $f = WebGUI::HTMLForm->new($self->session,-action=>$self->getUrl)
...code. create form using WebGUI::HTMLForm
$var{'form.content'} = $f->print;
return $self->processTemplate(\%var, undef, $self->{_viewTemplate});
}
Create a default template for your Wobject that outputs form.content by adding form.content to your default template in the import() method of your Wobject (see the Wobject Development Tutorial).
# add a template
my $import = WebGUI::Asset->getImportNode($session);
$import->addChild({
className=>"WebGUI::Asset::Template",
template=>q|
<div><a name="id<tmpl_var assetId>" id="id<tmpl_var assetId>"></a></div>
<tmpl_if session.var.adminOn>
<p><tmpl_var controls></p>
</tmpl_if>
<tmpl_if displayTitle>
<h1><tmpl_var title></h1>
</tmpl_if>
<tmpl_if description>
<p><tmpl_var description></p>
</tmpl_if>
<table class="content" width="100%">
<tr>
<td valign="top">
<tmpl_var form.start>
<tmpl_var form.content>
<tmpl_var form.end>
</td>
</tr>
</table>
|,
ownerUserId=>'3', # admin
groupIdView=>'7',
groupIdEdit=>'3', #admin
title=>"MyWobject",
menuTitle=>"MyWobject
",
url=>"templates/MyWobject
-default",
namespace=>"MyWobject
"
},'MyWobject
Tmpl00000001');
my $versionTag = WebGUI::VersionTag->getWorking($session);
$versionTag->set({name=>"Install MyWobject
Wobject Template"});
$versionTag->commit;
Keywords: asset collateral dev file upload