update
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user