Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workflow v3 transitions #67

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function filterTransitions(array $transitions, int $pk, int $workf
$transitions,
function ($var) use ($pk, $workflow_id)
{
return in_array($var['from_stage_id'], [-1, $pk]) && $var['to_stage_id'] != $pk && $workflow_id == $var['workflow_id'];
return in_array($var['from_stage_id'], [-1, $pk]) && $workflow_id == $var['workflow_id'];
}
)
);
Expand Down
70 changes: 41 additions & 29 deletions administrator/components/com_content/src/Model/ArticlesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,47 +580,59 @@ public function getTransitions()
return false;
}

$ids = array_column($items, 'stage_id');
$ids = ArrayHelper::toInteger($ids);
$ids = array_values(array_unique(array_filter($ids)));
$stage_ids = ArrayHelper::getColumn($items, 'stage_id');
$stage_ids = ArrayHelper::toInteger($stage_ids);
$stage_ids = array_values(array_unique(array_filter($stage_ids)));

$ids[] = -1;
$workflow_ids = ArrayHelper::getColumn($items, 'workflow_id');
$workflow_ids = ArrayHelper::toInteger($workflow_ids);
$workflow_ids = array_values(array_unique(array_filter($workflow_ids)));

$this->cache[$store] = array();

try
{
if (count($ids))
if (count($stage_ids) || count($workflow_ids))
{
Factory::getLanguage()->load('com_workflow', JPATH_ADMINISTRATOR);

$query = $db->getQuery(true);

$query->select(
[
$db->quoteName('t.id', 'value'),
$db->quoteName('t.title', 'text'),
$db->quoteName('t.from_stage_id'),
$db->quoteName('t.to_stage_id'),
$db->quoteName('s.id', 'stage_id'),
$db->quoteName('s.title', 'stage_title'),
$db->quoteName('s.workflow_id'),
]
)
->from($db->quoteName('#__workflow_transitions', 't'))
->join(
'LEFT',
$db->quoteName('#__workflow_stages', 's'),
$db->quoteName('t.from_stage_id') . ' IN (' . implode(',', $query->bindArray($ids)) . ')'
)
->where(
$query ->select(
[
$db->quoteName('t.to_stage_id') . ' = ' . $db->quoteName('s.id'),
$db->quoteName('t.published') . ' = 1',
$db->quoteName('s.published') . ' = 1',
]
)
->order($db->quoteName('t.ordering'));
$db->quoteName('t.id', 'value'),
$db->quoteName('t.title', 'text'),
$db->quoteName('t.from_stage_id'),
$db->quoteName('t.to_stage_id'),
$db->quoteName('s.id', 'stage_id'),
$db->quoteName('s.title', 'stage_title'),
$db->quoteName('t.workflow_id'),
])
->from($db->quoteName('#__workflow_transitions', 't'))
->innerJoin($db->quoteName('#__workflow_stages', 's'))
->where(
[
$db->quoteName('t.to_stage_id') . ' = ' . $db->quoteName('s.id'),
$db->quoteName('t.published') . ' = 1',
$db->quoteName('s.published') . ' = 1',
]
)
->order($db->quoteName('t.ordering'))
->group($db->quoteName('t.id'));

$where = [];

if (count($stage_ids))
{
$where[] = $db->quoteName('t.from_stage_id') . ' IN (' . implode(',', $query->bindArray($stage_ids)) . ')';
}

if (count($workflow_ids))
{
$where[] = '(' . $db->quoteName('t.from_stage_id') . ' = -1 AND ' . $db->quoteName('t.workflow_id') . ' IN (' . implode(',', $query->bindArray($workflow_ids)) . '))';
}

$query->where('(' . implode(') OR (', $where) . ')');

$transitions = $db->setQuery($query)->loadAssocList();

Expand Down
103 changes: 38 additions & 65 deletions administrator/components/com_content/src/View/Articles/HtmlView.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\Component\Content\Administrator\Extension\ContentComponent;
use Joomla\Component\Content\Administrator\Helper\ContentHelper;
use Joomla\CMS\Component\ComponentHelper;

/**
* View class for a list of articles.
Expand Down Expand Up @@ -64,6 +65,13 @@ class HtmlView extends BaseHtmlView
*/
public $activeFilters;

/**
* All transition, which can be executed of one if the items
*
* @var array
*/
protected $transitions = [];

/**
* Display the view
*
Expand All @@ -78,9 +86,13 @@ public function display($tpl = null)
$this->state = $this->get('State');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');
$this->transitions = $this->get('Transitions');
$this->vote = PluginHelper::isEnabled('content', 'vote');

if (ComponentHelper::getParams('com_content')->get('workflows_enable', 1))
{
$this->transitions = $this->get('Transitions');
}

// Check for errors.
if ((count($errors = $this->get('Errors'))) || $this->transitions === false)
{
Expand Down Expand Up @@ -117,56 +129,6 @@ public function display($tpl = null)
}
}

/*
@TODO Move to the plugin

$transitions = [
'publish' => [],
'unpublish' => [],
'archive' => [],
'trash' => []
];

foreach ($this->transitions as $transition)
{
switch ($transition['stage_condition'])
{
case ContentComponent::CONDITION_PUBLISHED:
$transitions['publish'][$transition['workflow_id']][$transition['from_stage_id']][] = $transition;
break;

case ContentComponent::CONDITION_UNPUBLISHED:
$transitions['unpublish'][$transition['workflow_id']][$transition['from_stage_id']][] = $transition;
break;

case ContentComponent::CONDITION_ARCHIVED:
$transitions['archive'][$transition['workflow_id']][$transition['from_stage_id']][] = $transition;
break;

case ContentComponent::CONDITION_TRASHED:
$transitions['trash'][$transition['workflow_id']][$transition['from_stage_id']][] = $transition;
break;
}
}

$this->document->addScriptOptions('articles.transitions', $transitions);

*/

$articles = [];

foreach ($this->items as $item)
{
$articles['article-' . (int) $item->id] = Text::sprintf('COM_CONTENT_STAGE_ARTICLE_TITLE', $this->escape($item->title), (int) $item->id);
}

$this->document->addScriptOptions('articles.items', $articles);

Text::script('COM_CONTENT_ERROR_CANNOT_PUBLISH');
Text::script('COM_CONTENT_ERROR_CANNOT_UNPUBLISH');
Text::script('COM_CONTENT_ERROR_CANNOT_TRASH');
Text::script('COM_CONTENT_ERROR_CANNOT_ARCHIVE');

return parent::display($tpl);
}

Expand All @@ -192,7 +154,7 @@ protected function addToolbar()
$toolbar->addNew('article.add');
}

if ($canDo->get('core.edit.state') || $canDo->get('core.execute.transition'))
if ($canDo->get('core.edit.state') || count($this->transitions))
{
$dropdown = $toolbar->dropdownButton('status-group')
->text('JTOOLBAR_CHANGE_STATUS')
Expand All @@ -203,15 +165,35 @@ protected function addToolbar()

$childBar = $dropdown->getChildToolbar();

if ($canDo->get('core.execute.transition'))
if (count($this->transitions))
{
$childBar->publish('articles.publish')->listCheck(true);

$childBar->unpublish('articles.unpublish')->listCheck(true);
$childBar->separatorButton('transition-headline')
->text('COM_CONTENT_RUN_TRANSITIONS')
->buttonClass('text-center py-2 h3');

$cmd = "Joomla.submitbutton('articles.runTransition');";
$messages = "{error: [Joomla.JText._('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST')]}";
$alert = 'Joomla.renderMessages(' . $messages . ')';
$cmd = 'if (document.adminForm.boxchecked.value == 0) { ' . $alert . ' } else { ' . $cmd . ' }';

foreach ($this->transitions as $transition)
{
$childBar->standardButton('transition')
->text($transition['text'])
->buttonClass('transition-' . (int) $transition['value'])
->icon('fas fa-project-diagram')
->onclick('document.adminForm.transition_id.value=' . (int) $transition['value'] . ';' . $cmd);
}

$childBar->separatorButton('transition-separator');
}

if ($canDo->get('core.edit.state'))
{
$childBar->publish('articles.publish')->listCheck(true);

$childBar->unpublish('articles.unpublish')->listCheck(true);

$childBar->standardButton('featured')
->text('JFEATURE')
->task('articles.featured')
Expand All @@ -221,20 +203,11 @@ protected function addToolbar()
->text('JUNFEATURE')
->task('articles.unfeatured')
->listCheck(true);
}

if ($canDo->get('core.execute.transition'))
{
$childBar->archive('articles.archive')->listCheck(true);
}

if ($canDo->get('core.edit.state'))
{
$childBar->checkin('articles.checkin')->listCheck(true);
}

if ($canDo->get('core.execute.transition'))
{
$childBar->trash('articles.trash')->listCheck(true);
}

Expand Down
53 changes: 12 additions & 41 deletions administrator/components/com_content/tmpl/articles/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Joomla\CMS\Button\FeaturedButton;
use Joomla\CMS\Button\PublishedButton;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\Utilities\ArrayHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Associations;
Expand Down Expand Up @@ -76,7 +77,7 @@
// @todo move the script to a file
$this->document->addScriptDeclaration($js);

HTMLHelper::_('script', 'com_content/admin-articles-workflow-buttons.js', ['relative' => true, 'version' => 'auto']);
HTMLHelper::_('script', 'com_workflow/admin-items-workflow-buttons.js', ['relative' => true, 'version' => 'auto']);

endif;

Expand All @@ -97,7 +98,7 @@
<?php echo Text::_('JGLOBAL_NO_MATCHING_RESULTS'); ?>
</div>
<?php else : ?>
<table class="table" id="articleList">
<table class="table itemList" id="articleList">
<caption id="captionTable" class="sr-only">
<?php echo Text::_('COM_CONTENT_ARTICLES_TABLE_CAPTION'); ?>, <?php echo Text::_('JGLOBAL_SORTED_BY'); ?>
</caption>
Expand Down Expand Up @@ -167,38 +168,13 @@

$transitions = ContentHelper::filterTransitions($this->transitions, (int) $item->stage_id, (int) $item->workflow_id);

$publish = 0;
$unpublish = 0;
$archive = 0;
$trash = 0;

/*
* @TODO This should be moved to a plugin + the data attributes in the tr below
foreach ($transitions as $transition) :
switch ($transition['stage_condition']) :
case ContentComponent::CONDITION_PUBLISHED:
++$publish;
break;
case ContentComponent::CONDITION_UNPUBLISHED:
++$unpublish;
break;
case ContentComponent::CONDITION_ARCHIVED:
++$archive;
break;
case ContentComponent::CONDITION_TRASHED:
++$trash;
break;
endswitch;
endforeach;*/
$transition_ids = ArrayHelper::getColumn($transitions, 'value');
$transition_ids = ArrayHelper::toInteger($transition_ids);

?>
<tr class="row<?php echo $i % 2; ?>" data-dragable-group="<?php echo $item->catid; ?>"
data-condition-publish="<?php echo (int) $publish > 0; ?>"
data-condition-unpublish="<?php echo (int) $unpublish > 0; ?>"
data-condition-archive="<?php echo (int) $archive > 0; ?>"
data-condition-trash="<?php echo (int) $trash > 0; ?>"
data-workflow_id="<?php echo (int) $item->workflow_id; ?>"
data-stage_id="<?php echo (int) $item->stage_id; ?>"
<tr class="row<?php echo $i % 2; ?>"
data-dragable-group="<?php echo $item->catid; ?>"
data-transitions="<?php echo implode(',', $transition_ids); ?>"
>
<td class="text-center">
<?php echo HTMLHelper::_('grid.id', $i, $item->id, false, 'cid', 'cb', $item->title); ?>
Expand Down Expand Up @@ -412,15 +388,10 @@
$this->loadTemplate('batch_body')
); ?>
<?php endif; ?>
<?php echo HTMLHelper::_(
'bootstrap.renderModal',
'stageModal',
array(
'title' => Text::_('JTOOLBAR_CHANGE_STATUS'),
'footer' => $this->loadTemplate('stage_footer'),
),
$this->loadTemplate('stage_body')
); ?>
<?php endif; ?>

<?php if ($workflow_enabled) : ?>
<input type="hidden" name="transition_id" value="">
<?php endif; ?>

<input type="hidden" name="task" value="">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,35 +156,6 @@ public function save($data)
$isNew = false;
}

if ($data['to_stage_id'] == $data['from_stage_id'])
{
$this->setError(Text::_('COM_WORKFLOW_MSG_FROM_TO_STAGE'));

return false;
}

$db = $this->getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__workflow_transitions'))
->where($db->quoteName('from_stage_id') . ' = ' . (int) $data['from_stage_id'])
->where($db->quoteName('to_stage_id') . ' = ' . (int) $data['to_stage_id']);

if (!$isNew)
{
$query->where($db->quoteName('id') . ' <> ' . (int) $data['id']);
}

$db->setQuery($query);
$duplicate = $db->loadResult();

if (!empty($duplicate))
{
$this->setError(Text::_("COM_WORKFLOW_TRANSITION_DUPLICATE"));

return false;
}

$workflowID = $app->getUserStateFromRequest($context . '.filter.workflow_id', 'workflow_id', 0, 'int');

if (empty($data['workflow_id']))
Expand Down
Loading