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.

Pages: 1 2 [3] 4 5 ... 8

Author Topic: [PACKAGE] Subcategories  (Read 37112 times)

centered

  • Guest
Re: [PACKAGE] Subcategories
« Reply #30 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
« Last Edit: February 25, 2008, 12:06:29 AM by equilni »
Logged

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [PACKAGE] Subcategories
« Reply #31 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.
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.

centered

  • Guest
Re: [PACKAGE] Subcategories
« Reply #32 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
« Last Edit: February 25, 2008, 04:23:07 PM by equilni »
Logged

Ken Dahlin

  • Full Member
  • ***
  • Karma: 30
  • Posts: 139
    • http://www.kendahlin.com/
Re: [PACKAGE] Subcategories
« Reply #33 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

Logged

centered

  • Guest
Re: [PACKAGE] Subcategories
« Reply #34 on: February 25, 2008, 07:30:16 PM »

I updated the first post with bug fixes, so everything is in one place.
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [PACKAGE] Subcategories
« Reply #35 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.  ;)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [PACKAGE] Subcategories
« Reply #36 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']; }}
« Last Edit: February 26, 2008, 02:06:02 PM by Armen »
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.

centered

  • Guest
Re: [PACKAGE] Subcategories
« Reply #37 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
Logged

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [PACKAGE] Subcategories
« Reply #38 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.
« Last Edit: February 26, 2008, 09:48:57 AM by Armen »
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.

centered

  • Guest
Re: [PACKAGE] Subcategories
« Reply #39 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....
Logged

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [PACKAGE] Subcategories
« Reply #40 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.
« Last Edit: February 26, 2008, 10:11:31 AM by Armen »
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.

centered

  • Guest
Re: [PACKAGE] Subcategories
« Reply #41 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]);}
}
« Last Edit: February 26, 2008, 10:07:45 AM by equilni »
Logged

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [PACKAGE] Subcategories
« Reply #42 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.
« Last Edit: February 26, 2008, 10:17:01 AM by Armen »
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.

centered

  • Guest
Re: [PACKAGE] Subcategories
« Reply #43 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*

« Last Edit: February 26, 2008, 10:25:36 AM by equilni »
Logged

Armen

  • Sr. Member
  • ****
  • Karma: 41
  • Posts: 338
    • http://www.funnydays.ru
Re: [PACKAGE] Subcategories
« Reply #44 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.
Logged
Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.
Pages: 1 2 [3] 4 5 ... 8