Subcategories

Locked
justingraybauer
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Sun Feb 26, 2006 10:45 am

Subcategories

Post by justingraybauer » Sat Mar 18, 2006 2:49 am

Subcategories for content

pauldavid
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Mar 20, 2006 11:16 pm

Re: Subcategories

Post by pauldavid » Tue Mar 21, 2006 1:11 am

We needed subcategories for a site we are currently working on at achieve internet. 
I found that it was pretty simple to add this functionality to joomla, as most of the work has already been done. 
I made three small changes to com_content and four changes to the admin com_content

Since categories already have a parent_id field in the database, I went ahead and created two new categories, setting their parent id's to that of another category in the database.
This made the structure section 1 > existing category > new subcategory > new sub-subcategory.

When I went to check out what the front did, I found that it almost worked.  The problem was that when you went to the section and any category, all of the categories in that section were listed. 
This was a simple fix.  I only had to add one line of code to two different functions.  One change was in showCategory and one was in showSection of com_content

In /components/content.php in showSection() line 279 I added

Code: Select all

. " AND a.parent_id = 0 " 
so that when you view categories in a section, only top level categories display.
The block of code that I changed looks like:

Code: Select all

// Main Query
$query = "SELECT a.*, COUNT( b.id ) AS numitems"
. "\n FROM #__categories AS a"
. "\n LEFT JOIN #__content AS b ON b.catid = a.id"
. $xwhere2
. "\n WHERE a.section = '$section->id'" 
. "\n AND a.parent_id = 0 " //This is my new line
. $xwhere
. $access
. "\n GROUP BY a.id"
. $empty
. $empty_sec
. "\n ORDER BY $orderby"
Next, I fixed it so that when you view a category, the only sub categories that display are subcategories of that category.
In /components/content.php showCategory() around line 400, I changed the SQL query to only get child categories rather then list all categories in that section.
I added

Code: Select all

. "\n AND c.parent_id = $id " 
to the following block of code:

Code: Select all

// get the list of other categories
$query = "SELECT c.*, COUNT( b.id ) AS numitems"
. "\n FROM #__categories AS c"
. "\n LEFT JOIN #__content AS b ON b.catid = c.id "
. $xwhere2
. ( $noauth ? "\n AND b.access <= $gid" : '' )
. "\n WHERE c.section = '$category->section'"
. $xwhere
. ( $noauth ? "\n AND c.access <= $gid" : '' )
. "\n AND c.parent_id = $id " //This is my new line
. "\n GROUP BY c.id"
. $empty
. "\n ORDER BY c.ordering"
;
After I did this, I noticed one problem.  If a category only had one subcategory, it would not display.  This too, was a simple fix.
Starting at line 93 of /components/content.html.php in the method showContentList() I changed `count( $other_categories ) > 1)` to count( $other_categories ) > 0)
Here is the block of code:

Code: Select all

<?php
			// Displays listing of Categories
			if ( ( ( count( $other_categories ) > 0 /*<- This was a 1*/ ) || ( count( $other_categories ) < 2 && count( $items ) < 1 ) ) ) {
				if ( ( $params->get( 'type' ) == 'category' ) && $params->get( 'other_cat' ) ) {
					HTML_content::showCategories( $params, $items, $gid, $other_categories, $catid, $id, $Itemid );
				}
				if ( ( $params->get( 'type' ) == 'section' ) && $params->get( 'other_cat_section' ) ) {
					HTML_content::showCategories( $params, $items, $gid, $other_categories, $catid, $id, $Itemid );
				}
			}
?>
Now it worked just as I wanted it to... but for one detail.  On the back end, I needed a way to select the the proper subcategory form a tree list so that I could tell what level I was putting the content in.
I used the treeSelectList() method in mosHTML to do this.

I modified administrator/components/com_content/admin.content.php

on lines 521 and 538 I replaced:

Code: Select all

$lists['catid'] = mosHTML::selectList( $categories, 'catid', 'class="inputbox" size="1"', 'id', 'name' );
with

Code: Select all

$lists['catid']=mosHTML::treeSelectList( $categories, 1, array(), 'catid', 'class="inputbox" size="1"', 'value', 'text', intval( $row->catid ) ) ;
The treeSelectList method needs the parent id of each list item, so I modified the preceding SQL statements to get the parent id.  I just added ", parent_id as parent " to:

Code: Select all

   $query = "SELECT id, name, parent_id as parent "
 . "\n FROM #__categories"
 . "\n WHERE section = '$section->id'"
 . "\n ORDER BY ordering" 
 ;

 $database->setQuery( $query );
AND line 522:

Code: Select all

 $query = "SELECT id, name, parent_id as parent "
 . "\n FROM #__categories"
 . $where
 . "\n ORDER BY ordering"
In the newest joomla version,  it was only in one place; line 498.

Code: Select all

 	$query = "SELECT id, name, section, parent_id as parent "
	. "\n FROM #__categories"
	. "\n WHERE section IN ( '$section_list' )"
	. "\n ORDER BY ordering"
	;

That was all it took.  Really, it was a pretty simple fix to what could be seen as a pretty big draw back to joomla.  I have posted the modified files at http://www.achieveinternet.com/blog/?p=7#more-7 . I am working with version 1.0.7 and the version I posted is from 1.0.8. so you may have to make a few more small changes.  Feel free to download and use them, or integrate my changes into future releases or joomla.
Last edited by pauldavid on Tue Mar 21, 2006 1:27 am, edited 1 time in total.

User avatar
focalguy
Joomla! Guru
Joomla! Guru
Posts: 909
Joined: Fri Aug 19, 2005 2:46 am
Location: Washington State, USA
Contact:

Re: Subcategories

Post by focalguy » Tue Mar 21, 2006 2:57 am

Quite interesting... Do you have a link to the site that is using it? If you don't discover any problems with it I think others would be interested.

sural98
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 214
Joined: Fri Feb 17, 2006 5:32 pm

How to Create Subcategory Under A Category At Backend

Post by sural98 » Tue Mar 21, 2006 12:56 pm

I replaced pauldavid's files with the existing ones. There were no errors but I do not know how to create subcategories under a category since there seems no options to do so at backend.

pauldavid
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Mar 20, 2006 11:16 pm

Re: Subcategories

Post by pauldavid » Tue Mar 21, 2006 5:05 pm

Like I said in my post, I had to create the categories and then set the the parent_id of the "subcategory" to the id of the parent category in the database manually.  Creating an interface in com_category for this is slightly more involved.  I will try to post on how to do that in the next day or two, but it involves some DHTML or AJAX to do it right.
Last edited by pauldavid on Tue Mar 21, 2006 6:18 pm, edited 1 time in total.

pauldavid
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Mar 20, 2006 11:16 pm

Re: Subcategories

Post by pauldavid » Wed Mar 22, 2006 1:28 am

focalguy wrote: Quite interesting... Do you have a link to the site that is using it? If you don't discover any problems with it I think others would be interested.
I dont have a link to my 1.0.7 changes I posted about, but I made similar changes to the newest development version (1.5) and have it up at:  http://joomla15.wpmdev.tzo.com/

Check out the news section. 
It goes :

news
|_sports
|  |_football
|  |_baseball
|_world
  |_Asia
  |_Europe

...where news is a section.  Sports and world are categories.  Football, baseball, Asia and Europe are all subcategories.

I will try to post about the admin side soon.

User avatar
focalguy
Joomla! Guru
Joomla! Guru
Posts: 909
Joined: Fri Aug 19, 2005 2:46 am
Location: Washington State, USA
Contact:

Re: Subcategories

Post by focalguy » Wed Mar 22, 2006 3:45 am

Looks nice. I'd be interested to hear the drawbacks the developers must see to this solution or I'm sure they would have implemented it. Looks like it works for you, so more power to you! :)

sural98
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 214
Joined: Fri Feb 17, 2006 5:32 pm

What should be the parent_ids of the subcategories?

Post by sural98 » Wed Mar 22, 2006 9:42 am

Parent ids of the cats are set to zero by default. A more precise explanation is needed. For example
Cat 1 and Cat 2 are the categories with parent_id=0.
What should be the parent_ids of the subcategories?

User avatar
Hackwar
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3763
Joined: Fri Sep 16, 2005 8:41 pm
Location: NRW - Germany
Contact:

Re: Subcategories

Post by Hackwar » Wed Mar 22, 2006 10:01 am

focalguy wrote: Looks nice. I'd be interested to hear the drawbacks the developers must see to this solution or I'm sure they would have implemented it. Looks like it works for you, so more power to you! :)
The devs didn't take up on this one, because it is a hack in a way and does not remove the restrictions set by the original system completely. They are working on a completely new categorization system, that will only consist of categories. Section are going to be deprecated then. Changing this system so completely is somewhat delecate due to the mass of compatibility issues. The implementation of the system itself is easy.
god doesn't play dice with the universe. not after that drunken night with the devil where he lost classical mechanics in a game of craps.

Since the creation of the Internet, the Earth's rotation has been fueled, primarily, by the collective spinning of English teachers in their graves.

User avatar
focalguy
Joomla! Guru
Joomla! Guru
Posts: 909
Joined: Fri Aug 19, 2005 2:46 am
Location: Washington State, USA
Contact:

Re: Subcategories

Post by focalguy » Wed Mar 22, 2006 3:26 pm

Thanks for the explanation Hackwar. I had just assumed it was the complexity of implementing it, but I see it's the backwards compatability issues that cause it to be difficult or time consuming at least.

Personally, I'll wait for now but if I have a pressing need I'll remember this post.

pauldavid
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Mar 20, 2006 11:16 pm

Re: What should be the parent_ids of the subcategories?

Post by pauldavid » Wed Mar 22, 2006 5:40 pm

sural98 wrote: Parent ids of the cats are set to zero by default. A more precise explanation is needed. For example
Cat 1 and Cat 2 are the categories with parent_id=0.
What should be the parent_ids of the subcategories?
If Cat one is the parent of cat 2, and its id is 11, then the id of cat one (11) should be put into the parent_id field of cat 2.  Cat 1 should have a parent id of 0 as it is a top level category.  I have a simple mod to com_category that I will post tonight, so that you can set it from the admin side rather then in the database.  I went ahead and created a non javascript admin form field as it seemed the simplest to use and implement.  The DHTML version took more code changes and made the interface slight less intuitive.

pauldavid
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Mon Mar 20, 2006 11:16 pm

Re: Subcategories

Post by pauldavid » Wed Mar 22, 2006 5:49 pm

Hackwar wrote: The devs didn't take up on this one, because it is a hack in a way and does not remove the restrictions set by the original system completely. They are working on a completely new categorization system, that will only consist of categories. Section are going to be deprecated then. Changing this system so completely is somewhat delecate due to the mass of compatibility issues. The implementation of the system itself is easy.
Thanks.  That was my guess.  If you are going to do it, do it right.  Some of us need to do it now though.  Since I couldn't findy and explanations as to how to do it at all, I thought I would post how I did it, for those of us who need it now.  Becaue it required so few changes to the system, and none to the database, I would be interested to an example of a backword compatablility issue, if anyone can think of one.  I know the breadcrumb needs to be tweeked, but my guess it that it will, again, be one ore two lines of code.

sswriter
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Thu May 18, 2006 1:46 am

Re: Subcategories

Post by sswriter » Wed May 24, 2006 11:15 pm

This is so cool because it does exactly what I need (and see as a major flaw in the design of Joomla).

What's the status of the admin side? How do I make the database changes without doing them manually? I don't have that many, but it would still be nice to have it done through a clean interface.

Thanks,

Sid

Beatrice
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Thu May 25, 2006 9:49 pm

Re: Subcategories

Post by Beatrice » Wed May 31, 2006 12:28 am

Hi!
Is there any chance to get the isntruction how to do the database change manually.

I have exchange the files from your zip-file to my joomla 1.0.8 installation at http://www.parlplatsen.se/CMS/

And if you look at the menu called Sy it has thre childs Peyote, Ladder, Brick. Peyote is a Category the other two are content pages.

Sy (category)
|-Peyote (category)
  |-Flat peyote (content item)
  |-Tubulär peyote (content item)
  |-Cirkulär peyote (content item)
|-Ladder (content item)
|-Brick (content item)

If I click on Sy (http://www.parlplatsen.se/CMS/index.php ... &Itemid=37) I would like Peyote to be shown in the list of categories/content items in the main page now it is just Ladder and Brick that is shown there.

Before I installed your files and had "Other Categories" set to Show for Sy all of my categories under Hantverk was shown in the list.

What do I have to do to be able to display the Peyote option in the listing as you have on your test site. I can't find what options to set and if I have to recrate a category and set som specific options or not or just do some database changes, but where and what.

Regards,
Béatrice

Beatrice
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Thu May 25, 2006 9:49 pm

Re: Subcategories

Post by Beatrice » Wed May 31, 2006 10:14 am

Beatrice wrote: Hi!
Is there any chance to get the isntruction how to do the database change manually.
I did it without instructions, I was just a little bit too tired yesterday night to figure it out. Now it is working as expected (just has to get rid of the annoying bullet infront of Peyote in the subgrouplist on the page "Sy".

Thank you so much for the files.

Regards,
Béatrice

irstudio
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Mon Sep 05, 2005 7:45 pm

Re: Subcategories

Post by irstudio » Wed May 31, 2006 11:28 pm

I always wondered why there were no subcategories in Joomla!
I thought of three possible reasons

1)
for the longest time I thought that the reason for not doing subcategories was that database changes were nto allowed until 2.x
BUT that's clearly not needed since parent_id is already in the database

2)
it might break backwards compatibility
BUT that's clearly not the case either if you keep sections for now in the 1.x branch

3)
the developers are planning on eliminating sections and moving to categories only
BUT that doesn't mean they couldn't add subcategories into 1.0.9 without breaking anything for now
        2.0 and its reworked categorization is at least a YEAR away based on the current history of releases
        and IMHO that is simply too long to wait for any improvement at all

anyway,
thank you so much for starting this work
I have an idea of how to do the section/category/category/... selection without requiring AJAX or Javascript or DHTML

you make one drop down menu called "Parent Item"
and you populate it like this:
(i am listing the name and the value of each item)
------------------------------------------------
Section1              (parent_item=1)
.  L category1    (parent_item=1_2)
.  L category2    (parent_item=1_2)
Section2              (parent_item=2)
.  L category3      (parent_item=2_3)
.  .  Lcategory4  (parent_item=2_4)
.  L category5      (parent_item=2_5)
-------------------------------------------------
once the user hits submit
the code can parse the value of parent_item to figure out the section and the category (just explode into an array on '_')
that the new submitted item or the newly created category belongs to

easy as pie really  :P
I will keep you posted of my progress in implementing this

=================
I really think once the admin/edit code is ready (so nobody has to edit the database manually),
this work should be pushed for inclusion into the core as quickly as possible.

Thanks again for starting this work
Last edited by irstudio on Wed May 31, 2006 11:34 pm, edited 1 time in total.

dancop
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Thu Nov 24, 2005 12:58 pm

Re: Subcategories

Post by dancop » Mon Jun 26, 2006 2:44 pm

Hello there!
I have made the changes proposed it this topic. I'm using 1.0.9 Joomla version.
Everything seems ok and working, besides admin end of sections.
When I create a new section (or try to modify a newly created section) I'm getting this message:
Fatal error: Call to a member function on a non-object in /home/przewodn/public_html/joomla/administrator/components/com_sections/admin.sections.php on line 235

I wonder how a change to a code regarding categories influenced the sections... I'm zero, null, nill in PHP :) I can't figure it out by myself.

The code of admin.sections.php around line 235 is:

Code: Select all

	// handling for MOSImage directories
		if ( trim( $row->params ) ) {
			// get params definitions
line 235 		$params = new mosParameters( $row->params, $mainframe->getPath( 'com_xml', 'com_sections' ), 'component' );
			$temps 	= $params->get( 'imagefolders', '' );
			
			$temps 	= explode( ',', $temps );
			foreach( $temps as $temp ) {
				$selected_folders[] = mosHTML::makeOption( $temp, $temp );
			}
		} else {
			$selected_folders[] = mosHTML::makeOption( '*1*' );
		}			
	} else {
		$row->scope 		= $scope;
		$row->published 	= 1;
		$menus 				= array();
Could anybody help? Please :)
Regards,
DW

PS. Looking for a complete subsections or subcategories solution. I really need it.

sjordan
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Thu May 25, 2006 6:23 pm

Re: Subcategories

Post by sjordan » Tue Jul 11, 2006 12:36 am

I'm using version 1.10 and I made all the changes.

The back end is not resulting in a nested tree structure when I go to ~/joomla/administrator/index2.php?option=com_content&sectionid=0&task=new

What should the section column value be for the subcategories in the bd table jos_categories?

User avatar
baijianpeng
Joomla! Guru
Joomla! Guru
Posts: 516
Joined: Mon Mar 20, 2006 3:17 pm
Location: China
Contact:

Re: Subcategories

Post by baijianpeng » Wed Sep 27, 2006 3:49 am

Thank you paul ! I had read your article about creating subcategories in Joomla.

In fact I had beeing seeking for such a resolution for nearly 3 months since I met Joomla.

I had applied one hack package called sef_patch. So now the files you mentioned seem different from the original version Joomla 1.0.10.

You can find this patch at:

http://extensions.joomla.org/component/ ... Itemid,35/

Could you please do some research in the sef_patch and give me instructions to hack those files agin to be able to set up subcategories ?

Thank you very much !
JoomlaGate - Chinese Joomla Users' Portal

http://www.joomlagate.com

User avatar
Gibzon
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Wed Sep 21, 2005 11:27 pm

Re: Subcategories

Post by Gibzon » Thu Oct 05, 2006 5:56 am

when Joomla is going to add a subcategories feature? :-\

robs1412
Joomla! Apprentice
Joomla! Apprentice
Posts: 34
Joined: Sat Mar 18, 2006 4:24 pm

Re: Subcategories

Post by robs1412 » Tue Jan 09, 2007 9:38 pm

Hi guys,

I have installed the patch on the latest Joomla 1.0.12 and I can´t seem to figure out why it´s not working. In order to create the subcategories from the backend, I am using the following component: Nested Categories 1.0.1, which I am attaching. Hope that helps someone. I just would like to know how to add this tree selection to the content item. Otherwise, the people won´t be able to add contents to the right subcategory in the frontend. I would REALLY appreciate it!
By the way ... Thanks for starting this, beause it directed me in the right direction!

all the best,

Robert
You do not have the required permissions to view the files attached to this post.

ipulxd
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Jan 19, 2007 1:45 pm

Re: Subcategories

Post by ipulxd » Thu Feb 22, 2007 5:53 am

pauldavid wrote:
I dont have a link to my 1.0.7 changes I posted about, but I made similar changes to the newest development version (1.5) and have it up at:  http://joomla15.wpmdev.tzo.com/
Sorry, but i cant open it.

m_aboali
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Apr 03, 2007 11:10 am

Re: Subcategories

Post by m_aboali » Tue Apr 03, 2007 5:44 pm

for the com_nestedcategories to work fine you have to
change the category query  in
/components/com_content/admin.content.php

that's

$query = "SELECT id, name, section, parent_id as parent "
. "\n FROM #__categories"
. "\n WHERE section IN ( '$section_list' )"
. "\n ORDER BY ordering"
;


to

$query = "SELECT id, name, section, parent_id as parent "
. "\n FROM #__categories"
. "\n WHERE  ( $section_list )"
. "\n ORDER BY ordering"
;

this is for joomla 1.0.12

User avatar
darb
Joomla! Ace
Joomla! Ace
Posts: 1320
Joined: Thu Jul 06, 2006 12:57 pm
Location: Stockholm Sweden
Contact:

Re: Subcategories

Post by darb » Sun Apr 15, 2007 8:53 pm

Yes this is important issue. I have started an discussion about it here.

http://forum.joomla.org/index.php/topic,160429.0.html

Cheers!

ssnobben
Success in the long run Its not about the code its about the people and community that's make it!
Its not what you say its what you do that matters!

Darb - aka ssnobben

MatteoSp
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon May 14, 2007 12:03 am

Re: Subcategories (solution is incomplete)

Post by MatteoSp » Mon May 14, 2007 12:09 am

When you select another Section, the javascript procedure that refresh the list of categories, put them in the standard plain list.

smchugh
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Aug 08, 2007 4:15 am

Re: Subcategories

Post by smchugh » Wed Aug 08, 2007 5:10 am

I haven't seen a post from pauldavid in a while, I hope he's still around.  I found that there's a small problem with breadcrumb.  When you click on a subcategory from the menu then the proper breadcrumb trail is present.  However, if you go to its parent category (the first level category after section for this case) and then click on the link for the sub category, the sub category never shows up in the breadcrumb trail.  This then persists down the line so that a content item in the sub category shows up as the next item after the parent category in the breadcrumb trail, skipping the subcategory altogether.  I found that this is also the case in the example page presented by Beatrice.

I would hope this is an easy fix for someone proficient in php.  I should note that I am using Joomla 1.0.13, but everything with this hack is working perfectly other than this little issue.  If anyone could help that would be great.  It's just one of those little things that should be fixed before a site goes live.

This is an amazingly useful hack! and much better than DeepPockets because it follows the conventions of the rest of the category structure.

User avatar
darb
Joomla! Ace
Joomla! Ace
Posts: 1320
Joined: Thu Jul 06, 2006 12:57 pm
Location: Stockholm Sweden
Contact:

Re: Subcategories

Post by darb » Mon Oct 01, 2007 7:12 am

smchugh wrote: I haven't seen a post from pauldavid in a while, I hope he's still around.  I found that there's a small problem with breadcrumb.  When you click on a subcategory from the menu then the proper breadcrumb trail is present.  However, if you go to its parent category (the first level category after section for this case) and then click on the link for the sub category, the sub category never shows up in the breadcrumb trail.  This then persists down the line so that a content item in the sub category shows up as the next item after the parent category in the breadcrumb trail, skipping the subcategory altogether.  I found that this is also the case in the example page presented by Beatrice.

I would hope this is an easy fix for someone proficient in php.  I should note that I am using Joomla 1.0.13, but everything with this hack is working perfectly other than this little issue.  If anyone could help that would be great.  It's just one of those little things that should be fixed before a site goes live.

This is an amazingly useful hack! and much better than DeepPockets because it follows the conventions of the rest of the category structure.
Looking at his web link I think they are "gone" now for Drupal bcs of this never happens to Joomla  :(
Success in the long run Its not about the code its about the people and community that's make it!
Its not what you say its what you do that matters!

Darb - aka ssnobben

User avatar
mcsmom
Joomla! Exemplar
Joomla! Exemplar
Posts: 7985
Joined: Thu Aug 18, 2005 8:43 pm
Location: New York
Contact:

Re: Subcategories

Post by mcsmom » Wed Oct 03, 2007 2:12 pm

Hi,

Maybe it would be good to summarize and restart this thread in the new tips and trips forum.  :)
So we must fix our vision not merely on the negative expulsion of war, but upon the positive affirmation of peace. MLK 1964.
http://officialjoomlabook.com Get it at http://www.joomla.org/joomla-press-official-books.html Buy a book, support Joomla!.


Locked

Return to “Wishlists and Feature Requests - Archive”