PDA

View Full Version : Articles with Tags (and Search Bar)


Tobias
02-03-2007, 07:42 PM
Hi,
since a fellow customer of ours at SDDepot (http://www.sddepot.com) asked for it, I took some time to implement the following for the "p2_news" plugin (SD 2.4.0 core plugin):


Tags display of article's "Metakeywords" (which have to be entered comma-separated in the article settings per article). If no Metakeywords are entered, then there will be no "Tags" line of course.
Tags are clickable and will result in display of only those articles within the current category, that have the same tag as well.
"Search Bar" custom plugin file (attached file "p2_search.php") offering "Search Text" and "Search By" controls. The "Search By" includes Article, Author, Description, Tags and Title.
This Custom Plugin file is not required for the Tags display, but looks fine either above or below the Articles plugin.
Usage: 1. Rename the attached file to "p2_search.php" and upload it into your "p2_news" folder on your server. 2. Create a new Custom Plugin within the ACP and specify as "Include File": "p2_news/p2_search.php" (without quotes; if applicable, change "2" to the cloned plugin number).
Tested on SD 2.4.0 with "Friendly URLs" on/off.


For the tags and search functionality there are 2 code blocks in below order to be inserted in the file "p2_news.php" which needs to be of SD 2.4.0:


Step 0: Open an unpatched SD 2.4.0 version of "p2_news.php" in the "p2_news" folder within your SD installation. If you have already changed your "p2_news.php" file, be cautios to find the right code locations!
Step 1:
At line 552 you shall find these lines:

$getarticles = $DB->query("SELECT * FROM " . TABLE_PREFIX . "p2_news WHERE categoryid = '$categoryid'
AND (settings & 2) " . $extraquery . "
AND (datestart = 0 OR datestart < '" . time() . "')
AND (dateend = 0 OR dateend > '" . time() . "')
ORDER BY (settings & 8192) DESC, " . $order . " LIMIT $start, " . ($limit + 1) . "");

REPLACE above lines with this block:

// SDDEPOT 20070203 - Start -----------------------------------------------
// Check if a "search text" was submitted (which should be "posted" by
// a secondary search bar OR a "&p2_search=" link.
// If so, use the "$extraquery" variable to specify a WHERE clause.
$p2_catselect = " AND categoryid = '$categoryid' ";
$p2_search = isset($_POST['p2_search']) ? $_POST['p2_search'] : (isset($_GET['p2_search']) ? $_GET['p2_search'] : '');
$p2_search = urldecode($p2_search);
$p2_searchby = isset($_POST['p2_searchby']) ? $_POST['p2_searchby'] : (isset($_GET['p2_searchby']) ? $_GET['p2_searchby'] : 'article');
if(!empty($p2_search))
{
if(!(($p2_searchby=='article')||($p2_searchby=='au thor')||
($p2_searchby=='description')||($p2_searchby=='tag s')||
($p2_searchby=='title')))
{
$p2_searchby = 'article';
}
if($p2_searchby=='tags') $p2_searchby = 'metakeywords';
//REMOVE the BLANK in front of below 039 strings!!!
$p2_search = str_replace('$# 039;', '$# 39;', $p2_search);
$p2_search = str_replace('&quot;', '"', $p2_search);
$p2_search = addslashes(str_replace('&# 39;', "'", $p2_search));
$extraquery = " AND ($p2_searchby LIKE '%$p2_search%')";
// *ONLY* uncomment below line if search shall search across ALL
// categories in all articles!
//$p2_catselect = '';
}
// SDDEPOT 20070203 - END -------------------------------------------------

$getarticles = $DB->query("SELECT * FROM " . TABLE_PREFIX . "p2_news
WHERE (settings & 2) " . $extraquery . " $p2_catselect
AND (datestart = 0 OR datestart < '" . time() . "')
AND (dateend = 0 OR dateend > '" . time() . "')
ORDER BY (settings & 8192) DESC, " . $order . "
LIMIT $start, " . ($limit + 1) . "");

IMPORTANT: when copying code from here, remove the blanks in both the quoted strings with the "39;" in it!!!
Step 2:
At line 451 you shall find these lines:

// display submit news link?
if(in_array(2, $userinfo['pluginsubmitids']))

INSERT ABOVE lines this block of code:

// SDDEPOT 20070203 - Start -------------------------------------------------
// Display Metakeywords of Article as "Tags":
if(!empty($article['metakeywords']))
{
echo '<br /><br />
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="text-align:left;padding-top:4px;padding-bottom:4px;" width="5%">Tags:</td>
<td style="text-align:left;padding-left:4px;padding-right:4px;" width="95%">
';
$atags = explode(',',$article['metakeywords']);
foreach($atags as $tag){
$tag = rtrim(ltrim($tag));
if(!empty($tag)){
echo '<span style="white-space:nowrap;padding-right:4px;">
<a href="'.RewriteLink('index.php?categoryid='.$categoryid. '&p2_searchby=tags&p2_search='.urlencode($tag)).'">'.$tag.'</a></span>
';
}
} //foreach

echo '</td>
</tr>
</table>
';
}
// SDDEPOT 20070203 - End ---------------------------------------------------

Step 3:
Around line 650ff (some lines below Step 1's location), you shall find these lines:

if($start > 0)
{
echo '<td><a href="' . RewriteLink('index.php?categoryid=' . $categoryid . '&p2_start=' . ($start - $limit)) . '">' . $p2_language['previous'] . '</a></td>';
}

if($rows > $limit)
{
echo '<td align="right"><a href="' . RewriteLink('index.php?categoryid=' . $categoryid . '&p2_start=' . ($start + $limit)) . '">' . $p2_language['next'] . '</a></td>';
}

REPLACE those lines with this block of code:
This shall ensure correct "Next" / "Previous" links whilst a search pattern is active.

// SDDEPOT - 20070205 - start
$extralink = '';
if(!empty($p2_search))
{
if($p2_searchby=='metakeywords') $p2_searchby = 'tags';
$extralink = '&p2_search='.urlencode($p2_search).'&p2_searchby='.$p2_searchby;
}
// SDDEPOT - 20070205 - end
// SDDEPOT - 20070205 - added $extralink variable to below links:
if($start > 0)
{
echo '<td><a href="' .
RewriteLink('index.php?categoryid=' . $categoryid . '&p2_start=' . ($start - $limit).$extralink) .
'">' . $p2_language['previous'] . '</a></td>';
}

if($rows > $limit)
{
echo '<td align="right"><a href="' .
RewriteLink('index.php?categoryid=' . $categoryid . '&p2_start=' . ($start + $limit).$extralink) .
'">' . $p2_language['next'] . '</a></td>';
}

Step X:
Make a backup of your "p2_news.php" on your server and upload the changed file.


EDIT (2/4/2007):
* A live demo page (http://www.sddepot.com/articles_demo) has been created!

EDIT (2/5/2007):
* Take into account "Next" / "Previous" links (revised Step 1 and added Step 3). Also the attached "search" file was updated to show a "Clear" button for the search.

EDIT (2/11/2007):
* Code for Step 1 updated to better handle single/double quotes. Please "repaste" that code into your "p2_news.php" file. IMPORTANT: I had to add a blank in front of the *39 in that one line or the vB editor would replace it, please remove that blank!

EDIT (2/12/2007):
* Code for Step 3: fixed Prev/Next links with Friendly URLs.

If there should be problems, please let me know.

Mass Media Pro Plugin - Live Demo : Mass Media Pro (http://www.sddepot.com/mass_media/)
My SDDepot Blog (http://www.sddepot.com/community/blog/tobias/) - SDDepot - Skins and Plugins (http://www.sddepot.com/)

SKiZZ
02-04-2007, 02:02 AM
Anyone have this on a live site yet?

Tobias
02-04-2007, 09:30 AM
SKiZZ:
Please find the link to a live demo page in the first post.

abcohen
02-05-2007, 04:47 AM
Very Very Very Very Very Coooooool!!!!!!

Usurper
02-05-2007, 06:26 AM
Awesome mod!

I think I'll install this, since it doesn't look like SD is going to update the p2_news plugin any time soon. Thanks much for sharing.

Wow, just realized I'll finally be able to have a set of links to all of an author's articles in one category. That'll make sorting our reviews easier. I wonder how hard it would be to make your search script search in all categories.

Edit: Ha, nevermind, you built the way to search all categories right into the script. Now I just need to figure out how to give myself the option to search just the category or across all.

Edit #2: Did I set it up wrong, or does it only display the ten most recent items that match the search? The "next" link just takes me to the default second page of articles for that category.

Tobias
02-05-2007, 08:49 AM
Hi Usurper:
1) The search across all categories can be done, I tested it here already.
BUT the way the news plugin works, it does not allow to display the actual article outside of it's configured category and would display "article is offline" message (it always checks the categoryid in multiple places :( ).
To make this "search all" work, quite a few changes would be required and therefore I'd rather not go that way now.

2) I tried to provide a "first start" with the mod to show the possibility of tags and honestly forgot to think of the search "pagination" for that matter.
EDIT: first post has been updated to include a step for correct Next / Previous links! :)

The first post's code include above lines now.

Cheers,
Tobias

kreasta
02-05-2007, 09:56 AM
How about some impruvmetns liek 'find and replace'

the script must find the Tag in the News content and Replaceit with a Link (too much bold?)

Like on www.Gizmodo.com

ex:

Subdreamer is the most powerfull cms in the web. it has features and you can download in a second.
The best skins are made by Indigo.Media and you can download them for free.

-----------
Tags: Subdreamer , cms , features , download , Indigo.Media

Tobias
02-05-2007, 11:06 AM
Usurper: first post has an additional step for correct Next / Previous links.

Kreasta: IMHO replacing all tags within an article may be "visual overkill" (most likely ruining the actual article), not to mention the work required to make such an automated replacement mechanism fault-free. ;) I think this should be part of any core improvement of the plugin by SD itself.

Usurper
02-11-2007, 03:18 AM
Thanks for the quick fix.

I've noticed that using an apostrophe in a keyword creates a sql error. Is it possible to fix that, or is it a limitation we'll have to work around?

HeavyEddie
02-11-2007, 03:24 AM
Hi Usurper:
1) The search across all categories can be done, I tested it here already.
BUT the way the news plugin works, it does not allow to display the actual article outside of it's configured category and would display "article is offline" message (it always checks the categoryid in multiple places :( ).
To make this "search all" work, quite a few changes would be required and therefore I'd rather not go that way now.



Tobias, you could use an article placer link to show the article despite it's category. Something to think about.

Tobias
02-11-2007, 07:47 AM
Usurper:

I haven't seen that SQL error myself, but I nevertheless revised the code for Step 1 in the first post to better handle single/double quotes.

Please "repaste" that code into your "p2_news.php" file.

IMPORTANT: I had to add a blank in front of the *39 in 2 lines or the vB editor would replace it, please remove those blanks in front of the *39!

Hopefully this fixes the problem.

Ed:
maybe in time I'll find the time to try it out with the Article Placer, thanks for the idea.

Usurper
02-11-2007, 08:58 AM
You the man, sir. I'll test it Sunday or Monday and get back to you on it.

Usurper
02-11-2007, 11:33 PM
The quote fix is great; no more sql errors.

However, the prev/next links still don't appear to be working. If I try it on my site in my reviews category, searching by author, the url I get is formed as category/p2_start/10&p2_search=Usurper&p2_searchby=author

The link simply returns the first 10 articles in the category instead of skipping to the next 10 by myself.

I tried forming a similar link on your demo page and "http://www.sddepot.com/articles_demo/p2_start/2&p2_search=Tobias&p2searchby=author" behaves the same way.

Can you confirm that it's forming the correct url, or did I mispaste something?

Tobias
02-12-2007, 05:20 AM
Usurper:
yes, you're right, the previous/next links weren't correct for Friendly URLs as I had placed the search variables just outside the link function. :rolleyes:
Sorry for that oversight, please repaste the code from Step 3 of the first post.
Regards,
Tobias

Usurper
02-12-2007, 10:55 AM
Working now. Thanks again!

I daresay I'm almost violently offended that everyone isn't using this mod. Of the 3 downloads of the attached file, I'm two of them.

SKiZZ
02-12-2007, 08:59 PM
I'll be using it shortly, just haven't had time to implement it yet. Since I'm the only staff member of my site.

Community
02-21-2007, 10:20 PM
Is there are fixed mod? because I don't want to mess up my live site. But this mod looks great ;)

Tobias
02-21-2007, 11:06 PM
Community: the code and attachment in the first post are current and work fine. :)

X10tion
02-22-2007, 11:16 AM
up and running :D thanks a hell of a lot .. looks NEAT

william
02-22-2007, 10:42 PM
I am glad you got the demo up Tobais, I was eager to see what it looked like...sadly I guess I have see to many Tag sites recently and was expecting something more like on this page (http://www.builderau.com.au/) [right side down 5th box down]

Maybe this could be a suggestion for an actual plugin? Is it even possible?

Tobias
02-22-2007, 11:01 PM
William: the kind of "weighted tags" display seems to be pretty modern these days, I've seen similar on German news sites. It is indeed a nice visual representation.
And yes, I guess with one additional table (for tag-hit-counters) and some extra code that kind of plugin is well doable. ;)

william
02-22-2007, 11:58 PM
oooooooooo :D

recon
03-13-2007, 08:45 AM
Great!

for sd version > 2.41

code have been changed
this part not:

$getarticles = $DB->query("SELECT * FROM " . TABLE_PREFIX . "p2_news WHERE categoryid = '$categoryid'
AND (settings & 2) " . $extraquery . "
AND (datestart = 0 OR datestart < '" . time() . "')
AND (dateend = 0 OR dateend > '" . time() . "')
ORDER BY (settings & 8192) DESC, " . $order . " LIMIT $start, " . ($limit + 1) . "");

change with:
$getarticles = $DB->query("SELECT * FROM {p2_news} WHERE categoryid = %d
AND (settings & 2) " . $extraquery . "
AND (datestart = 0 OR datestart < %d)
AND (dateend = 0 OR dateend > %d)
ORDER BY (settings & 8192) DESC, " . $order . " LIMIT %d, %d",
$categoryid, time(), time(), $start, ($limit + 1));

by this mode below:
// SDDEPOT 20070203 - END -------------------------------------------------
follow:

$getarticles = $DB->query("SELECT * FROM {p2_news} WHERE categoryid = %d
AND (settings & 2) " . $extraquery . "
AND (datestart = 0 OR datestart < %d)
AND (dateend = 0 OR dateend > %d)
ORDER BY (settings & 8192) DESC, " . $order . " LIMIT %d, %d",
$categoryid, time(), time(), $start, ($limit + 1));

Tobias
03-13-2007, 10:13 AM
Recon: your code is for SD >= 2.4.0, yes, though SD customers who may use e.g. 2.3.5 may use the code I provided :)

kboy_16
05-21-2007, 05:03 AM
very CoOl!! ;)

Usurper
03-12-2008, 02:06 AM
Any reason this wouldn't work in SD 2.5? I had the no-longer-available SDDepot version until I overwrote it with the last upgrade. I didn't really need the front-end editing aspects anyway, just the awesome search bar and tags.

This really is one of the best SD mods ever.

Tobias
03-13-2008, 11:48 PM
I'll eventually integrate this mod into the Subdreamer core, most likely v3.

Usurper
03-14-2008, 12:14 AM
Very glad of that. I'm going to try adding the mod into 2.5. Might as well do all my xhtml edits while I'm at it. Hopefully there aren't any side effects. I know you're probably too busy with SD to support this mod in its current state, so fingers are crossed.

Edit: It seems to be working so far.

Bidibooum
03-26-2008, 11:45 AM
Is that works with the latest subdreamer version?

Usurper
03-27-2008, 10:45 PM
Haven't found any bugs yet. Seems to work just fine.

amasilviama
02-23-2009, 01:33 PM
I don't know too much about google or programming, but, from what i've heard, if you post 10 tags on an article, and each of them becomes a link towards the article, isn't that called spam ? What i want to know is : does google penalize the sites that have these tags ?

thomas
02-23-2009, 11:04 PM
well they dont exactly lead back to the one article.

A tag url will they show a search result of articles with that "tag" so it is unique content

amasilviama
02-24-2009, 07:52 AM
Thank you very much for answering me!

amasilviama
03-26-2009, 12:43 PM
Hi,

I was wondering if those tags, i have them on my site btw, could become a "Tag Cloud" ? is there a way to gather them into one single place from all articles ?

amasilviama
04-07-2009, 02:09 PM
Hi,

I come back with this question : can you guys pull these tags from the articles in a tag cloud ? i already have those tags on my site and i don't know if what thomas did would affect my site, because they were indexed by google and i'm afraid to use thomas's plugin for now.

Thank you!

thomas
04-07-2009, 03:22 PM
the tags are generated from the "keywords" field on each article (which should be seperated by a comma"

amasilviama
04-08-2009, 07:29 AM
Hello Thomas,

The thing is that i already made the changes in the p2_news.php file as the guy from the sddepot said, and it really works. I know the way they are created, from metakeywords. The fact that i don't know is, if i go with your plugin, will that affect the tags i already have on my site ? I don't know if you created the tag cloud plugin all by yourself, from the ground, or you made something out of these changes that sddepot suggested at the beginning of this thread. Because if it is so, the tag cloud would work on my site, if it's something else, it wouldn't.
That's what i don't know.

thomas
04-08-2009, 08:44 AM
sddepot is no longer around or supporting any plugins.

You should look for alternative solutions as the modifications in this thread are not supported.