/**
  * test fetchChildListByVersionStatus
  */
 public function testFetchChildListByVersionStatus()
 {
     //create object
     $top = new ezpObject('article', 2);
     $top->name = 'TOP ARTICLE';
     $top->publish();
     $child = new ezpObject('article', $top->mainNode->node_id);
     $child->name = 'THIS IS AN ARTICLE';
     $child->publish();
     $child2 = new ezpObject('article', $top->mainNode->node_id);
     $child2->name = 'THIS IS AN ARTICLE2';
     $child2->publish();
     $pendingChild = new ezpObject('article', $top->mainNode->node_id);
     $pendingChild->name = 'THIS IS A PENDING ARTICLE';
     $pendingChild->publish();
     $version = $pendingChild->currentVersion();
     $version->setAttribute('status', eZContentObjectVersion::STATUS_PENDING);
     $version->store();
     $idList = array($top->mainNode->node_id);
     $arrayResult = eZNodeAssignment::fetchChildListByVersionStatus($idList, eZContentObjectVersion::STATUS_PENDING, false);
     $this->assertEquals($pendingChild->id, $arrayResult[0]['contentobject_id']);
     $arrayResult = eZNodeAssignment::fetchChildListByVersionStatus($idList, eZContentObjectVersion::STATUS_PUBLISHED, true);
     $this->assertEquals($child->id, $arrayResult[0]->attribute('contentobject_id'));
     $countResult = eZNodeAssignment::fetchChildCountByVersionStatus($idList, eZContentObjectVersion::STATUS_PENDING);
     $this->assertEquals(1, $countResult);
     $countResult = eZNodeAssignment::fetchChildCountByVersionStatus($idList, eZContentObjectVersion::STATUS_PUBLISHED);
     $this->assertEquals(2, $countResult);
 }
 static function removeSubtrees($deleteIDArray, $moveToTrash = true, $infoOnly = false)
 {
     $moveToTrashAllowed = true;
     $deleteResult = array();
     $totalChildCount = 0;
     $totalLoneNodeCount = 0;
     $canRemoveAll = true;
     $hasPendingObject = false;
     $db = eZDB::instance();
     $db->begin();
     foreach ($deleteIDArray as $deleteID) {
         $node = eZContentObjectTreeNode::fetch($deleteID);
         if ($node === null) {
             continue;
         }
         $object = $node->attribute('object');
         if ($object === null) {
             continue;
         }
         $class = $object->attribute('content_class');
         $canRemove = $node->attribute('can_remove');
         $canRemoveSubtree = true;
         $nodeID = $node->attribute('node_id');
         $nodeName = $object->attribute('name');
         $childCount = 0;
         $newMainNodeID = false;
         $objectNodeCount = 0;
         $readableChildCount = 0;
         if ($canRemove) {
             $moveToTrashAllowed = $node->isNodeTrashAllowed();
             $readableChildCount = $node->subTreeCount(array('Limitation' => array()));
             $childCount = $node->subTreeCount(array('IgnoreVisibility' => true));
             $totalChildCount += $childCount;
             $allAssignedNodes = $object->attribute('assigned_nodes');
             $objectNodeCount = count($allAssignedNodes);
             // We need to find a new main node ID if we are trying
             // to remove the current main node.
             if ($node->attribute('main_node_id') == $nodeID) {
                 if (count($allAssignedNodes) > 1) {
                     foreach ($allAssignedNodes as $assignedNode) {
                         $assignedNodeID = $assignedNode->attribute('node_id');
                         if ($assignedNodeID == $nodeID) {
                             continue;
                         }
                         $newMainNodeID = $assignedNodeID;
                         break;
                     }
                 }
             }
             if ($infoOnly) {
                 // Find the number of items in the subtree we are allowed to remove
                 // if this differs from the total count it means we have items we cannot remove
                 // We do this by fetching the limitation list for content/remove
                 // and passing it to the subtree count function.
                 $currentUser = eZUser::currentUser();
                 $accessResult = $currentUser->hasAccessTo('content', 'remove');
                 if ($accessResult['accessWord'] == 'limited') {
                     $limitationList = $accessResult['policies'];
                     $removeableChildCount = $node->subTreeCount(array('Limitation' => $limitationList, 'IgnoreVisibility' => true));
                     $canRemoveSubtree = $removeableChildCount == $childCount;
                     $canRemove = $canRemoveSubtree;
                 }
                 //check if there is sub object in pending status
                 $limitCount = 100;
                 $offset = 0;
                 while (1) {
                     $children = $node->subTree(array('Limitation' => array(), 'SortBy' => array('path', false), 'Offset' => $offset, 'Limit' => $limitCount, 'IgnoreVisibility' => true, 'AsObject' => false));
                     // fetch pending node assignment(pending object)
                     $idList = array();
                     //add node itself into idList
                     if ($offset === 0) {
                         $idList[] = $nodeID;
                     }
                     foreach ($children as $child) {
                         $idList[] = $child['node_id'];
                     }
                     if (count($idList) === 0) {
                         break;
                     }
                     $pendingChildCount = eZNodeAssignment::fetchChildCountByVersionStatus($idList, eZContentObjectVersion::STATUS_PENDING);
                     if ($pendingChildCount !== 0) {
                         // there is pending object
                         $hasPendingObject = true;
                         break;
                     }
                     $offset += $limitCount;
                 }
             }
             // We will only remove the subtree if are allowed
             // and are told to do so.
             if ($canRemove and !$infoOnly) {
                 $moveToTrashTemp = $moveToTrash;
                 if (!$moveToTrashAllowed) {
                     $moveToTrashTemp = false;
                 }
                 // Remove children, fetching them by 100 to avoid memory overflow.
                 // removeNodeFromTree -> removeThis handles cache clearing
                 while (1) {
                     // We should remove the latest subitems first,
                     // so we should fetch subitems sorted by 'path_string' DESC
                     $children = $node->subTree(array('Limitation' => array(), 'SortBy' => array('path', false), 'Limit' => 100, 'IgnoreVisibility' => true));
                     if (!$children) {
                         break;
                     }
                     foreach ($children as $child) {
                         $child->removeNodeFromTree($moveToTrashTemp);
                         eZContentObject::clearCache();
                     }
                 }
                 $node->removeNodeFromTree($moveToTrashTemp);
             }
         }
         if (!$canRemove) {
             $canRemoveAll = false;
         }
         // Do not create info list if we are removing subtrees
         if (!$infoOnly) {
             continue;
         }
         $soleNodeCount = $node->subtreeSoleNodeCount();
         $totalLoneNodeCount += $soleNodeCount;
         if ($objectNodeCount <= 1) {
             ++$totalLoneNodeCount;
         }
         $item = array("nodeName" => $nodeName, "childCount" => $childCount, "additionalWarning" => '', 'node' => $node, 'object' => $object, 'class' => $class, 'node_name' => $nodeName, 'child_count' => $childCount, 'object_node_count' => $objectNodeCount, 'sole_node_count' => $soleNodeCount, 'can_remove' => $canRemove, 'can_remove_subtree' => $canRemoveSubtree, 'real_child_count' => $readableChildCount, 'new_main_node_id' => $newMainNodeID);
         $deleteResult[] = $item;
     }
     $db->commit();
     if (!$infoOnly) {
         return true;
     }
     if ($moveToTrashAllowed and $totalLoneNodeCount == 0) {
         $moveToTrashAllowed = false;
     }
     return array('move_to_trash' => $moveToTrashAllowed, 'total_child_count' => $totalChildCount, 'can_remove_all' => $canRemoveAll, 'delete_list' => $deleteResult, 'has_pending_object' => $hasPendingObject, 'reverse_related_count' => eZContentObjectTreeNode::reverseRelatedCount($deleteIDArray));
 }