From 0867129b3709454853e86f2571249fe607b68536 Mon Sep 17 00:00:00 2001 From: Ernesto Buenrostro Date: Thu, 8 Mar 2018 04:36:24 -0800 Subject: [PATCH] Adding fetch function to retrieve the related objects matching specific meta data (#6) --- classes/mugoobjectrelationsfetchfunctions.php | 94 +++++++++++++++++++ .../function_definition.php | 24 +++++ settings/module.ini.append.php | 7 ++ settings/site.ini | 6 ++ 4 files changed, 131 insertions(+) create mode 100644 classes/mugoobjectrelationsfetchfunctions.php create mode 100644 modules/mugoobjectrelations/function_definition.php create mode 100644 settings/module.ini.append.php create mode 100644 settings/site.ini diff --git a/classes/mugoobjectrelationsfetchfunctions.php b/classes/mugoobjectrelationsfetchfunctions.php new file mode 100644 index 0000000..123ac35 --- /dev/null +++ b/classes/mugoobjectrelationsfetchfunctions.php @@ -0,0 +1,94 @@ + + * {def $illustrations = fetch( 'mugoobjectrelations', 'filtered_relations', hash( + * 'attribute', $node.data_map.media, + * 'filter_by', hash( 'extra_fields', hash( 'meta_data_1', 'illustration' ) ), + * ) )} + * + * + * @param eZContentObjectAttribute $attribute + * @param array $filterBy Filter to use in the attribute meta data + * @return array + */ + public static function fetchFunctionFilteredRelations( eZContentObjectAttribute $attribute, array $filterBy ) + { + $result = array(); + + if ( $attribute->attribute( 'data_type_string' ) !== 'mugoobjectrelationlist' ) + { + // Unsupported attribute type + return $result; + } + + + $attributeContent = $attribute->attribute( 'content' ); + $relatedContentList = $attributeContent['relation_list']; + + if ( empty( $filterBy ) ) + { + // no filter, fetch all the related objects + $filteredRelations = $relatedContentList; + } + else + { + // Filtering the related objects + $filteredRelations = array_filter( $relatedContentList, function($relatedItem) use( $filterBy ) + { + $result = false; + foreach ( $filterBy as $key => $value ) + { + if ( isset( $relatedItem[$key] ) && $relatedItem[$key] ) + { + foreach ( $value as $filterKey => $filterValue ) + { + if ( isset( $relatedItem[$key][$filterKey] ) && $relatedItem[$key][$filterKey] && ($relatedItem[$key][$filterKey]['identifier'] === $filterValue) ) + { + $result = true; + } + } + } + + // If one of the fields match, no need to keep going + if ( $result ) + { + break; + } + } + + return $result; + } ); + } + + // Fetching the filtered related objects + foreach ( $filteredRelations as $relatedItem ) + { + $tmpObject = eZFunctionHandler::execute( 'content', 'object', array( + 'object_id' => $relatedItem['contentobject_id'] + ) ); /* @var $tmpObject eZContentObject */ + if ( $tmpObject instanceof eZContentObject ) + { + $result[] = $tmpObject; + } + unset( $tmpObject ); + } + + return array( 'result' => $result ); + } + +} diff --git a/modules/mugoobjectrelations/function_definition.php b/modules/mugoobjectrelations/function_definition.php new file mode 100644 index 0000000..4b18908 --- /dev/null +++ b/modules/mugoobjectrelations/function_definition.php @@ -0,0 +1,24 @@ + 'filtered_relations', + 'operation_types' => array( 'read' ), + 'call_method' => array( + 'class' => 'MugoObjectRelationsFetchFunctions', + 'method' => 'fetchFunctionFilteredRelations' ), + 'parameter_type' => 'standard', + 'parameters' => array( + array( + 'name' => 'attribute', + 'type' => 'object', + 'required' => true + ), + array( + 'name' => 'filter_by', + 'type' => 'array', + 'required' => true + ) + ) +); diff --git a/settings/module.ini.append.php b/settings/module.ini.append.php new file mode 100644 index 0000000..8bf0c6a --- /dev/null +++ b/settings/module.ini.append.php @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/settings/site.ini b/settings/site.ini new file mode 100644 index 0000000..960d9c9 --- /dev/null +++ b/settings/site.ini @@ -0,0 +1,6 @@ + \ No newline at end of file