-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 86c0470
Showing
6 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# version 0.0.1 | ||
- Initial import |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Copyright (c) 2015, Raoul | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# yii2-workflow-view | ||
|
||
[![Latest Stable Version](https://poser.pugx.org/raoul2000/yii2-workflow/v/stable)](https://packagist.org/packages/raoul2000/yii2-workflow-view) | ||
[![Total Downloads](https://poser.pugx.org/raoul2000/yii2-workflow/downloads)](https://packagist.org/packages/raoul2000/yii2-workflow-view) | ||
[![License](https://poser.pugx.org/raoul2000/yii2-workflow/license)](https://packagist.org/packages/raoul2000/yii2-workflow-view) | ||
|
||
*A Widget to display your workflow* | ||
|
||
## Installation | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require --prefer-dist raoul2000/yii2-workflow-view "*" | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"raoul2000/yii2-workflow-view": "*" | ||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
||
## Usage | ||
|
||
|
||
License | ||
------- | ||
|
||
**yii2-workflow-view** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details. | ||
|
||
[![Yii2](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](http://www.yiiframework.com/) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name" : "raoul2000/yii2-workflow-view", | ||
"description" : "A basic widget to display your workflows", | ||
"keywords" : [ | ||
"yii2", | ||
"workflow", | ||
"view", | ||
"widget" | ||
], | ||
"homepage" : "http://www.yiiframework.com/", | ||
"type" : "yii2-extension", | ||
"license" : "BSD-3-Clause", | ||
"support" : { | ||
"issues" : "https://github.com/raoul2000/yii2-workflow-view/issues", | ||
"source" : "https://github.com/raoul2000/yii2-workflow-view", | ||
"email" : "[email protected]" | ||
}, | ||
"minimum-stability" : "stable", | ||
"require" : { | ||
"php" : ">=5.4.0", | ||
"yiisoft/yii2" : "*", | ||
"raoul2000/yii2-workflow" : ">=0.0.18" | ||
}, | ||
"config" : { | ||
"process-timeout" : 1800 | ||
}, | ||
"autoload" : { | ||
"psr-4" : { | ||
"raoul2000\\workflow\\view\\" : "src/" | ||
} | ||
}, | ||
"authors" : [{ | ||
"name" : "Raoul", | ||
"email" : "[email protected]", | ||
"homepage" : "https://github.com/raoul2000/yii2-workflow-view" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace raoul2000\workflow\view; | ||
|
||
use Yii; | ||
use yii\web\AssetBundle; | ||
|
||
class WorkflowViewAsset extends AssetBundle | ||
{ | ||
public $sourcePath = '@bower/vis/dist'; | ||
/** | ||
* @see \yii\web\AssetBundle::init() | ||
*/ | ||
public function init() | ||
{ | ||
$this->js = [ | ||
'vis'.( YII_ENV_DEV ? '.js' : '.min.js' ) | ||
]; | ||
$this->css = [ | ||
'vis'.( YII_ENV_DEV ? '.css' : '.min.css' ) | ||
]; | ||
return parent::init(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
namespace raoul2000\workflow\view; | ||
|
||
use yii\base\Widget; | ||
use yii\base\InvalidConfigException; | ||
|
||
class WorkflowViewWidget extends Widget | ||
{ | ||
/** | ||
* @var mixed the Workflow object to display, or a model attached to a | ||
* SimpleWorkflowBehavior. In this case, if the model is in a status, its parent workflow | ||
* is used, otherwise the default workflow is used. | ||
*/ | ||
public $workflow; | ||
/** | ||
* @var string Id of the HTML element that is as a container for the | ||
* workflow view | ||
*/ | ||
public $containerId; | ||
/** | ||
* @var string Id of the VIS javascript objects instance created by this widget to render the workflow. | ||
* If not set a unique id is automatically created. | ||
*/ | ||
public $visNetworkId; | ||
/** | ||
* @var WorkflowInterface the workflow instance to display | ||
*/ | ||
private $_workflow; | ||
/** | ||
* @var string unique Id | ||
*/ | ||
private $_visId; | ||
|
||
/** | ||
* (non-PHPdoc) | ||
* @see \yii\base\Object::init() | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
if ( ! isset($this->containerId)){ | ||
throw new InvalidConfigException("Parameter 'containerId' is missing "); | ||
} | ||
if ( ! isset($this->visNetworkId)){ | ||
$this->visNetworkId = uniqid('vis_'); | ||
} | ||
if ( ! isset($this->workflow)){ | ||
throw new InvalidConfigException("Parameter 'workflow' is missing "); | ||
} | ||
|
||
if ( $this->workflow instanceof \yii\base\Model ) { | ||
|
||
if ( ! \raoul2000\workflow\base\SimpleWorkflowBehavior::isAttachedTo($this->workflow)) { | ||
throw new InvalidConfigException("The model passed as parameter 'workflow' is not attached to a SimpleWorkflowBehavior."); | ||
} | ||
|
||
$this->_workflow = $this->workflow->getWorkflow(); | ||
if ( $this->_workflow == null) { | ||
$this->_workflow = $this->workflow->getWorkflowSource()->getWorkflow( | ||
$this->workflow->getDefaultWorkflowId() | ||
); | ||
} | ||
} elseif ( $this->workflow instanceof \raoul2000\workflow\base\WorkflowInterface ) { | ||
$this->_workflow = $this->workflow; | ||
} | ||
|
||
if ( $this->_workflow == null) { | ||
throw new InvalidConfigException("Failed to find workflow instance from parameter 'workflow'"); | ||
} | ||
$this->_visId = uniqid(); | ||
} | ||
|
||
/** | ||
* @see \yii\base\Widget::run() | ||
*/ | ||
public function run() | ||
{ | ||
$this->getView()->registerJs( | ||
$this->createJs() | ||
); | ||
} | ||
|
||
/** | ||
* Creates and returns the JS code. | ||
* The JS code is used to initialize the VIS instances in charge of displaying the workflow. | ||
* | ||
* @return string the JS code | ||
*/ | ||
private function createJs() | ||
{ | ||
$nodes = $this->_workflow->getAllStatuses(); | ||
$trList = []; | ||
$nodeList = []; | ||
foreach($nodes as $node) { | ||
$n = new \stdClass(); | ||
$n->id = $node->getId(); | ||
$n->label = $node->getLabel(); | ||
if( $node->getMetadata('color') ){ | ||
if( $node->getWorkflow()->getInitialStatusId() == $node->getId() ){ | ||
$n->borderWidth = 4; | ||
$n->color = new \stdClass(); | ||
$n->color->border = 'rgb(0,255,42)'; | ||
// $n->color->background = $node->getMetadata('color'); | ||
} else { | ||
// $n->color = $node->getMetadata('color'); | ||
} | ||
} | ||
$nodeList[] = $n; | ||
|
||
$transitions = $node->getTransitions(); | ||
foreach($transitions as $transition){ | ||
$t = new \stdClass(); | ||
$t->from = $n->id; | ||
$t->to = $transition->getEndStatus()->getId(); | ||
$t->arrows = 'to'; | ||
$trList[] = $t; | ||
} | ||
} | ||
$jsonNodes = \yii\helpers\Json::encode($nodeList); | ||
$jsonTransitions = \yii\helpers\Json::encode($trList); | ||
|
||
$js=<<<EOS | ||
var {$this->visNetworkId} = new vis.Network( | ||
document.getElementById('{$this->containerId}'), | ||
{ | ||
nodes: new vis.DataSet($jsonNodes), | ||
edges: new vis.DataSet($jsonTransitions) | ||
}, | ||
{ | ||
"physics": { | ||
"solver": "repulsion" | ||
} | ||
} | ||
); | ||
EOS; | ||
return $js; | ||
} | ||
} |