smagEntityWorkflowPlugin - 1.0.1

Handle workflow for Objects

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Dependencies Releases Changelog Contribute
Show source

smagEntityWorkflowPlugin

Author: Sören Rohweder rohweder@spot-media.de

This Plugin is for handling entity based workflows. By defining workflownodes in the configuration the entity can determine which possible steps could be used next.

Example:

an Object is in state A and the next possible steps are B or C. But C only if a condition is met.

workflow:
 A:
   title: 'Some useful Title'
   next:
    -
     next: B
    -
     condition:
        param: paramName
        value: paramValue
     next: C
  B:
   title: 'The Title for the B State'
  C:
   title: 'The Title for the C State'

If the workflow is now asked which states it could reach the returning list will contain B/C but C only if the parameter paramName == paramValue

If Plugin enabled there will be an Instance of the WorkflowManager in

 sfContext::getInstance()->get('workflowmanager');

The WorkflowManager is able to check for a Workflow for an Object. There only has to exist a <objectClass>-workflow.yml for each type of object.

If the actual Object has no WorkflowMapping entry there will be one created with the first node found in the Configurationfile.

Normaly all you need to know the WorkflowManager is able to handle. Get the possible Nodes, get the actual Node and set a new Node

Take a look into the workflow.sample.yml to get a more detailed description

Handle an Object

Get the actual node to handle the workflowchange

$actualWFNode =  ifContext::getInstance()->get('workflowmanager')->getActualStepForObject($object);

Check for the right to change the workflow and

if($this->getUser()->hasCredential(Workflow::WF_PREFIX.'_owner_'.$actualWFNode->getName()))

{
sfContext::getInstance()->get('workflowmanager')->setNewWorkflowStep($object,$statusName);
}

Prepare the Forms handled by the Workflow

The forms which should be handled by the workflow have to implement the Workflowable Interface Forms implementing this interface are called by an event wich then calls the handleWorkflow method to disable all formWidgets

class ObjectForm extends BaseForm implements Workflowable

/**
* handles the workflow stuff
*/

public function handleWorkflow()
{
  if(!sfConfig::get('app_workflow_enabled',false))
  {
    return;
  }

  $object = $this->getObject();

  if($object->isNew())
  {
    return;
  }

  $wfNode = sfContext::getInstance()->get('workflowmanager')->getActualStepForObject($object);
  $credentialName = 'wf_'.$wfNode->getName().'_form_'.get_class($this);

  if(!sfContext::getInstance()->getUser()->hasCredential($credentialName))
  {
    $this->reconfigureDisabled();
  }
}

And the BaseForm has to read as follow

class BaseForm extends sfForm
/**
 * disables the form widgets if neccesary for the workflow
 *
 * @param sfFormFieldSchema $schema
 */
public function reconfigureDisabled(sfFormFieldSchema $schema = null)
{
  if (!$schema)
  {
    $schema = $this->getFormFieldSchema();
  }

  foreach ($schema as $key => $field)
  {
    if (!$field instanceof sfFormFieldSchema)
    {
      $field->getWidget()->setAttributes(array('disabled' => 'disabled', 'class' => 'disabled'));
    }
    else
    {
      $this->reconfigureDisabled($field);
    }
  }
}