Skip to content

Commit

Permalink
add activeContentVersions RO property, add asString argument to getAc…
Browse files Browse the repository at this point in the history
…tiveContet to pull criteria projection of only content string
  • Loading branch information
jclausen committed Jan 13, 2025
1 parent 156a874 commit 50498b5
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions modules/contentbox/models/content/BaseContent.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,28 @@ component
singularName="contentVersion"
fieldtype ="one-to-many"
type ="array"
lazy ="true"
lazy ="extra"
batchsize ="25"
cfc ="contentbox.models.content.ContentVersion"
orderby ="version desc"
fkcolumn ="FK_contentID"
inverse ="true"
cascade ="all-delete-orphan";

property
name ="activeContentVersions"
singularName="activeContentVersion"
fieldtype ="one-to-many"
type ="array"
lazy ="extra"
where ="isActive = 1"
batchsize ="2"
cfc ="contentbox.models.content.ContentVersion"
orderby ="version desc"
fkcolumn ="FK_contentID"
insert = false
update = false;

// M20 -> Parent Page loaded as a proxy
property
name ="parent"
Expand Down Expand Up @@ -1120,34 +1134,28 @@ component
* Retrieves the latest content string from the latest version un-translated
*/
string function getContent(){
return getActiveContent().getContent();
return getActiveContent( true );
}

/**
* Get the latest active version object, empty new one if none assigned
*/
ContentVersion function getActiveContent(){
any function getActiveContent( asString = false ){
// If we don't have any versions, send back a new one
if ( !hasContentVersion() ) {
return variables.contentVersionService.new();
}

// Load up the active content if not set yet
if ( isNull( variables.activeContent ) ) {
// Iterate and find, they are sorted descending, so it should be quick, unless we don't have one and that's ok.
for ( var thisVersion in variables.contentVersions ) {
if ( thisVersion.getIsActive() ) {
variables.activeContent = thisVersion;
break;
}
}
// We didn't find one, something is out of sync, return just an empty version
if ( isNull( variables.activeContent ) ) {
return variables.contentVersionService.new();
}
if ( !hasActiveContentVersion() ) {
return arguments.asString ? "" : variables.contentVersionService.new();
} else if( arguments.asString ) {
var activeContentStruct = contentVersionService.newCriteria()
.isEq( "relatedContent.contentID", getContentID() )
.isEq( "isActive", javacast( "boolean", true ) )
.withProjections( property="content" )
.asStruct()
.order( "version", "desc" )
.list( max=1 ).first();
return activeContentStruct[ "content" ];
} else {
return getActiveContentVersions().first();
}

return variables.activeContent;
}

/**
Expand All @@ -1164,17 +1172,7 @@ component
* Verify if this content object has an active version with content
*/
boolean function hasActiveContent(){
// If we are not persisted, then no exit out.
if ( !hasContentVersion() || !isLoaded() ) {
return false;
}
// Iterate and find, they are sorted descending, so it should be quick, unless we don't have one and that's ok.
for ( var thisVersion in variables.contentVersions ) {
if ( thisVersion.getIsActive() ) {
return true;
}
}
return false;
return !isLoaded() || !hasActiveContentVersion();
}

/**
Expand Down

0 comments on commit 50498b5

Please sign in to comment.