Skip to content

Commit

Permalink
Add workflow plugin trait (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
HLeithner authored May 10, 2020
1 parent 4353029 commit c0d12ce
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 67 deletions.
77 changes: 77 additions & 0 deletions libraries/src/Workflow/WorkflowPluginTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\CMS\Workflow;

\defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Form\Form;
use function method_exists;

/**
* Trait for component workflow plugins.
*
* @since 4.0.0
*/
trait WorkflowPluginTrait {

/**
* Add different parameter options to the transition view, we need when executing the transition
*
* @param Form $form The form
* @param \stdClass $data The data
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
protected function enhanceWorkflowTransitionForm(Form $form, $data) {

$workflow = $this->getWorkflow((int) ($data->workflow_id ?? $form->getValue('workflow_id')));

if (empty($workflow->id) || !$this->isSupported($workflow->extension))
{
return false;
}

if (file_exists(__DIR__ . '/forms/action.xml'))
{
$form->loadFile(__DIR__ . '/forms/action.xml');
}

return $workflow;
}

protected function getWorkflow(int $workflow_id = null) {

$workflow_id = $workflow_id ?? $this->app->input->getInt('workflow_id');

if (is_array($workflow_id)) {
return false;
}

return $this->app->bootComponent('com_workflow')
->getMVCFactory()
->createModel('Workflow', 'Administrator', ['ignore_request' => true])
->getItem($workflow_id);
}


/**
* Check if the current plugin should execute workflow related activities
*
* @param string $context
* @return boolean
*
* @since 4.0.0
*/
protected function isSupported($context)
{
return false;
}
}
53 changes: 19 additions & 34 deletions plugins/workflow/featuring/featuring.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Joomla\CMS\MVC\View\ViewInterface;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Table\TableInterface;
use Joomla\CMS\Workflow\WorkflowPluginTrait;
use Joomla\CMS\Workflow\WorkflowServiceInterface;
use Joomla\String\Inflector;

Expand All @@ -27,6 +28,8 @@
*/
class PlgWorkflowFeaturing extends CMSPlugin
{
use WorkflowPluginTrait;

/**
* Load the language file on instantiation.
*
Expand All @@ -51,11 +54,12 @@ class PlgWorkflowFeaturing extends CMSPlugin
*/
protected $supportFunctionality = 'core.featured';


/**
* The form event.
*
* @param Form $form The form
* @param stdClass $data The data
* @param \stdClass $data The data
*
* @return boolean
*
Expand All @@ -66,44 +70,14 @@ public function onContentPrepareForm(Form $form, $data)
$context = $form->getName();

// Extend the transition form
if ($context == 'com_workflow.transition')
{
return $this->enhanceTransitionForm($form, $data);
}

return $this->enhanceItemForm($form, $data);
}

/**
* Add different parameter options to the transition view, we need when executing the transition
*
* @param Form $form The form
* @param stdClass $data The data
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
protected function enhanceTransitionForm(Form $form, $data)
{
$model = $this->app->bootComponent('com_workflow')
->getMVCFactory()->createModel('Workflow', 'Administrator', ['ignore_request' => true]);

$workflow_id = (int) ($data->workflow_id ?? $form->getValue('workflow_id'));

if (empty($workflow_id))
if ($context === 'com_workflow.transition')
{
$workflow_id = (int) $this->app->input->getInt('workflow_id');
}

$workflow = $model->getItem($workflow_id);
$this->enhanceWorkflowTransitionForm($form, $data);

if (!$this->isSupported($workflow->extension))
{
return true;
}

$form->loadFile(__DIR__ . '/forms/action.xml');
$this->enhanceItemForm($form, $data);

return true;
}
Expand Down Expand Up @@ -184,6 +158,8 @@ protected function enhanceItemForm(Form $form, $data)
* @param string $context
* @param ViewInterface $view
* @param string $result
*
* @since 4.0.0
*/
public function onAfterDisplay(string $context, ViewInterface $view, string $result)
{
Expand Down Expand Up @@ -244,6 +220,8 @@ public function onAfterDisplay(string $context, ViewInterface $view, string $res
* @param object $transition The value to change to
*
* @return boolean
*
* @since 4.0.0
*/
public function onWorkflowBeforeTransition($context, $pks, $transition)
{
Expand Down Expand Up @@ -287,6 +265,8 @@ public function onWorkflowBeforeTransition($context, $pks, $transition)
* @param object $transition The value to change to
*
* @return boolean
*
* @since 4.0.0
*/
public function onWorkflowAfterTransition($context, $pks, $transition)
{
Expand Down Expand Up @@ -331,7 +311,10 @@ public function onWorkflowAfterTransition($context, $pks, $transition)
* @param string $context The context
* @param array $pks IDs of the items
* @param int $value The value to change to
*
* @return boolean
*
* @since 4.0.0
*/
public function onContentBeforeChangeFeatured(string $context, array $pks, int $value): bool
{
Expand Down Expand Up @@ -393,6 +376,8 @@ public function onContentBeforeSave($context, TableInterface $table, $isNew, $da
*
* @param string $context
* @return boolean
*
* @since 4.0.0
*/
protected function isSupported($context)
{
Expand Down
67 changes: 34 additions & 33 deletions plugins/workflow/publishing/publishing.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Joomla\CMS\MVC\View\ViewInterface;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Table\TableInterface;
use Joomla\CMS\Workflow\WorkflowPluginTrait;
use Joomla\CMS\Workflow\WorkflowServiceInterface;
use Joomla\String\Inflector;

Expand All @@ -27,6 +28,8 @@
*/
class PlgWorkflowPublishing extends CMSPlugin
{
use WorkflowPluginTrait;

/**
* Load the language file on instantiation.
*
Expand All @@ -49,13 +52,13 @@ class PlgWorkflowPublishing extends CMSPlugin
* @var string
* @since __DEPLOY_VERSION__
*/
protected $supportname = 'core.state';
protected $supportFunctionality = 'core.state';

/**
* The form event.
*
* @param Form $form The form
* @param stdClass $data The data
* @param \stdClass $data The data
*
* @return boolean
*
Expand All @@ -66,12 +69,16 @@ public function onContentPrepareForm(Form $form, $data)
$context = $form->getName();

// Extend the transition form
if ($context == 'com_workflow.transition')
if ($context === 'com_workflow.transition')
{
return $this->enhanceTransitionForm($form, $data);
$this->enhanceTransitionForm($form, $data);

return true;
}

return $this->enhanceItemForm($form, $data);
$this->enhanceItemForm($form, $data);

return true;
}

/**
Expand All @@ -86,33 +93,14 @@ public function onContentPrepareForm(Form $form, $data)
*/
protected function enhanceTransitionForm(Form $form, $data)
{
$model = $this->app->bootComponent('com_workflow')
->getMVCFactory()->createModel('Workflow', 'Administrator', ['ignore_request' => true]);
$workflow = $this->enhanceWorkflowTransitionForm($form, $data);

$workflow_id = !empty($data->workflow_id) ? (int) $data->workflow_id : (int) $form->getValue('workflow_id');

if (empty($workflow_id))
{
$workflow_id = $this->app->input->getInt('workflow_id');
}

$workflow = $model->getItem($workflow_id);

if (!$this->isSupported($workflow->extension))
if (!$workflow)
{
return true;
}

$form->loadFile(__DIR__ . '/forms/action.xml');

if ($workflow_id)
{
$form->setFieldAttribute('publishing', 'extension', $workflow->extension, 'options');
}
else
{
$form->setFieldAttribute('publishing', 'disabled', 'true', 'options');
}
$form->setFieldAttribute('publishing', 'extension', $workflow->extension, 'options');

return true;
}
Expand Down Expand Up @@ -191,11 +179,13 @@ protected function enhanceItemForm(Form $form, $data)
/**
* Manipulate the generic list view
*
* @param type $context
* @param type $view
* @param type $result
* @param string $context
* @param ViewInterface $view
* @param string $result
*
* @since 4.0.0
*/
public function onAfterDisplay(string $context, ViewInterface $view, $result)
public function onAfterDisplay(string $context, ViewInterface $view, string $result)
{
$parts = explode('.', $context);

Expand Down Expand Up @@ -242,6 +232,8 @@ public function onAfterDisplay(string $context, ViewInterface $view, $result)
";

$app->getDocument()->addScriptDeclaration($js);

return true;
}

/**
Expand All @@ -252,6 +244,8 @@ public function onAfterDisplay(string $context, ViewInterface $view, $result)
* @param object $transition The value to change to
*
* @return boolean
*
* @since 4.0.0
*/
public function onWorkflowBeforeTransition($context, $pks, $transition)
{
Expand Down Expand Up @@ -295,6 +289,8 @@ public function onWorkflowBeforeTransition($context, $pks, $transition)
* @param object $transition The value to change to
*
* @return boolean
*
* @since 4.0.0
*/
public function onWorkflowAfterTransition($context, $pks, $transition)
{
Expand Down Expand Up @@ -339,7 +335,10 @@ public function onWorkflowAfterTransition($context, $pks, $transition)
* @param string $context The context
* @param array $pks IDs of the items
* @param int $value The value to change to
*
* @return boolean
*
* @since 4.0.0
*/
public function onContentBeforeChangeState($context, $pks, $value)
{
Expand Down Expand Up @@ -399,8 +398,10 @@ public function onContentBeforeSave($context, TableInterface $table, $isNew, $da
/**
* Check if the current plugin should execute workflow related activities
*
* @param type $context
* @param string $context
* @return boolean
*
* @since 4.0.0
*/
protected function isSupported($context)
{
Expand All @@ -416,7 +417,7 @@ protected function isSupported($context)

if (!$component instanceof WorkflowServiceInterface
|| !$component->isWorkflowActive($context)
|| !$component->supportFunctionality($this->supportname, $context))
|| !$component->supportFunctionality($this->supportFunctionality, $context))
{
return false;
}
Expand Down

0 comments on commit c0d12ce

Please sign in to comment.