sNews Forum

sNews 1.6 (previous version) => Mods/addons => Topic started by: centered on February 22, 2008, 05:20:01 am

Title: [PACKAGE] Subcategories
Post by: centered on February 22, 2008, 05:20:01 am
* DOWNLOADS ARE DOWN ATM *

Probably one of the most requested modifications, is pretty much complete. Big Thank yous to Keyrocks, Rui, Joost and bakercad for thier help with making this a reality!

This is a core package, 1 level only. The ability to build further is there if the desire is to do that. If you plan your site accordingly, 1 level is sometimes all one needs.

Please note a few things:
1. No Home Category.
    -  Home has been replaced by an 'Uncategorized' category since there is a great debate about home being a category.
2. Option to show each article on the front page (thanks to Bakercad)
    - With Home being gone as a category, we needed a way to show articles on the front page, just a click away
3. Same name functionality (thanks to Bakercad)
    - for instance a store would have Mens Coats and Women's Coats, well you can have Mens -> Coats as category and subcategory, then Womens -> Coats as the same.
4. Booster modifications
   - Thanks to Joost and Invarbrass, I have added booster mods to speed up the engine.

NOTE :
1. In index.php - find and remove
Code: [Select]
<? timing('end'); ?>
Validation fix:
Replace function subcategories
Code: [Select]
// DISPLAY SUBCATEGORIES
// Subcategories new function
function subcategories($parent) {
$categorySEF = get_id('category');
if (s('num_categories') != 'on') { $sql = ''; }
else { $sql = ", (select count(id) from articles where category = cat.id AND position = 1 AND published = 1) as cat_articles "; }
$subresult = mysql_query("select cat.id, cat.name, cat.seftitle, cat.description$sql from categories as cat WHERE subcat = ".$parent." AND published = 'YES' ORDER BY catorder,id ASC");
// validation fix
if (mysql_num_rows($subresult) > 0) { echo '<ul>'; }
while ($s = mysql_fetch_array($subresult)) {
$subcat_title = $s['seftitle']; $subcatSEF = cat_rel($s['id'], 'seftitle');
$class = $subcat_title == $categorySEF ? ' class="current"' : '';
if (s('num_categories') != 'on') { $num = ''; } else { $num = ' ('.$s['cat_articles'].')'; }
echo '<li class="subcat"><a'.$class.' href="'.db('website').$subcatSEF.'/" title="'.$s['description'].'">'.$s['name'].$num.'</a>';
/* Brave souls could do multiple levels just by looping this function
$parent = $s['id'];
if ($category_title == $categorySEF) { subcategories($parent); }
$subcat = retrieve('subcat','categories','seftitle',$categorySEF);
if ($subcat == $parent) { subcategories($subcat); }
*/
echo '</li>';
}
// validation fix
if (mysql_num_rows($subresult) > 0) { echo '</ul>'; }
}

Bugs -

This removes home as a category and makes sure the user doesn't delete the default category (which should be 1)
. Find:
Code: [Select]
echo html_input('hidden', 'id', 'id', $categoryid, '', '', '', '', '', '', '', '', '', '', '');
echo html_input('submit', 'delete_category', 'delete_category', l('delete'), '', 'button', 'onclick="javascript: return pop()"', '', '', '', '', '', '', '', '');

Replace with:
Code: [Select]
echo html_input('hidden', 'id', 'id', $categoryid, '', '', '', '', '', '', '', '', '', '', '');
// subcategories
if ($categoryid != 1) {
echo html_input('submit', 'delete_category', 'delete_category', l('delete'), '', 'button', 'onclick="javascript: return pop()"', '', '', '', '', '', '', '', ''); } else { echo ''; }
}


Another BUG: From admin panel if you try to "view" a PAGE, you won't get it - engine doesn't add "home" into the link - so the link remains "localhost/subs//page/" instead of "localhost/subs/home/gerberrerebe/"
Code: [Select]
// subcategories
// echo '<p>'.date(s('date_format'), strtotime($r['date'])).' <strong>'.$r['title'].'</strong>'.$cat_subcat.' '.l('divider').' <a href="'.db('website').cat_rel($r['category'],'seftitle').'/'.$articleSEF.'">'.l('view').'</a> ';
$catSEF = $r['category'] == 0 ? l('home_sef') : cat_rel($r['category'], 'name');
echo '<p>'.date(s('date_format'), strtotime($r['date'])).' <strong>'.$r['title'].'</strong>'.$cat_subcat.' '.l('divider').' <a href="'.db('website').$catSEF.'/'.$articleSEF.'">'.l('view').'</a> ';

One can still unpublish it and when adding an article, he/she'd get an empty category-selection-dropdown-list... It's pretty dirty, don't you think?

Now that is a bug right there. Thanks i will get to fixing that too. 
FIX:
Find:
Code: [Select]
if ($subcat=='cat') echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_category'), '', '', '', '', $frm_publish, '', '', '', '', '');  else echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_subcategory'), '', '', '', '', $frm_publish, '', '', '', '', '');
Replace with:
Code: [Select]
if ($subcat=='cat' && $categoryid != 1) {
echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_category'), '', '', '', '', $frm_publish, '', '', '', '', ''); } 
elseif ($subcat!='cat' && $categoryid != 1) {
echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_subcategory'), '', '', '', '', $frm_publish, '', '', '', '', '');
}
else { echo ''; }

New fix:
Find:
Code: [Select]
$name = empty($categoryid) || $subcat!='cat' ? retrieve('name','categories','id',$sub_cat):'';

Replace with:
Code: [Select]
if ($sub_cat!='cat') { $jresult = mysql_query("select name from ".db('prefix')."categories where id = $sub_cat"); while($j = mysql_fetch_array($jresult)) { $name = $j['name']; }}

Codetwist Booster thanks to Armen!
Replace get_id with these 2 functions:
Code: [Select]
function get_id($parameter) {
$url = array();
$url = explode('/', $_GET['category']);
$get_id = array();
if ($url['1']) {
  $sub_cat = cleanGetSef($url['1']);
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'");
if (!$result || !mysql_num_rows($result)) { $subcat=''; } else {
$r = mysql_fetch_array($result); {$subcat = $r['seftitle'];}
$get_id['subcategory'] = $subcat;}
}
$get_id['category'] = cleanGetSef($url['0']);
if ($url['1'] && !$subcat) {
$get_id['article'] = cleanGetSef($url['1']);
} elseif ($url['2'] && $subcat) {
$get_id['article'] = cleanGetSef($url['2']);
}
if ($url['2'] && !$subcat) {
$get_id['commentspage'] = cleanGetSef($url['2']);
} elseif ($url['3'] && $subcat) {
$get_id['commentspage'] = cleanGetSef($url['3']);}
if (isset($get_id[$parameter])) {return cleanGetSef($get_id[$parameter]);}
}

function cleanGetSef($inSef) {
    if ($inSef == '') { $sef = ''; }
    elseif (is_int($inSef)) { $sef = (int) $inSef; } else {
      if (! preg_match('/^[a-z0-9\-_#\.]+$/', $inSef)) { $sef = false; }
      else { $sef = $inSef; }} return $sef;}

In function articles()
replace teh line after:
Code: [Select]
$home = l('home_sef'); $categorySEF = get_id('category');

with:
Code: [Select]
$categorySEF = empty($categorySEF) ? $home : $categorySEF;
Then find:
Code: [Select]
paginator($categorySEF, $currentPage, $totalPages, '', l('paginator'));Before that line add:
Code: [Select]
if (!empty($subcatSEF)) { $categorySEF = $categorySEF.'/'.$subcatSEF; } else { $categorySEF = $categorySEF; }
Title: Re: [PACKAGE] Subcategories
Post by: jlhaslip on February 22, 2008, 07:12:28 am
Do you have a Demo site?
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 22, 2008, 12:34:30 pm
No sorry, no demo site as of yet
Title: Re: [PACKAGE] Subcategories
Post by: Nicu Alecu on February 22, 2008, 12:38:04 pm
Now this is something I would really like ... I'll test it over the weekend (I'm too lazy today, it's one of those Fridays ;)).
If everythings works ok (and I'm over my laziness tomorrow) I'll post a test url.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 22, 2008, 12:52:23 pm
Thanks Nick!
Title: Re: [PACKAGE] Subcategories
Post by: Fred K on February 22, 2008, 02:38:02 pm
I know exactly where I can use this mod! New site in the works... stay tuned (as someone reported having heard Steve-o say ...whether it was FSJ or RSJ or some other Steve-o I have no idea...)
;D
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 22, 2008, 03:46:11 pm
Thanks for that Baker!
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on February 22, 2008, 04:12:48 pm
nice package!  holy crap, I sound like Patric.

Hehe... I'll vouch for that!  ;D
Title: Re: [PACKAGE] Subcategories
Post by: mosh on February 22, 2008, 05:25:06 pm
 8) awesome work equilni
thank you +karma 2u

update:
that was the most needed mod.
that made a very big step to sNewsCMS development.

and a huge thank you to all dev/mod/duds group for all improvments.

 :o

awesome day 2 all
Title: Re: [PACKAGE] Subcategories
Post by: Phil on February 22, 2008, 05:35:41 pm
Thanks equilni and the others who contributed to this mod.  Does this mod work for both vertical and horizontal layouts?
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 22, 2008, 05:58:10 pm
If you can style it correct, i don't see why not. Currently my test site gives me an output of:
Code: [Select]
<h3>Categories:</h3>
<ul>
<li><a class="current" href="http://localhost:8888/subCat/uncategorized/" title="">Uncategorized (0)</a>
<ul>
<li class="subcat"><a href="http://localhost:8888/subCat/uncategorized/unsorted/" title="">Unsorted (0)</a></li>
</ul>
</li>
</ul>

Just give your ul a class or id and you should be good to go
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on February 22, 2008, 06:27:31 pm
Thanks equilni and the others who contributed to this mod.  Does this mod work for both vertical and horizontal layouts?

Yes it does... provided you style the functions to work with your menus as EQ noted. Horizontal dropdowns with 2 child levels are tricky though.  ;)
Title: Re: [PACKAGE] Subcategories
Post by: Fred K on February 22, 2008, 09:04:24 pm
Jason, Jason, Jason ... we need to get you a different stash for downloads than Mediafire... I forgot to control-click the download link (Leopard has unfortunately altered my downloading behavior, these days I just normal-click dl-links...) and of course now I can't change your text encoding from Western (Mac OS Roman) to UTF-8 which I need. Don't know why though. It just doesn't work. And if fu*ks things up... And Mediafire is under maintenance so I can't re-download...
What? Patience? ME!?!?! lol
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on February 22, 2008, 09:52:12 pm
Jason, Jason, Jason ... we need to get you a different stash for downloads than Mediafire... I forgot to control-click the download link (Leopard has unfortunately altered my downloading behavior, these days I just normal-click dl-links...) and of course now I can't change your text encoding from Western (Mac OS Roman) to UTF-8 which I need. Don't know why though. It just doesn't work. And if fu*ks things up... And Mediafire is under maintenance so I can't re-download...
What? Patience? ME!?!?! lol

We're starting the "Get Jason some WebSpace Campaign. All donations for Jason will be favorably received by Fred.  ;)
Title: Re: [PACKAGE] Subcategories
Post by: Ken Dahlin on February 23, 2008, 05:22:19 am
Oh me oh my, I can't wait to take this for a spin.  :D
Title: Re: [PACKAGE] Subcategories
Post by: Rui Mendes on February 23, 2008, 10:28:53 am
I create downloads webspace for us
You can download here (http://snews.engcivil.net/downloads/subCat.zip)
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 23, 2008, 12:04:15 pm
Thanks Rui!!
Title: Re: [PACKAGE] Subcategories
Post by: Ken Dahlin on February 23, 2008, 04:58:31 pm
I've started testing this and I thank you for your hard work on this. Those interested please feel free to try it out at http://snews.kendahlin.com/subCat/ (http://snews.kendahlin.com/subCat/). Admin - pass is test - test as usual, and nothing has been changed except the file function has been disabled for security purposes.

One thing I'd like to see is that if a visitor clicks on a parent category, all subcategory articles display on that page too... I also think the parent category count should represent both itself and subcategories.

One last thing, I'm really grateful for all the smart people here.  :D
Title: Re: [PACKAGE] Subcategories
Post by: henrich on February 23, 2008, 06:27:57 pm
Notes in the code as usual?
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 23, 2008, 06:31:18 pm
I've started testing this and I thank you for your hard work on this. Those interested please feel free to try it out at http://snews.kendahlin.com/subCat/ (http://snews.kendahlin.com/subCat/). Admin - pass is test - test as usual, and nothing has been changed except the file function has been disabled for security purposes.

One thing I'd like to see is that if a visitor clicks on a parent category, all subcategory articles display on that page too... I also think the parent category count should represent both itself and subcategories.

One last thing, I'm really grateful for all the smart people here.  :D
try here:

http://snewscms.com/forum/index.php?topic=6936.msg47989#msg47989
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 23, 2008, 06:31:41 pm
Notes in the code as usual?

of course
Title: Re: [PACKAGE] Subcategories
Post by: alfaso on February 23, 2008, 09:51:33 pm
plz
can you tel me what you add to master files
index.php
news.php
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 23, 2008, 11:11:48 pm
This should help you get started:
http://snewscms.com/help/installing-snews/

Only the db information is being added to snews.php only
Title: Re: [PACKAGE] Subcategories
Post by: Ken Dahlin on February 24, 2008, 07:30:01 am

try here:

http://snewscms.com/forum/index.php?topic=6936.msg47989#msg47989

I haven't tried that yet, but I imagine the code will help steer me in the right direction for what I want to do. What I'm really looking for is that the function articles() would display all articles under a parent category including it's child categories (up to the configurable limit per page). So when you go to a parent category URL like http://snews.kendahlin.com/subCat/web-design/ it would also display articles from http://snews.kendahlin.com/subCat/web-design/templates/ and http://snews.kendahlin.com/subCat/web-design/themes/ on that page, but if one were to navigate to the URL of the child categories, the listing would be more focused and show only child category articles displayed. This is more of a standard navigational practice for sites with subcategories rather than just my personal preference I think.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 24, 2008, 08:01:07 am
It works sort of like that.

The categories in the enhanced menu are clickable showing all articles beneath it and any child categories.  The child categories are clickable as well showing articles beneath it as well.

Adding a limit clause should be easy
Title: Re: [PACKAGE] Subcategories
Post by: sibas on February 24, 2008, 01:58:14 pm
Hi equilni
Is it possibly to post the complete code for this mod?
I have in my site one heavy snews and like this I must to add all the modís that have installed from beginning  :'(
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 24, 2008, 03:26:28 pm
follow the commented code...
Title: Re: [PACKAGE] Subcategories
Post by: henrich on February 24, 2008, 07:35:19 pm
+sibas, as equilni has said follow the commented code and where you need to update (not only to add) code, you will verify old code and also new code and you will realise if a copy paste is enough or if you need to add also parts of the code from old code. For example some fields in the queries. Depending on mod's you already use. I have not tested thid mod yet, i'm just trying to give you a good suggestion. Good luck :)
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 24, 2008, 07:47:43 pm
Thank you.

BUG: First try removing (disabling) all categories,
then try to add an article. No category to chose, Makes it unclear: what happens to the article if a user posts it? (Well, this way it gets "home" category, but is it supposed to? I think there must be a message in this case, something like "Please, add a category first"... Needs some work, I think).

Another BUG: From admin panel if you try to "view" a PAGE, you won't get it - engine doesn't add "home" into the link - so the link remains "localhost/subs//page/" instead of "localhost/subs/home/gerberrerebe/"
Title: Re: [PACKAGE] Subcategories
Post by: sibas on February 24, 2008, 11:55:27 pm
I give a try to package to see how is going  :)
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 25, 2008, 12:03:58 am
Thank you.

BUG: First try removing (disabling) all categories,
then try to add an article. No category to chose, Makes it unclear: what happens to the article if a user posts it? (Well, this way it gets "home" category, but is it supposed to? I think there must be a message in this case, something like "Please, add a category first"... Needs some work, I think).

Another BUG: From admin panel if you try to "view" a PAGE, you won't get it - engine doesn't add "home" into the link - so the link remains "localhost/subs//page/" instead of "localhost/subs/home/gerberrerebe/"

1. Find:
Code: [Select]
echo html_input('hidden', 'id', 'id', $categoryid, '', '', '', '', '', '', '', '', '', '', '');
echo html_input('submit', 'delete_category', 'delete_category', l('delete'), '', 'button', 'onclick="javascript: return pop()"', '', '', '', '', '', '', '', '');

Replace with:
Code: [Select]
echo html_input('hidden', 'id', 'id', $categoryid, '', '', '', '', '', '', '', '', '', '', '');
// subcategories
if ($categoryid != 1) {
echo html_input('submit', 'delete_category', 'delete_category', l('delete'), '', 'button', 'onclick="javascript: return pop()"', '', '', '', '', '', '', '', ''); } else { echo ''; }
}

This removes home as a category and makes sure the user doesn't delete the default category (which should be 1)

2. Find the commented line and add the follwoing two:
Code: [Select]
// subcategories
// echo '<p>'.date(s('date_format'), strtotime($r['date'])).' <strong>'.$r['title'].'</strong>'.$cat_subcat.' '.l('divider').' <a href="'.db('website').cat_rel($r['category'],'seftitle').'/'.$articleSEF.'">'.l('view').'</a> ';
$catSEF = $r['category'] == 0 ? l('home_sef') : cat_rel($r['category'], 'name');
echo '<p>'.date(s('date_format'), strtotime($r['date'])).' <strong>'.$r['title'].'</strong>'.$cat_subcat.' '.l('divider').' <a href="'.db('website').$catSEF.'/'.$articleSEF.'">'.l('view').'</a> ';

Let me know if it does the trick
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 25, 2008, 10:47:59 am
Yes, it hepled. Pages are displayed correctly.

But still...

I don't consider "Uncategorized" category to be mandatory.
One can still unpublish it and when adding an article, he/she'd get an empty category-selection-dropdown-list... It's pretty dirty, don't you think?

Besides, I think that user doesn't have to dump SQL code with some mandatory category in it.
When adding an article user should be informed of adding a category first (if there aren't any).
That's logical, right? That's how serious universal systems function.
And I still seriously consider sNews to be highly universal CMS.

Thank you for this modification. I think subcategories should be in the core of sNews 1.7. Again, that's logical. =)

BTW, just for this piece of code:

Code: [Select]
$join_result = mysql_query("SELECT parent.name FROM ".db('prefix')."categories as child INNER JOIN ".db('prefix')."categories as parent ON parent.id = child.subcat WHERE child.id = $categoryid");
you get my personal respect. Well done.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 25, 2008, 11:30:25 am
One can still unpublish it and when adding an article, he/she'd get an empty category-selection-dropdown-list... It's pretty dirty, don't you think?

Now that is a bug right there. Thanks i will get to fixing that too. 
FIX:
Find:
Code: [Select]
if ($subcat=='cat') echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_category'), '', '', '', '', $frm_publish, '', '', '', '', '');  else echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_subcategory'), '', '', '', '', $frm_publish, '', '', '', '', '');
Replace with:
Code: [Select]
if ($subcat=='cat' && $categoryid != 1) {
echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_category'), '', '', '', '', $frm_publish, '', '', '', '', ''); } 
elseif ($subcat!='cat' && $categoryid != 1) {
echo html_input('checkbox', 'publish', 'pub', 'YES', l('publish_subcategory'), '', '', '', '', $frm_publish, '', '', '', '', '');
}
else { echo ''; }

I choose Uncategorized to be the default category, because there is always a big argument that Home is not a category
Title: Re: [PACKAGE] Subcategories
Post by: Ken Dahlin on February 25, 2008, 04:28:23 pm

Thank you for this modification. I think subcategories should be in the core of sNews 1.7. Again, that's logical. =)


I'd like to second that opinion. I'm hoping this, along with the speed enhancements, makes 1.7

Title: Re: [PACKAGE] Subcategories
Post by: centered on February 25, 2008, 07:30:16 pm
I updated the first post with bug fixes, so everything is in one place.
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on February 25, 2008, 09:57:09 pm
Thank you for this modification. I think subcategories should be in the core of sNews 1.7. Again, that's logical. =)
I'd like to second that opinion. I'm hoping this, along with the speed enhancements, makes 1.7

This is part of the grand plan.  ;)
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 07:43:42 am
I've succesfully installed your MOD into my own modded installation of sNews (the one I develop and use for all current projects) and it works as it should.
Thank you for commenting the code, it really helped.
Just an advice (sure helps me): when altering the code, comment out old code, don't remove it, so it would be easier to install your mod (and other ones) into any snews installation.

Another BUG I've found when coding: in function admin_articles($contents) you have:

Code: [Select]
$catSEF = $r['category'] == 0 ? l('home_sef') : cat_rel($r['category'], 'name');
Which puts category's name into the article's "view" link in admin panel. Its name can be obviously different from it's SEF, so it should be changed to this:

Code: [Select]
$catSEF = $r['category'] == 0 ? l('home_sef') : cat_rel($r['category'], 'seftitle');
So it becomes the SEF.

BTW, as for get_id, I'm using Codetwist's approach to input cleansing. As he already mentioned: "That cleanXSS() just do a lot of code execution - that means slower response time. When really controlled data sanitization is needed - like to clean inputs for further storage in database - then cleanXSS() probably is quite good choice. But for functions like get_id() much simpler checks do good enough."

Replace function get_id($parameter) with this snippet (for subcategories-snews edition only, of course):

Code: [Select]
// GET ID BOOSTED
// Subcategory Add - replace entire function (cleanGetSef MODDED)
function get_id($parameter) {
$url = array();
$url = explode('/', $_GET['category']);
$get_id = array();
  $sub_cat = cleanGetSef($url['1']);
// SpeedUps - check if url[1] exists - then check whether is's a subcat sef
if ($url['1']) {
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'"); }
if (!$result || !mysql_num_rows($result)) { $subcat=''; } else {
while ($r = mysql_fetch_array($result)) {$subcat = $r['seftitle'];}
$get_id['subcategory'] = $subcat;}
$get_id['category'] = cleanGetSef($url['0']);
if (isset($url['1']) && empty($subcat)) {
$get_id['article'] = cleanGetSef($url['1']);
} elseif (isset($url['2']) && !empty($subcat)) {
$get_id['article'] = cleanGetSef($url['2']);
}
if (isset($url['2']) && empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['2']);
} elseif (isset($url['3']) && !empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['3']);}
if (isset($get_id[$parameter])) {return cleanGetSef($get_id[$parameter]);}
}

// Codie's new pretty function for cleaning the SEF
function cleanGetSef( $inSef ) {
   if ( $inSef == '' ) {
      $sef = '';
   }
   elseif ( is_int($inSef) ) {
      $sef = (int) $inSef;
   }
   else {
      if (! preg_match('/^[a-z0-9\-_#\.]+$/', $inSef) ) {
         // $inSef is malformed - ignored completely
         $sef = false;
      }
      else {
         // $inSef is ok - only lower case ascii, '-', '_', '#' and '.' characters are included
         $sef = $inSef;
      }
   }
   return $sef;
}

Gives a real boost to sNews.

What I'm trying to do now is to make get_id check the database only when needed (when we do look for a subcategory, not when we request an index page or a regular category). It would boost the function even further.

DONE! Now it's as fast as snews without subcats and with codie's mod!
I've added a condidtion to lookup subcat SEF only when nessesary, so it's now as fast as stock boosted version of snews.
No penalities!

I've updated the code. Update yours.

P.S. Where do we apply the new fix you've posted? This one:
Code: [Select]
if ($sub_cat!='cat') { $jresult = mysql_query("select name from ".db('prefix')."categories where subcat = $sub_cat"); while($j = mysql_fetch_array($jresult)) { $name = $j['name']; }}
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 26, 2008, 09:29:10 am
Another BUG I've found when coding: in function admin_articles($contents) you have:

Code: [Select]
$catSEF = $r['category'] == 0 ? l('home_sef') : cat_rel($r['category'], 'name');
Which puts category's name into the article's "view" link in admin panel. Its name can be obviously different from it's SEF, so it should be changed to this:

Code: [Select]
$catSEF = $r['category'] == 0 ? l('home_sef') : cat_rel($r['category'], 'seftitle');
So it becomes the SEF.

I just did a fresh install and I didn't see that code, I see:
Code: [Select]
$categoryNAME = $r['category'] == 0 ? $home : cat_rel($r['category'], 'name');which is legit

BTW, as for get_id, I'm using Codetwist's approach to input cleansing. As he already mentioned: "That cleanXSS() just do a lot of code execution - that means slower response time. When really controlled data sanitization is needed - like to clean inputs for further storage in database - then cleanXSS() probably is quite good choice. But for functions like get_id() much simpler checks do good enough."


What I'm trying to do now is to make get_id check the database only when needed (when we do look for a subcategory, not when we request an index page or a regular category). It would boost the function even further.
You and joost are on the same wave length... and kicking my ass about it!  lol I had already changed that before you even mentioned anything.

P.S. Where do we apply the new fix you've posted? This one:
Code: [Select]
if ($sub_cat!='cat') { $jresult = mysql_query("select name from ".db('prefix')."categories where subcat = $sub_cat"); while($j = mysql_fetch_array($jresult)) { $name = $j['name']; }}
oh it says "subcat=" should be "id="

*smack's head*  replace
Code: [Select]
$name = empty($categoryid) || $subcat!='cat' ? retrieve('name','categories','id',$sub_cat):'';

with
Code: [Select]
if ($sub_cat!='cat') { $jresult = mysql_query("select name from ".db('prefix')."categories where id = $sub_cat"); while($j = mysql_fetch_array($jresult)) { $name = $j['name']; }}

dude, you are aawesome, keep checking and letting me know anyother bugs
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 09:37:19 am
Thanks. BTW, check the updated boosted version of get_id (with a new condition). You'll be surprized =)

Hm. didn't find this BUG in newly downloaded copy of your subcat-snews. Weird =)
Code: [Select]
$catSEF = $r['category'] == 0 ? l('home_sef') : cat_rel($r['category'], 'name');Oh, never mind then. It's fixed. It's been moved into link echo as "cat_rel($r['category'],'seftitle')". Sweet.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 26, 2008, 09:51:06 am
Why are you checking url['2']?
Code: [Select]
// SpeedUps - check if url[2] exists - then check whether is's a subcat sef
if ($url['2']) {
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'"); }
if (!$result || !mysql_num_rows($result)) { $subcat=''; } else {
while ($r = mysql_fetch_array($result)) {$subcat = $r['seftitle'];}
$get_id['subcategory'] = $subcat;}
Change that to a 1 and it works with subcats.

also, compared to my get_id, in the first post and noted here:
Code: [Select]
function get_id($parameter) {
$url = array();
$url = explode('/', clean($_GET['category']));
$get_id = array();
  $sub_cat = cleanGetSef($url['1']);
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'");
if (!$result || !mysql_num_rows($result)) { $subcat=''; } else {
while ($r = mysql_fetch_array($result)) {$subcat = $r['seftitle'];}
$get_id['subcategory'] = $subcat;}
$get_id['category'] = cleanGetSef($url['0']);
if (isset($url['1']) && empty($subcat)) {
$get_id['article'] = cleanGetSef($url['1']);
} elseif (isset($url['2']) && !empty($subcat)) {
$get_id['article'] = cleanGetSef($url['2']);
}
if (isset($url['2']) && empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['2']);
} elseif (isset($url['3']) && !empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['3']);}
if (isset($get_id[$parameter])) {return cleanGetSef($get_id[$parameter]);}
}

function cleanGetSef($inSef) {
    if ($inSef == '') { $sef = ''; }
    elseif (is_int($inSef)) { $sef = (int) $inSef; } else {
      if (! preg_match('/^[a-z0-9\-_#\.]+$/', $inSef)) { $sef = false; }
      else { $sef = $inSef; }} return $sef;}

adding Joosts populator mod here: http://snewscms.com/forum/index.php?topic=6601.msg44857#msg44857
 mine seems a hair faster than yours.  Not sure why atm....
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 09:56:50 am
I check whether url[1] exists so get_id doesn't have to check the db every time, only if exploded adress consists of 2 or more items (it may be an article, page or subcat => we need to connect to db to check).

Connecting to db takes time. So. You see where I'm going with this.

You're right. It must be url[1]. Still faster though =)

Your timer must be wrong =) It's obvious that when quiering an index page or a category, conditioned (if (url[1])) mysql quiery is ignored, thus making get_id work faster. I've just checked again.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 26, 2008, 10:05:21 am
if url0 = parent category, then url2 = article or comments page.  Wouldn't it be url1?

try this one:
Code: [Select]
function get_id($parameter) {
$url = array();
$url = explode('/', clean($_GET['category']));
$get_id = array();
$get_id['category'] = cleanGetSef($url['0']);
// changed
if (!empty($url['1'])) {
  $sub_cat = cleanGetSef($url['1']);
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'");
if (!$result || !mysql_num_rows($result)) { $subcat=''; } else {
$r = mysql_fetch_array($result); {$subcat = $r['seftitle'];}
$get_id['subcategory'] = $subcat;}
}
// end change
if (isset($url['1']) && empty($subcat)) {
$get_id['article'] = cleanGetSef($url['1']);
} elseif (isset($url['2']) && !empty($subcat)) {
$get_id['article'] = cleanGetSef($url['2']);
}
if (isset($url['2']) && empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['2']);
} elseif (isset($url['3']) && !empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['3']);}
if (isset($get_id[$parameter])) {return cleanGetSef($get_id[$parameter]);}
}
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 10:10:57 am
Right. Check my previous post. I've updated it when you just replied =)

I prefer not to use if (!empty($url['1'])). It's slower then if (url['1']). There are also other issues with empty as I've heard...

So I guess it is:

Code: [Select]
if ($url['1']) {
  $sub_cat = cleanGetSef($url['1']);
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'");
if (!$result || !mysql_num_rows($result)) { $subcat=''; } else {
$r = mysql_fetch_array($result); {$subcat = $r['seftitle'];}
$get_id['subcategory'] = $subcat;}
}

Now not only it's fast, it's clean and pretty. Sweet.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 26, 2008, 10:16:50 am
now.. to speed up archives... wrong thread though...

karma to you dude!!! Thanks!!

*looking back at old internal thread, Rui had the same idea you had. msack me for not listening Rui*

Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 10:30:49 am
(sh) it happens, man. We're all here to learn and evolve.

Now, what's wrong with archives? I find archive() function to be pretty rapid.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 26, 2008, 10:32:29 am
psst - wish Rui a happy birthday!!

i don't know, with joost's populator, and my test install with 200 articles, archives takes a half a second to complete (per ghassem's timing mod) making it the slowest so far....
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 10:51:54 am
Just to make it even cleaner:

Code: [Select]
function get_id($parameter) {
$url = array();
$url = explode('/', $_GET['category']);
$get_id = array();
if ($url['1']) {
  $sub_cat = cleanGetSef($url['1']);
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'");
if (!$result || !mysql_num_rows($result)) { $subcat=''; } else {
$r = mysql_fetch_array($result); {$subcat = $r['seftitle'];}
$get_id['subcategory'] = $subcat;}
}
$get_id['category'] = cleanGetSef($url['0']);
if ($url['1'] && !$subcat) {
$get_id['article'] = cleanGetSef($url['1']);
} elseif ($url['2'] && $subcat) {
$get_id['article'] = cleanGetSef($url['2']);
}
if ($url['2'] && !$subcat) {
$get_id['commentspage'] = cleanGetSef($url['2']);
} elseif ($url['3'] && $subcat) {
$get_id['commentspage'] = cleanGetSef($url['3']);}
if (isset($get_id[$parameter])) {return cleanGetSef($get_id[$parameter]);}
}

Just GET_ID. But without unnessesary "!empty" and "isset" checks.
According to my tests, became slightly faster and takes less RAM togenerate a page.
Server response is better too, because it empties the RAM faster.

Next lie my questionable ideas about "empty".

Pleeease, people, don't use internal "!empty()" construct until it's really nessesary. Use "empty()" as it is, not the opposite way. It just sucks... Sucks so hard. I've been there...
Just for the sake of it: quote from php.net:

Quote
In response to a previous post regarding the speed of:

if (empty($var))
versus
if (!$var)

or

if (!empty($var))
versus
if ($var)

In fact I found that the empty() function is always slower. I have tested this on three different machines; Celeron, Pentium M, and an AMD Athlon 64. These all run php 5.2, and I have repeated the tests a number of times using different loops and variable types. My results show that, on average, empty() takes around 15 - 20% longer to complete its task.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 26, 2008, 10:58:15 am
wow good advice!!  thanks for that!!
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 10:59:34 am
I guess it's time for me to review subcat-snews once again to get rid of !empty().

There's a lot of it, I see now.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 26, 2008, 11:12:18 am
hmm I see conflicting opinions:
http://us2.php.net/manual/en/function.empty.php#79780
http://us2.php.net/manual/en/function.empty.php#73926 - in response to your notated comment
http://us2.php.net/manual/en/function.empty.php#72508
http://us2.php.net/manual/en/function.empty.php#58675
http://us2.php.net/manual/en/function.empty.php#61327

I would hold off on the empty's, but that is just me.

Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 11:19:29 am
UPDATED:

Oh... "Empty" may be not logical, but it sure doesn't generate lots of E_NOTICE errors like if($var) does...
I guess PHP founders know better. I guess I was wrong. Well. Learn on my mistakes =)
Don't read below.


empty() [NOT !empty()] seems to be faster on php4, but slower on php5.

Also !empty is like... Like "If NOT empty". Logically it should be "If TRUE", right?
Why the opposite?

So if ($var) performs and logically stands better.

But that's not for discussing in this thread.

It may be... Just for the sake of it, to keep in mind:

if ($var) - to check if TRUE

And

empty($var) - to check if FALSE, I guess.

As far as it's the language construct, it should be used in the correct way. Not like !empty, but empty.

Again, why the opposite? Right? For an array, allright. But always?.. No.

http://retosphere.de/Main/Show.php?File=../prjphp/phpempty_annotation.php
Title: Re: [PACKAGE] Subcategories
Post by: Joost on February 26, 2008, 12:29:59 pm
As I understand it, empty is very consistent, always save. I ran into a similar issue when using this:
if ($_GET)

 instead of:

if (isset($_GET))

In this case - the context in which it is used - if  (!empty($_GET)) behaves like if ($_GET)
So the effects are not similar all the time.
To be or not to be, that's the question. ???
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 26, 2008, 12:49:47 pm
Well, in fact,

if (!empty($_POST['name']))

behaves like

if (isset($_POST['name']) && $_POST['name'])

Because it also checks if var's been set. And thus it doesn't generate E_NOTICE warning, which would be gen'd if simply checked with if ($var). empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

More - here (http://www.php.net/manual/en/function.empty.php#45005).

equilni, sorry for offtopic. Offtopic closed.
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 27, 2008, 11:23:50 am
Back to business.

Another attempt to boost get_id for subcategories, this time - using not only cleanGetSef, but also SESSIONS.
Idea by Ghassem Tofighi.
Speed comparisons placed below.

Replace your function get_id($parameter) with these two functions:

Code: [Select]
// GET ID
// CleanGetSef MODDED, SESSION-based.
// Modded by Armen. Inspired by Ghassem Tofighi's get_id() booster.
function get_id($parameter) {
   if ($_SESSION['last']['GET']!= $_GET){
      unset ($_SESSION['last']);
      $_SESSION['last']['GET'] = $_GET;
  $url = array();
      $url = explode('/', $_GET['category']);
      $get_id = array();
  $get_id['category'] = cleanGetSef($url['0']);
      $_SESSION['last']['category'] = $get_id['category'];
  if (isset($url['1'])) {
  $sub_cat = cleanGetSef($url['1']);
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'");
if (!$result || !mysql_num_rows($result)) {
$subcat = NULL;
}
else {
$r = mysql_fetch_array($result); {$subcat = $r['seftitle'];
}
$get_id['subcategory'] = $subcat;
$_SESSION['last']['subcategory'] = $get_id['subcategory'];
}
      if (isset($url['1']) && empty($subcat)) {
         $get_id['article'] = cleanGetSef($url['1']);
         $_SESSION['last']['article'] = $get_id['article'];
      }
   elseif (isset($url['2']) && !empty($subcat)) {
$get_id['article'] = cleanGetSef($url['2']);
$_SESSION['last']['article'] = $get_id['article'];
  }
  if (isset($url['2']) && empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['2']);
$_SESSION['last']['commentspage'] = $get_id['commentspage'];
  }
   elseif (isset($url['3']) && !empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['3']);
$_SESSION['last']['commentspage'] = $get_id['commentspage'];
   }
}
   }
   else {
      $get_id['subcategory'] = $_SESSION['last']['subcategory'];
      $get_id['category'] = $_SESSION['last']['category'];
      $get_id['article'] = $_SESSION['last']['article'];
      $get_id['commentspage'] = $_SESSION['last']['commentspage'];
}
 if (isset($get_id[$parameter])) {
 return cleanGetSef($get_id[$parameter]);
 }
}

// Codie's function for cleaning the SEF
function cleanGetSef( $inSef ) {
   if ( $inSef == '' ) {
      $sef = '';
   }
   elseif ( is_int($inSef) ) {
      $sef = (int) $inSef;
   }
   else {
      if (! preg_match('/^[a-z0-9\-_#\.]+$/', $inSef) ) {
         $sef = false;
      }
      else {
         $sef = $inSef;
      }
   }
   return $sef;
}

Speed compared to the previous (not session-based) get_id:

"category->article"-class pages: 25-30% faster;
"subcategory->article"-class pages: 30-40% faster;
"category, subcategory"-class pages: no change;

I still can't believe subcategory version of my modded snews installation finally became even faster then non-subcategory one. Sweeeeet.

Another idea to enchance subcat-snews experience.
This will make your website title show "Subcategory - Category" instead of "Subcategory/Category".
I find it more appealing, because everything else in the title is divided with " - ".
But still, your choice it remains.

Change this:
Code: [Select]
if (!empty($subcatName)) { $title .= $subcatName.'/';}
To this:
Code: [Select]
if (!empty($subcatName)) { $title .= $subcatName.' - ';}
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 27, 2008, 01:16:41 pm
Thanks I will take a look at the new get_id after work. 
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 27, 2008, 08:57:59 pm
The sessions doesn't pass anything harmful or possibly harmful?  Your not cleaning anything with the session lines...
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 27, 2008, 09:12:25 pm
I'm not cleaning anything, because everything is already clean - cleaned with CleanGetSef.

Sessions are better for the server - use of sessions improves server's responsiveness, if server's configured properly.

In this particular case no vital information is stored in sessions (if it's what you're asking for).

If one feels paranoid, he/she can double check var, recieved from session, or call session_regenerate_id() in the index.php (when calling snews.php).
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 28, 2008, 11:42:51 am
I'm not cleaning anything, because everything is already clean - cleaned with CleanGetSef.
....
If one feels paranoid, he/she can double check var, recieved from session, or call session_regenerate_id() in the index.php (when calling snews.php).

We are trying to be secure for the user against threats and such right?

I think it should be recleaned, at least once, maybe here:
$_SESSION['last']['GET'] = $_GET;

I am not sure about the other options mentioned... just work and I have a non functioning brain here....
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 28, 2008, 12:20:49 pm
Don't be so hard on yourself, man.

The thing is, $_GET is being exploded any way, being legit or not. Then every array entry is being cleaned.

SESSION is just a special array. It responds and stores data like any ordinary array variable, with the difference of being stored on the server. If you still don't trust SESSIONS and think they may be hijacked/injected this way, you can check $_GET on the way. But it will slow down get_id for 20-30%, thus eliminating all SESSION-based get_id benefits.

Here's where one may want to check for XSS. But it's not nessesary.

Code: [Select]
$url = explode('/', $_GET['category']);
Final version (both functions):

Code: [Select]
// GET ID
// CleanGetSef MODDED, SESSION-based.
// Modded by Armen. Inspired by Ghassem Tofighi's get_id() booster.
function get_id($parameter) {
   if ($_SESSION['last']['GET']!= $_GET){
      unset ($_SESSION['last']);
      $_SESSION['last']['GET'] = $_GET;
  $url = array();
      $url = explode('/', $_GET['category']);
      $get_id = array();
  $get_id['category'] = cleanGetSef($url['0']);
      $_SESSION['last']['category'] = $get_id['category'];
  if (isset($url['1'])) {
  $sub_cat = cleanGetSef($url['1']);
$result = mysql_query("SELECT seftitle FROM ".db('prefix')."categories WHERE seftitle = '$sub_cat'");
if (!$result || !mysql_num_rows($result)) {
$subcat = NULL;
}
else {
$r = mysql_fetch_array($result); {$subcat = $r['seftitle'];
}
$get_id['subcategory'] = $subcat;
$_SESSION['last']['subcategory'] = $get_id['subcategory'];
}
      if (isset($url['1']) && empty($subcat)) {
         $get_id['article'] = cleanGetSef($url['1']);
         $_SESSION['last']['article'] = $get_id['article'];
      }
   elseif (isset($url['2']) && !empty($subcat)) {
$get_id['article'] = cleanGetSef($url['2']);
$_SESSION['last']['article'] = $get_id['article'];
  }
  if (isset($url['2']) && empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['2']);
$_SESSION['last']['commentspage'] = $get_id['commentspage'];
  }
   elseif (isset($url['3']) && !empty($subcat)) {
$get_id['commentspage'] = cleanGetSef($url['3']);
$_SESSION['last']['commentspage'] = $get_id['commentspage'];
   }
}
   }
   else {
  $get_id['subcategory'] = $_SESSION['last']['subcategory'];
  $get_id['category'] = $_SESSION['last']['category'];
  $get_id['article'] = $_SESSION['last']['article'];
  $get_id['commentspage'] = $_SESSION['last']['commentspage'];
}
 if (isset($get_id[$parameter])) {
 return cleanGetSef($get_id[$parameter]);
 }
}

// Codie's function for cleaning the SEF (OPTIMIZED)
function cleanGetSef($inSef) {
$sef = preg_match('/^[a-z0-9\-_#\.]+$/', $inSef) ? $inSef : NULL; return $sef;}

BTW, notice, what I've done to Codie's function:

Code: [Select]
// Codie's function for cleaning the SEF (OPTIMIZED)
function cleanGetSef($inSef) { $sef = preg_match('/^[a-z0-9\-_#\.]+$/', $inSef) ? $inSef : NULL; return $sef;}

Smaller, isn't it? Faster too. Let it be?
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 28, 2008, 06:35:16 pm
haha I type it wrong... I ment to say I just woke up and my brain is not functioning yet...

anyway, good work so far! 

XSS slows down the process that is why (I assume of course) codie has his new function in place:
$url = explode('/', clean(cleanXSS($_GET['category'])));
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 28, 2008, 06:50:49 pm
haha I type it wrong... I ment to say I just woke up and my brain is not functioning yet...

anyway, good work so far! 

XSS slows down the process that is why (I assume of course) codie has his new function in place:
$url = explode('/', clean(cleanXSS($_GET['category'])));

Codie's function breaks the process down if used here: $url = explode... Only cleanXSS is aplicable here.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 28, 2008, 06:57:05 pm
Also to note, a few months ago in development I merged an beta version of subcategories and Skidoo's subpages modification.  The result was beautiful.

Index
Category
   Subcategory
      Article
         SubPage

Now add the alpha sections mod to that.....

Section (index)
   Category
      Subcategory
         Article
            SubPage
Section
   Category
      Subcategory
         Article
            SubPage
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 28, 2008, 07:04:52 pm
Concerning subpage mod:

Man! You're reading my mind! That's one of the first ideas that came to my mind when I started testing your mod.


Could you share the resulting snews core file? Just to see, how you did it.

Or I will start developing it by myself, because I'm ready to do this. Have been for some time now = )

I've almost finished improving subpage modification to resemble normal articles (with keywords and stuff) instead of simple pages, but still using subarticles table. Wanna use articles table, but need to add a new column and think it trough, because that would need a LOT of modifications of admin section and core snews behaviour. I think, I'll stick to subarticles table for now.

The problem is - he didn't comment out the changes in the code. So it takes some time to find out where the changes are.
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 28, 2008, 07:21:23 pm
Yes I know I asked him if he could send me a copy of his mod with comments.

How I did it?  I just took his file and added my code in it (duh lol) Let me find it...
Title: Re: [PACKAGE] Subcategories
Post by: Armen on February 28, 2008, 07:25:56 pm
Yes I know I asked him if he could send me a copy of his mod with comments.

How I did it?  I just took his file and added my code in it (duh lol) Let me find it...

That's how I was going to to this =) but then decided not to - no use...
I need to know where the changes are, to be sure script is as failsafe and secure as possible.
"Trust, but Verify", as russians say. = )
Title: Re: [PACKAGE] Subcategories
Post by: Fred K on February 29, 2008, 02:03:26 am
Jason, I've run into a problem with the subCat package. Just downloaded a fresh version of it, and I'm running into the old problem of a) SEF links not being created when creating a new article (SEFs have to be entered manually) and b) none of Preview or Customize fieldsets open onClick, all in Safari/Mac. In fact no fieldset toggles work at all...
Fresh snews.php from the subCat package, fresh db, only the index.php is old but with new functions in place ... it's odd.
I'll do a test with another, non subCat snews installation just to see if there's a difference.
This is on localhost with MySQL 5/PHP 5 if it makes a difference.


Located problem, has nothing to do with subCat or snews.php -- my mistake. (And a silly one at that...)
Title: Re: [PACKAGE] Subcategories
Post by: sibas on February 29, 2008, 12:58:53 pm
I have one question.
Assume that this mod is stable, how to add from another sNews all the articles.
My structure for one of my sites is something like this, multiply snews,
snews
snews-html
snews-css

So in snews I change to snews subcut, I donít know how to add all articles from snews, snews-html, and snews-css.
Any idea  ::)
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 29, 2008, 04:22:34 pm
I have one question.
Assume that this mod is stable, how to add from another sNews all the articles.
My structure for one of my sites is something like this, multiply snews,
snews
snews-html
snews-css

So in snews I change to snews subcut, I don’t know how to add all articles from snews, snews-html, and snews-css.
Any idea  ::)


Your assumption is correct. The only thing changes are in snews.php and the database.

The articles and categories tables gets a new column.

Not understanding how your site is set up.
snews, snews-html, and snews-css  You mean snews.php, index.php, style.css?
Title: Re: [PACKAGE] Subcategories
Post by: sibas on February 29, 2008, 05:11:01 pm
To make a pseudo sub categories
I install a root snews and after I install a second which I name it html and another one css.
So my structure looks like this
snews, root site, example.com
when I go to html I have example.com/html/
and when I go to css example.com/css
Every folder is a separate snews with different snews and index

Now with this most wanted mod I have to put all those articles from different snews to one, subcut, but how I do this?? 
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on February 29, 2008, 05:34:36 pm
To make a pseudo sub categories
I install a root snews and after I install a second which I name it html and another one css.
So my structure looks like this
snews, root site, example.com
when I go to html I have example.com/html/
and when I go to css example.com/css
Every folder is a separate snews with different snews and index
Now with this most wanted mod I have to put all those articles from different snews to one, subcut, but how I do this?? 

What you are showing us does not make sense... structurally speaking that is.
You can run more than one snews install, each in separate folders within the domain root... but why would you name the folders as html and css. Why not name them something like... install1, install2, install3... and so on?

In any case, if each installation has its own snews.php (engine) file, and you want all of them to have the SubCategories funtionality, then you would need to replace all category-only engine files with the new one that has the subcategories modifications in it... and each of the installations would also need the articles & categories tables modified with the new rows added... within their respective databases.

I have not tried adding the Subcategories snews.php file and database changes to an existing database that has lots of articles already in it. I imagine that if you did... you would then need to edit every article in order to re-assign it within the proper category or subcategory to give them their proper SEF titles... since they would need to be different than they were beforehand.
Title: Re: [PACKAGE] Subcategories
Post by: sibas on February 29, 2008, 06:29:08 pm
Why not name them something like... install1, install2, install3... and so on?
because i want to have example.com/css, example.com/html, anyway this is not our point  ;)

So how i understand is not a easy task to put all those articles to subcut.
Title: Re: [PACKAGE] Subcategories
Post by: Fred K on February 29, 2008, 07:23:43 pm
Minor eyesore in the subCat snews.php: The "Add subcategory" legend has redundant paranthesiseses, like this:
Quote
Add subcategory ()

Oh, wait, it's not picking up $name, that's the problem. Line 1362:
Quote
$frm_add_edit = empty($sub_cat) ? l('add_category') : l('add_subcategory').' ('.$name.')';

Is that special for my file or do others see that as well?  ???
Title: Re: [PACKAGE] Subcategories
Post by: centered on February 29, 2008, 07:32:17 pm
Check the bugs on the first post.. it is fixed
Title: Re: [PACKAGE] Subcategories
Post by: Fred K on February 29, 2008, 08:06:09 pm
Check the bugs on the first post.. it is fixed

Ah, ok ... I downloaded the file on Feb 28 and since the first post was edited Feb 26 so I just assumed the fixes were in the distro. No worries, I'll edit the one I have (I know your plate is kinda full already...)
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on March 02, 2008, 03:56:32 pm
NOTICE: POSTS REMOVED
I've removed posts by Armen, EQ and I as were related to a performance solution posted by Armen. These were removed at Armen's request. Armen may post his suggested performance solution at a later date elsewhere.  :)
Title: Re: [PACKAGE] Subcategories
Post by: sibas on March 03, 2008, 12:36:54 pm
This mod is possible to work also with snewsMU?
Title: Re: [PACKAGE] Subcategories
Post by: centered on March 03, 2008, 12:56:45 pm
My answer would be: I don't know.
My gut answer would be:  I don't see why not.
The person to ask is Doug (Keyrocks) since he may know better on that.
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on March 03, 2008, 02:13:32 pm
My answer would be: I don't know. My gut answer would be:  I don't see why not.
The person to ask is Doug (Keyrocks) since he may know better on that.

Yes, it does. During its development, I installed it as it was at the end of December in 3 sNews 1.6 projects - default and MU with no other mods, and in a heavily modded MEMU and it works fine in all three. The version released here has several improvements over the one I used, and I have not had time yet to update my 3 projects yet.
Title: Re: [PACKAGE] Subcategories
Post by: mosh on March 04, 2008, 12:04:02 pm
hey all
i am looking at the paginator function,
it has the problem with missing part of the url.

Code: [Select]
http://localhost/website//p_2/

awesome day
Title: Re: [PACKAGE] Subcategories
Post by: centered on March 04, 2008, 12:42:52 pm
In function articles()
replace teh line after:
Code: [Select]
$home = l('home_sef'); $categorySEF = get_id('category');

with:
Code: [Select]
$categorySEF = empty($categorySEF) ? $home : $categorySEF;
Then find:
Code: [Select]
paginator($categorySEF, $currentPage, $totalPages, '', l('paginator'));Before that line add:
Code: [Select]
if (!empty($subcatSEF)) { $categorySEF = $categorySEF.'/'.$subcatSEF; } else { $categorySEF = $categorySEF; }
Title: Re: [PACKAGE] Subcategories
Post by: mosh on March 04, 2008, 02:12:23 pm
 8) works great, thank you
Title: Re: [PACKAGE] Subcategories
Post by: centered on March 04, 2008, 02:56:35 pm
hmm it may be a band aid solution now that I review it...... may not be an ideal solution though...
Title: Re: [PACKAGE] Subcategories
Post by: Nicu Alecu on March 26, 2008, 08:25:40 am
Hi, EQ.
Does the download package contain all the notes, bug fixes and boosters? I mean ... is it stable? I finally have some time to test this mod (and some need to use it ;), of course), and you guys kinda lost me along the 6 pages of "coding esperanto" ;D
Title: Re: [PACKAGE] Subcategories
Post by: centered on March 26, 2008, 09:46:42 am
Just add the fixes and you will be fine.... or wait a week..
Title: Re: [PACKAGE] Subcategories
Post by: tb0111 on March 28, 2008, 04:14:57 am
hi Keyrocks,

can you give me a quick guide to add this mod into MEMU.
Thank you
Title: Re: [PACKAGE] Subcategories
Post by: Nicu Alecu on March 28, 2008, 05:52:58 am
Just add the fixes and you will be fine.... or wait a week..

Thanks EQ. I think I'll wait ... I'm in no rush  ;)
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on March 28, 2008, 01:28:18 pm
hi Keyrocks,
can you give me a quick guide to add this mod into MEMU.
Thank you

A quick guide:
1) Download the latest subcats package and the latest MU package.
2) Install both on your localhost server environment (XAMPP or WAMP) on your computer and get them both running and tested OK.
3) Open the snews.php files for both of them. Examine each carefully, function-by-function, noting where subcats mod locations
   are in snews.php (subcats) and where the same locations are in the snewsMU.php.

Just looking through the Dec.29.07 version of the subcats snews.php... I notice that the mod locations are prefixed with Subcategory Add. I can't remember if EQ put those in the file or if I did after doing a file comparison but I think EQ did. I think there are at least 28 mod locations... some are just a single string replacement while others are a complete function replacement or a complete new function. I haven't downloaded EQ's latest package so I can't say if all mod locations are prefixed with comments in it.

So... from there it's a matter of getting familiar with the code in each function and adding the Subcategory Add stuff into the snewsMU.php file... replacing or adding to each function as you go through the files. Making comment notes in the file along the way is also a good thing to do so you have references as to what you did when you go to trouble-shoot.
Title: Re: [PACKAGE] Subcategories
Post by: centered on March 28, 2008, 07:02:25 pm
Yes my mod's are (sort-of) clearly commeted.  This mod has // subcategory in all places. 
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on July 14, 2008, 09:22:24 pm
Several months have passed and I've continued working with the original Subcategories mod as part of the continued revision process with my current sNews 1,6 MEMU+++ project... now slowly emerging in its first Online Application Here (http://www.mycallander.ca/).

Today I applied expandable Category Titles in the Sitemap Page (http://www.mycallander.ca/sitemap/) and to the SubCategory titles (under them) as well. As an example... click the Bylaws & Policies Subcat under the Government Category to see how they work nicely. I also applied expandable Month/year titles in Archive Page (http://www.mycallander.ca/archive/) and it works nicely too.

UPDATED: July 16.08
DISPLAYING CATEGORY / SUBCATEGORY TITLES ON ARCHIVE PAGE LINKS

As you notice... looking at the links in a default Archives page using the Subcategories Mods... the Category OR SubCategory title is displayed at the end of each link in brackets... pretty much as it does in a default archive function for both 1.6 and 1.7RC. There really isn't any way to tell if an article link is in a Category or a Subcategory within a Category... all titles are displayed only as one "Category" title.

This Modification will generate the Category Title if the article is listed under a Category... and generate both the Category AND SubCategory titles if the article is listed under a SubCategory within a Category.
EXAMPLES:
1 - Article Title in a Category (parent):  Article Title (Category Title)
2 - Article Title in a SubCategory (child):  Subcat Article Title (Category Title / Subcat Title)

Step One:
I've provided the complete function below. Though it has a few mods not in a default function... it will function OK for testing within a default sNews 1.6 package that has the Subcategories mods in it. To get the Category / Subcategory title displaying... we've replaced the $cat_name string with a new one, just below the comment-line that starts with # NEW STRING:.

Code: [Select]
<?php

// ARCHIVE - July 14.08 (keyrocks) - lists article & page links under expandable month/year titles
function archive() {
echo '<h2>'.l('archive').'</h2>';
$query "SELECT date FROM ".db('prefix')."articles WHERE position IN (1,3) AND published = 1 GROUP BY month(date) ORDER BY date DESC";
$result mysql_query($query);
$num mysql_num_rows($result);
$home s('home_sef');
$home_name l('home');
$month_names explode(', 'l('month_names'));
$i 1;
while ($r mysql_fetch_array($result)) {
   $year_month date('Y-m',strtotime($r['date']));
   $year substr($r['date'], 04);
   $month substr($r['date'], 52) -1;
   $month_name = (substr($month01) == 0) ? $month_names[substr($month11)] : $month_names[$month];
   # month/year titles, expandable
   echo '<div class="filelist_div"><p class="filelist_title"><a title="'.$month_name.', '.$year.'" onclick="toggle(\'sub'.$i.'\')" style="cursor: pointer;"><strong>'.$month_name.', '.$year.'</strong></a></p></div>';
   echo '<div id="sub'.$i.'" style="display: none;">';

   $query1 "SELECT * FROM ".db('prefix')."articles WHERE position IN (1,3) AND published = 1 AND date LIKE '$year_month%' ORDER BY date DESC";
   $result1 mysql_query($query1);

         while (
$r1 mysql_fetch_array($result1)) {
$catSEF $r1['category'] != find_cat_sef($r1['category']) : $home;
$articleSEF retrieve('seftitle''articles''id'$r1['id']);
# NEW STRING: shows cat / subcat titles. Replaces string below it.
$cat_name $r1['category'] == $home catname_rel($r1['category'], 'name');
  // $cat_name = $r1['category'] != 0 ? retrieve('name', 'categories', 'seftitle', $catSEF) : $home_name;
$categorySEF $r1['category'] == $home cat_rel($r1['category'], 'seftitle');
# article & page title links
echo '<div class="icon_indent"><a href="'.db('website').$categorySEF.'/'.$articleSEF.'/" title="'.$r1['title'].'">'.$r1['title'].'</a> ('.$cat_name.')</div>';
$last $year.$month;
}
echo '</div>';
$i++;
}
}

?>


Step Two:
Assuming you already have the Subcategories Mods added in... search your engine file for - function cat_rel($var, $column) which is the first line of a function that is part of the Subcategories Mods. This cat_rel function is used in the archives function (and others) to generate the category SEF and subcategory SEF links together as one (within the article title links).

This function can be used to generate the Category & SubCategory Titles as well... but there is no space between them and the slash the separates them, making them a little difficult to read. We cannot add any space in this function (where the slash is generated) because that would cause SEF link errors... so to get the space... we clone (copy) the function... paste it below it... and change its name... and put the spaces in so they only work with the NEW string we placed in the archives function. In this case... we've called the "new" function - catname_rel so we know it generates category & subcategory names that are related to the article title.

Code: [Select]
<?php

// CAT/SUBCAT TITLE GENERATOR (keyrocks July 16.08)
// Subcategory Add - new function to build parent/child titles. Gets parent/child from an id
function catname_rel($var$column) {
$categoryid $var;
$join_result mysql_query("SELECT parent.$column
FROM "
.db('prefix')."categories as child
INNER JOIN "
.db('prefix')."categories as parent
ON parent.id = child.subcat
WHERE child.id = 
$categoryid");
while($j mysql_fetch_array($join_result)) {
$parent $j[$column].' / ';
}
$subresult mysql_query("SELECT * FROM ".db('prefix')."categories WHERE id = '".$categoryid."'");
while ($c mysql_fetch_array($subresult)) {
$child $c[$column];
}
return $parent.$child;
}

?>


And that's it. Your Article Titles in the Archive Page will now have both the Category and SubCategory titles displayed when articles are located in a SubCategory.  :)
Title: Re: [PACKAGE] Subcategories
Post by: poosk on August 11, 2008, 12:42:23 pm
Is this kind of structure possible with this mod?

   --   top level 1 menu --
side level 2 menucontent
------------- footer --------------

Basically I'd need top menu (home, products, contact etc) and fex product page would have level 2 type of navigation for different products...
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on August 11, 2008, 04:06:45 pm
Is this kind of structure possible with this mod?

          <table><tr><td>   --   top level 1 menu -- </td></tr>
          <tr><td>  side level 2 menu</td><td>content</td></tr>
          <tr><td> ------------- footer -------------- </td></tr>
          </table>

Basically I'd need top menu (home, products, contact etc) and fex product page would have level 2 type of navigation for different products...

The answer is Yes. But... we use CSS only... no table-and-cell structure as you have indicated. It will be up to you to adapt this for your own template.

Let's say you have your top-level menu displaying the categories and Products is one of your categories... and you have several articles posted (assigned to) that Products category. And, when you click on Products... from the top-level menu, you would get the main articles page for that category.

If you want to have all of the article links for any given category (or sub-category) automatically displayed on all article pages within that category (or sub-category) ... then you would need to use a function that will automatically generate those links in a horizontal format (it can also be done in a vertical format within a side-bar if you want).

This function, based on one originally authored by Joost (for a default category-only sNews install) has been modified to do this in an sNews project that has the Subcategories mods in it. To use it, you would copy this function into your snews.php file... under the subcategories function (excluding the php tags):

Code: [Select]
<?php

// HORIZONTAL ARTICLE MENU - in current Category/SubCategory (Joost, keys, equilni)
function harticles_menu_incat() {
   
$categorySEF get_id('category');
   
$subcatSEF get_id('subcategory');
   
$exist check_category($categorySEF);
   if (!empty(
$subcatSEF)){  $categorySEF $subcatSEF; }
   if (!empty(
$categorySEF) && $exist == false) {
echo"\n\t".'<ul id="this_category">';
$cat retrieve('id','categories','seftitle',$categorySEF);
$query "SELECT * FROM ".db('prefix')."articles WHERE category = $cat AND position = 1 AND published = 1 ORDER BY artorder ASC";
$result mysql_query($query);
while ($r mysql_fetch_array($result)) {
    $pageSEF get_id('article');
    $catSEF cat_rel($cat,'seftitle');
    if ($pageSEF == $r['seftitle']){
echo '<li class="horiz_cat_menu"><span id="artikel">|&nbsp;&nbsp;'.$r['title'].'&nbsp;&nbsp;</span></li>';
    } else {
echo'<li class="horiz_cat_menu"><a href="'.db('website').$catSEF.'/'.$r['seftitle'].'/" >|&nbsp;&nbsp;'.$r['title'].'</a>&nbsp;&nbsp;</li>';
    }
        }
echo'</ul>'."\n";
   }
}

?>


Then you would paste this little bit into your style.css file to make the links display horizontally:
          .horiz_cat_menu { display: inline; }

Then... wherever you want the new function to display its menu in your template (index.php file)... you would paste this into place to call the function:
          <?php harticles_menu_incat(); ?>

Title: Re: [PACKAGE] Subcategories
Post by: antvoi on September 03, 2008, 03:21:01 pm
i am a newbie in Snews. could you please explain how to add sub categories to Snews Multiple Languages (http://snewscms.com/forum/index.php?topic=4617.0)?

Thank you!  ;D
Title: Re: [PACKAGE] Subcategories
Post by: nuno on September 10, 2008, 01:00:01 pm
Sorry if this is a sill question... could someone point me in the direction of the latest package for this?

Many thanks
Title: Re: [PACKAGE] Subcategories
Post by: bobcat on October 08, 2008, 03:48:03 am
Really need this mod. Does any one have the package for download, the thread says it is unavailable at the moment.

Thanks.
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on October 08, 2008, 04:07:04 am
Really need this mod. Does any one have the package for download, the thread says it is unavailable at the moment.
Thanks.

I posted this link somewhere else on these forums around Sept.27.08 to a complete snews 1.6 Subcats Zip Package (http://snews.ca/d-loads/snews1.6_subcats.zip) for someone else who asked the same question. It is a development package as I had it working at the end of December, 2007. It works fine but it doesn't have all the 1.6 Patches that were posted from January 2008 on.
Title: Re: [PACKAGE] Subcategories
Post by: bobcat on October 08, 2008, 06:18:02 am
I had searched but couldn't find. Anyway thanks keyrocks for the link, i will give it a try.
Title: Re: [PACKAGE] Subcategories
Post by: bobcat on October 08, 2008, 06:52:16 am
Is this available as a mod instead of a package?

I have a heavily modded snews which i can't just exchange with another .

Is there anywhere to get a list of the changes that need to be made to implement subcategories.

I have searched the forums now for 2 days but can't find anything complete/finished in this form.

Thanks.
Title: Re: [PACKAGE] Subcategories
Post by: Joost on October 08, 2008, 07:05:41 am
Virtually every function is affected by this modification.
Title: Re: [PACKAGE] Subcategories
Post by: bobcat on October 08, 2008, 07:17:26 am
Virtually every function is affected by this modification.

OK, thanks. Then i guess it will be easier to have 2 installations of snews to be able have a news section and an articles section? (like below)

home - articles > categories
   |
news > articles
Title: Re: [PACKAGE] Subcategories
Post by: Joost on October 08, 2008, 07:27:47 am
Yes, in fact I am in favour of that approach
I've written something about that here (http://snewscms.com/forum/index.php?topic=5266.msg34191#msg34191)

Title: Re: [PACKAGE] Subcategories
Post by: bobcat on October 08, 2008, 07:39:15 am
Yes, in fact I am in favour of that approach
I've written something about that here (http://snewscms.com/forum/index.php?topic=5266.msg34191#msg34191)



Thanks Joost. I will go check that out :)
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on October 08, 2008, 02:57:50 pm
Is this available as a mod instead of a package?
Is there anywhere to get a list of the changes that need to be made to implement subcategories.

@ Bobcat... (and anyone else looking for adventure...)
If you download the package I linked to above yesterday... and open the snews.php file that comes with it (in your good code editor)... and search for - Subcategory Add... you will find every change occurence in the file related to this Mod. I purposely left a lot of comments in the file so that anyone could find all their locations and transfer them to another snews.php file.

One very good way to find the differences in one file compared to another is to open both in WinMerge (http://winmerge.org/)... and you can copy sections from one side (file) to the other as you identify all of the different (highlighted) sections.
Title: Re: [PACKAGE] Subcategories
Post by: bobcat on October 08, 2008, 03:20:05 pm
Thanks keyrocks, never thought of that. Also thanks for the link to winmerge, sounds like it could be quite useful.  :)
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on October 08, 2010, 10:39:13 pm
It's been a long time since the last activity on this thread.
But I'm trying to do something that might not be possible with the 1.6 Category-Subcategory function-set and would appreciate some help.

The function-set I am working with displays text-links to both articles and pages under expandable Category and Sub-Category titles.
I am doing a variation of it to display only links to the latest articles or pages... as a vertical side-bar menu.

So, in this case, function categories() is now function latest_content_menu($start, $size) ... and the subcategories function is now function subcontent_menu($parent,$submenu).

I am using the $start  and $size parameters (used in 1.6's // MENU ARTICLES to limit what is fetched from the articles table.... and the function-tag uses the same parameters... like - latest_content_menu(0,3);
This works fine for article and page title links listed under expandable Category titles.

Problem is... I cannot seem to get these parameters to apply to article and page title links under the Sub-categories.

I'm kinda hoping Jason drops by and picks up on this one as he knows these functions inside and out.  8)
Title: Re: [PACKAGE] Subcategories
Post by: nukpana on October 09, 2010, 01:20:27 am
Hey Doug, how are you?

I assume you are referring to the $start & $size parameters to the subcategory function...
if so...
You need to pass the $start, $size to the subcontent_menu() function like so:
subcontent_menu($parent, $submenu, $start, $size)

So within latest_content_menu(0,3) it will call (as an example):
subcontent_menu($r['id'], $menutype, 0, 3)
Title: Re: [PACKAGE] Subcategories
Post by: Keyrocks on October 09, 2010, 04:46:02 pm
Thanks Jason... that does it!
I had added the $start and $size  parameters to the first line of the subcontent_menu function to be
        subcontent_menu($parent, $submenu, $start, $size)
... but I neglected to add them to the function-tag within function latest_content_menu ... which was the problem.

Again... thanks and Karma awarded!  :)