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

[5.2][Events] Use event classes for Sampledata plugins #43636

Open
wants to merge 4 commits into
base: 5.2-dev
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
SampleData blog
  • Loading branch information
Fedik committed Jun 8, 2024
commit f382f4bda25f6421b56cfd5f762ea0218d5dac20
120 changes: 85 additions & 35 deletions plugins/sampledata/blog/src/Extension/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\Plugin\AjaxEvent;
use Joomla\CMS\Event\SampleData\GetOverviewEvent;
use Joomla\CMS\Extension\ExtensionHelper;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Multilanguage;
Expand All @@ -21,6 +23,7 @@
use Joomla\CMS\Session\Session;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\ParameterType;
use Joomla\Event\SubscriberInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -31,7 +34,7 @@
*
* @since 3.8.0
*/
final class Blog extends CMSPlugin
final class Blog extends CMSPlugin implements SubscriberInterface
{
use DatabaseAwareTrait;

Expand All @@ -53,14 +56,34 @@ final class Blog extends CMSPlugin
*/
private $menuItemModel;

/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onSampledataGetOverview' => 'onSampledataGetOverview',
'onAjaxSampledataApplyStep1' => 'onAjaxSampledataApplyStep1',
'onAjaxSampledataApplyStep2' => 'onAjaxSampledataApplyStep2',
'onAjaxSampledataApplyStep3' => 'onAjaxSampledataApplyStep3',
'onAjaxSampledataApplyStep4' => 'onAjaxSampledataApplyStep4',
];
}

/**
* Get an overview of the proposed sampledata.
*
* @return \stdClass|void Will be converted into the JSON response to the module.
* @param GetOverviewEvent $event Event instance
*
* @return void
*
* @since 3.8.0
*/
public function onSampledataGetOverview()
public function onSampledataGetOverview(GetOverviewEvent $event): void
{
if (!$this->getApplication()->getIdentity()->authorise('core.create', 'com_content')) {
return;
Expand All @@ -73,17 +96,19 @@ public function onSampledataGetOverview()
$data->icon = 'wifi';
$data->steps = 4;

return $data;
$event->addResult($data);
}

/**
* First step to enter the sampledata. Content.
*
* @return array|void Will be converted into the JSON response to the module.
* @param AjaxEvent $event Event instance
*
* @return void
*
* @since 3.8.0
*/
public function onAjaxSampledataApplyStep1()
public function onAjaxSampledataApplyStep1(AjaxEvent $event): void
{
if (!Session::checkToken('get') || $this->getApplication()->getInput()->get('type') != $this->_name) {
return;
Expand All @@ -94,7 +119,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = true;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_SKIPPED', 1, 'com_tags');

return $response;
$event->addResult($response);
return;
}

// Get some metadata.
Expand Down Expand Up @@ -138,7 +164,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

$tagIds[] = $modelTag->getItem()->id;
Expand All @@ -149,7 +176,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = true;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_SKIPPED', 1, 'com_content');

return $response;
$event->addResult($response);
return;
}

if (ComponentHelper::isEnabled('com_fields') && $user->authorise('core.create', 'com_fields')) {
Expand Down Expand Up @@ -183,7 +211,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

$groupId = $groupModel->getItem()->id;
Expand Down Expand Up @@ -246,7 +275,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

// Get ID from the field we just added
Expand All @@ -273,7 +303,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $this->getApplication()->getLanguage()->_($workflowTable->getError()));

return $response;
$event->addResult($response);
return;
}

// Get ID from workflow we just added
Expand All @@ -299,7 +330,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $this->getApplication()->getLanguage()->_($stageTable->getError()));

return $response;
$event->addResult($response);
return;
}
}

Expand Down Expand Up @@ -449,7 +481,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $this->getApplication()->getLanguage()->_($trTable->getError()));

return $response;
$event->addResult($response);
return;
}
}
}
Expand Down Expand Up @@ -500,7 +533,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

// Get ID from category we just added
Expand Down Expand Up @@ -718,7 +752,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 1, $this->getApplication()->getLanguage()->_($articleModel->getError()));

return $response;
$event->addResult($response);
return;
}

// Get ID from article we just added
Expand Down Expand Up @@ -766,17 +801,19 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = true;
$response['message'] = $this->getApplication()->getLanguage()->_('PLG_SAMPLEDATA_BLOG_STEP1_SUCCESS');

return $response;
$event->addResult($response);
}

/**
* Second step to enter the sampledata. Menus.
*
* @return array|void Will be converted into the JSON response to the module.
* @param AjaxEvent $event Event instance
*
* @return void
*
* @since 3.8.0
*/
public function onAjaxSampledataApplyStep2()
public function onAjaxSampledataApplyStep2(AjaxEvent $event): void
{
if (!Session::checkToken('get') || $this->getApplication()->getInput()->get('type') != $this->_name) {
return;
Expand All @@ -787,7 +824,8 @@ public function onAjaxSampledataApplyStep2()
$response['success'] = true;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_SKIPPED', 2, 'com_menus');

return $response;
$event->addResult($response);
return;
}

// Detect language to be used.
Expand Down Expand Up @@ -825,7 +863,8 @@ public function onAjaxSampledataApplyStep2()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 2, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

$menuTypes[] = $menuTable->menutype;
Expand Down Expand Up @@ -1053,7 +1092,8 @@ public function onAjaxSampledataApplyStep2()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 2, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

// Insert level 1 (Link in the footer as alias)
Expand Down Expand Up @@ -1122,7 +1162,8 @@ public function onAjaxSampledataApplyStep2()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 2, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

$this->getApplication()->setUserState('sampledata.blog.menuIdsLevel1', $menuIdsLevel1);
Expand Down Expand Up @@ -1259,7 +1300,8 @@ public function onAjaxSampledataApplyStep2()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 2, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

// Add a third level of menuItems - use article title also for menuItem title
Expand Down Expand Up @@ -1306,24 +1348,27 @@ public function onAjaxSampledataApplyStep2()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 2, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

$response = [];
$response['success'] = true;
$response['message'] = $this->getApplication()->getLanguage()->_('PLG_SAMPLEDATA_BLOG_STEP2_SUCCESS');

return $response;
$event->addResult($response);
}

/**
* Third step to enter the sampledata. Modules.
*
* @return array|void Will be converted into the JSON response to the module.
* @param AjaxEvent $event Event instance
*
* @return void
*
* @since 3.8.0
*/
public function onAjaxSampledataApplyStep3()
public function onAjaxSampledataApplyStep3(AjaxEvent $event): void
{
if (!Session::checkToken('get') || $this->getApplication()->getInput()->get('type') != $this->_name) {
return;
Expand All @@ -1336,7 +1381,8 @@ public function onAjaxSampledataApplyStep3()
$response['success'] = true;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_SKIPPED', 3, 'com_modules');

return $response;
$event->addResult($response);
return;
}

// Detect language to be used.
Expand Down Expand Up @@ -1708,7 +1754,8 @@ public function onAjaxSampledataApplyStep3()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 3, $this->getApplication()->getLanguage()->_($model->getError()));

return $response;
$event->addResult($response);
return;
}
}

Expand Down Expand Up @@ -1740,7 +1787,8 @@ public function onAjaxSampledataApplyStep3()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_BLOG_STEP_FAILED', 3, $this->getApplication()->getLanguage()->_($model->getError()));

return $response;
$event->addResult($response);
return;
}
}
}
Expand All @@ -1749,17 +1797,19 @@ public function onAjaxSampledataApplyStep3()
$response['success'] = true;
$response['message'] = $this->getApplication()->getLanguage()->_('PLG_SAMPLEDATA_BLOG_STEP3_SUCCESS');

return $response;
$event->addResult($response);
}

/**
* Final step to show completion of sampledata.
*
* @return array|void Will be converted into the JSON response to the module.
* @param AjaxEvent $event Event instance
*
* @return void
*
* @since 4.0.0
*/
public function onAjaxSampledataApplyStep4()
public function onAjaxSampledataApplyStep4(AjaxEvent $event): void
{
if ($this->getApplication()->getInput()->get('type') != $this->_name) {
return;
Expand All @@ -1768,7 +1818,7 @@ public function onAjaxSampledataApplyStep4()
$response['success'] = true;
$response['message'] = $this->getApplication()->getLanguage()->_('PLG_SAMPLEDATA_BLOG_STEP4_SUCCESS');

return $response;
$event->addResult($response);
}

/**
Expand Down