Latest JAS v1.1.0
144 plugins online

Modules / Workflow

Ah the good old workflow module... or rather modules as workflowTransitions is closely intertwined. These modules can help you to setup a workflow and attach it to a certain (other) module. This would allow you to enforces a certain process for the module. Let's imagine we have an "issue" process where we want to have some approval steps built in.

Let's first build the basic issues module...
Issues module with workflow
PHP
require_once($_path["include"]."module.php");

class Module_issues extends Module
{
  function __construct(&$data)
  {
    parent::__construct($data);

    $this->data->title="Issues";
    $this->data->defaultFields();

    $this->data->addField("summary", "text", "all", "title", 1, 255, false, "Summary");
    $this->data->addField("description", "text", "all", "markup", 0, 65536, false, "Description");

    //workflow
    $this->data->addField("workflow", "nr", "all", "", 1, false, " Issue", "Workflow");
    $this->data->addReference("workflow", "workflow", "nr", "*:1");

  }

//more to come...

So this should look pretty familiar up until the workflow section. Now this isn't rocket science either but it requires some explanation. The default value of the workflow field "Issue" relates to the name of the workflow you define using the workflow module (this must match exactly). The addReference line create a reference to the workflow module so it can show the correct values. To do so however it'll need to customize the field by using some module specific functions:

Determine states with MSFs
PHP
  function postInit()[if($this->data->action=="add"){return $this->customizeFields();] return true;}
  function copyPostGetReferences()[return $this->customizeFields();]
  function editPostGetReferences()[return $this->customizeFields();]

  function addPreSubmitErrorCheck()[return $this->checkValues();]
  function copyPreSubmitErrorCheck()[return $this->checkValues();]
  function editPreSubmitErrorCheck()[return $this->checkValues();]

  function customizeFields()
  {
    if($this->data->getReferenceData("workflow")&&!$this->data->getReferenceData("workflow")->moduleObject->determineState($this->data))
    {
    $this->data->e->add($this->data->getReferenceData("workflow")->e);
    return false;
    }

    return true;
  }

  function checkValues()
  {
    if($this->data->getReferenceData("workflow")&&!$this->data->getReferenceData("workflow")->moduleObject->validateState($this->data))
    {
    $this->data->e->add($this->data->getReferenceData("workflow")->e);
    return false;
    }

    return true;
  }


The first 6 functions are the MSFs that will ensure the field customization are done at the right time (when adding a new entry or copy/editing an existing one). The key in the customization is "determineState" which will set up the correct options and validateState" which will only allow the workflow to progress to subsequent states if all conditions are met.

Additionally the determineState and validateState allow a second argument to disable specific workflow states. This is done by providing the "workflow state code" (or an array of codes) that will need to be disabled. Subsequently these the workflow can't progress to those states. With this mechanism you can easily extend the default "workflow transitions restrictions" with you're own custom ones.

Close class curly brace
PHP
}
//EOF...

Now that the module is done you can define the workflow with the workflow module via the backend. Note that the "code" fields can be quite relevant as they can be used in your code to build custom behavior. Also, when you've defined a workflow, you may want to have a look at the workflowTransitions as these will allow you to put some (simple) restriction on the transition.

Comments (0)