Skip to content

Instantly share code, notes, and snippets.

@mooror
Last active January 9, 2019 21:35
Show Gist options
  • Save mooror/52ab1b4f1f827b2363318339879c1430 to your computer and use it in GitHub Desktop.
Save mooror/52ab1b4f1f827b2363318339879c1430 to your computer and use it in GitHub Desktop.
Workspace Extension for silverstripe DataObjects
---
Name: coreconfig
---
# Add relationship abstraction methods to the DataObject via a extension
SilverStripe\ORM\DataObject:
extensions:
- Sitelease\Core\Extensions\WorkflowDataObjectExtension
<?php
namespace Sitelease\Core\Extensions;
use ReflectionClass;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataExtension;
/**
* Adds a simple workflow framework to the DataObject class.
*/
class WorkflowDataObjectExtension extends DataExtension
{
/**
* When set to true the DataObject will include everything needed
* to setup a simple workflow
* @var
* @config
*/
private static $workflow_enabled = false;
public function augmentDatabase()
{
$owner = $this->owner;
$workflowEnabled = $owner->config()->get('workflow_enabled');
if ($workflowEnabled) {
$class = get_class($owner);
$schema = $owner->getSchema();
$table = $schema->tableName($class);
$fields = $schema->databaseFields($class, false);
$indexes = $schema->databaseIndexes($class, false);
$extensions = $this->owner->database_extensions($class);
// Add in a DB field for storing the current workflow step
$fields["WorkflowStep"] = "Int";
// Only build the table if we've actually got fields
if ($fields) {
$hasAutoIncPK = get_parent_class($owner) === $class;
DB::require_table(
$table,
$fields,
$indexes,
$hasAutoIncPK,
$owner->config()->get('create_table_options'),
$extensions
);
} else {
DB::dont_require_table($table);
}
}
}
public function populateDefaults()
{
$owner = $this->owner;
$workflowEnabled = $owner->config()->get('workflow_enabled');
if ($workflowEnabled) {
$owner->WorkflowStep = '1';
}
}
/**
* An array of key value pairs that will
* be used for creating headings for the workflow steps
*
*Example:
* ```
* private static $workflow_steps = [
* "1" => "Step 1 - General Information",
* "2" => "Step 2 - Create or Select a Product",
* "3" => "Step 3 - Create or Select a User",
*]
* ```
*
* @var
* @config
*/
private static $workflow_steps = [];
/**
* Returns a integer representing the current
* step in the workflow
*
* @author Benjamin Blake (sitelease.ca)
*
* @return int
*/
public function getCurrentStep()
{
$owner = $this->owner;
$workflowEnabled = $owner->config()->get('workflow_enabled');
if ($workflowEnabled) {
return (int) $owner->WorkflowStep;
} else {
return 0;
}
}
/**
* Returns the title of the passed in step. If no
* step is passed in then returns the title of the
* current step
*
* @author Benjamin Blake (sitelease.ca)
*
* @param int|string $stepNumber
* @return string
*/
public function getStepTitle($stepNumber = null)
{
$owner = $this->owner;
$workflowEnabled = $owner->config()->get('workflow_enabled');
if ($workflowEnabled) {
$stepTitleArray = $owner->config()->get("workflow_steps");
if (!$stepNumber) {
$stepNumber = $owner->WorkflowStep;
}
$stepNumber = (string) $stepNumber;
if (array_key_exists($stepNumber, $stepTitleArray)) {
return $stepTitleArray[$stepNumber];
} else {
return "";
}
} else {
return "";
}
}
public function onBeforeWrite()
{
$owner = $this->owner;
$workflowEnabled = $owner->config()->get('workflow_enabled');
if ($workflowEnabled) {
// Increment the workflow step field
$currentStep = $owner->WorkflowStep;
$owner->WorkflowStep = (int) $currentStep + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment