From 472a03103a1ea9f36842ffb0a8cc925c3364277e Mon Sep 17 00:00:00 2001
From: Petr Mifek <petr.mifek@anapol.cz>
Date: Sun, 11 May 2014 17:29:17 +0200
Subject: [PATCH] Proposed fix for https://jira.ez.no/browse/EZP-22844, allows
 limiting metaData gathering traversal length through related objects,
 implemented for ezobjectrelationlist.

---
 .../ezobjectrelationlisttype.php                   | 43 ++++++++++++++++++++++
 kernel/classes/ezcontentobjectattribute.php        | 14 +++++--
 2 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/kernel/classes/datatypes/ezobjectrelationlist/ezobjectrelationlisttype.php b/kernel/classes/datatypes/ezobjectrelationlist/ezobjectrelationlisttype.php
index a3fae9f..53cbbb7 100644
--- a/kernel/classes/datatypes/ezobjectrelationlist/ezobjectrelationlisttype.php
+++ b/kernel/classes/datatypes/ezobjectrelationlist/ezobjectrelationlisttype.php
@@ -1512,6 +1512,49 @@ class eZObjectRelationListType extends eZDataType
     }
 
     /*!
+     Returns the meta data used for storing search indexes with related objects nesting level limit
+    the same function as metaData basically, just passing the level limit along and stopping if it is 0.
+    */
+    function metaDataLimited( $contentObjectAttribute, $relationsLevelLimit=-1 )
+    {
+        if ($relationsLevelLimit == 0) {
+            return array();
+        }
+        $relationsLevelLimit--;
+
+        $metaDataArray = $attributes = array();
+        $content = $contentObjectAttribute->content();
+        $language = $contentObjectAttribute->attribute( 'language_code' );
+        foreach( $content['relation_list'] as $relationItem )
+        {
+            $subObjectID = $relationItem['contentobject_id'];
+            if ( !$subObjectID )
+                continue;
+
+            if ( isset( $content['temp'] ) )
+                $attributes = $content['temp'][$subObjectID]['attributes'];
+            else
+            {
+                $subObjectVersion = $relationItem['contentobject_version'];
+                $object = eZContentObject::fetch( $subObjectID );
+                if ( eZContentObject::recursionProtect( $subObjectID ) )
+                {
+                    if ( !$object )
+                    {
+                        continue;
+                    }
+                    $attributes = $object->contentObjectAttributes( true, $subObjectVersion, $language );
+                }
+            }
+
+            $attributeMetaDataArray = eZContentObjectAttribute::metaDataArray( $attributes, $relationsLevelLimit );
+            $metaDataArray = array_merge( $metaDataArray, $attributeMetaDataArray );
+        }
+
+        return $metaDataArray;
+    }
+
+    /*!
      \return string representation of an contentobjectattribute data for simplified export
 
     */
diff --git a/kernel/classes/ezcontentobjectattribute.php b/kernel/classes/ezcontentobjectattribute.php
index 190efda..27ceb99 100644
--- a/kernel/classes/ezcontentobjectattribute.php
+++ b/kernel/classes/ezcontentobjectattribute.php
@@ -1122,12 +1122,18 @@ class eZContentObjectAttribute extends eZPersistentObject
     /*!
      Returns the metadata. This is the pure content of the attribute used for
      indexing data for search engines.
+
+    $relationsLevelLimit - counter how many levels deep in related objects should we descent
      */
-    function metaData()
+    function metaData($relationsLevelLimit=-1)
     {
         $dataType = $this->dataType();
         if ( $dataType )
-            return $dataType->metaData( $this );
+            if (method_exists($dataType, 'metaDataLimited')) {
+                return $dataType->metaDataLimited( $this, $relationsLevelLimit);
+            } else {
+                return $dataType->metaData( $this );
+            }
         else
             return false;
     }
@@ -1162,7 +1168,7 @@ class eZContentObjectAttribute extends eZPersistentObject
      Goes trough all attributes and fetches metadata for the ones that is searchable.
      \return an array with metadata information.
     */
-    static function metaDataArray( &$attributes )
+    static function metaDataArray( &$attributes, $relationsLevelLimit=-1 )
     {
         $metaDataArray = array();
         if ( !is_array( $attributes ) )
@@ -1172,7 +1178,7 @@ class eZContentObjectAttribute extends eZPersistentObject
             $classAttribute = $attribute->contentClassAttribute();
             if ( $classAttribute->attribute( 'is_searchable' ) )
             {
-                $attributeMetaData = $attribute->metaData();
+                $attributeMetaData = $attribute->metaData($relationsLevelLimit);
                 if ( $attributeMetaData !== false )
                 {
                     if ( !is_array( $attributeMetaData ) )
-- 
1.8.4.2

