eZContentOperationCollection::updateObjectState PHP Method

updateObjectState() public static method

Update a contentobject's state
public static updateObjectState ( integer $objectID, integer $selectedStateIDList ) : array
$objectID integer
$selectedStateIDList integer
return array An array with operation status, always true
    public static function updateObjectState($objectID, $selectedStateIDList)
    {
        $object = eZContentObject::fetch($objectID);
        // we don't need to re-assign states the object currently already has assigned
        $currentStateIDArray = $object->attribute('state_id_array');
        $selectedStateIDList = array_diff($selectedStateIDList, $currentStateIDArray);
        // filter out any states the current user is not allowed to assign
        $canAssignStateIDList = $object->attribute('allowed_assign_state_id_list');
        $selectedStateIDList = array_intersect($selectedStateIDList, $canAssignStateIDList);
        foreach ($selectedStateIDList as $selectedStateID) {
            $state = eZContentObjectState::fetchById($selectedStateID);
            $object->assignState($state);
        }
        //call appropriate method from search engine
        eZSearch::updateObjectState($objectID, $selectedStateIDList);
        // Triggering content/state/assign event for persistence cache purge
        ezpEvent::getInstance()->notify('content/state/assign', array($objectID, $selectedStateIDList));
        eZContentCacheManager::clearContentCacheIfNeeded($objectID);
        return array('status' => true);
    }

Usage Example

 function execute($process, $event)
 {
     // get object being published
     $parameters = $process->attribute('parameter_list');
     $objectID = $parameters['object_id'];
     eZDebug::writeDebug('Update object state for object: ' . $objectID);
     $object = eZContentObject::fetch($objectID);
     $state_before = $event->attribute('state_before');
     $state_after = $event->attribute('state_after');
     if ($object == null) {
         eZDebug::writeError('Update object state failed for inexisting object: ' . $objectID, __METHOD__);
         return eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
     }
     if ($state_before == null || $state_after == null) {
         eZDebug::writeError('Update object state failed: badly configured states', __METHOD__);
         return eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
     }
     $currentStateIDArray = $object->attribute('state_id_array');
     if (in_array($state_before->attribute('id'), $currentStateIDArray)) {
         $canAssignStateIDList = $object->attribute('allowed_assign_state_id_list');
         if (!in_array($state_after->attribute('id'), $canAssignStateIDList)) {
             eZDebug::writeWarning("Not enough rights to assign state to object {$objectID}: " . $state_after->attribute('id'), __METHOD__);
         } else {
             eZDebug::writeDebug('Changing object state from ' . $state_before->attribute('name') . ' to ' . $state_after->attribute('name'), __METHOD__);
             if (eZOperationHandler::operationIsAvailable('content_updateobjectstate')) {
                 $operationResult = eZOperationHandler::execute('content', 'updateobjectstate', array('object_id' => $objectID, 'state_id_list' => array($state_after->attribute('id'))));
             } else {
                 eZContentOperationCollection::updateObjectState($objectID, array($state_after->attribute('id')));
             }
         }
     }
     return eZWorkflowType::STATUS_ACCEPTED;
 }
All Usage Examples Of eZContentOperationCollection::updateObjectState