BLOG:CMS :: Support Forum

Support Forum for BLOG:CMS

You are not logged in.

#1 30.01.2005 16:49

mitten
BLOG:CMS Junior
Registered: 13.06.2004
Posts: 40
Website

NP_TechnoratiTags

This plugin allows you to easily add links to Technorati's tagging facility.  For more information on tagging, see Technorati Tags

This is my first plugin, so if you have any trouble with it, please let me know!

Code:

<?php
/*
  Description:
      - This plugin creates links to Technorati from a list of user supplied 'tags.'
      - Output is either a series of plain <a href>-style links or an <ul> containing the
        <a href> links 
      - See http://www.technorati.com/tag/ for more information on tagging
  
  History:
    v0.1
      - original release

  Installaton instructions:
      - Upload the file to your plugins directory (yoursite.com/nucleus/plugins)
      - In the admin area, go to Plugins and install the plugin
      - Set options as desired
      
  Usage instructions:
      - When you add or edit a post, enter a list of tags, separated by commas.
        See http://www.technorati.com/tag/ for more information.
      - Add <%TechnoratiTags%> to the templates where you would like the tag links to appear.
        It needs to be placed in an Items area, such as on the Homepage and Detailed templates.
      - Options: 
          - Default output is a series of <a href> links, separated by spaces
          - By selecting the <ul> option, each link will be enclosed in a <li> element
            and the whole list in an <ul> element
          - You can enter a CSS class name for the <ul> if desired
          - You can specify whether to delete the database entries when uninstalling the plugin
      - For best results, also make sure that you are pinging Technorati with your new posts

  This plugin is based in large part on the code for the NP_Customfield plugin, 
  written by Tim Broddin.  That plugin also incorporated code from NP_Views and NP_Trackback.
  
*/

class NP_TechnoratiTags extends NucleusPlugin {

   // name of plugin
   function getName() {
      return 'Technorati Tags';
   }

   // author of plugin
   function getAuthor()  {
      return 'Laura Fisher';
   }

   // an URL to the plugin website
   // can also be of the form mailto:foo@bar.com
   function getURL()
   {
      return 'mailto:blog@mittenartworks.com';
   }

   // version of the plugin
   function getVersion() {
      return '0.1';
   }

   // a description to be shown on the installed plugins listing
   function getDescription() {
      return 'Generates a link to Technorati for each user-supplied tag';
   }

   function supportsFeature($what) {
      switch($what) {
        case 'SqlTablePrefix':
          return 1;
        default:
          return 0;
      }
   }

   function install() {
      // create database
      sql_query('CREATE TABLE IF NOT EXISTS ' . sql_table('plug_technoratitags') . ' (id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY , itemid INT(9) NOT NULL , name VARCHAR(255) NOT NULL , value VARCHAR(255) NOT NULL)');

      // allow user to enter title text to appear before list
      $this->createOption('linklisttitle','Enter text to appear before list:','text','Technorati Tags: ');

      // allow user to choose whether to enclose list in an <ul>
      $this->createOption('unorderedlist','Enclose link list in an <ul>?','yesno','no');

      // allow user to enter a CSS class name for the <ul>
      $this->createOption('classname','CSS class name for <ul> (if desired):','text','');

      // uninstall option
      $this->createOption('cleanup','Remove Technorati Tags data from table when uninstalling plugin?','yesno','no');
   }

   function unInstall() {
      if ($this->getOption('cleanup') == 'yes')
        mysql_query('DROP TABLE ' . sql_table('plug_technoratitags'));
   }

   function getEventList() {
      return array('PostAddItem','PreUpdateItem','AddItemFormExtras', 'EditItemFormExtras', 'PostDeleteItem');
   }

   function getTableList() {
     return sql_table('plug_technoratitags');
   }

   function event_AddItemFormExtras($data) {

      // Print the title
          echo '<h3>Technorati Tags</h3>';

      // Display the field
        echo '<p>';
        echo '<label for="plug_tt">Technorati Tags (separate tags with commas): </label>';
        echo '<input type="text" value="" id="plug_tt" name="plug_tt" size="30" />';
        echo '</p>';
   }

   function event_EditItemFormExtras($data) {

      // Print the title
          echo '<h3>Technorati Tags</h3>';

      // Query for the contents of the field
        $itemid = $data['variables']['itemid'];
        $query = "SELECT value FROM " . sql_table('plug_technoratitags') . " WHERE itemid='$itemid' ";
        $result = mysql_query($query);
        $num = mysql_num_rows($result);

            // Are there any matches?
            // If the field doesn't exist for this item, create it
            if ($num == 0) {
               $contents = "";
               $query = "INSERT INTO " .sql_table('plug_technoratitags'). "(id,itemid,value) VALUES('',$itemid,'');";
               mysql_query($query);

            } else {
               $contents = mysql_result($result,0,'value');
            }

      // Print the field
        echo '<p>';
        echo '<label for="plug_tt">Technorati Tags (separate tags with commas): </label>';
        echo '<input type="text" value="' . $contents . '" id="plug_tt" name="plug_tt" size="30" />';
        echo '</p>';
         
   }

   function event_PostAddItem($data) {
      
         $value = requestVar('plug_tt');
         $itemid = $data['itemid'];
         $query = "INSERT INTO " . sql_table('plug_technoratitags') . " (id,itemid,value) VALUES('',$itemid,'$value');";
         mysql_query($query);
         
   }

   function event_PreUpdateItem($data) {
      
         $value = requestVar('plug_tt');
         $itemid = $data['itemid'];
         $query = "UPDATE " . sql_table('plug_technoratitags') . " SET value='$value' WHERE itemid=$itemid ";
         if(mysql_query($query)) {
               if (mysql_affected_rows() == 0) {
                  $query = "INSERT INTO " . sql_table('plug_technoratitags') . " (id,itemid,value) VALUES('','$itemid','$value');";
                  mysql_query($query);
               }
         }      
   }

   function event_PostDeleteItem($data) {
      $itemid = $data['itemid'];
      $query = "DELETE FROM " . sql_table('plug_technoratitags') . " WHERE itemid=$itemid";
      mysql_query($query);
   }
   
   function doTemplateVar(&$item) {
      $itemid = $item->itemid;
      $query = "SELECT value FROM " . sql_table('plug_technoratitags') . " WHERE itemid='$itemid' ";
      $result = mysql_query($query);
      $num = mysql_num_rows($result);
      // Are there any matches?
      if ($num == 0) {
         echo "";
      } else {
         $value = mysql_result($result,0,'value');
         $tagarray = explode(',',$value);
         $cssclass = $this->getOption('classname');
         $listtitle = $this->getOption('linklisttitle');
         
         // print list title, if supplied
         if ($listtitle) echo $listtitle.' ';
         
         // enclose list in <ul> if that option is selected
         if ($this->getOption('unorderedlist') == 'yes') {
             echo '<ul';
             // add class name to <ul> if supplied
             if ($cssclass) {
                 echo ' class="'.$cssclass.'">';
             } else {
                 echo '>';
             }
         }
         
         // for each array value, print a link
         foreach ($tagarray as $taglink) {
             // trim spaces from string
             $taglink = trim($taglink);
         
             // enclose list in <li> if that option is selected
            if ($this->getOption('unorderedlist') == 'yes') echo '<li>';
            
            // print link
             echo '<a href="http://technorati.com/tag/'.$taglink.'" rel="tag">';
             echo $taglink;
             echo '</a> ';
            
             // close <li> if that option is selected
            if ($this->getOption('unorderedlist') == 'yes') echo '</li>';
             }
         
         // close <ul> if that option is selected
         if ($this->getOption('unorderedlist') == 'yes') echo '</ul>';
      }
   }
}
?>

Last edited by mitten (30.01.2005 16:52)

Offline

 

#2 30.01.2005 17:41

Radek Hulán
Site Admin
From: Prague, Czech Republic
Registered: 17.03.2004
Posts: 2509
Website

Re: NP_TechnoratiTags

mitten, just a suggestion, as BLOG:CMS supports MySQL 4.0 (mysql), MySQL 4.1 (mysqli) and SQLite database, it is not safe to use simple mysql_XXX calls,you should rather use functions provided in /admin/libs/db.php


--= BLOG:CMS developer =--

Offline

 

#3 30.01.2005 23:38

mitten
BLOG:CMS Junior
Registered: 13.06.2004
Posts: 40
Website

Re: NP_TechnoratiTags

Oh wow - I didn't realize there was such a big structural difference between 3.4 and 3.5 - you've got an all new directory structure!

As I'm still pretty new to all this (and most of this plugin was written by someone else!), if I understand you correctly, what I need to do is to replace the mysql_blah with the appropriate function from the new db.php, right?

So, for example, where I have

Code:

$result = mysql_query($query);

I should change it to

Code:

$result = sql_query($query);

Is that right?  Do I need to do anything else special in the plugin, like include those db.php functions, or are they already available to the plugin?

And one last question, I see what look like similar functions for most of the mysql_blah functions, but not the mysql_result function.  Should that be replaced with one of the 'fetch' functions?

Thanks for the suggestions and help, Radek.  I really am trying to learn how to do this right!

Offline

 

#4 31.01.2005 22:06

Radek Hulán
Site Admin
From: Prague, Czech Republic
Registered: 17.03.2004
Posts: 2509
Website

Re: NP_TechnoratiTags

I tried to stay as much compatible as possible, so, yes, using sql_query() instead of mysql_query() and/or sql_fetch_object() instead of mysql_fetch_object() does the trick :-)

The DB-layer will take care about using proper mysql, mysqli and/or sqlite functions (postgresql is being worked on as well)


--= BLOG:CMS developer =--

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson

TOPlist