So, I wrote up some code that duplicates a K2 triple-nested category. It's only good for 3 levels since a) I'm not super at coding and b) that's all I needed.
______In views/categories/view.html.php on line 73 :______________
JToolBarHelper::custom('Duplicate','restore.png','restore_f2.png',JText::_('Duplicate'), true);
______In controllers/categories.php on line 99:____________________________________
function duplicate() {
$mainframe = &JFactory::getApplication();
$model = & $this->getModel('categories');
$model->duplicate();
}
______In models/categories.php at the end:____________________________________
function duplicate() {
$mainframe = &JFactory::getApplication();
$cid = JRequest::getVar('cid');
$db = & JFactory::getDBO();
$idpush = 1;
// GET CATEGORY //
$query = "SELECT id, name, alias, description, parent, extraFieldsGroup, published, access, ordering, image, params, trash, plugins FROM #__k2_categories WHERE id={$cid[0]}";
$db->setQuery($query);
$result = $db->loadObject();
// GET HIGHEST ID //
$maxIDquery = "SELECT id FROM #__k2_categories ORDER BY id DESC LIMIT 3";
$db->setQuery($maxIDquery);
$maxID = $db->loadResult();
$highID = $maxID + $idpush;
$result->id = $highID;
$topparent = $highID;
$highID++;
// INSERT TOP-LEVEL PARENT //
$db->insertObject( '#__k2_categories', $result , 'id');
// GET CHILDREN //
$childrenquery = "SELECT id,trash FROM #__k2_categories WHERE parent={$cid[0]} AND trash=0";
$db->setQuery($childrenquery);
// CHILDREN / SUBCHILDREN //
$children = $db->loadResultArray();
$subChildren = array();
// FIND SUBCHILDREN, INSERT CHILD //
foreach($children as $child) {
$query = "SELECT id, name, alias, description, parent, extraFieldsGroup, published, access, ordering, image, params, trash, plugins FROM #__k2_categories WHERE id={$child} AND trash=0";
$db->setQuery($query);
$result1 = $db->loadObject();
$result1->id = $highID;
$result1->parent = $topparent;
$highID++;
// INSERT CHILD //
$db->insertObject( '#__k2_categories', $result1 , 'id');
$childquery = "SELECT id,trash FROM #__k2_categories
WHERE parent={$child} AND trash=0";
$db->setQuery($childquery);
$subChildrenQuery = $db->loadResultArray();
if($subChildrenQuery[0]){
foreach ($subChildrenQuery as $subChild) {
$query = "SELECT id, name, alias, description, parent, extraFieldsGroup, published, access, ordering, image, params, trash, plugins FROM #__k2_categories WHERE id={$subChild} AND trash=0";
$db->setQuery($query);
$result = $db->loadObject();
$result->id = $highID;
$result->parent = $result1->id;
$highID++;
// INSERT CHILD //
$db->insertObject( '#__k2_categories', $result , 'id');
}
}
}
$row = & JTable::getInstance('K2Category', 'Table');
foreach ($cid as $id) {
$row->load($id);
$row->publish($id, 1);
}
$cache = & JFactory::getCache('com_k2');
$cache->clean();
$msg = JText::_(count($children));
$mainframe->redirect('index.php?option=com_k2&view=categories', $msg);
}
}