/**
  * Switches the status of a document to target state.
  */
 public function changestateAction()
 {
     $docId = $this->getRequest()->getParam('docId');
     $targetState = $this->getRequest()->getParam('targetState');
     $document = $this->__documentsHelper->getDocumentForId($docId);
     // Check if document identifier is valid
     if (!isset($document)) {
         return $this->_redirectTo('index', array('failure' => $this->view->translate('admin_document_error_novalidid')), 'documents', 'admin');
     }
     // Check if valid target state
     if (!$this->__workflowHelper->isValidState($targetState)) {
         return $this->_redirectTo('index', array('failure' => $this->view->translate('admin_workflow_error_invalidstate')), 'document', 'admin', array('id' => $docId));
     }
     // Check if allowed target state
     if (!$this->__workflowHelper->isTransitionAllowed($document, $targetState)) {
         return $this->_redirectTo('index', array('failure' => $this->view->translate('admin_workflow_error_illegal_transition', $targetState)), 'document', 'admin', array('id' => $docId));
     }
     // Check if document is already in target state
     if ($document->getServerState() === $targetState) {
         // if defined used custom message for state, other use common key
         $key = 'admin_workflow_error_already_' . $targetState;
         if (!$this->view->translate()->getTranslator()->isTranslated($key)) {
             $key = 'admin_workflow_error_alreadyinstate';
         }
         return $this->_redirectTo('index', array('failure' => $this->view->translate($key, $targetState)), 'document', 'admin', array('id' => $docId));
     }
     if ($this->__confirmChanges) {
         if ($this->getRequest()->isPost()) {
             $form = $this->_getConfirmationForm($document, $targetState);
             $sureyes = $this->getRequest()->getPost('sureyes');
             if ($form->isValid($this->getRequest()->getPost()) && isset($sureyes) === true) {
                 return $this->_changeState($document, $targetState, $form);
             }
             return $this->_redirectTo('index', null, 'document', 'admin', array('id' => $docId));
         }
         // show confirmation page
         $this->view->documentAdapter = new Util_DocumentAdapter($this->view, $document);
         $this->view->title = $this->view->translate('admin_workflow_' . $targetState);
         $this->view->text = $this->view->translate('admin_workflow_' . $targetState . '_sure', $docId);
         $this->view->form = $this->_getConfirmationForm($document, $targetState);
     } else {
         return $this->_changeState($document, $targetState);
     }
 }