update
This commit is contained in:
@@ -12,18 +12,62 @@
|
|||||||
|
|
||||||
namespace JchOptimize\Log;
|
namespace JchOptimize\Log;
|
||||||
|
|
||||||
use Joomla\CMS\Log\DelegatingPsrLogger;
|
use Joomla\CMS\Log\Log;
|
||||||
|
use Psr\Log\AbstractLogger;
|
||||||
|
use Psr\Log\LogLevel;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
\defined('_JEXEC') or exit('Restricted Access');
|
\defined('_JEXEC') or exit('Restricted Access');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @psalm-suppress all
|
* @psalm-suppress all
|
||||||
*/
|
*/
|
||||||
class DelegatingPsrLoggerExtended extends DelegatingPsrLogger
|
class DelegatingPsrLoggerExtended extends AbstractLogger
|
||||||
{
|
{
|
||||||
public function log($level, $message, array $context = [])
|
/**
|
||||||
|
* @var Log
|
||||||
|
*/
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<string, int>
|
||||||
|
*/
|
||||||
|
protected $priorityMap = [
|
||||||
|
LogLevel::EMERGENCY => Log::EMERGENCY,
|
||||||
|
LogLevel::ALERT => Log::ALERT,
|
||||||
|
LogLevel::CRITICAL => Log::CRITICAL,
|
||||||
|
LogLevel::ERROR => Log::ERROR,
|
||||||
|
LogLevel::WARNING => Log::WARNING,
|
||||||
|
LogLevel::NOTICE => Log::NOTICE,
|
||||||
|
LogLevel::INFO => Log::INFO,
|
||||||
|
LogLevel::DEBUG => Log::DEBUG,
|
||||||
|
];
|
||||||
|
|
||||||
|
public function __construct(Log $logger)
|
||||||
{
|
{
|
||||||
$context = \array_merge($context, ['category' => 'com_jchoptimize']);
|
$this->logger = $logger;
|
||||||
parent::log($level, $message, $context);
|
}
|
||||||
|
|
||||||
|
public function log($level, string|Stringable $message, array $context = []): void
|
||||||
|
{
|
||||||
|
if (!\array_key_exists($level, $this->priorityMap)) {
|
||||||
|
throw new \InvalidArgumentException('An invalid log level has been given.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$priority = $this->priorityMap[$level];
|
||||||
|
$context = \array_merge($context, ['category' => 'com_jchoptimize']);
|
||||||
|
|
||||||
|
$category = null;
|
||||||
|
$date = null;
|
||||||
|
|
||||||
|
if (!empty($context['category'])) {
|
||||||
|
$category = $context['category'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($context['date'])) {
|
||||||
|
$date = $context['date'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->logger::add((string) $message, $priority, $category, $date, $context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,60 +24,35 @@ class PlgContentJt_Copymoduleassignments extends CMSPlugin
|
|||||||
|
|
||||||
public function onContentAfterSave($context, &$table, $isNew)
|
public function onContentAfterSave($context, &$table, $isNew)
|
||||||
{
|
{
|
||||||
// Debugging output
|
if ($context != 'com_menus.item' || !$isNew) {
|
||||||
Factory::getApplication()->enqueueMessage('Context: ' . $context);
|
|
||||||
Factory::getApplication()->enqueueMessage('Is New: ' . ($isNew ? 'Yes' : 'No'));
|
|
||||||
|
|
||||||
// Return if invalid context
|
|
||||||
if ($context != 'com_menus.item') {
|
|
||||||
Factory::getApplication()->enqueueMessage('Invalid context, exiting.', 'error');
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only proceed if the item is new
|
$originalMenuId = Factory::getApplication()->input->getInt('id', 0);
|
||||||
if ($isNew) {
|
|
||||||
// Get the original menu item ID from the submitted data (this assumes the ID is part of the submitted form data)
|
|
||||||
$originalMenuId = Factory::getApplication()->input->getInt('id', 0);
|
|
||||||
|
|
||||||
// Debugging output
|
$query1 = $this->db->getQuery(true)
|
||||||
Factory::getApplication()->enqueueMessage('New Menu Item ID: ' . $table->id);
|
->select($this->db->quoteName('moduleid'))
|
||||||
Factory::getApplication()->enqueueMessage('Original Menu ID: ' . $originalMenuId);
|
->from($this->db->quoteName('#__modules_menu'))
|
||||||
|
->where($this->db->quoteName('menuid') . ' = ' . (int) $originalMenuId);
|
||||||
|
$this->db->setQuery($query1);
|
||||||
|
|
||||||
// Proceed with fetching assigned modules for the original menu ID
|
try {
|
||||||
$query1 = $this->db->getQuery(true)
|
$modules = (array) $this->db->loadColumn();
|
||||||
->select($this->db->quoteName('moduleid'))
|
} catch (Exception $e) {
|
||||||
->from($this->db->quoteName('#__modules_menu'))
|
return false;
|
||||||
->where($this->db->quoteName('menuid') . ' = ' . (int) $originalMenuId);
|
}
|
||||||
$this->db->setQuery($query1);
|
|
||||||
|
|
||||||
try {
|
if (!empty($modules)) {
|
||||||
$modules = (array) $this->db->loadColumn();
|
foreach ($modules as $mid) {
|
||||||
Factory::getApplication()->enqueueMessage('Modules Found: ' . count($modules));
|
$mdl = new stdClass();
|
||||||
} catch (Exception $e) {
|
$mdl->moduleid = $mid;
|
||||||
Factory::getApplication()->enqueueMessage('Error fetching modules: ' . $e->getMessage(), 'error');
|
$mdl->menuid = $table->id;
|
||||||
return false;
|
try {
|
||||||
}
|
$this->db->insertObject('#__modules_menu', $mdl);
|
||||||
|
} catch (Exception $e) {
|
||||||
// Assign all found modules to copied menu item
|
continue;
|
||||||
if (!empty($modules)) {
|
|
||||||
foreach ($modules as $mid) {
|
|
||||||
$mdl = new stdClass();
|
|
||||||
$mdl->moduleid = $mid;
|
|
||||||
$mdl->menuid = $table->id; // This is the new menu item ID
|
|
||||||
try {
|
|
||||||
$this->db->insertObject('#__modules_menu', $mdl);
|
|
||||||
Factory::getApplication()->enqueueMessage('Assigned module ID: ' . $mid . ' to new menu item ID: ' . $table->id);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
Factory::getApplication()->enqueueMessage('Error assigning module ID ' . $mid . ': ' . $e->getMessage(), 'error');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Factory::getApplication()->enqueueMessage('No modules to assign', 'warning');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Continue with any additional logic for exception modules if needed...
|
|
||||||
} else {
|
|
||||||
Factory::getApplication()->enqueueMessage('Item is being edited, not duplicating.', 'warning');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user