Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Finish support for "restore from trash"; fix 2 bugs; more formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Oct 21, 2013
1 parent 662dd5f commit 8f7c773
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 32 deletions.
78 changes: 78 additions & 0 deletions classes/controllers/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,84 @@ public function doRemove()
return $result;
}

/**
* Handle PUT request to restore a trashed content object from its [remote] id
*
* Request:
* - PUT /api/contentstaging/content/trash/remote/<remoteId>?parentRemoteId=<>
* - PUT /api/contentstaging/content/trash/<Id>?parentId=<>
*
* @return ezpRestMvcResult
*/
public function doRestoreFromTrash()
{
$object = $this->object();
if ( !$object instanceof eZContentObject )
{
return $object;
}

// check if item is in trash, return 404 otherwise
/// @todo is this check enough? We trust the db to be coherent...
if ( $object->attribute( 'status' ) != eZContentObject::STATUS_ARCHIVED )
{
return self::errorResult( ezpHttpResponseCodes::NOT_FOUND, "The requested content is not in the trash" );
}

if ( !isset( $this->request->get['parentRemoteId'] ) && !isset( $this->request->get['parentId'] ) )
{
return self::errorResult( ezpHttpResponseCodes::BAD_REQUEST, 'The "parentRemoteId"or "parentId" parameters are missing' );
}

if ( isset( $this->request->get['parentRemoteId'] ) )
{
$parentRemoteId = $this->request->get['parentRemoteId'];
$parentNode = eZContentObjectTreeNode::fetchByRemoteID( $parentRemoteId );
if ( !$parentNode instanceof eZContentObjectTreeNode )
{
return self::errorResult( ezpHttpResponseCodes::NOT_FOUND, "Cannot find the location with remote id '{$parentRemoteId}'" );
}
}
else
{
$parentId = $this->request->get['parentId'];
$parentNode = eZContentObjectTreeNode::fetch( $parentId );
if ( !$parentNode instanceof eZContentObjectTreeNode )
{
return self::errorResult( ezpHttpResponseCodes::NOT_FOUND, "Cannot find the location with id '{$parentId}'" );
}
}

// workaround bug #0xxx to be able to publish
$moduleRepositories = eZModule::activeModuleRepositories();
eZModule::setGlobalPathList( $moduleRepositories );

$newNode = eZContentStagingContent::restoreFromTrash( $object, $parentNode );

/// @todo return a 401 in case of permission problems!
if ( !$newNode instanceof eZContentObjectTreeNode )
{
return self::errorResult( ezpHttpResponseCodes::BAD_REQUEST, $newNode );
}

// patch remote id of new node if caller said so
$inputVariables = $this->getRequestVariables();
if ( isset( $inputVariables['remoteId'] ) )
{
if ( ( $result = eZContentStagingLocation::updateRemoteId(
$newNode,
$inputVariables['remoteId'] )
) !== 0 )
{
return self::errorResult( ezpHttpResponseCodes::BAD_REQUEST, $result );
}
}

$result = new ezpRestMvcResult();
$result->variables['Location'] = (array) new eZContentStagingLocation( $newNode );
return $result;
}

/**
* Handle DELETE request for a content object from its [remote] id
*
Expand Down
120 changes: 91 additions & 29 deletions classes/models/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static public function createContent( eZContentObjectTreeNode $parent, $input, $
}
$content->store();

$nodeAssignment = eZNodeAssignment::create(
eZNodeAssignment::create(
array(
'contentobject_id' => $content->attribute( 'id' ),
'contentobject_version' => $content->attribute( 'current_version' ),
Expand All @@ -251,8 +251,7 @@ static public function createContent( eZContentObjectTreeNode $parent, $input, $
'sort_field' => $class->attribute( 'sort_field' ),
'sort_order' => $class->attribute( 'sort_order' )
)
);
$nodeAssignment->store();
)->store();

$version = $content->version( 1 );
// The date of version creation we set to object publication date,
Expand All @@ -269,8 +268,10 @@ static public function createContent( eZContentObjectTreeNode $parent, $input, $
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$version->store();

$attributes = $content->contentObjectAttributes( true, false, $input['initialLanguage'] );
self::updateAttributesList( $attributes, $input['fields'] );
self::updateAttributesList(
$content->contentObjectAttributes( true, false, $input['initialLanguage'] );
$input['fields']
);

$db->commit();

Expand Down Expand Up @@ -356,9 +357,7 @@ static public function addLocation( eZContentObject $object, eZContentObjectTree
{
$newNode = false;

$ini = eZINI::instance( 'contentstagingtarget.ini' );
$dotriggers = ( $ini->variable( 'GeneralSettings', 'ExecuteTriggers' ) != 'disabled' );
if ( $dotriggers && eZOperationHandler::operationIsAvailable( 'content_addlocation' ) )
if ( self::isOperationAvailableAndEnabled( 'content_addlocation' ) )
{
/// @todo what if this triggers a reported action (eg. approval?)
$operationResult = eZOperationHandler::execute(
Expand Down Expand Up @@ -460,9 +459,7 @@ static public function addLocation( eZContentObject $object, eZContentObjectTree
*/
static public function updateSection( eZContentObject $object, $sectionId )
{
$ini = eZINI::instance( 'contentstagingtarget.ini' );
$dotriggers = ( $ini->variable( 'GeneralSettings', 'ExecuteTriggers' ) != 'disabled' );
if ( $dotriggers && eZOperationHandler::operationIsAvailable( 'content_updatesection' ) )
if ( self::isOperationAvailableAndEnabled( 'content_updatesection' ) )
{
eZOperationHandler::execute(
'content',
Expand Down Expand Up @@ -491,15 +488,14 @@ static public function updateSection( eZContentObject $object, $sectionId )
*/
static public function updateStates( eZContentObject $object, $states )
{
$ini = eZINI::instance( 'contentstagingtarget.ini' );
$dotriggers = ( $ini->variable( 'GeneralSettings', 'ExecuteTriggers' ) != 'disabled' );
if ( $dotriggers && eZOperationHandler::operationIsAvailable( 'content_updateobjectstate' ) )
if ( self::isOperationAvailableAndEnabled( 'content_updateobjectstate' ) )
{
eZOperationHandler::execute(
'content',
'updateobjectstate',
array( 'object_id' => $object->attribute( 'id' ),
'state_id_list' => $states
array(
'object_id' => $object->attribute( 'id' ),
'state_id_list' => $states
)
);
}
Expand All @@ -524,9 +520,7 @@ static public function remove( eZContentObject $object, $moveToTrash )
{
$nodeIDs[] = $node->attribute( 'node_id' );
}
$ini = eZINI::instance( 'contentstagingtarget.ini' );
$dotriggers = ( $ini->variable( 'GeneralSettings', 'ExecuteTriggers' ) != 'disabled' );
if ( $dotriggers && eZOperationHandler::operationIsAvailable( 'content_delete' ) )
if ( self::isOperationAvailableAndEnabled( 'content_delete' ) )
{
eZOperationHandler::execute(
'content',
Expand All @@ -550,9 +544,7 @@ static public function remove( eZContentObject $object, $moveToTrash )
*/
static public function updateInitialLanguage( eZContentObject $object, $initialLanguage )
{
$ini = eZINI::instance( 'contentstagingtarget.ini' );
$dotriggers = ( $ini->variable( 'GeneralSettings', 'ExecuteTriggers' ) != 'disabled' );
if ( $dotriggers && eZOperationHandler::operationIsAvailable( 'content_updateinitiallanguage' ) )
if ( self::isOperationAvailableAndEnabled( 'content_updateinitiallanguage' ) )
{
eZOperationHandler::execute(
'content',
Expand All @@ -578,9 +570,7 @@ static public function updateInitialLanguage( eZContentObject $object, $initialL
*/
static public function updateAlwaysAvailable( eZContentObject $object, $alwaysAvailable )
{
$ini = eZINI::instance( 'contentstagingtarget.ini' );
$dotriggers = ( $ini->variable( 'GeneralSettings', 'ExecuteTriggers' ) != 'disabled' );
if ( $dotriggers && eZOperationHandler::operationIsAvailable( 'content_updatealwaysavailable' ) )
if ( self::isOperationAvailableAndEnabled( 'content_updatealwaysavailable' ) )
{
eZOperationHandler::execute(
'content',
Expand All @@ -596,7 +586,7 @@ static public function updateAlwaysAvailable( eZContentObject $object, $alwaysAv
}
else
{
eZContentOperationCollection::updateAlwaysAvailable( $objectID, $alwaysAvailable );
eZContentOperationCollection::updateAlwaysAvailable( $object->attribute( 'id' ), $alwaysAvailable );
}
}

Expand Down Expand Up @@ -634,9 +624,7 @@ static public function updateRemoteId( eZContentObject $object, $remoteId )
*/
static public function removeTranslations( eZContentObject $object, $translations )
{
$ini = eZINI::instance( 'contentstagingtarget.ini' );
$dotriggers = ( $ini->variable( 'GeneralSettings', 'ExecuteTriggers' ) != 'disabled' );
if ( $dotriggers && eZOperationHandler::operationIsAvailable( 'content_removetranslation' ) )
if ( self::isOperationAvailableAndEnabled( 'content_removetranslation' ) )
{
eZOperationHandler::execute(
'content',
Expand All @@ -655,4 +643,78 @@ static public function removeTranslations( eZContentObject $object, $translation
eZContentOperationCollection::removeTranslation( $object->attribute( 'id' ), $translations );
}
}

/**
* Code taken from kernel/content/restore.php
* @todo perms checking
*/
static public function restoreFromTrash( eZContentObject $object, eZContentObjectTreeNode $parentNode )
{
$db = eZDB::instance();
$db->begin();

$version = $object->attribute( 'current' );

// Remove all existing assignments, only our new ones should be present.
foreach ( $version->attribute( 'node_assignments' ) as $assignment )
{
$assignment->purge();
}

// Add the new location
$version->assignToNode( $parentNode->attribute( 'node_id' ), true );

$object->setAttribute( 'status', eZContentObject::STATUS_DRAFT );
$object->store();
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$version->store();

$object->restoreObjectAttributes();

$objectID = $object->attribute( 'id' );
$operationResult = eZOperationHandler::execute(
'content',
'publish',
array(
'object_id' => $objectID,
'version' => $version->attribute( 'version' ) ) );
if ( ( array_key_exists( 'status', $operationResult ) && $operationResult['status'] != eZModuleOperationInfo::STATUS_CONTINUE ) )
{
switch ( $operationResult['status'] )
{
case eZModuleOperationInfo::STATUS_HALTED:
case eZModuleOperationInfo::STATUS_CANCELLED:
return false;
}
}

// re-fetch object
$object = eZContentObject::fetch( $objectID );

eZContentObjectTrashNode::purgeForObject( $objectID );

if ( $object->attribute( 'contentclass_id' ) == eZINI::instance()->variable( "UserSettings", "UserClassID" ) )
{
eZUser::purgeUserCacheByUserId( $objectID );
}

eZContentObject::fixReverseRelations( $objectID, 'restore' );

$db->commit();

return $object->attribute( 'main_node' );
}

static private function isOperationAvailableAndEnabled( $operation )
{
static $enabled = null;

if ( $enabled === null)
{
$enabled = eZINI::instance( "contentstagingtarget.ini" )->variable( "GeneralSettings", "ExecuteTriggers" ) !== "disabled";
}

return $enabled && eZOperationHandler::operationIsAvailable( $operation );
}

}
4 changes: 3 additions & 1 deletion classes/models/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,9 @@ static protected function transformRemoteLinksToLinks( DOMNodeList $nodeList, eZ

/**
* @param array $items array of array
* @param eZContentStagingRemoteIdGenerator|null $ridGenerator Remote id generator used, if null, the local remote_id is used
*/
static protected function transformBlockItemsToRemote( $items, eZContentStagingRemoteIdGenerator $ridGenerator = null )
static protected function transformBlockItemsToRemote( array $items, eZContentStagingRemoteIdGenerator $ridGenerator = null )
{
$out = array();
foreach ( $items as $i => $item )
Expand All @@ -1129,6 +1130,7 @@ static protected function transformBlockItemsToRemote( $items, eZContentStagingR
if ( $ridGenerator )
{
$array['remote_node_id'] = $ridGenerator->buildRemoteId( $item->attribute( 'node_id' ), $node->attribute( 'remote_id' ) );
}
else
{
$array['remote_node_id'] = $item->attribute( 'node_id' );
Expand Down
4 changes: 2 additions & 2 deletions classes/models/location.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static public function updateSort( eZContentObjectTreeNode $node, $sortField, $s

if ( self::isTriggersExecutionEnabled() && eZOperationHandler::operationIsAvailable( 'content_sort' ) )
{
$operationResult = eZOperationHandler::execute(
eZOperationHandler::execute(
'content',
'sort',
array(
Expand Down Expand Up @@ -135,7 +135,7 @@ static public function updatePriority( eZContentObjectTreeNode $node, $priority

if ( self::isTriggersExecutionEnabled() && eZOperationHandler::operationIsAvailable( 'content_updatepriority' ) )
{
$operationResult = eZOperationHandler::execute(
eZOperationHandler::execute(
'content',
'updatepriority',
array(
Expand Down

0 comments on commit 8f7c773

Please sign in to comment.