Skip to content

Instantly share code, notes, and snippets.

@Opencontent
Last active June 8, 2016 16:01
Show Gist options
  • Save Opencontent/21982c5848cc66b8ae8d6e0e52e3501f to your computer and use it in GitHub Desktop.
Save Opencontent/21982c5848cc66b8ae8d6e0e52e3501f to your computer and use it in GitHub Desktop.
eZ Publish Legacy Add Location
<?php
function addLocation(
eZContentObject $object,
$parentNodeId,
$addAsMain = false
) {
$node = $object->mainNode();
$parentNode = eZContentObjectTreeNode::fetch($parentNodeId);
if ($node instanceof eZContentObjectTreeNode && $parentNode instanceof eZContentObjectTreeNode) {
$nodeID = $node->attribute('node_id');
$objectID = $node->attribute('contentobject_id');
$selectedNodeIDArray = array($parentNode->attribute('node_id'));
/** @var eZContentObject $parentNodeObject */
$parentNodeObject = $parentNode->attribute('object');
/** @var eZContentObject $object */
$object = $node->attribute('object');
/** @var eZContentClass $class */
$class = $object->contentClass();
$canCreate = (
( $parentNode->checkAccess('create', $class->attribute('id'),
$parentNodeObject->attribute('contentclass_id')) == 1 )
|| ( $parentNode->canAddLocation() && $node->canRead() )
);
if ($canCreate) {
eZContentOperationCollection::addAssignment($nodeID, $objectID, $selectedNodeIDArray);
if ($addAsMain) {
$mainAssignmentParentID = $parentNode->attribute('node_id');
foreach ($node->object()->assignedNodes() as $assignedNode) {
if ($assignedNode->attribute('parent_node_id') == $mainAssignmentParentID) {
eZContentOperationCollection::updateMainAssignment(
$assignedNode->attribute('node_id'),
$objectID,
$mainAssignmentParentID
);
}
}
}
return true;
} else {
eZDebug::writeError("Current user can not add a location in $parentNodeId to object $objectID", __METHOD__);
}
} else {
eZDebug::writeError("Object corrupted or parent node $parentNodeId not found", __METHOD__);
}
return false;
}
@Opencontent
Copy link
Author

Per rendere principale la nuova collocazione impostare $addAsMain a true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment