Keyword

Copy Category

  • dome412
  • dome412's Avatar Topic Author
  • Offline
  • New Member
More
14 years 8 months ago #80869 by dome412
Copy Category was created by dome412
Hello to everybody!First of all I'm very sorry for my english.......so I try to explain my problem.My goal is: I would like to create a menu that has got the same structure of my "k2-categories".category1--subcategory1----subcategory1.1
subcategory1.1.1
subcategory1.1.2----subcategory1.2
subcategory1.2.1
subcategory1.2.2category2--subcategory1
----subcategory1.1
subcategory1.1.1
subcategory1.1.2
----subcategory1.2
subcategory1.2.1
subcategory1.2.2
and so on for category 3, 4, 5, etc..As you can see every different category has got the same subcategories.What I need is to have a copy function that permit me to copy the root category in another one "copy of category"(that I will rename) with related subcategories.In k2 category section, there isn't a copy button like the standard joomla category section.Could anybody help me?Thanks a lot!Domenico

Please Log in or Create an account to join the conversation.

More
14 years 8 months ago #80870 by Mario
Replied by Mario on topic Copy Category
I have the same issue like dome412.
My case is the follow.
I have a Portfolio Page. Portfolio is also my parent Category.
Then i have some Subcategories like Film, Musicvideo and some else.
On my "Home" Page, my first site on the Website, i would like to show some Categories from my Subcategories.
But k2 has no option to do that, because it is not possible to copy a Subcategory to another parent Category.
I dont understand this. Its a Joomla out of the box feature to show Articles from some Category on another Page in another Category, without to copy the Article.

I hope for this feature in the future. Or has anybody a little plugin or hack for this?

Thanks!

Please Log in or Create an account to join the conversation.

More
14 years 7 months ago #80871 by Chrysanthos
Replied by Chrysanthos on topic Copy Category
i need something like this!!! any suggestions?

Please Log in or Create an account to join the conversation.

More
14 years 3 months ago #80872 by Jacob Friedman
Replied by Jacob Friedman on topic Copy Category
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);

}
}

Please Log in or Create an account to join the conversation.

More
14 years 3 months ago #80873 by Jacob Friedman
Replied by Jacob Friedman on topic Copy Category
Sorry, everything here's located in /administrator/components/com_k2. Drop it in, ready to roll. Cheers :)

Please Log in or Create an account to join the conversation.

More
14 years 2 months ago #80874 by Peter Gasser
Replied by Peter Gasser on topic Copy Category
Great job, it works perfect!!
I have my "duplicate categories button" now
Thanks
Peter

Jacob Friedman said:Sorry, everything here's located in /administrator/components/com_k2. Drop it in, ready to roll. Cheers :)

Please Log in or Create an account to join the conversation.

More
14 years 2 months ago #80875 by irish
Replied by irish on topic Copy Category
Thank you for this "Jacob Friedman" it has been a big help. I had to figure out how to get it to work and since there were only two files in your post I went ahead and sorted out my issues and created a zip file with the correct paths and attached files. Hope you don't mind.
The problem I was having with your code was that in the "models/categories.php at the end:" part, I had to replace the last " } " with your code.

Now all you will have to do is download the attached zip file and extract it on your local system or hard drive and upload the com_k2 folder and all contents to your servers: administrator/components/ folder and overwrite the com_k2 folders and files. Only the changed files for the duplication (copy categories) will be overwritten.
I have only tested this on K2 Component v2.3

Always! before changing core files make a backup in case you have problems.
Personally I keep a copy or backup of my server files on a disk in house. On test servers I usually browse all files to be changed on the server and rename them by adding "-saved" without the "quotes " to the end of a file name then upload the new files.

I have noticed that when trying to copy third level categories in number that it will only copy one at a time but if you copy any parent category along with its subs everything works just fine.
If you only want to copy the sub categories of the parent (to add them to another parent) just include the parent and once parent and subs are duplicated or copied select all subs and: 1) move the selected subs to an existing category or 2) rename the the parent and don't forget to check and see if the copied subs are attached to the correct parent which they should be once parent is renamed.
Hope this addition to Jacob Friedman's contribution helps...

Personally I think something like this should be expanded and included in the K2 Components core files as it is implicated in Joomla's core!
How about everyone else? please post comments so we can get this done....

Thanks again
irish
Attachments:

Please Log in or Create an account to join the conversation.

More
14 years 2 months ago #80876 by Peter Gasser
Replied by Peter Gasser on topic Copy Category
The above code works perfect to copy categories, but I would also like to copy the sample content in this categories for various clients.
Anybody has code for that?
Thanks
Peter

Please Log in or Create an account to join the conversation.

More
14 years 2 months ago #80877 by irish
Replied by irish on topic Copy Category
I was not saying the code didn't work the directions just confused me. I just figured maybe someone else with limited knowledge in scripting might need some more assistance in making the changes as I did.

Peter Gasser said:The above code works perfect to copy categories, but I would also like to copy the sample content in this categories for various clients. Anybody has code for that?
Thanks
Peter

Please Log in or Create an account to join the conversation.

More
14 years 2 months ago #80878 by Peter Gasser
Replied by Peter Gasser on topic Copy Category
The above code works perfect to copy categories, but I would also like to copy categories and the sample content in this categories for various clients.
Anybody has code for that?
Thanks
Peter

Jacob Friedman said:Sorry, everything here's located in /administrator/components/com_k2. Drop it in, ready to roll. Cheers :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum