WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: KonTrax on May 01, 2009, 05:11:37 AM

Title: Droplet : Header links for a newsgroup
Post by: KonTrax on May 01, 2009, 05:11:37 AM
I guess you allready have a lot of scripts like this one.
Just a simple droplet that generates linked headlines from a newsgroup.

USAGE : [[GetNewsHeads?group=2&page=false&max=2&start_date=-3 day&stop_date=now&order=ASC]]

group : the ID of the newsgroup you want to use  *  optional ("0" by default)

page : the ID of the newspage you want to use  *  optional ("false" by default)

max : max newspost headlines to collect  *  optional ("10" by default)

active : active/inactive (1/0) posts filter  *  optional ("1" by default)(false not supported)

start_date : earlyest newspost headlines to collect  *  optional ("-10 day" by default)

stop_date : latest newspost headlines to collect  *  optional ("now" by default)

order : Newest or Oldest posts first  *  optional ("ASC" or "DESC", "DESC" by default)


false :
When using "false" as a parameter that filter will ignore default value. This allowes to mix and get all.
example: ?group=false


Date examples:
now
- / +1 day
- / +1 week
- / +1 month
- / +1 year
- / +1 year 1 month 1 week 1 day
10 September 2000
next Thursday
last Monday


NEW! 20 May 2010 (use size 6 tabs in editor for best readability)
Code: [Select]
if( !function_exists('doif') ){
function doif( $pre , $str , $strict=FALSE ){
if( !$strict ){
if( $str ) return $pre.$str;
}else{
if( !is_bool($strict) ){
if( $str!==$strict ) return $pre.$str;
}else{
if( $str!==FALSE ) return $pre.$str;
}
}
return NULL;
}
}

global $database, $wb;
$mod_query = $database->query(
"SELECT title, link, published_when"
." FROM ".TABLE_PREFIX."mod_news_posts"
." WHERE"
." active = ".
((isset($active)) ? $active : 1 )
.doif(" AND group_id = ",
((isset($group)) ? $group : 0 ),'false')
.doif(" AND published_when >= ",
((isset($start_date)) ? strtotime($start_date) : strtotime("-10 day") ))
.doif(" AND published_when <= ",
((isset($stop_date)) ? strtotime($stop_date) : strtotime("now") ))
.doif(" AND page_id = " ,
((isset($page)) ? $page : false ),TRUE)
.doif(" ORDER BY post_id ",
((isset($order)) ? $order : "DESC" ),'false')
.doif(" LIMIT ",
((isset($max)) ? $max : 10 ),'false')
);
$mod_list = "";
while ( $row = $mod_query->fetchRow() ){
$mod_list .= '<a href="'.WB_URL.PAGES_DIRECTORY.$row["link"].PAGE_EXTENSION.'">'.$row["title"].'</a><br>';
}
return $mod_list;
UPDATED CODE
20:05:10

ADDED :
page
active
=false (get all)



The old code
Code: [Select]
if ( !isset($max) ){    $max = 10;    }
if ( !isset($start_date) ){    $start_date= "-10 day";    }
if ( !isset($stop_date) ){    $stop_date= "now";    }
if ( !isset($order) ){    $order= "DESC";    }
global $database, $wb;
$mod_query = $database->query("SELECT title, link, published_when FROM ".TABLE_PREFIX.
"mod_news_posts WHERE active='1' AND group_id = ".$group.
" AND published_when>=".strtotime($start_date).
" AND published_when<=".strtotime($stop_date).
" ORDER BY post_id ".$order." LIMIT ".$max);
$mod_list = " ";

while ( $row =& $mod_query->fetchRow()){

  $mod_list .= '<a href="'.WB_URL.PAGES_DIRECTORY.$row["link"].PAGE_EXTENSION.'">'.$row["title"].'</a><br>';

}
return $mod_list;

UPDATED CODE
03:05:09

ADDED :
start_date
stop_date
order
Title: Re: Droplet : Header links for a newsgroup
Post by: erpe0812 on May 01, 2009, 10:58:28 AM
Hi

please send it with this form (http://www.websitebakers.com/pages/droplets/submit-droplet.php) and let the droplet get part of the official library.

rgds

erpe
Title: Re: Droplet : Header links for a newsgroup
Post by: Ruud on May 01, 2009, 11:36:08 AM
Hi KonTrax,

Nice to see a first time user contribute to the Droplet library.
It is a nice idea too..

I made a few changes to your original Droplet.

1. The Max value was interpreted with one line less than it should. (< $max is now <= $max)
2. The url was not built correctly. WB_PATH was used, it should be WB_URL.
Also the /pages/ and the .php extention can be modified in WB. It is better to use the defined constants PAGES_DIRECTORY and PAGE_EXTENSION to build the correct url.

Code: [Select]
global $database, $wb;
$mod_query = $database->query("SELECT title, link FROM ".TABLE_PREFIX."mod_news_posts WHERE group_id = ".$group);

$i = 0;
while ($row =& $mod_query->fetchRow() && $i <= $max) {
$i++;

// To change how the links are presented, have fun with this variable
     $mod_list .= '<a href="'.WB_URL.PAGES_DIRECTORY.$row["link"].PAGE_EXTENSION.'">'.$row["title"].'</a><br>';

}

return $mod_list;

No need to submit the Droplet using the Form.  :-D
It is added on this page (http://www.websitebakers.com/pages/droplets/official-library/navigation/getnewsheads.php).

Ruud
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on May 01, 2009, 03:16:24 PM
Hi KonTrax,

Nice to see a first time user contribute to the Droplet library.
It is a nice idea too..

I made a few changes to your original Droplet.

1. The Max value was interpreted with one line less than it should. (< $max is now <= $max)
2. The url was not built correctly. WB_PATH was used, it should be WB_URL.
Also the /pages/ and the .php extention can be modified in WB. It is better to use the defined constants PAGES_DIRECTORY and PAGE_EXTENSION to build the correct url.

Thanks.

Just to mention, the $max starts at 0 so <= will give you one header more than you ask for.
Title: Re: Droplet : Header links for a newsgroup
Post by: Ruud on May 01, 2009, 03:38:51 PM
You're right.
I was put on a wrong track a bit because I was testing with group=0 (no group)
In that group there is an empty record (created by the installer) that was included but, since there is no title, wasn't displayed.

I updated the Droplet in the library.
It is now using $i < $ max again but the query has an extra WHERE clause to select only active items.

Ruud
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on May 01, 2009, 03:50:11 PM
I updated the Droplet in the library.
It is now using $i < $ max again but the query has an extra WHERE clause to select only active items.

Ruud

Of course.. active items, totaly forgot that! Thanks god that someone though of it

BTW Ruud, The Library says Example Call : [[[[GetNewsHeads?group=2&max=2]]]]
Title: Re: Droplet : Header links for a newsgroup
Post by: Ruud on May 01, 2009, 04:01:35 PM
BTW Ruud, The Library says Example Call : [[[[GetNewsHeads?group=2&max=2]]]]
Fixed..
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on May 01, 2009, 06:39:34 PM
I found a bug in my Droplet

New code is updated.

Ruud could you update it in the library?
Title: Re: Droplet : Header links for a newsgroup
Post by: Ruud on May 01, 2009, 11:56:33 PM
I updated the library after changing it a bit again  8-)

The first test ($i < $max) would give a notice that $max is not existing.
So I switched the tests for $max. Working without errors/notices now.

Also another notice was given about the $mod_list that was not initialized.
I added a $mod_list = '';

Now it actually should have an additional test for the optional start_date / end_date.  :-P
That would make it perfect.

Ruud
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on May 03, 2009, 06:49:11 PM
Now it actually should have an additional test for the optional start_date / end_date.  :-P
That would make it perfect.

Ruud

I'm on it
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on May 03, 2009, 09:29:48 PM
New features added. Ready for library update Ruud.

Information about the update in main post
Title: Re: Droplet : Header links for a newsgroup
Post by: Ruud on May 03, 2009, 11:53:33 PM
Nice work,

It is updated in the Official Droplet Library (http://www.websitebakers.com/pages/droplets/official-library/navigation/getnewsheads.php)

Ruud
Title: Re: Droplet : Header links for a newsgroup
Post by: LordDarkman on October 25, 2009, 02:30:02 AM
I've a small problem with the droplet. My call is
Code: [Select]
[[GetNewsHeads?group=2&max=10&start_date=-90 day&stop_date=now&order=ASC]] The droplet code
Code: [Select]
if ( !isset($max) ){    $max = 10;    }
if ( !isset($start_date) ){    $start_date= "-10 day";    }
if ( !isset($stop_date) ){    $stop_date= "now";    }
if ( !isset($order) ){    $order= "DESC";    }
global $database, $wb;
$mod_query = $database->query("SELECT title, link, published_when FROM ".TABLE_PREFIX.
"mod_news_posts WHERE active='1' AND group_id = ".$group.
" AND published_when>=".strtotime($start_date).
" AND published_when<=".strtotime($stop_date).
" ORDER BY published_until ".$order." LIMIT ".$max);
$mod_list = " ";

while ( $row =& $mod_query->fetchRow()){

  $mod_list .= '**** N E W S ****';
  $mod_list .= '<a href="'.WB_URL.PAGES_DIRECTORY.$row["link"].PAGE_EXTENSION.'">'.$row["title"].'</a>';

}
return $mod_list.'**** N E W S ****';
Only thing I changed is ORDER BY. But it displays news wich are not active. We use it to publish stream times at a webradio and I wand the news to disapear after Date is over.
When I hit on a not active News it shows me
Quote
Kein aktiver Inhalt auf dieser Seite vorhanden

Zurück
and the Droplet code is visibel. Can someone tell me how to change this?
Link to page http://gatesofdoom.de/ (http://gatesofdoom.de/)

CU Moritz
Title: Re: Droplet : Header links for a newsgroup
Post by: LordDarkman on October 25, 2009, 06:40:01 PM
I think I found a "workaround". My new call is
Code: [Select]
[[GetNewsHeads?group=2&max=10&start_date=-90 day&stop_date=+0&order=ASC]] This way it skips the old News. Can someone please test this to so we can change the droplet?
Just try stop_date=now and stop_date=+0

Thanks Moritz
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on November 13, 2009, 03:14:35 PM
I'm looking in to it
Title: Re: Droplet : Header links for a newsgroup
Post by: dbs on April 12, 2010, 06:32:33 PM
hello,
my news aren't in groups.
how ist the call?
or must delete the part in the droplet: AND group_id = ".$group."   ?

dbs
Title: Re: Droplet : Header links for a newsgroup
Post by: Ruud on April 13, 2010, 11:26:43 AM
The group "No Group" is group_id = 0,
Removing the group from the query would use "All groups".
Title: Re: Droplet : Header links for a newsgroup
Post by: dbs on April 13, 2010, 11:37:19 AM
group_id=0 display a white page...

removing groups works better for me ;-) thx
Title: Re: Droplet : Header links for a newsgroup
Post by: SickBoy75 on May 20, 2010, 11:50:33 AM
nice work, but i needed more...

What if you have more than one news page in a web??
I have 3 in mine, so i needed to list the news form every page, but in diferent places.

Yeah, i know i can use groups, but it's not exactlly the same.

BTW, it was so easy to modify that i couldn't resist, jejeje

this is how i did it:
Code: [Select]
if ( !isset($max) ){    $max = 10;    }
if ( !isset($start_date) ){    $start_date= "-10 day";    }
if ( !isset($stop_date) ){    $stop_date= "now";    }
if ( !isset($order) ){    $order= "DESC";    }
$txt_search="";
if ( isset($pageid) ){    $txt_search= " AND page_id=".$pageid;    }
global $database, $wb;
$mod_query = $database->query("SELECT title, link, published_when FROM ".TABLE_PREFIX.
"mod_news_posts WHERE active='1' AND group_id = ".$group.
" AND published_when>=".strtotime($start_date).
" AND published_when<=".strtotime($stop_date).
$txt_search.
" ORDER BY post_id ".$order." LIMIT ".$max);
$mod_list = " ";

while ( $row =& $mod_query->fetchRow()){

  $mod_list .= '<a href="'.WB_URL.PAGES_DIRECTORY.$row["link"].PAGE_EXTENSION.'">'.$row["title"].'</a><br>';

}
return $mod_list;

A new param is added, pageid, it's the ID of the news page you want to show.
It's optional, if not used nothing happends

This is what i add
Code: [Select]
$txt_search="";
if ( isset($pageid) ){    $txt_search= " AND page_id=".$pageid;    }
.....
Code: [Select]
" AND published_when<=".strtotime($stop_date).
$txt_search.

$txt_search do the trick, it's blank if no pageid is set.

If you like this, just say thanks.
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on May 20, 2010, 06:03:35 PM
nice work, but i needed more...

What if you have more than one news page in a web??
I have 3 in mine, so i needed to list the news form every page, but in diferent places.

Yeah, i know i can use groups, but it's not exactlly the same.

BTW, it was so easy to modify that i couldn't resist, jejeje

If you like this, just say thanks.

Thanx.

I just rewrote the hole thing with the page and some other small filter features.
Title: Re: Droplet : Header links for a newsgroup
Post by: SickBoy75 on May 20, 2010, 08:05:43 PM
I like the new code!!!
Title: Re: Droplet : Header links for a newsgroup
Post by: KonTrax on May 20, 2010, 10:56:26 PM
Thanks, good to hear.

I hope i'ts an easy read. Implemented my doif() function for max readability. btw, recommend size 6 tabs in editor