From 50498b5743d2e8858aa84f4d5ec3f6cfe10985e0 Mon Sep 17 00:00:00 2001
From: Jon Clausen <jclausen@ortussolutions.com>
Date: Mon, 13 Jan 2025 13:23:15 -0500
Subject: [PATCH] add activeContentVersions RO property, add asString argument
 to getActiveContet to pull criteria projection of only content string

---
 .../contentbox/models/content/BaseContent.cfc | 64 +++++++++----------
 1 file changed, 31 insertions(+), 33 deletions(-)

diff --git a/modules/contentbox/models/content/BaseContent.cfc b/modules/contentbox/models/content/BaseContent.cfc
index e27d0fec0..4cc9b97ca 100644
--- a/modules/contentbox/models/content/BaseContent.cfc
+++ b/modules/contentbox/models/content/BaseContent.cfc
@@ -311,7 +311,7 @@ component
 		singularName="contentVersion"
 		fieldtype   ="one-to-many"
 		type        ="array"
-		lazy        ="true"
+		lazy        ="extra"
 		batchsize   ="25"
 		cfc         ="contentbox.models.content.ContentVersion"
 		orderby     ="version desc"
@@ -319,6 +319,20 @@ component
 		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"
@@ -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;
 	}
 
 	/**
@@ -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();
 	}
 
 	/**