Huge reply alert ... probably several "whore a blog" alerts as well
Ok, lets start at the beginning with a typical post work flow ( for our blog software ) :
First you hit the write screen ... kinda an obvious first step .... and slap in a really cool title ( laden with keywords if you play the seo game ) for your post, choose a couple of categories and then you type your expertly crafted article that's going to blow the minds of the millions of adoring fans that follow your every word with bated breath and a star-struck look in their glazing eyes.... or, if it's my blog ... for the psychotic few that can't tell genius from blonde
And then you stare at the tags field for a while, wondering 2 things
1) How can I sum up such genius into tags?
2) What tags have a I used previously ( kinda important, because we also have a related posts plugin that uses tags to function [
Playing with relations ] )
That's where this plugin comes in. As you start typing tags it scurries off to the server and prods it to come up with a list of tags that have been used in previous posts that start with the same letter. Once the server stops sulking at having to do some work it spits back a list of each tag and wanders off muttering nasty things under it's breath .... it's never really forgiven me for the time I accidentally replaced the PHP process with a BASH script and promptly fried it's brains with load levels in excess of 600
When the script gets the reply it adds all the answers ( if any ) to the tags array. We chose to use a huge memory intensive array to make it quicker for the code to "predict" tags as you typed, rather than a smaller array which would be kinder to your pcs memory but would take slightly longer to search .... it's probably easier to show you by example ... free-typed, so don't complain if they fry your cpu when testing
Small array :
Code:
var tags = new Array('a_tag', 'another_tag', 'another_tag_2', 'tag_4', 'tag_5');
var current_tag = 'ta';// this is the tag that's being "typed"
var suggest_tags = new Array();
for( tag in tags )
{
if( tags[tag].susbtr( 0, current_tags.length ) == current_tag )
{ // starts with same letters as current tag
suggest_tags[ suggest_tags.length ] = tags[ tag ];
}
}
suggest_tags is now an array of potential tags. Now lets look at the memory intensive version :
Code:
var tags['a'] = new Array('a_tag', 'another_tag', 'another_tag_2' );
tags['t'] = new Array( 'tag_4', 'tag_5' );
tags['a_'] = new Array( 'a_tag' );
tags['a_t' ] = new Array( 'a_tag' );
tags['a_ta' ] = new Array( 'a_tag' );
tags['an' ] = new Array( 'another_tag', 'another_tag_2' );
tags['ano' ] = new Array( 'another_tag', 'another_tag_2' );
tags['anot' ] = new Array( 'another_tag', 'another_tag_2' );
tags['anoth' ] = new Array( 'another_tag', 'another_tag_2' );
// etc etc etc, until all tags are indexed like the above
var current_tag = 'ta';// this is the tag that's being "typed"
var suggest_tags = ( typeof( tags[current_tag] ) == 'undefined' ? Array( current_tag ) : tags[current_tag] );
As you can see, the second example is a lot more memory intensive, but getting suggested tags is a one liner
Your code can now take the suggested tags ( whichever way you decide to code the arrays ) and display a list to the user, our plugin also "auto-completes" the first suggested tags so the user can just press the right-arrow and move on with their tags.
Once you've picked all your tags, and ticked a few other boxes, you hit submit ... and the server, begrudgingly, slaps your new post into the database, creates entries for any unknown tags and then links all post tags to the new post in the "item_tags" table, ready for when the droves flock to read your latest masterpiece.
Quote:
Originally Posted by JoeyDaly
If only could be re-used as a standalone script.
|
It's actually quite easy to convert this into a standalone script with very few changes, although you will need to recode the database bit to match your own tables/methods as the plugin, obviously, uses the DB class that's already available and tag insertion etc are handled by the core.
First off lets meander through the javascript file because that's where most of the changes are required
Code:
/* line 6 : change item_tags to the name of your input box for tags*/
var amTagsField = document.getElementById( "item_tags" );
/* add the following line and change separator to suit */
var amTagsSeparator = ',';
/* line 22 : itemform_tags needs to be the id of the parent container for your item_tags <input>*/
document.getElementById( 'itemform_tags' ).appendChild( ourBox );
/* line 64 : htsrv_url needs to be the url to your php page that will return the tags */
var script = amTagsCreateElement( 'script', 'type="text/javascript" src="' + htsrv_url +'&am_tags_start='+amTagsLetter + '"' );
You can actually take most of the code in the php file and throw it out the window, it's merely there to inform our blog software that it's a plugin and can do some stuff with events. The only bit you'll want is this section, and even that's going to need a re-write to suit your setup :
PHP Code:
if( $startLetter = param( 'am_tags_start', 'string' ) )
{ // we have a start letter
if( $startLetter = substr( preg_replace( '~[^a-z]~i', '', $startLetter ), 0, 1 ) )
{ // single letter only ;)
global $DB;
$sql = 'SELECT tag_name from T_items__tag WHERE tag_name LIKE "'.$DB->escape( $startLetter ).'%" ORDER BY tag_name ASC';
if( $tagList = $DB->get_results( $sql, ARRAY_A ) )
{ // we have matching tags
foreach( $tagList as $aTag )
{
echo 'amTagsAddTag( "'.$aTag['tag_name'].'" );'."\n";
}
}
echo 'amTagsFetchTags(0);'."\n";
exit;
}
}
All this does is find out which start letter you're interested in and then it hits the database and ask for all currently known tags that begin with that letter. Then it simply wanders through the list and adds a javascript call for each item ( note to self : forgot the javascript headers :p ). It wouldn't be hard to recode that to use your own database.
As you can see from the code it doesn't do any analysis of your post to find/suggest tags, it just works off ones used in previous posts to make life easier when trying to tag up your pearls of wisdom
¥