From 5fcfacaf62d46589f74f2729f91ab74b275a5b4e Mon Sep 17 00:00:00 2001 From: Peter Keung Date: Wed, 19 Sep 2018 05:01:44 -0700 Subject: [PATCH] Fix EZP-29600: eZ Flow pool table not updated on node swap (#81) (cherry picked from commit 70a896460babb7b7a624e3fae9be46657a675473) --- .../event/ezpageswap/ezpageswaptype.php | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/packages/ezflow_extension/ezextension/ezflow/eventtypes/event/ezpageswap/ezpageswaptype.php b/packages/ezflow_extension/ezextension/ezflow/eventtypes/event/ezpageswap/ezpageswaptype.php index 9c3c1248..0f936d40 100644 --- a/packages/ezflow_extension/ezextension/ezflow/eventtypes/event/ezpageswap/ezpageswaptype.php +++ b/packages/ezflow_extension/ezextension/ezflow/eventtypes/event/ezpageswap/ezpageswaptype.php @@ -40,6 +40,8 @@ function __construct() function execute( $process, $event ) { + $db = eZDB::instance(); + $db->begin(); $parameters = $process->attribute( 'parameter_list' ); $oldNodeID = $parameters['node_id']; @@ -62,7 +64,68 @@ function execute( $process, $event ) $newNodeBlock->setAttribute( 'node_id', $oldNodeID ); $newNodeBlock->store(); } + + // Get the object IDs to update the pool items + $oldNode = eZContentObjectTreeNode::fetch( $oldNodeID ); + $oldNodeObjectID = $oldNode->attribute( 'contentobject_id' ); + $newNode = eZContentObjectTreeNode::fetch( $newNodeID ); + $newNodeObjectID = $newNode->attribute( 'contentobject_id' ); + // Update with the new object IDs + // Object IDs are keys, so we have to watch out for the case where the swapped nodes are part of the same block, in which case we need to swap the node IDs instead + $swappedBlockIDs = array(); + $oldNodePoolItems = eZFlowPoolItem::fetchObjectList( eZFlowPoolItem::definition(), null, array( 'node_id' => $oldNodeID ) ); + $newNodePoolItems = eZFlowPoolItem::fetchObjectList( eZFlowPoolItem::definition(), null, array( 'node_id' => $newNodeID ) ); + + // Loop over fetched pool items for old node + foreach( $oldNodePoolItems as $oldNodePoolItem ) + { + // Check if the swapped node is part of the same block + $swappedNodePoolItem = eZFlowPoolItem::fetchObjectList( eZFlowPoolItem::definition(), null, array( 'node_id' => $newNodeID, 'block_id' => $oldNodePoolItem->attribute( 'block_id' ) ) ); + if( empty( $swappedNodePoolItem ) ) + { + $updateParameters = array( + 'definition' => eZFlowPoolItem::definition(), + 'update_fields' => array( 'object_id' => $oldNodeObjectID ), + 'conditions' => array( + 'object_id' => $oldNodePoolItem->attribute( 'object_id' ), + 'block_id' => $oldNodePoolItem->attribute( 'block_id' ) + ) + ); + eZFlowPoolItem::updateObjectList( $updateParameters ); + } + else + { + // Swap node IDs + $oldNodePoolItem->setAttribute( 'node_id', $newNodeID ); + $oldNodePoolItem->store(); + $swappedNodePoolItem[0]->setAttribute( 'node_id', $oldNodeID ); + $swappedNodePoolItem[0]->store(); + // Do not process this block ID again + $swappedBlockIDs[] = $oldNodePoolItem->attribute( 'block_id' ); + } + } + + // Loop over fetched pool items for new node + foreach( $newNodePoolItems as $newNodePoolItem ) + { + // Don't process this block if both nodes IDs were already updated + if( in_array( $newNodePoolItem->attribute( 'block_id' ), $swappedBlockIDs ) ) + { + continue; + } + $updateParameters = array( + 'definition' => eZFlowPoolItem::definition(), + 'update_fields' => array( 'object_id' => $newNodeObjectID ), + 'conditions' => array( + 'object_id' => $newNodePoolItem->attribute( 'object_id' ), + 'block_id' => $newNodePoolItem->attribute( 'block_id' ) + ) + ); + eZFlowPoolItem::updateObjectList( $updateParameters ); + } + + $db->commit(); return eZWorkflowType::STATUS_ACCEPTED; } }