Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sticky object relations #1271

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,14 @@ function createNewObject( $contentObjectAttribute, $name )
return $newObjectInstance->attribute( 'id' );
}

/**
* @param eZContentObjectAttribute $attribute
* @return bool
*/
function storeObjectAttribute( $attribute )
{
$content = $attribute->content();

if ( isset( $content['new_object'] ) )
{
$newID = $this->createNewObject( $attribute, $content['new_object'] );
Expand All @@ -403,66 +408,111 @@ function storeObjectAttribute( $attribute )
$attribute->setContent( $content );
}

$contentClassAttributeID = $attribute->ContentClassAttributeID;
$contentObjectID = $attribute->ContentObjectID;
$contentObjectVersion = $attribute->Version;

$obj = $attribute->object();
//get eZContentObjectVersion
$currVerobj = $obj->version( $contentObjectVersion );

// create translation List
// $translationList will contain for example eng-GB, ita-IT etc.
$translationList = $currVerobj->translations( false );

// get current language_code
$langCode = $attribute->attribute( 'language_code' );
// get count of LanguageCode in translationList
$countTsl = count( $translationList );
// order by asc
sort( $translationList );

// check if previous relation(s) should first be removed
if ( !$attribute->contentClassAttributeCanTranslate() )
{
$obj->removeContentObjectRelation( false, $contentObjectVersion, $contentClassAttributeID, eZContentObject::RELATION_ATTRIBUTE );
}
$this->updateObjectRelations( $attribute );

foreach( $content['relation_list'] as $relationItem )
{
// Installing content object, postUnserialize is not called yet,
// so object's ID is unknown.
if ( !$relationItem['contentobject_id'] || !isset( $relationItem['contentobject_id'] ) )
continue;
if( isset( $relationItem[ 'contentobject_id' ] ) && (int) $relationItem[ 'contentobject_id' ] )
{
// Edit in place? - Move to a function
$subObjectID = $relationItem[ 'contentobject_id' ];
$subObjectVersion = $relationItem[ 'contentobject_version' ];

$subObjectID = $relationItem['contentobject_id'];
$subObjectVersion = $relationItem['contentobject_version'];
if ( $relationItem['is_modified'] && isset( $content['temp'][$subObjectID]['object' ] ) )
{
// handling sub-objects
$object = $content['temp'][$subObjectID]['object'];
if ( $object )
{
$attributes = $content['temp'][$subObjectID]['attributes'];
$attributeInputMap = $content['temp'][$subObjectID]['attribute-input-map'];
$object->storeInput( $attributes,
$attributeInputMap );
$version = eZContentObjectVersion::fetchVersion( $subObjectVersion, $subObjectID );
if ( $version )
{
$version->setAttribute( 'modified', time() );
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$version->store();
}

$object->store();
}
}
}
}

eZContentObject::fetch( $contentObjectID )->addContentObjectRelation( $subObjectID, $contentObjectVersion, $contentClassAttributeID, eZContentObject::RELATION_ATTRIBUTE );
return $this->storeObjectAttributeContent( $attribute, $content );
}

/**
* Building a list of all object relations for all translations, using
* published attributes in all available translations. For the current
* language we're not using the published version but the submitted $attribute
*
* @param eZContentObjectAttribute $attribute
* @return array
*/
private function updateObjectRelations( $attribute )
{
$contentObjectIds = array();

if ( $relationItem['is_modified'] && isset( $content['temp'][$subObjectID]['object' ] ) )
$publishedVersion = $attribute->object()->currentVersion();
$languageMask = eZContentLanguage::decodeLanguageMask( $publishedVersion->languageMask() );

foreach( $languageMask[ 'language_list' ] as $languageId )
{
if( $languageId != $attribute->LanguageID )
{
// handling sub-objects
$object = $content['temp'][$subObjectID]['object'];
if ( $object )
$loopAttribute = eZContentObjectAttribute::fetchByClassAttributeID(
$attribute->ContentClassAttributeID,
$attribute->ContentObjectID,
$publishedVersion->Version,
$languageId
);
}
else
{
$loopAttribute = $attribute;
}

if( $loopAttribute )
{
$content = $loopAttribute->content();
if( !empty( $content[ 'relation_list' ] ) )
{
$attributes = $content['temp'][$subObjectID]['attributes'];
$attributeInputMap = $content['temp'][$subObjectID]['attribute-input-map'];
$object->storeInput( $attributes,
$attributeInputMap );
$version = eZContentObjectVersion::fetchVersion( $subObjectVersion, $subObjectID );
if ( $version )
foreach( $content[ 'relation_list' ] as $relation )
{
$version->setAttribute( 'modified', time() );
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$version->store();
$contentObjectIds[ $relation[ 'contentobject_id' ] ] = $relation[ 'contentobject_id' ];
}

$object->store();
}
}
}
return $this->storeObjectAttributeContent( $attribute, $content );

// Remove all relations for the handled version
$attribute->object()->removeContentObjectRelation(
false,
$attribute->Version,
$attribute->ContentClassAttributeID,
eZContentObject::RELATION_ATTRIBUTE
);

if( !empty( $contentObjectIds ) )
{
foreach( $contentObjectIds as $id )
{
$attribute->object()->addContentObjectRelation(
$id,
$attribute->Version,
$attribute->ContentClassAttributeID,
eZContentObject::RELATION_ATTRIBUTE
);
}
}

return $this;
}

function onPublish( $contentObjectAttribute, $contentObject, $publishedNodes )
Expand Down Expand Up @@ -1165,12 +1215,14 @@ static function isItemPublished( $relationItem )
return is_numeric( $relationItem['node_id'] ) and $relationItem['node_id'] > 0;
}

/*!
\private
Removes the relation object \a $deletionItem if the item is owned solely by this
version and is not published in the content tree.
*/
static function removeRelationObject( $contentObjectAttribute, $deletionItem )
/**
* Removes the relation object \a $deletionItem if the item is owned solely by this
* version and is not published in the content tree and not in the trash.
*
* @param eZContentObjectAttribute $contentObjectAttribute
* @param array $deletionItem
*/
static private function removeRelationObject( $contentObjectAttribute, $deletionItem )
{
if ( self::isItemPublished( $deletionItem ) )
{
Expand Down
14 changes: 14 additions & 0 deletions kernel/classes/ezcontentobjectattribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ static function fetchListByClassID( $id, $version = false, $limit = null, $asObj
}
}

/**
* @param $classAttributeID
* @param $objectID
* @param $version
* @param $languageID
* @param bool $asObject
* @return eZContentObjectAttribute
*/
static function fetchByClassAttributeID( $classAttributeID, $objectID, $version, $languageID, $asObject = true )
{
return eZPersistentObject::fetchObject( eZContentObjectAttribute::definition(),
Expand Down Expand Up @@ -395,6 +403,9 @@ function language( $languageCode = false, $asObject = true )
$asObject );
}

/**
* @return eZContentObject|null
*/
function object()
{
if( isset( $this->ContentObjectID ) and $this->ContentObjectID )
Expand All @@ -404,6 +415,9 @@ function object()
return null;
}

/**
* @return eZContentObjectVersion
*/
function objectVersion()
{
return eZContentObjectVersion::fetchVersion( $this->Version, $this->ContentObjectID );
Expand Down