static function addGroup($workflowID, $workflowVersion, $selectedGroup)
 {
     list($groupID, $groupName) = explode('/', $selectedGroup);
     $ingroup = eZWorkflowGroupLink::create($workflowID, $workflowVersion, $groupID, $groupName);
     $ingroup->store();
     return true;
 }
else
{
    // if WorkflowID was not given then create new workflow
    $user = eZUser::currentUser();
    $user_id = $user->attribute( "contentobject_id" );
    $workflow = eZWorkflow::create( $user_id );
    $workflowCount = eZWorkflow::fetchListCount();
    ++$workflowCount;
    $workflow->setAttribute( "name", ezpI18n::tr( 'kernel/workflow/edit', "New Workflow" ) . "$workflowCount" );

    $db = eZDB::instance();
    $db->begin();
    $workflow->store();
    $WorkflowID = $workflow->attribute( "id" );
    $WorkflowVersion = $workflow->attribute( "version" );
    $ingroup = eZWorkflowGroupLink::create( $WorkflowID, $WorkflowVersion, $GroupID, $GroupName );
    $ingroup->store();
    $db->commit();
    return $Module->redirectTo( $Module->functionURI( 'edit' ) . '/' . $WorkflowID . '/' . $GroupID );
}

$http = eZHTTPTool::instance();
$WorkflowVersion = $workflow->attribute( "version" );

if ( $http->hasPostVariable( "DiscardButton" ) )
{
    $workflow->setVersion( 1 );

    $db = eZDB::instance();
    $db->begin();
    $workflow->removeThis( true );
 /**
  * Creates the test workflow.
  * @todo Currently only handles the fetchtemplaterepeat event. Will have to be
  *       refactored to handle more events when necessary
  *
  * @return eZWorkflow
  */
 function createWorkFlow($adminUserID)
 {
     $registerResult = $this->registerCustomWorkflowEvent('fetchtemplaterepeat', 'tests/toolkit/extras/tests-files/workflowevent_regression_fetchtemplaterepeat.php');
     if ($registerResult == false) {
         $this->markTestSkipped("Unable to register the workflow event");
     }
     $workflow = eZWorkflow::create($adminUserID);
     $workflow->setAttribute("name", "eZWorkflowEventRegression Workflow");
     $workflow->store();
     $workflowID = $workflow->attribute("id");
     $workflowVersion = $workflow->attribute("version");
     $groupID = 1;
     $groupName = "Standard";
     $ingroup = eZWorkflowGroupLink::create($workflowID, $workflowVersion, $groupID, $groupName);
     $ingroup->store();
     $regressionEvent = eZWorkflowEvent::create($workflowID, "event_fetchtemplaterepeat");
     $regressionEventType = $regressionEvent->eventType();
     $regressionEventType->initializeEvent($regressionEvent);
     $regressionEvent->store();
     $eventList = array($regressionEvent);
     $workflow->store($eventList);
     eZWorkflowGroupLink::removeWorkflowMembers($workflowID, 0);
     $workflowgroups = eZWorkflowGroupLink::fetchGroupList($workflowID, 1);
     foreach ($workflowgroups as $workflowgroup) {
         $workflowgroup->setAttribute("workflow_version", 0);
         $workflowgroup->store();
     }
     // Remove version 1
     eZWorkflowGroupLink::removeWorkflowMembers($workflowID, 1);
     // eZWorkflow::removeEvents( false, $workflowID, 0 );
     eZWorkflow::removeEvents(false, $workflowID, 1);
     eZWorkflow::removeWorkflow($workflowID, 1);
     $workflow->setVersion(0, $eventList);
     $workflow->adjustEventPlacements($eventList);
     $workflow->storeDefined($eventList);
     $workflow->cleanupWorkFlowProcess();
     $workflow->store($eventList);
     return $workflow;
 }
 function createApprovalWorkFlow($approvalUserID)
 {
     $workflow = eZWorkflow::create($approvalUserID);
     $workflow->setAttribute("name", "eZApproveTypeRegression Workflow");
     $workflow->store();
     $workflowID = $workflow->attribute("id");
     $workflowVersion = $workflow->attribute("version");
     $groupID = 1;
     $groupName = "Standard";
     $ingroup = eZWorkflowGroupLink::create($workflowID, $workflowVersion, $groupID, $groupName);
     $ingroup->store();
     $approveEvent = eZWorkflowEvent::create($workflowID, "event_ezapprove");
     $approveEventType = $approveEvent->eventType();
     $approveEventType->initializeEvent($approveEvent);
     // Affected sections, -1 == All sections
     $approveEvent->setAttribute("data_text1", "-1");
     // Affected languages, 0 == All languages
     $approveEvent->setAttribute("data_int2", 0);
     // Affected versions
     $approveEvent->setAttribute("data_int3", eZApproveType::VERSION_OPTION_ALL);
     // Editors
     $approveEvent->setAttribute("data_text3", $approvalUserID);
     $approveEvent->store();
     $eventList = array($approveEvent);
     $workflow->store($eventList);
     eZWorkflowGroupLink::removeWorkflowMembers($workflowID, 0);
     $workflowgroups = eZWorkflowGroupLink::fetchGroupList($workflowID, 1);
     foreach ($workflowgroups as $workflowgroup) {
         $workflowgroup->setAttribute("workflow_version", 0);
         $workflowgroup->store();
     }
     // Remove version 1
     eZWorkflowGroupLink::removeWorkflowMembers($workflowID, 1);
     eZWorkflow::removeEvents(false, $workflowID, 0);
     $workflow->setVersion(0, $eventList);
     $workflow->adjustEventPlacements($eventList);
     $workflow->storeDefined($eventList);
     $workflow->cleanupWorkFlowProcess();
     $workflow->store($eventList);
     return $workflow;
 }
    function execute( $xml )
    {
        $workflowGroupList = $xml->getElementsByTagName( 'WorkflowGroup' );
        $user              = eZUser::currentUser();
        $userID            = $user->attribute( "contentobject_id" );

        foreach ( $workflowGroupList as $workflowGroupNode )
        {
            $groupName        = $workflowGroupNode->getAttribute( 'name' );
            $referenceID      = $workflowGroupNode->getAttribute( 'referenceID' );
            $groupExistAction = $workflowGroupNode->getAttribute( 'groupExistAction' );
            $workflowGroup    = null;

            if ( $groupExistAction == 'keep' )
            {
                $workflowGroupList = eZWorkflowGroup::fetchList();
                foreach ( $workflowGroupList as $workflowGroupItem )
                {
                    if ( $workflowGroupItem->attribute( 'name' ) == $groupName )
                    {
                        $workflowGroup = $workflowGroupItem;
                        break;
                    }
                }
            }
            if ( $workflowGroup !== null )
            {
                $this->writeMessage( "\tWorkflow Group '$groupName' already exists." , 'notice' );
            }
            else
            {
                $this->writeMessage( "\tWorkflow Group '$groupName' will be created." , 'notice' );
                $workflowGroup = eZWorkflowGroup::create( $userID );
                $workflowGroup->setAttribute( "name", $groupName );
                $workflowGroup->store();
            }

            $WorkflowGroupID = $workflowGroup->attribute( "id" );
            $refArray        = array();

            if ( $referenceID )
            {
                $refArray[$referenceID] = $WorkflowGroupID;
            }
            $this->addReference( $refArray );

            $workflowList = $workflowGroupNode->getElementsByTagName( 'Workflow' );
            foreach ( $workflowList as $workflowNode )
            {
                $refArray = array();

                $workflowName        = $workflowNode->getAttribute( 'name' );
                $workflowTypeString  = $workflowNode->getAttribute( 'workflowTypeString' );
                $referenceID         = $workflowNode->getAttribute( 'referenceID' );
                $workflowExistAction = $workflowNode->getAttribute( 'workflowExistAction' );
                $WorkflowID          = $workflowNode->getAttribute( 'id' );
                $workflow            = null;
                $hasWorkflowDraft    = false;
                $db                  = eZDB::instance();

                if ( !$workflowExistAction )
                {
                    $workflowExistAction = 'extend';
                }

                if ( $WorkflowID )
                {
                    $workflow = eZWorkflow::fetch( $WorkflowID, true, 1 );
                    if ( !is_object( $workflow ) )
                    {
                        $workflow = eZWorkflow::fetch( $WorkflowID, true, 0 );
                        if ( is_object( $workflow ) )
                        {
                            $workflowGroups = eZWorkflowGroupLink::fetchGroupList( $WorkflowID, 0, true );

                            $db->begin();
                            foreach ( $workflowGroups as $workflowGroup )
                            {
                                $groupID   = $workflowGroup->attribute( "group_id" );
                                $groupName = $workflowGroup->attribute( "group_name" );
                                $ingroup   = eZWorkflowGroupLink::create( $WorkflowID, 1, $groupID, $groupName );

                                $ingroup->store();
                            }
                            $db->commit();

                        }
                        else
                        {
                            $this->writeMessage( "\tFailed to fetch workflow with ID '$WorkflowID'." , 'notice' );
                            $workflow = null;
                        }
                    }
                }

                $db->begin();

                if ( $workflow === null )
                {
                    $this->writeMessage( "\tWorkflow '$workflowName' will be created." , 'notice' );
                    $workflow = eZWorkflow::create( $userID );
                    $workflow->setAttribute( "name",  $workflowName );
                    if ( $workflowTypeString )
                    {
                        $workflow->setAttribute( "workflow_type_string",  $workflowTypeString );
                    }
                    $workflow->store();
                    $ingroup = eZWorkflowGroupLink::create( $workflow->attribute( "id" ), $workflow->attribute( "version" ), $WorkflowGroupID, $groupName );
                    $ingroup->store();
                }
                else
                {
                    $hasWorkflowDraft = true;
                    switch ( $workflowExistAction )
                    {
                        case 'extend':
                        {
                            $this->writeMessage( "\tExtending existing workflow '" . $workflow->attribute( 'name' ) . "'." , 'notice' );
                        }
                        break;
                        case 'replace':
                        {
                            $this->writeMessage( "\tReplacing existing workflow '" . $workflow->attribute( 'name' ) . "'." , 'notice' );
                            eZWorkflow::removeEvents( false, $workflow->attribute( "id" ), $workflow->attribute( "version" ) );
                        }
                        break;
                        default:
                        {
                            $this->writeMessage( "\tUnknown workflowExistAction '" . $workflowExistAction . "'." , 'notice' );
                        }
                    }
                }

                $WorkflowID      = $workflow->attribute( "id" );
                $WorkflowVersion = $workflow->attribute( "version" );

                $db->commit();

                if ( $referenceID )
                {
                    $refArray[$referenceID] = $WorkflowID;
                }

                $eventList     = $workflow->fetchEvents();
                $eventNodeList = $workflowNode->getElementsByTagName( 'Event' );
                $maxPlacement  = -1;

                foreach ( $eventList as $event )
                {
                    if ( $event->attribute( 'placement' ) > $maxPlacement )
                    {
                        $maxPlacement = $event->attribute( 'placement' );
                    }
                }

                foreach ( $eventNodeList as $eventNode )
                {
                    $description        = $eventNode->getAttribute( 'description' );
                    $workflowTypeString = $eventNode->getAttribute( 'workflowTypeString' );
                    $placement          = $eventNode->getAttribute( 'placement' );
                    $event              = eZWorkflowEvent::create( $WorkflowID, $workflowTypeString );
                    $eventType          = $event->eventType();

                    $db->begin();

                    $workflow->store( $eventList );
                    $eventType->initializeEvent( $event );

                    if ( is_numeric( $placement ) )
                    {
                        $eventType->setAttribute( 'placement', (int)$placement );
                    }
                    else
                    {
                        ++$maxPlacement;
                        $eventType->setAttribute( 'placement', $maxPlacement );
                    }

                    $eventDataNode = $eventNode->getElementsByTagName( 'Data' )->item( 0 );

                    if ( $eventDataNode )
                    {
                        $attributes = $eventDataNode->childNodes;
                        foreach ( $attributes as $attribute )
                        {
                            if ( $event->hasAttribute( $attribute->nodeName ) )
                            {
                                $data = $this->parseAndReplaceStringReferences( $attribute->textContent );
                                $event->setAttribute( $attribute->nodeName, $data );
                            }
                        }
                    }
                    $event->store();

                    $db->commit();

                    $eventList[] = $event;

                }

                // Discard existing events, workflow version 1 and store version 0
                $db->begin();

                $workflow->store( $eventList ); // store changes.

                // Remove old version 0 first
                eZWorkflowGroupLink::removeWorkflowMembers( $WorkflowID, 0 );

                $workflowgroups = eZWorkflowGroupLink::fetchGroupList( $WorkflowID, 1 );
                foreach( $workflowgroups as $workflowgroup )
                {
                    $workflowgroup->setAttribute("workflow_version", 0 );
                    $workflowgroup->store();
                }
                // Remove version 1
                eZWorkflowGroupLink::removeWorkflowMembers( $WorkflowID, 1 );

                eZWorkflow::removeEvents( false, $WorkflowID, 0 );
                $workflow->removeThis( true );
                $workflow->setVersion( 0, $eventList );
                $workflow->adjustEventPlacements( $eventList );
                $workflow->storeDefined( $eventList );
                $workflow->cleanupWorkFlowProcess();

                $db->commit();


                if ( $referenceID )
                {
                    $refArray[$referenceID] = $WorkflowID;
                }
                $this->addReference( $refArray );
            }
        }

        $triggerList = $xml->getElementsByTagName( 'Trigger' );
        foreach ( $triggerList as $triggerNode )
        {
            $module      = $triggerNode->getAttribute( 'module' );
            $operation   = $triggerNode->getAttribute( 'operation' );
            $connectType = $triggerNode->getAttribute( 'connectType' );
            $workflowID  = $this->getReferenceID( $triggerNode->getAttribute( 'workflowID' ) );

            $this->writeMessage( "\tTrigger '$module/$operation/$connectType' will be created/updated." , 'notice' );

            if ( $connectType == 'before' )
            {
                $connectType = 'b';
            }
            else
            {
                $connectType = 'a';
            }

            $parameters = array();
            $parameters['module']      = $module;
            $parameters['function']    = $operation;
            $parameters['connectType'] = $connectType;

            $triggerList = eZTrigger::fetchList( $parameters );

            if ( count( $triggerList ) )
            {
                $trigger = $triggerList[0];
                $trigger->setAttribute( 'workflow_id', $workflowID );
                $trigger->store();
            }
            else
            {
                $db = eZDB::instance();
                $db->begin();
                $newTrigger = eZTrigger::createNew( $module, $operation, $connectType, $workflowID );
                $db->commit();
            }
        }
    }