package WebGUI::Asset; =head1 LEGAL ------------------------------------------------------------------- WebGUI is Copyright 2001-2007 Plain Black Corporation. ------------------------------------------------------------------- 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 ------------------------------------------------------------------- =cut use Carp qw( croak confess ); use Scalar::Util qw( blessed ); use WebGUI::AssetBranch; use WebGUI::AssetClipboard; use WebGUI::AssetExportHtml; use WebGUI::AssetLineage; use WebGUI::AssetMetaData; use WebGUI::AssetPackage; use WebGUI::AssetTrash; use WebGUI::AssetVersioning; use strict; use Tie::IxHash; use WebGUI::AdminConsole; use WebGUI::Cache; use WebGUI::Form; use WebGUI::HTML; use WebGUI::HTMLForm; use WebGUI::Keyword; use WebGUI::Search::Index; use WebGUI::TabForm; use WebGUI::Utility; =head1 NAME Package WebGUI::Asset =head1 DESCRIPTION Package to manipulate items in WebGUI's asset system. =head1 SYNOPSIS An asset is the basic class of content in WebGUI. This handles security, urls, and other basic information common to all content items. A lineage is a concatenated series of sequence numbers, each six digits long, that explain an asset's position in its familiy tree. Lineage describes who the asset's ancestors are, how many ancestors the asset has in its family tree (lineage length), and the asset's position (rank) amongst its siblings. In addition, lineage provides enough information about an asset to generate a list of its siblings and descendants. use WebGUI::Asset; =head1 METHODS These methods are available from this class: =cut #------------------------------------------------------------------- =head2 addEditLabel ( ) Generate an internationalized label for the add/edit screens that says whether you're adding or editing an Asset, for clarity. =cut sub addEditLabel { my $self = shift; my $i18n = WebGUI::International->new($self->session,'Asset_Wobject'); my $addEdit = ($self->session->form->process("func") eq 'add') ? $i18n->get('add') : $i18n->get('edit'); return $addEdit.' '.$self->getName; } #------------------------------------------------------------------- =head2 addMissing ( url ) Displays a message to the admin that they have requested a non-existent page and give them an option to create it. =head3 url The missing URL. =cut sub addMissing { my $self = shift; my $assetUrl = shift; return unless ($self->session->var->isAdminOn); my $ac = $self->getAdminConsole; my $i18n = WebGUI::International->new($self->session, "Asset"); my $output = $i18n->get("missing page query"); $output .= '
'; return $ac->render($output); } #------------------------------------------------------------------- =head2 assetDbProperties ( session, assetId, className, revisionDate ) Class method to return all properties in all tables used by a particular Asset. Returns a hash ref with data from the table. =head3 session A reference to the current session. =head3 assetId The assetId of the asset you're creating an object reference for. Must not be blank. =head3 className By default we'll use whatever class it is called by like WebGUI::Asset::File->new(), so WebGUI::Asset::File would be used. =head3 revisionDate An epoch date that represents a specific version of an asset. =cut sub assetDbProperties { my $class = shift; my $session = shift; my ($assetId, $className, $revisionDate) = @_; my $sql = "select * from asset"; my $where = " where asset.assetId=?"; my $placeHolders = [$assetId]; foreach my $definition (@{$className->definition($session)}) { $sql .= ",".$definition->{tableName}; $where .= " and (asset.assetId=".$definition->{tableName}.".assetId and ".$definition->{tableName}.".revisionDate=".$revisionDate.")"; } return $session->db->quickHashRef($sql.$where, $placeHolders); } #------------------------------------------------------------------- =head2 assetExists ( session, assetId, className, revisionDate ) Class method that checks to see if an asset exists in all the proper tables for the requested asset class. Returns true or false. =head3 session A reference to the current session. =head3 assetId The assetId of the asset you're creating an object reference for. Must not be blank. =head3 className By default we'll use whatever class it is called by like WebGUI::Asset::File->new(), so WebGUI::Asset::File would be used. =head3 revisionDate An epoch date that represents a specific version of an asset. =cut sub assetExists { my $class = shift; my $session = shift; my ($assetId, $className, $revisionDate) = @_; my $dbProperties = $class->assetDbProperties($session, $assetId, $className, $revisionDate); return exists $dbProperties->{assetId}; } #------------------------------------------------------------------- =head2 canAdd ( session, [userId, groupId] ) Verifies that the user has the privileges necessary to add this type of asset. Return a boolean. =head3 session The session variable. =head3 userId Unique hash identifier for a user. If not supplied, current user. =head3 groupId Only developers extending this method should use this parameter. By default WebGUI will check groups in this order, whichever is defined: Group id assigned in the config file for each asset. Group assigned by the developer in the asset itself if s/he extended this method to do so. The "turn admin on" group which is group id 12. =cut sub canAdd { my $className = shift; my $session = shift; my $userId = shift || $session->user->userId; my $user = WebGUI::User->new($session, $userId); my $subclassGroupId = shift; my $addPrivs = $session->config->get("assetAddPrivilege"); my $groupId = $addPrivs->{$className} || $subclassGroupId || '12'; return $user->isInGroup($groupId); } #------------------------------------------------------------------- =head2 canEdit ( [userId] ) Verifies group and user permissions to be able to edit asset. Returns 1 if owner is userId, otherwise returns the result checking if the user is a member of the group that can edit. =head3 userId Unique hash identifier for a user. If not supplied, current user. =cut sub canEdit { my $self = shift; my $userId = shift || $self->session->user->userId; my $user = WebGUI::User->new($self->session, $userId); if ($userId eq $self->get("ownerUserId")) { return 1; } return $user->isInGroup($self->get("groupIdEdit")); } #------------------------------------------------------------------- =head2 canView ( [userId] ) Verifies group and user permissions to be able to view asset. Returns 1 if user is owner of asset. Returns 1 if within the visibility date range of the asset AND user in the View group of asset. Otherwise, returns the result of the canEdit. Only the owner and the editors can always see the asset, regardless of time/date restrictions on the asset. =head3 userId Unique hash identifier for a user. If not specified, uses current userId. =cut sub canView { my $self = shift; my $eh = $self->session->errorHandler; my $userId = shift; my $user; if (defined $userId) { $user = WebGUI::User->new($self->session, $userId); } else { $user = $self->session->user; $userId = $user->userId(); } if ($userId eq $self->get("ownerUserId")) { return 1; } elsif ($user->isInGroup($self->get("groupIdView"))) { return 1; } return $self->canEdit($userId); } #------------------------------------------------------------------- =head2 checkView ( ) Returns error messages if a user can't view due to publishing problems, otherwise it sets the cookie and returns undef. This is sort of a hack until we find something better. =cut sub checkView { my $self = shift; return $self->session->privilege->noAccess() unless $self->canView; my ($conf, $env, $var, $http) = $self->session->quick(qw(config env var http)); if ($conf->get("sslEnabled") && $self->get("encryptPage") && $env->get("HTTPS") ne "on" && !$env->get("SSLPROXY")) { # getUrl already changes url to https if 'encryptPage' $http->setRedirect($self->getUrl); return "redirect"; } elsif ($var->isAdminOn && $self->get("state") =~ /^trash/) { # show em trash $http->setRedirect($self->getUrl("func=manageTrash")); return "redirect"; } elsif ($var->isAdminOn && $self->get("state") =~ /^clipboard/) { # show em clipboard $http->setRedirect($self->getUrl("func=manageClipboard")); return "redirect"; } elsif ($self->get("state") ne "published") { # tell em it doesn't exist anymore $http->setStatus("410"); my $notFound = WebGUI::Asset->getNotFound($self->session); $self->session->asset($notFound); return $notFound->www_view; } $self->logView(); return; } #------------------------------------------------------------------- =head2 definition ( session, [ definition ] ) Basic definition of an Asset. Properties, default values. Returns an array reference containing tableName,className,properties =head3 session The current session object. =head3 definition An array reference containing additional information to include with the default definition. =cut sub definition { my $class = shift; my $session = shift; my $definition = shift || []; my $i18n = WebGUI::International->new($session, "Asset"); my %properties; tie %properties, 'Tie::IxHash'; %properties = ( title=>{ tab=>"properties", label=>$i18n->get(99), hoverHelp=>$i18n->get('99 description'), fieldType=>'text', defaultValue=>'Untitled', filter=>'fixTitle', }, menuTitle=>{ tab=>"properties", label=>$i18n->get(411), hoverHelp=>$i18n->get('411 description'), uiLevel=>1, fieldType=>'text', filter=>'fixTitle', defaultValue=>'Untitled', }, url=>{ tab=>"properties", label=>$i18n->get(104), hoverHelp=>$i18n->get('104 description'), uiLevel=>3, fieldType=>'text', defaultValue=>'', filter=>'fixUrl' }, isHidden=>{ tab=>"display", label=>$i18n->get(886), hoverHelp=>$i18n->get('886 description'), uiLevel=>6, fieldType=>'yesNo', defaultValue=>0, }, newWindow=>{ tab=>"display", label=>$i18n->get(940), hoverHelp=>$i18n->get('940 description'), uiLevel=>9, fieldType=>'yesNo', defaultValue=>0 }, encryptPage=>{ fieldType => ($session->config->get("sslEnabled") ? 'yesNo' : 'hidden'), tab => "security", label => $i18n->get('encrypt page'), hoverHelp => $i18n->get('encrypt page description'), uiLevel => 6, defaultValue => 0, }, ownerUserId=>{ tab=>"security", label=>$i18n->get(108), hoverHelp=>$i18n->get('108 description'), uiLevel=>6, fieldType=>'user', filter=>'fixId', defaultValue=>'3' }, groupIdView=>{ tab=>"security", label=>$i18n->get(872), hoverHelp=>$i18n->get('872 description'), uiLevel=>6, fieldType=>'group', filter=>'fixId', defaultValue=>'7' }, groupIdEdit=>{ tab=>"security", label=>$i18n->get(871), excludeGroups=>[1,7], hoverHelp=>$i18n->get('871 description'), uiLevel=>6, fieldType=>'group', filter=>'fixId', defaultValue=>'4' }, synopsis=>{ tab=>"meta", label=>$i18n->get(412), hoverHelp=>$i18n->get('412 description'), uiLevel=>3, fieldType=>'textarea', defaultValue=>undef }, extraHeadTags=>{ tab=>"meta", label=>$i18n->get("extra head tags"), hoverHelp=>$i18n->get('extra head tags description'), uiLevel=>5, fieldType=>'textarea', defaultValue=>undef }, isPackage=>{ label=>$i18n->get("make package"), tab=>"meta", hoverHelp=>$i18n->get('make package description'), uiLevel=>7, fieldType=>'yesNo', defaultValue=>0 }, isPrototype=>{ tab=>"meta", label=>$i18n->get("make prototype"), hoverHelp=>$i18n->get('make prototype description'), uiLevel=>9, fieldType=>'yesNo', defaultValue=>0 }, status=>{ noFormPost=>1, fieldType=>'hidden', defaultValue=>'pending' }, assetSize=>{ noFormPost=>1, fieldType=>'hidden', defaultValue=>0 }, ); push(@{$definition}, { assetName=>$i18n->get("asset"), tableName=>'assetData', autoGenerateForms=>1, className=>'WebGUI::Asset', icon=>'assets.gif', properties=>\%properties } ); return $definition; } #------------------------------------------------------------------- =head2 DESTROY ( ) Completely remove an asset from existence. =cut sub DESTROY { my $self = shift; # something bad happens when the following is enabled, not sure why # must check this out later #$self->{_parent}->DESTROY if (exists $self->{_parent}); $self->{_firstChild}->DESTROY if (exists $self->{_firstChild}); $self->{_lastChild}->DESTROY if (exists $self->{_lastChild}); $self = undef; } #------------------------------------------------------------------- =head2 fixId ( id, fieldName ) Returns the default Id for a field if we get an invalid Id, otherwise returns the id passed in. An valid id either looks like a GUID or is an integer. =head3 id The id to check. =head3 fieldName The name of the property we're checking. This is used to retrieve whatever the default is set to in the definition. =cut sub fixId { my $self = shift; my $id = shift; my $field = shift; if ($id =~ m/\A \d+ \z/xms || $id =~ m/\A [A-Za-z0-9\-\_]{22} \z/xms) { return $id; } return $self->getValue($field); } #------------------------------------------------------------------- =head2 fixTitle ( string ) Fixes a title by eliminating HTML from it. =head3 string Any text string. Most likely will have been the Asset's name or title. =cut sub fixTitle { my $self = shift; my $string = shift; if (lc($string) eq "untitled" || $string eq "") { $string = $self->getValue("title") || 'Untitled'; } return WebGUI::HTML::filter($string, 'all'); } #------------------------------------------------------------------- =head2 fixUrl ( string ) Returns a URL, removing invalid characters and making it unique by adding a digit to the end if necessary. URLs are not allowed to be children of the extrasURL, the uploadsURL, or any defined passthruURL. If not URL is passed, a URL will be constructed from the Asset's parent and the menuTitle. Assets have a maximum length of 250 characters. Any URL longer than 250 characters will be truncated to the initial 220 characters. URLs will be passed through $session->url->urlize to make them WebGUI compliant. That includes any languages specific constraints set up in the default language pack. =head3 string Any text string. Most likely will have been the Asset's name or title. =cut sub fixUrl { my $self = shift; my $url = shift; # build a URL from the parent unless ($url) { $url = $self->getParent->get("url"); $url =~ s/(.*)\..*/$1/; $url .= '/'.$self->getValue("menuTitle"); } $url = $self->session->url->urlize($url); # fix urls used by uploads and extras # and those beginning with http my @badUrls = ( $self->session->config->get("extrasURL"), $self->session->config->get("uploadsURL"), ); foreach my $badUrl (@badUrls) { $badUrl =~ s{ / $ }{}x; # Remove trailing slashes from the end of the URL if ($badUrl =~ /^http/) { $badUrl =~ s{ ^ http .* / } {}x; #Remove everything but the final path fragment from the badUrl } else { $badUrl =~ s{ ^ / }{}x; #Remove leading slashes from bare URLs } if ($url =~ /^$badUrl/) { $url = "_".$url; } } # urls can't be longer than 250 characters if (length($url) > 250) { $url = substr($url,0,220); } # remove multiple extensions from the url if there are some $url =~ s{ (\.\w+)* # Strip off any number of extensions (?=/) # Followed by a slash }{}xg; # And delete all of them in the string # add automatic extension if we're supposed to if ($self->session->setting->get("urlExtension") ne "" #don't add an extension if one isn't set && !($url =~ /\./) # don't add an extension of the url already contains a dot && lc($self->get("url")) eq lc($self->getId) # only add it if we're creating a new url && $url ne lc($self->getId) # but don't assign it the original time ) { $url .= ".".$self->session->setting->get("urlExtension"); } # check to see if the url already exists or not, and increment it if it does if ($self->urlExists($self->session, $url, {assetId=>$self->getId})) { my @parts = split(/\./,$url); if ($parts[0] =~ /(.*)(\d+$)/) { $parts[0] = $1.($2+1); } else { $parts[0] .= "2"; } $url = join(".",@parts); $url = $self->fixUrl($url); } return $url; } #------------------------------------------------------------------- =head2 get ( [propertyName] ) Returns a reference to a list of properties (or specified property) of an Asset. =head3 propertyName Any of the values associated with the properties of an Asset. Default choices are "title", "menutTitle", "synopsis", "url", "groupIdEdit", "groupIdView", "ownerUserId", "keywords", and "assetSize". =cut sub get { my $self = shift; my $propertyName = shift; if (defined $propertyName) { if ($propertyName eq "keywords") { return WebGUI::Keyword->new($self->session)->getKeywordsForAsset({asset => $self}); } return $self->{_properties}{$propertyName}; } my %copyOfHashRef = %{$self->{_properties}}; return \%copyOfHashRef; } #------------------------------------------------------------------- =head2 getAdminConsole ( ) Returns a reference to a WebGUI::AdminConsole object. =cut sub getAdminConsole { my $self = shift; unless (exists $self->{_adminConsole}) { $self->{_adminConsole} = WebGUI::AdminConsole->new($self->session,"assets"); } $self->{_adminConsole}->setIcon($self->getIcon); return $self->{_adminConsole}; } #------------------------------------------------------------------- =head2 getAssetAdderLinks ( [addToUrl, type] ) Returns an arrayref that contains a label (name of the class of Asset) and url (url link to function to add the class). =head3 addToUrl Any text to append to the getAssetAdderLinks URL. Usually name/variable pairs to pass in the url. If addToURL is specified, the character ";" and the text in addToUrl is appended to the returned url. =head3 type A string indicating which type of adders to return. Defaults to "assets". Choose from "assets", "assetContainers", or "utilityAssets". =cut sub getAssetAdderLinks { my $self = shift; my $addToUrl = shift; my $type = shift || "assets"; my %links; my $classesInType = $self->session->config->get($type); if (ref $classesInType ne "ARRAY") { $classesInType = []; } foreach my $class (@{$classesInType}) { next unless $class; my %properties = ( className=>$class, dummy=>1 ); my $newAsset = WebGUI::Asset->newByPropertyHashRef($self->session,\%properties); next unless $newAsset; my $uiLevel = eval{$newAsset->getUiLevel()}; if ($@) { $self->session->errorHandler->error("Couldn't get UI level of ".$class.". Root cause: ".$@); next; } next if ($uiLevel > $self->session->user->profileField("uiLevel"));# && !$self->session->user->isInGroup(3)); my $canAdd = eval{$class->canAdd($self->session)}; if ($@) { $self->session->errorHandler->error("Couldn't determine if user can add ".$class." because ".$@); next; } next unless ($canAdd); my $label = eval{$newAsset->getName()}; if ($@) { $self->session->errorHandler->error("Couldn't get the name of ".$class."because ".$@); next; } my $url = $self->getUrl("func=add;class=".$class); $url = $self->session->url->append($url,$addToUrl) if ($addToUrl); $links{$label}{url} = $url; $links{$label}{icon} = $newAsset->getIcon; $links{$label}{'icon.small'} = $newAsset->getIcon(1); } my $constraint; if ($type eq "assetContainers") { $constraint = $self->session->db->quoteAndJoin($self->session->config->get("assetContainers")); } elsif ($type eq "utilityAssets") { $constraint = $self->session->db->quoteAndJoin($self->session->config->get("utilityAssets")); } else { $constraint = $self->session->db->quoteAndJoin($self->session->config->get("assets")); } if ($constraint) { my $sth = $self->session->db->read("select asset.className,asset.assetId,assetData.revisionDate from asset left join assetData on asset.assetId=assetData.assetId where assetData.isPrototype=1 and asset.state='published' and asset.className in ($constraint) and assetData.revisionDate=(SELECT max(revisionDate) from assetData where assetData.assetId=asset.assetId) group by assetData.assetId"); while (my ($class,$id,$date) = $sth->array) { my $asset = WebGUI::Asset->new($self->session,$id,$class,$date); next unless ($asset->canView && $asset->canAdd($self->session) && $asset->getUiLevel <= $self->session->user->profileField("uiLevel")); my $url = $self->getUrl("func=add;class=".$class.";prototype=".$id); $url = $self->session->url->append($url,$addToUrl) if ($addToUrl); $links{$asset->getTitle}{url} = $url; $links{$asset->getTitle}{icon} = $asset->getIcon; $links{$asset->getTitle}{'icon.small'} = $asset->getIcon(1); $links{$asset->getTitle}{'isPrototype'} = 1; $links{$asset->getTitle}{'asset'} = $asset; } $sth->finish; } my @sortedLinks; foreach my $label (sort keys %links) { push(@sortedLinks,{ label=>$label, url=>$links{$label}{url}, icon=>$links{$label}{icon}, 'icon.small'=>$links{$label}{'icon.small'}, isPrototype=>$links{$label}{isPrototype}, asset=>$links{$label}{asset} }); } return \@sortedLinks; } #------------------------------------------------------------------- =head2 getContainer ( ) Returns a reference to the container asset. If this asset is a container it returns a reference to itself. If this asset is not attached to a container it returns its parent. =cut sub getContainer { my $self = shift; if (WebGUI::Utility::isIn($self->get("className"), @{$self->session->config->get("assetContainers")})) { return $self; } else { # $self->session->asset($self->getParent); return $self->getParent; } } #------------------------------------------------------------------- =head2 getDefault ( session ) Constructor. Returns the default object, which is also known by some as the "Home Page". The default object is set in the settings. =head3 session A reference to the current session. =cut sub getDefault { my $class = shift; my $session = shift; return $class->newByDynamicClass($session, $session->setting->get("defaultPage")); } #------------------------------------------------------------------- =head2 getEditTabs () Returns a list of arrayrefs, one per extra tab to add to the edit form. The default is no extra tabs. Override this in a subclass to add extra tabs. =cut sub getEditTabs { my $self = shift; return (); } #------------------------------------------------------------------- =head2 getEditForm () Creates and returns a tabform to edit parameters of an Asset. =cut sub getEditForm { my $self = shift; my $i18n = WebGUI::International->new($self->session, "Asset"); my $ago = $i18n->get("ago"); my $rs = $self->session->db->read("select revisionDate from assetData where assetId=? order by revisionDate desc limit 5", [$self->getId]); my $uiLevelOverride = $self->get("className"); $uiLevelOverride =~ s/\:\:/_/g; my $tabform = WebGUI::TabForm->new($self->session,undef,undef,$self->getUrl(),$uiLevelOverride); if ($self->session->config->get("enableSaveAndCommit")) { $tabform->submitAppend(WebGUI::Form::submit($self->session, { name => "saveAndCommit", value => $i18n->get("save and commit"), })); } $tabform->hidden({ name=>"func", value=>"editSave" }); if ($self->getId eq "new") { $tabform->hidden({ name=>"assetId", value=>"new" }); $tabform->hidden({ name=>"class", value=>$self->session->form->process("class","className") }); } else { my $ac = $self->getAdminConsole; $ac->addSubmenuItem($self->getUrl("func=manageRevisions"),$i18n->get("revisions").":"); while (my ($version) = $rs->array) { my ($interval, $units) = $self->session->datetime->secondsToInterval(time() - $version); $ac->addSubmenuItem($self->getUrl("func=edit;revision=".$version), $interval." ".$units." ".$ago); } } if ($self->session->form->process("proceed")) { $tabform->hidden({ name=>"proceed", value=>$self->session->form->process("proceed") }); } # create tabs $tabform->addTab("properties",$i18n->get("properties")); $tabform->addTab("display",$i18n->get(105),5); $tabform->addTab("security",$i18n->get(107),6); $tabform->addTab("meta",$i18n->get("Metadata"),3); # process errors my $errors = $self->session->stow->get('editFormErrors'); if ($errors) { $tabform->getTab("properties")->readOnly( -value=>"Some error(s) occurred:
'. $i18n->get('Add new field'). '
', -hoverHelp=>$i18n->get('Add new field description'), ); } } return $tabform; } #------------------------------------------------------------------- =head2 getExtraHeadTags ( ) Returns the extraHeadTags stored in the asset. Called in $self->session->style->generateAdditionalHeadTags if this asset is the current session asset. Also called in WebGUI::Layout::view for its child assets. Overriden in Shortcut.pm. =cut sub getExtraHeadTags { my $self = shift; return $self->get("extraHeadTags"); } #------------------------------------------------------------------- =head2 getIcon ( [small] ) Returns the icon located under extras/adminConsole/assets.gif =head3 small If this evaluates to True, then the smaller extras/adminConsole/small/assets.gif is returned. =cut sub getIcon { my $self = shift; my $small = shift; my $definition = $self->definition($self->session); my $icon = $definition->[0]{icon} || "assets.gif"; return $self->session->url->extras('assets/small/'.$icon) if ($small); return $self->session->url->extras('assets/'.$icon); } #------------------------------------------------------------------- =head2 getId ( ) Returns the assetId of an Asset. =cut sub getId { my $self = shift; return $self->get("assetId"); } #------------------------------------------------------------------- =head2 getImportNode ( session ) Constructor. Returns the import node asset object. This is where developers will templates, files, etc to the asset tree that have no other obvious attachment point. =head3 session A reference to the current session. =cut sub getImportNode { my $class = shift; my $session = shift; return WebGUI::Asset->newByDynamicClass($session, "PBasset000000000000002"); } #------------------------------------------------------------------- =head2 getMedia ( session ) Constructor. Returns the media folder. =head3 session A reference to the current session. =cut sub getMedia { my $class = shift; my $session = shift; return WebGUI::Asset->newByDynamicClass($session, "PBasset000000000000003"); } #------------------------------------------------------------------- =head2 getMenuTitle ( ) Returns the menu title of this asset. If it's not specified or it's "Untitled" then the asset's name will be returned instead. =cut sub getMenuTitle { my $self = shift; if ($self->get("menuTitle") eq "" || lc($self->get("menuTitle")) eq "untitled") { return $self->getName; } return $self->get("menuTitle"); } #------------------------------------------------------------------- =head2 getName ( ) Returns the internationalization of the word "Asset". =cut sub getName { my $self = shift; my $definition = $self->definition($self->session); return $definition->[0]{assetName}; } #------------------------------------------------------------------- =head2 getNotFound ( session ) Constructor. Returns the not found object. The not found object is set in the settings. =head3 session A reference to the current session. =cut sub getNotFound { my $class = shift; my $session = shift; return WebGUI::Asset->newByDynamicClass($session, $session->setting->get("notFoundPage")); } #------------------------------------------------------------------- =head2 getRoot ( session ) Constructor. Returns the root asset object. =head3 session A reference to the current session. =cut sub getRoot { my $class = shift; my $session = shift; return WebGUI::Asset->new($session, "PBasset000000000000001"); } #------------------------------------------------------------------- =head2 getTempspace ( session ) Constructor. Returns the tempspace folder. =head3 session A reference to the current session. =cut sub getTempspace { my $class = shift; my $session = shift; return WebGUI::Asset->newByDynamicClass($session, "tempspace0000000000000"); } #------------------------------------------------------------------- =head2 getTitle ( ) Returns the title of this asset. If it's not specified or it's "Untitled" then the asset's name will be returned instead. =cut sub getTitle { my $self = shift; if ($self->get("title") eq "" || lc($self->get("title")) eq "untitled") { return $self->getName; } return $self->get("title"); } #------------------------------------------------------------------- =head2 getToolbar ( ) Returns a toolbar with a set of icons that hyperlink to functions that delete, edit, promote, demote, cut, and copy. =cut sub getToolbar { my $self = shift; return unless $self->canEdit; return $self->{_toolbar} if (exists $self->{_toolbar}); my $userUiLevel = $self->session->user->profileField("uiLevel"); my $uiLevels = $self->session->config->get("assetToolbarUiLevel"); my $i18n = WebGUI::International->new($self->session, "Asset"); my $toolbar = ""; my $commit; if ($self->canEditIfLocked) { $toolbar .= $self->session->icon->delete('func=delete',$self->get("url"),$i18n->get(43)) if ($userUiLevel >= $uiLevels->{"delete"}); $toolbar .= $self->session->icon->edit('func=edit',$self->get("url")) if ($userUiLevel >= $uiLevels->{"edit"}); } else { $toolbar .= $self->session->icon->locked('func=manageRevisions',$self->get("url")) if ($userUiLevel >= $uiLevels->{"revisions"}); } $toolbar .= $self->session->icon->cut('func=cut',$self->get("url")) if ($userUiLevel >= $uiLevels->{"cut"}); # if this asset has children, create a more full-featured menu for copying if ($self->getChildCount) { my $copy = ''; $toolbar .= $copy; } else { $toolbar .= $self->session->icon->copy('func=copy',$self->get("url")) if ($userUiLevel >= $uiLevels->{"copy"}); } $toolbar .= $self->session->icon->shortcut('func=createShortcut',$self->get("url")) if ($userUiLevel >= $uiLevels->{"shortcut"} && !($self->get("className") =~ /Shortcut/)); $self->session->style->setLink($self->session->url->extras('contextMenu/contextMenu.css'), {rel=>"stylesheet",type=>"text/css"}); $self->session->style->setScript($self->session->url->extras('contextMenu/contextMenu.js'), {type=>"text/javascript"}); my $output = ''.$toolbar; return $output; } #------------------------------------------------------------------- =head2 getToolbarState ( ) Returns 0 if the state is normal, and 1 if the toolbar state has been toggled. See toggleToolbar() for details. =cut sub getToolbarState { my $self = shift; return $self->{_toolbarState}; } #------------------------------------------------------------------- =head2 getUiLevel ( ) Returns the UI Level specified in the asset definition or from the config file if it's overridden. And if neither of those is specified, then it returns 1. =cut sub getUiLevel { my $self = shift; my $definition = $self->get("className")->definition($self->session); my $uiLevel = $self->session->config->get("assetUiLevel"); if ($uiLevel && ref $uiLevel eq 'HASH') { return $uiLevel->{$definition->[0]{className}} || $definition->[0]{uiLevel} || 1 ; } else { return $definition->[0]{uiLevel} || 1 ; } } #------------------------------------------------------------------- =head2 getUrl ( params ) Returns a URL of Asset based upon WebGUI's gateway script. =head3 params Name value pairs to add to the URL in the form of: name1=value1;name2=value2;name3=value3 =cut sub getUrl { my $self = shift; my $params = shift; my $url = $self->get("url"); $url = $self->session->url->gateway($url,$params); if ($self->get("encryptPage")) { $url = $self->session->url->getSiteURL().$url; $url =~ s/http:/https:/; } return $url; } #------------------------------------------------------------------- =head2 getValue ( key ) Returns the value of anything it can find with an index of key, or else it returns undefined. =head3 key An asset property name, or a propertyDefinition. =cut sub getValue { my $self = shift; my $key = shift; if (defined $key) { my $storedValue = $self->get($key); return $storedValue if (defined $storedValue); unless (exists $self->{_propertyDefinitions}) { # check to see if the definitions have been merged and cached my %properties; foreach my $definition (@{$self->definition($self->session)}) { %properties = (%properties, %{$definition->{properties}}); } $self->{_propertyDefinitions} = \%properties; } return $self->{_propertyDefinitions}{$key}{defaultValue}; } return; } #------------------------------------------------------------------- =head2 indexContent ( ) Returns an indexer object for this asset. When this method is called the asset's base content gets stored in the index. This method is often overloaded so that a particular asset can insert additional content other than the basic properties. Such uses include indexing attached files or collateral data. =cut sub indexContent { my $self = shift; my $indexer = WebGUI::Search::Index->create($self); $indexer->setIsPublic(0) if ($self->getId eq "PBasset000000000000001"); return $indexer; } #------------------------------------------------------------------- =head2 loadModule ( $session, $className ) Loads an asset module if it's not already in memory. This is a class method. Returns undef on failure to load, otherwise returns the classname. =cut sub loadModule { my ($class, $session, $className) = @_; (my $module = $className . '.pm') =~ s{::|'}{/}g; if (eval { require $module; 1 }) { return $className; } $session->errorHandler->error("Couldn't compile asset package: ".$className.". Root cause: ".$@); return; } #------------------------------------------------------------------- =head2 logView ( ) Logs the view of this asset to the passive profiling mechanism. =cut sub logView { my $self = shift; if ($self->session->setting->get("passiveProfilingEnabled")) { WebGUI::PassiveProfiling::add($self->session,$self->getId); WebGUI::PassiveProfiling::addPage($self->session,$self->getId) if ($self->get("className") eq "WebGUI::Asset::Wobject::Layout"); } return; } #------------------------------------------------------------------- =head2 manageAssets ( ) Main page to manage assets. Renders an AdminConsole with a list of assets. If canEdit returns False, renders an insufficient privilege page. Is called by www_manageAssets =cut sub manageAssets { my $self = shift; my $i18n = WebGUI::International->new($self->session, "Asset"); my $userUILevel = $self->session->user->profileField("uiLevel"); my $uiLevels = $self->session->config->get("assetToolbarUiLevel"); my $ancestors = $self->getLineage(["self","ancestors"],{returnObjects=>1}); my @crumbtrail; foreach my $ancestor (@{$ancestors}) { if ($ancestor->getId eq $self->getId) { my $title = $self->getTitle; $title =~ s/\'/\\\'/g; my $more = '\n"; push(@crumbtrail,$more); } else { push(@crumbtrail,''.$ancestor->getTitle.''); } } my $output = '