diff --git a/administrator/components/com_jchoptimize/lib/src/Log/DelegatingPsrLoggerExtended.php b/administrator/components/com_jchoptimize/lib/src/Log/DelegatingPsrLoggerExtended.php index d489003..b7fee19 100644 --- a/administrator/components/com_jchoptimize/lib/src/Log/DelegatingPsrLoggerExtended.php +++ b/administrator/components/com_jchoptimize/lib/src/Log/DelegatingPsrLoggerExtended.php @@ -12,18 +12,62 @@ 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'); /** * @psalm-suppress all */ -class DelegatingPsrLoggerExtended extends DelegatingPsrLogger +class DelegatingPsrLoggerExtended extends AbstractLogger { - public function log($level, $message, array $context = []) + /** + * @var Log + */ + protected $logger; + + /** + * @var array + */ + 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']); - parent::log($level, $message, $context); + $this->logger = $logger; + } + + 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); } } diff --git a/plugins/content/jt_copymoduleassignments/jt_copymoduleassignments.php b/plugins/content/jt_copymoduleassignments/jt_copymoduleassignments.php index 8161d64..a1d0242 100644 --- a/plugins/content/jt_copymoduleassignments/jt_copymoduleassignments.php +++ b/plugins/content/jt_copymoduleassignments/jt_copymoduleassignments.php @@ -24,60 +24,35 @@ class PlgContentJt_Copymoduleassignments extends CMSPlugin public function onContentAfterSave($context, &$table, $isNew) { - // Debugging output - 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'); + if ($context != 'com_menus.item' || !$isNew) { return true; } - // Only proceed if the item is new - 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); + $originalMenuId = Factory::getApplication()->input->getInt('id', 0); - // Debugging output - Factory::getApplication()->enqueueMessage('New Menu Item ID: ' . $table->id); - Factory::getApplication()->enqueueMessage('Original Menu ID: ' . $originalMenuId); + $query1 = $this->db->getQuery(true) + ->select($this->db->quoteName('moduleid')) + ->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 - $query1 = $this->db->getQuery(true) - ->select($this->db->quoteName('moduleid')) - ->from($this->db->quoteName('#__modules_menu')) - ->where($this->db->quoteName('menuid') . ' = ' . (int) $originalMenuId); - $this->db->setQuery($query1); + try { + $modules = (array) $this->db->loadColumn(); + } catch (Exception $e) { + return false; + } - try { - $modules = (array) $this->db->loadColumn(); - Factory::getApplication()->enqueueMessage('Modules Found: ' . count($modules)); - } catch (Exception $e) { - Factory::getApplication()->enqueueMessage('Error fetching modules: ' . $e->getMessage(), 'error'); - return false; - } - - // Assign all found modules to copied menu item - 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'); - } + if (!empty($modules)) { + foreach ($modules as $mid) { + $mdl = new stdClass(); + $mdl->moduleid = $mid; + $mdl->menuid = $table->id; + try { + $this->db->insertObject('#__modules_menu', $mdl); + } catch (Exception $e) { + continue; } - } 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;