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

Add featuring plugin #84

Merged
merged 7 commits into from
May 6, 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 @@ -127,7 +127,8 @@ INSERT INTO `#__workflow_transitions` (`id`, `asset_id`, `published`, `ordering`

INSERT INTO `#__extensions` (`package_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES
(0, 'com_workflow', 'component', 'com_workflow', '', 1, 1, 0, 0, '', '{}', 0, NULL, 0, 0),
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, '', '{}', 0, NULL, 0, 0);
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, '', '{}', 0, NULL, 0, 0),
(0, 'plg_workflow_featuring', 'plugin', 'featuring', 'workflow', 0, 1, 1, 0, '', '{}', 0, NULL, 0, 0);

--
-- Creating Associations for existing content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ INSERT INTO "#__workflow_transitions" ("id", "asset_id", "published", "ordering"

INSERT INTO "#__extensions" ("package_id", "name", "type", "element", "folder", "client_id", "enabled", "access", "protected", "manifest_cache", "params", "checked_out", "checked_out_time", "ordering", "state") VALUES
(0, 'com_workflow', 'component', 'com_workflow', '', 1, 1, 0, 0, '', '{}', 0, '1970-01-01 00:00:00', 0, 0),
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, '', '{}', 0, NULL, 0, 0);
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, '', '{}', 0, NULL, 0, 0),
(0, 'plg_workflow_featuring', 'plugin', 'featuring', 'workflow', 0, 1, 1, 0, '', '{}', 0, NULL, 0, 0);

--
-- Creating Associations for existing content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class ContentComponent extends MVCComponent implements
CategoryServiceTrait::getStateColumnForSection insteadof TagServiceTrait;
}

/** @var array Supported functionality */
protected $supportedFunctionality = [
'core.featured' => [
'com_content.articles'
],
'joomla.state' => true,
];

/**
* The trashed condition
*
Expand Down
71 changes: 68 additions & 3 deletions administrator/components/com_content/src/Model/ArticleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,49 @@ class ArticleModel extends AdminModel implements WorkflowModelInterface
*/
protected $associationsContext = 'com_content.item';

/**
* The event to trigger before changing featured status one or more items.
*
* @var string
* @since 4.0
*/
protected $event_before_change_featured = null;

/**
* The event to trigger after changing featured status one or more items.
*
* @var string
* @since 4.0
*/
protected $event_after_change_featured = null;

/**
* Constructor.
*
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
* @param MVCFactoryInterface $factory The factory.
* @param FormFactoryInterface $formFactory The form factory.
*
* @since 1.6
* @throws \Exception
*/
public function __construct($config = array(), MVCFactoryInterface $factory = null, FormFactoryInterface $formFactory = null)
{
$config['events_map'] = $config['events_map'] ?? [];

$config['events_map'] = array_merge(
['featured' => 'content'],
$config['events_map']
);

parent::__construct($config, $factory, $formFactory);

// Set the featured status change events
$this->event_before_change_featured = $config['event_before_change_featured'] ?? $this->event_before_change_featured;
$this->event_before_change_featured = $this->event_before_change_featured ?? 'onContentBeforeChangeFeatured';
$this->event_after_change_featured = $config['event_after_change_featured'] ?? $this->event_after_change_featured;
$this->event_after_change_featured = $this->event_after_change_featured ?? 'onContentAfterChangeFeatured';

$this->setUpWorkflow('com_content.article');
}

Expand Down Expand Up @@ -825,9 +864,15 @@ public function save($data)
public function featured($pks, $value = 0, $featuredUp = null, $featuredDown = null)
{
// Sanitize the ids.
$pks = (array) $pks;
$pks = ArrayHelper::toInteger($pks);
$value = (int) $value;
$pks = (array) $pks;
$pks = ArrayHelper::toInteger($pks);
$value = (int) $value;
$context = $this->option . '.' . $this->name;

$this->workflowBeforeStageChange();

// Include the plugins for the change of state event.
PluginHelper::importPlugin($this->events_map['featured']);

// Convert empty strings to null for the query.
if ($featuredUp === '')
Expand All @@ -849,6 +894,16 @@ public function featured($pks, $value = 0, $featuredUp = null, $featuredDown = n

$table = $this->getTable('Featured', 'Administrator');

// Trigger the before change state event.
$result = Factory::getApplication()->triggerEvent($this->event_before_change_featured, array($context, $pks, $value));

if (\in_array(false, $result, true))
{
$this->setError($table->getError());

return false;
}

try
{
$db = $this->getDbo();
Expand Down Expand Up @@ -942,6 +997,16 @@ public function featured($pks, $value = 0, $featuredUp = null, $featuredDown = n

$table->reorder();

// Trigger the change state event.
$result = Factory::getApplication()->triggerEvent($this->event_after_change_featured, array($context, $pks, $value));

if (\in_array(false, $result, true))
{
$this->setError($table->getError());

return false;
}

$this->cleanCache();

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@
'disabled' => !$canChange
];

if ($workflow_enabled) :
$options['disabled'] = true;
endif;

echo (new FeaturedButton)
->render((int) $item->featured, $i, $options, $item->featured_up, $item->featured_down);
?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@
'disabled' => !$canChange
];

if ($workflow_enabled) :
$options['disabled'] = true;
endif;

echo (new FeaturedButton)
->render((int) $item->featured, $i, $options, $item->featured_up, $item->featured_down);
?>
Expand Down
11 changes: 11 additions & 0 deletions administrator/language/en-GB/plg_workflow_featuring.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
; Joomla! Project
; Copyright (C) 2005 - 2019 Open Source Matters. All rights reserved.
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

PLG_WORKFLOW_FEATURING="Workflow - Featuring"
PLG_WORKFLOW_FEATURING_XML_DESCRIPTION="Add featuring actions to the workflow transitions for your items"
PLG_WORKFLOW_FEATURING_TRANSITION_ACTIONS_FEATURING_LABEL="Featuring state"
PLG_WORKFLOW_FEATURING_TRANSITION_ACTIONS_FEATURING_DESC="Define the featured state an item should be set, when executing this transition"
PLG_WORKFLOW_FEATURING_CHANGE_STATE_NOT_ALLOWED="You're not allowed to change the featured state of this item. Please use a workflow transition."
PLG_WORKFLOW_FEATURING_FEATURED="Featured: %s"
7 changes: 7 additions & 0 deletions administrator/language/en-GB/plg_workflow_featuring.sys.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; Joomla! Project
; Copyright (C) 2005 - 2019 Open Source Matters. All rights reserved.
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

PLG_WORKFLOW_FEATURING="Workflow - Featuring"
PLG_WORKFLOW_FEATURING_XML_DESCRIPTION="Add featuring options to the workflow transitions for your items"
3 changes: 2 additions & 1 deletion installation/sql/mysql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ INSERT INTO `#__extensions` (`package_id`, `name`, `type`, `element`, `folder`,
(0, 'plg_media-action_rotate', 'plugin', 'rotate', 'media-action', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_system_accessibility', 'plugin', 'accessibility', 'system', 0, 0, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_system_webauthn', 'plugin', 'webauthn', 'system', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0);
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_workflow_featuring', 'plugin', 'featuring', 'workflow', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0);

-- Templates
INSERT INTO `#__extensions` (`package_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `locked`, `manifest_cache`, `params`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES
Expand Down
5 changes: 3 additions & 2 deletions installation/sql/postgresql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ INSERT INTO "#__extensions" ("package_id", "name", "type", "element", "folder",
(0, 'plg_media-action_rotate', 'plugin', 'rotate', 'media-action', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_system_accessibility', 'plugin', 'accessibility', 'system', 0, 0, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_system_webauthn', 'plugin', 'webauthn', 'system', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0);
(0, 'plg_workflow_publishing', 'plugin', 'publishing', 'workflow', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0),
(0, 'plg_workflow_featuring', 'plugin', 'featuring', 'workflow', 0, 1, 1, 0, 1, '', '{}', 0, NULL, 0, 0);

-- Templates
INSERT INTO "#__extensions" ("package_id", "name", "type", "element", "folder", "client_id", "enabled", "access", "protected", "locked", "manifest_cache", "params", "checked_out", "checked_out_time", "ordering", "state") VALUES
Expand Down Expand Up @@ -1181,4 +1182,4 @@ INSERT INTO "#__workflow_transitions" ("id", "asset_id", "published", "ordering"
(3, 63, 1, 3, 1, 'Trash', '', -1, 3, '{"publishing":"-2"}', NULL, 0),
(4, 64, 1, 4, 1, 'Archive', '', -1, 4, '{"publishing":"2"}', NULL, 0);

SELECT setval('#__workflow_transitions_id_seq', 5, false);
SELECT setval('#__workflow_transitions_id_seq', 5, false);
19 changes: 11 additions & 8 deletions libraries/src/Workflow/WorkflowServiceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ trait WorkflowServiceTrait
*/
abstract public function getMVCFactory(): MVCFactoryInterface;

/** @var array Supported functionality */
protected $supportedFunctionality = [
'joomla.state' => true,
'joomla.featured' => true,
];

/**
* Check if the functionality is supported by the context
* Check if the functionality is supported by the component
* The variable $supportFunctionality has the following structure
* [
* 'core.featured' => [
* 'com_content.article',
* ],
* 'core.state' => [
* 'com_content.article',
* ],
* ]
*
* @param string $functionality The functionality
* @param string $context The context of the functionality
Expand All @@ -57,7 +60,7 @@ public function supportFunctionality($functionality, $context): bool
return true;
}

return in_array($context, $this->supportedFunctionality[$functionality]);
return in_array($context, $this->supportedFunctionality[$functionality], true);
}

/**
Expand Down
Loading