<?
/*
  History:
    v1.1
      - add getTableList
    v1.2
      - use sql_table
      - Added silent mode (configure via option) to support NP_MostViews
      - Added Cleanup upon uninstall option
      - Added "just number" minimalist mode
      - Added supportsFeature
    V1.2a
      - Added min version support
    V1.3
      - Replaced doSkinVar with DoTemplateVar
    V1.3a
      - Added repeat views ignore function aka repeat F5s from those bored and lonely one
    V1.3b
      - Fixed counting off by 1 bug
    V1.3c
      - Added <%Views(skipCount)%> to allow skipping count when used in template (ie to not count on click in main page)
    V1.4
      - Fixed ignoe same IP count problem
 
      to do:
    - http://forum.nucleuscms.org/viewtopic.php?p=23203#23203
    - add blogid
 
*/
class NP_Views extends NucleusPlugin {
 
   // Note: I never run this plugin on 2.0 and have no idea whether it
   //       wil work on <2.5. A user can simply chnage it to return
   //       '200' and see if it works (likely will). I will gladly
   //       change the min version to 2.0 and add the sql_table fix
   //       upon such report. 8)
   function getMinNucleusVersion() { return '250'; }
   function getName() {   return 'Views';    }
   function getAuthor()  { return 'Rodrigo Moraes | Edmond Hui (admun)';    }
   function getURL() {      return 'http://www.tipos.com.br'; }
   function getVersion() {   return '1.4'; }
   function getDescription() {
      return 'This plugin counts how many times an entry has been displayed.';
   }
 
   function supportsFeature($what) {
     switch($what) {
       case 'SqlTablePrefix':
         return 1;
       default:
         return 0;
     }
   }
 
   function getTableList() {
     return sql_table('plugin_views');
   }
 
   function install() {
      sql_query('CREATE TABLE IF NOT EXISTS ' . sql_table('plugin_views') . ' (id int(11) NOT NULL default "0", views int(15) NOT NULL default "0", ip char(15))');
      $this->createOption('Slient','Silent mode - No #Display shown in Item (still need to add the skinVar, for use with MostViewed)','yesno','no');
      $this->createOption('IgnoreRepeat','Ignore repeat viewing from same IP?','yesno','yes');
      $this->createOption('Cleanup','Cleanup views table from MySQL upon uninstall?','yesno','yes');
      $this->createOption('JustNum','Only return the number of display?','yesno','no');
   }
 
   function unInstall() {
      if ($this->getOption('Cleanup') == 'yes')
        mysql_query('DROP TABLE ' . sql_table('plugin_views'));
   }
 
   function doTemplateVar(&$item, $skipCountInTemp) {
      $itemid = $item->itemid;
 
      $remote_ip = ServerVar('REMOTE_ADDR');
 
      if ($itemid != 0) {
         $query = "SELECT views,ip FROM " . sql_table('plugin_views') . " WHERE id=" . $itemid;
         $res = mysql_query($query);
         $row = mysql_fetch_object($res);
         $views = intval($row->views);
 
         // pass in skipCount to overridr counting in default template allow to skip count for main page
         if ($skipCountInTemp != 'skipCount') {
            if (mysql_num_rows($res) == 0) {
               $query = "INSERT INTO " . sql_table('plugin_views') . " VALUES ('$itemid','1', '$remote_ip')";
               $res = mysql_query($query);
               $views = 1;
            } else {
               if ($this->getOption('IgnoreRepeat') == 'no') {
                  $views++;
                  $query = "UPDATE " . sql_table('plugin_views') . " SET ip='$remote_ip', views='$views' WHERE id=$itemid";
                  $res = mysql_query($query);
               } else {
                  if ($remote_ip != $row->ip) {
                     $views++;
                     $query = "UPDATE " . sql_table('plugin_views') . " SET ip='$remote_ip', views='$views' WHERE id=$itemid";
                     $res = mysql_query($query);
                  }
               }
            }
         }
 
         if ($this->getOption('Slient') == 'no') {
            if ($this->getOption('JustNum') == 'yes') {
               echo $views;
            } else {
               echo "This post was displayed ".$views." time";
               if ($views > 1) echo "s";
               echo ".";
            }
         }
      }
   }
}
?>
viewscode.txt · Last modified: 2006/07/05 13:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki