Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [Mod Addon] Expandable Menu for Subcategories  (Read 3704 times)

centered

  • Guest
[Mod Addon] Expandable Menu for Subcategories
« on: February 22, 2008, 11:20:23 pm »

This is an add on for the Subcategories modification.  This mod switches between the basic menu and a new enhanced menu that shows a list of articles underneath each Category and subcategory.

You need to switch this in the settings to turn the expanding menu on and off

Big thanks to Keyrocks for pushing this and Bakercad for cleaning up code!

0.  Backup

1. We need a new database setting:
Code: [Select]
INSERT INTO settings VALUES (75, 'menutype', '');
2. In languages add:
Code: [Select]
$l['menutype'] = 'Display enhanced category menu';
3. In title, before the last closing bracket:
Code: [Select]
jsuser();
4. Replace Categories and Subcategories
Code: [Select]
// DISPLAY CATEGORIES
function categories() {
$menutype = s('menutype') == 'on';
$categorySEF = get_id('category');
$class = $category_title == $categorySEF ? ' class="current"' : '';
$query = "SELECT * FROM ".db('prefix')."categories WHERE published = 'YES' AND subcat = 0 ORDER BY catorder,id ASC";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) { echo '<li>'.l('article_not_exist').'</li>'; }
while ($r = mysql_fetch_array($result)) {
$cm_result = mysql_query("SELECT * FROM ".db('prefix')."articles WHERE position = 1 AND category = $r[id] AND published = 1");
$num_rows = mysql_num_rows($cm_result);
$subresult = mysql_query("SELECT * FROM ".db('prefix')."categories WHERE subcat = '".$r['id']."' ORDER BY catorder,id ASC");
$subnum_rows = mysql_num_rows($subresult);
$category_title = $r['seftitle'];
$class = $category_title == $categorySEF ? ' class="current"' : ''; 
$content = 'c'.$r['id'];
// If menutype is enhanced
if (!empty($menutype)) { 
echo '<li><a'.$class.' title="'.$r['description'].'" onclick="toggle(\''.$content.'\')" style="cursor: pointer;">'.$r['name'];
echo (s('num_categories') == 'on' ? ' ('.$num_rows.')' : '').'</a>';
// Check if there are articles and/or subcategories, then display the ul
  if (($num_rows > 0) || ($subnum_rows > 0)) {
echo '<ul id="'.$content.'" style="display: none;">';
// If there are articles, display them
if ($num_rows > 0) {
while ($art = mysql_fetch_array($cm_result)) {
echo '<li>'.l('divider').' <a href="'.db('website').$category_title.'/'.$art['seftitle'].'/" title="'.$art['title'].'">'.$art['title'].'</a></li>';         
}
}
// If there are subcategories, display them
if ($subnum_rows > 0) {
subcategories($r['id'],$menutype);
}
echo '</ul>';
} else { // Throw up an error message
echo '<ul id="'.$content.'" style="display: none;"><li>'.l('article_not_exist').'</li></ul>';
}
} else { // Basic menu is wanted
echo '<li><a'.$class.' href="'.db('website').$category_title.'/" title="'.$r['description'].'">'.$r['name'];
echo (s('num_categories') == 'on' ? ' ('.$num_rows.')' : '').'</a>';
$parent = $r['id'];
if ($category_title == $categorySEF) { subcategories($parent,$menutype); }
$subcat = retrieve('subcat','categories','seftitle',$categorySEF);
if ($subcat == $parent) { subcategories($subcat,$menutype); }
}
echo '</li>';
}
}

// DISPLAY SUBCATEGORIES
function subcategories($parent,$submenu) {
if (empty($submenu)) { echo '<ul>'; }
$categorySEF = get_id('category');
$subquery = "SELECT * FROM ".db('prefix')."categories WHERE published = 'YES' AND subcat = '".$parent."' ORDER BY catorder,id ASC";
$subresult = mysql_query($subquery);
while ($s = mysql_fetch_array($subresult)) {
$subcalc_num_query = "SELECT * FROM ".db('prefix')."articles WHERE position = 1 AND category = $s[id] AND published = 1"; 
$subcm_result = mysql_query($subcalc_num_query);
$subnum_rows = mysql_num_rows($subcm_result);
$subcat_title = $s['seftitle'];
$subcatSEF = cat_rel($s['id'], 'seftitle');
$class = $subcat_title == $categorySEF ? ' class="current"' : '';
$subcontent = 's'.$s['id'];
// If menutype is enhanced
if (!empty($submenu)) {
echo '<li class="subcat"><a'.$class.' title="'.$s['description'].'" onclick="toggle(\''.$subcontent.'\')" style="cursor: pointer;">'.$s['name'];
echo (s('num_categories') == 'on' ? ' ('.$subnum_rows.')' : '').'</a>';
// If there are articles, display them
if ($subnum_rows > 0) {
echo '<ul id="'.$subcontent.'" style="display: none;">';
while ($sart = mysql_fetch_array($subcm_result)) {
echo '<li>'.l('divider').' <a href="'.db('website').$subcatSEF.'/'.$sart['seftitle'].'/" title="'.$sart['title'].'">'.$sart['title'].'</a></li>';         
}
echo '</ul>';
} else { // Throw up an error message
echo '<ul id="'.$subcontent.'" style="display: none;"><li>'.l('article_not_exist').'</li></ul>';
}
} else { // Basic menu is wanted
echo '<li class="subcat"><a'.$class.' href="'.db('website').$subcatSEF.'/" title="'.$s['description'].'">'.$s['name'];
echo (s('num_categories') == 'on' ? ' ('.$subnum_rows.')' : '').'</a>';
}
echo '</li>';
}
if (empty($submenu)) { echo '</ul>'; }
}

5. In settings, find the first line and add the second:
Code: [Select]
echo html_input('checkbox', 'num_categories', 'nc', '', l('a_num_categories'), '', '', '', '', (s('num_categories') == 'on' ? 'ok' : ''), '', '', '', '', '');
echo html_input('checkbox', 'menutype', 'mt', '', l('menutype'), '', '', '', '', (s('menutype') == 'on' ? 'ok' : ''), '', '', '', '', '');

6. In processing, same pattern:
Code: [Select]
$word_filter_change = $_POST['word_filter_change'];
$menutype = $_POST['menutype'];

6a. At the end of the big array after the previous line, BEFORE the closing parenthesis add:
Code: [Select]
'menutype' => $menutype,
7. At the very end of the file add:
Code: [Select]
<?php
// user javascript -- toggle
function jsuser() { ?>

<script type="text/javascript">
//<![CDATA[
// toggle dynamic divs
function toggle(div) {
if (document.getElementById(div).style.display=='') {
document.getElementById(div).style.display = 'none';
return
} document.getElementById(div).style.display = '';
}
//]]>
</script>
<?php }?>
Logged

DataVoyager

  • Newbie
  • *
  • Karma: 0
  • Posts: 6
Re: [Mod Addon] Expandable Menu for Subcategories
« Reply #1 on: September 26, 2008, 02:45:29 am »

Fantastic addition. I've seen it working and I think it makes a big difference. I tried to get it to work with the Multi User Version but the categories just disappeared. Is this my ineptitude or has it not been implemented with the Multi User version yet?

Cheers

DV
Logged

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [Mod Addon] Expandable Menu for Subcategories
« Reply #2 on: September 26, 2008, 08:44:03 am »

Great job, Jase...

Data, welcome. Please keep in mind to not post duplicates in various threads. Hope you'll enjoy your stay here and that you'll keep coming back.

Logged
"It's only dead fish that goes with the flow... "
Updated