public function __construct(RokCommon_Dispatcher $dispatcher) { JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_advancedmodules/tables'); if (JPluginHelper::isEnabled('system', 'advancedmodules') && $this->isComponentEnabled()) { RokCommon_Composite::addPackagePath('roksprocket', dirname(__FILE__) . '/AdvancedModuleManager', 25); $dispatcher->connect('roksprocket.module.get', array($this, 'getModule')); $dispatcher->connect('roksprocket.module.save', array($this, 'saveModule')); $dispatcher->connect('roksprocket.module.duplicate', array($this, 'duplicateModule')); } }
/** * Method to duplicate modules. * * @param array &$pks An array of primary key IDs. * * @return boolean True if successful. * * @since 1.6 * @throws Exception */ public function duplicate(&$pks) { // Initialise variables. $user = JFactory::getUser(); $db = $this->getDbo(); // Access checks. if (!$user->authorise('core.create', 'com_modules')) { throw new Exception(JText::_('JERROR_CORE_CREATE_NOT_PERMITTED')); } $table = $this->getTable(); foreach ($pks as $pk) { if ($table->load($pk, true)) { // Reset the id to create a new record. $table->id = 0; // Alter the title. $m = null; if (preg_match('#\\((\\d+)\\)$#', $table->title, $m)) { $table->title = preg_replace('#\\(\\d+\\)$#', '(' . ($m[1] + 1) . ')', $table->title); } else { $table->title .= ' (2)'; } // Unpublish duplicate module $table->published = 0; if (!$table->check() || !$table->store()) { throw new Exception($table->getError()); } // $query = 'SELECT menuid' // . ' FROM #__modules_menu' // . ' WHERE moduleid = '.(int) $pk // ; $query = $db->getQuery(true); $query->select('menuid'); $query->from('#__modules_menu'); $query->where('moduleid=' . (int) $pk); $this->_db->setQuery((string) $query); $rows = $this->_db->loadColumn(); foreach ($rows as $menuid) { $tuples[] = '(' . (int) $table->id . ',' . (int) $menuid . ')'; } if ($table->module == 'mod_roksprocket') { $this->dispatcher->connect('roksprocket.module.duplicate', array($this, 'duplicatePerItemsForModule'), 5); } // Give Addons a chance to load the menu item $this->dispatcher->notify(new RokCommon_Event($this, 'roksprocket.module.duplicate', array('old_pk' => $pk, 'new_pk' => $table->id))); if ($table->module == 'mod_roksprocket') { $this->dispatcher->disconnect('roksprocket.module.duplicate', array($this, 'duplicatePerItemsForModule'), 5); } } else { throw new Exception($table->getError()); } } if (!empty($tuples)) { // Module-Menu Mapping: Do it in one query $query = 'INSERT INTO #__modules_menu (moduleid,menuid) VALUES ' . implode(',', $tuples); $this->_db->setQuery($query); if (!$this->_db->query()) { return JError::raiseWarning(500, $this->_db->getErrorMsg()); } } // Clear modules cache $this->cleanCache(); return true; }