<?php
 
/**
  * Plugin for Nucleus CMS (http://plugins.nucleuscms.org/)
  * Copyright (C) 2003 The Nucleus Plugins Project
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * see license.txt for the full license
  */
 
/**
 * Usage:
 *       
 *
 * Versions:
 *
 *  TODO
 *      - documentation
 *      - etc
 */
 
class NP_MultipleCategories extends NucleusPlugin {
 
   function NP_MultipleCategories() {
      $this->table_multiple_categories = sql_table('plug_multiple_categories');
   }
 
   function getName()   { return 'Multiple Categories'; }
   function getAuthor()     { return 'Anand'; }
   function getURL()    { return 'http://tamizhan.com/'; }
   function getVersion()    { return '0.1'; }
   function getDescription()
   {
      return 'Multiple Categories';
   }
 
   function supportsFeature($what) {
      switch($what)
      {
         case 'SqlTablePrefix':
            return 1;
         default:
            return 0;
      }
   }
 
   function install() {
 
      // create the table that will keep track of notifications
      $query =  'CREATE TABLE '. $this->table_multiple_categories. '(';   
      $query .= ' item_id int(11) NOT NULL,';
      $query .= ' categories varchar(255) default NULL,';      
       $query .= ' PRIMARY KEY  (item_id)';
      $query .= ') TYPE=MyISAM;';
 
      sql_query($query);
 
   }
 
   function unInstall() {
      sql_query('DROP TABLE ' .$this->table_multiple_categories);
   }
 
   function getTableList() {
      return array($this->table_multiple_categories);
   }
 
   function getEventList() {
      return array('PostAddItem','AddItemFormExtras', 'EditItemFormExtras', 'PostUpdateItem', 'PostDeleteItem');
   }   
 
   function init() {
   }
 
   /*
    * Adds an entry to the 'Quick Menu' on the Nucleus administration pages.
    * The entry will link to the commentcontrol admin page
    */
 
   function _getCategories($id)
        {
 
          $aResult = array();   
 
          $query = 'SELECT catid, cname FROM '.sql_table('category').' WHERE cblog=' . intval($id);
 
          $res = sql_query($query);   
 
          while ($o = mysql_fetch_object($res))       
          {           
 
         array_push($aResult, array(
            'catid'          => $o->catid,
            'name'             => $o->cname,
         ));
 
          }
 
     return $aResult;   
 
        }
 
   function event_AddItemFormExtras(&$data) {
 
         global $blogid, $blog;
 
               $blo = $data['blog'];
 
                      echo '<h2>Multiple Categories</h2>';
 
               $aList = $this->_getCategories($blogid);           
         if ( count($aList) > 0 )
           {
                   // Print the title
 
           $num = 0;
           foreach ($aList as $aCategory)
           {
 
              echo '<p>';
         echo '<input type="checkbox" id=cat'.$num.' name="cat['.$num++.']" value="'.$aCategory['catid'].'"size="30" />';
                   echo '<label for="plug_multiple_categories">'.$aCategory['name'].'</label>';                   
                echo '</p>';
         }
 
         }
        }
 
   function event_EditItemFormExtras(&$data) {
         global $blogid, $itemid;
 
         $blogid = $data['blog']->blogid;
 
           $query = "SELECT categories FROM ".$this->table_multiple_categories." WHERE item_id=".intval($itemid);
 
          $result = sql_query($query);
         $num = mysql_num_rows($result);
 
         if ( $num == 0 ) {
          $this->event_AddItemFormExtras(&$data);
                      }
         else {
 
               echo '<h2>Multipe Categories</h2>';
 
               $o = mysql_fetch_object($result);
 
                     $aList = $this->_getCategories($blogid);           
               if ( count($aList) > 0 )
                  {
                   // Print the title
                 $num = 0;
                 foreach ($aList as $aCategory)
                 {
           $data = explode(",",$o->categories);
 
           $checked = "";
           foreach ($data as $aCat)
           {
                  if ( intval($aCat) == intval($aCategory['catid']) )
              $checked = "checked=yes";
                     }
 
                echo '<p>';
           echo '<input type="checkbox" id=cat'.$num.' name="cat['.$num++.']" '.$checked.' value="'.$aCategory['catid'].'"size="30" />';
                     echo '<label for="plug_multiple_categories">'.$aCategory['name'].'</label>';                   
                  echo '</p>';
            }
           }
 
         }
 
   }
 
   function event_PostAddItem(&$data) {
 
      global $blogid, $catid;
 
      $selected = requestIntArray('cat');
 
      $itemid = $data['itemid'];        
 
      $cat_string = "";
 
      if ( count ($selected) > 0 )
      {
         foreach ($selected as $aCat)
         {
           if ( $cat_string == '' )
            $cat_string = intval($aCat);
           else
             $cat_string .= ','.intval($aCat);
 
                   }
 
         $query = "SELECT categories FROM ".$this->table_multiple_categories." WHERE item_id=".intval($itemid);
 
               $result = sql_query($query);
              $num = mysql_num_rows($result);
 
         if ( $num == 0) {
             $query = 'INSERT INTO '.$this->table_multiple_categories.' (item_id,categories) VALUES( "'.intval($itemid).'" , "'.addslashes($cat_string).'");';
                 sql_query($query);   
         }
         else {
             $query = 'UPDATE '.$this->table_multiple_categories
                      .' SET categories="'
                 .addslashes($cat_string)
                 .'" WHERE item_id='.intval($itemid);
                  sql_query($query);   
         }
      }
   }
 
   function doSKinVar()
   {
      global $blogid, $catid, $CONF, $manager;
 
      $amountEntries = 50;
 
      if (func_num_args() == 2) {
         if (func_get_arg(1) )   
         $templateName = func_get_arg(1);
              else
          $templateName = 'default';
 
             $b =& $manager->getBlog($CONF['DefaultBlog']);
 
      }
           else if (func_num_args() == 3) {
           $templateName = func_get_arg(1);
           $amountEntries = intval(func_get_arg(2));     
           if ( $blog )
           $b = &$blog;
           else
                   $b =& $manager->getBlog($blogid);
      }
           else if (func_num_args() == 4) {
           $templateName = func_get_arg(1);
           $amountEntries = intval(func_get_arg(2));     
           $catid    = getCatIDFromName(func_get_arg(3));
                $b =& $manager->getBlog($blogid);         
      }
 
      $query =  'SELECT i.inumber as itemid, i.iblog as blog, i.ititle as title, i.ibody as body, m.mname as author, m.mrealname as authorname, UNIX_TIMESTAMP(i.itime) as timestamp, i.imore as more, m.mnumber as authorid, m.memail as authormail, m.murl as authorurl, c.cname as category, i.icat as catid, i.iclosed as closed'
             . ' FROM nucleus_item as i, nucleus_member as m, nucleus_category as c'
             . ' WHERE i.iauthor=m.mnumber'
             . ' and i.iblog='.$blogid
             . ' and i.icat=c.catid'
             . ' and i.idraft=0'
             . ' and i.itime<=' . mysqldate($b->getCorrectTime());
 
        $query .= $extraQuery.' ORDER BY i.itime DESC';
 
        if ($amountEntries > 0)
         $query .= ' LIMIT ' . intval($startpos).',' . intval($amountEntries);
 
 
        $highlight = '';
        $comments = 0;
        $dateheads = 1;
 
        $lastVisit = cookieVar('lastVisit');
 
        if ($lastVisit != 0)
         $lastVisit = $b->getCorrectTime($lastVisit);
 
      // set templatename as global variable (so plugins can access it)
               global $currentTemplateName;
        $currentTemplateName = $templateName;
 
        $template = TEMPLATE::read($templateName);
 
      // create parser object & action handler
        $actions = new ITEMACTIONS($b);
        $parser = new PARSER($actions->getDefinedActions(),$actions);
        $actions->setTemplate($template);
        $actions->setHighlight($highlight);
        $actions->setLastVisit($lastVisit);
        $actions->setParser($parser);
        $actions->setShowComments($comments);
 
      // execute query
        $items = sql_query($query);
 
      // loop over all items
        while ($item = mysql_fetch_object($items)) {
 
      if ( $catid )
      {
        // we are in trouble !
 
             $mc_query = 'SELECT * FROM nucleus_item WHERE inumber='.$item->itemid.' and icat='.intval($catid);
 
        $mc_res = sql_query($mc_query);
 
        $mc_num = mysql_num_rows($mc_res);
 
        if ( $mc_num == 0 )
        {
 
           $mc_query = 'SELECT categories FROM nucleus_plug_multiple_categories WHERE item_id='.$item->itemid;
 
           $mc_res = sql_query($mc_query);
 
           $mc_num = mysql_num_rows($mc_res);
 
           if ( $mc_num == 0 )
           {
              continue;
                }
           else {
              $mc_item = mysql_fetch_object($mc_res);
 
              $cat_string = $mc_item->categories;
 
              $data = explode(",",$cat_string);
 
              $mc_flag = 0;
              foreach ($data as $aCat)
              {
                if ( $catid == intval($aCat) )
                 $mc_flag = 1;
                   }
 
              if ( $mc_flag == 0 )
               continue;
                 }
 
         }
           }
      // action handler needs to know the item we're handling
           $actions->setCurrentItem($item);
 
         // add date header if needed
      if ($dateheads) {
                   $new_date = date('dFY',$item->timestamp);
            if ($new_date != $old_date) {
               // unless this is the first time, write date footer
               $timestamp = $item->timestamp;
               if ($old_date != 0) {
                  $oldTS = strtotime($old_date);
                  $manager->notify('PreDateFoot',array('blog' => &$b, 'timestamp' => $oldTS));
                  $parser->parse(strftime($template['DATE_FOOTER'], $oldTS));
                  $manager->notify('PostDateFoot',array('blog' => &$b, 'timestamp' => $oldTS));                  
               }
               $manager->notify('PreDateHead',array('blog' => &$b, 'timestamp' => $timestamp));
               // note, to use templatvars in the dateheader, the %-characters need to be doubled in
               // order to be preserved by strftime
               $parser->parse(strftime($template['DATE_HEADER'],$timestamp));
               $manager->notify('PostDateHead',array('blog' => &$b, 'timestamp' => $timestamp));               
            }
            $old_date = $new_date;
         }
 
         // parse item
         $parser->parse($template['ITEM_HEADER']);
         $manager->notify('PreItem', array('blog' => &$b, 'item' => &$item));         
         $parser->parse($template['ITEM']);         
         $manager->notify('PostItem', array('blog' => &$b, 'item' => &$item));         
         $parser->parse($template['ITEM_FOOTER']);
 
      }
 
      $numrows = mysql_num_rows($items);
 
      // add another date footer if there was at least one item
      if (($numrows > 0) && $dateheads) {
         $manager->notify('PreDateFoot',array('blog' => &$b, 'timestamp' => strtotime($old_date)));      
         $parser->parse($template['DATE_FOOTER']);
         $manager->notify('PostDateFoot',array('blog' => &$b, 'timestamp' => strtotime($old_date)));
      }
 
      mysql_free_result($items);   // free memory
 
      return $numrows;
 
 
   }
 
   function event_PostDeleteItem(&$data)
   {
     $item_id = $data['itemid'];
 
     $query = 'DELETE FROM ' . $this->table_multiple_categories . ' WHERE item_id=' . intval($item_id);
 
     sql_query($query);
 
   }
 
   function event_PostUpdateItem(&$data) {
      $this->event_PostAddItem(&$data);
   }
 
   function _getCatNameFromID($id)
      {
         return quickQuery('SELECT cname as result FROM '.sql_table('category').' WHERE catid='.$id);
   }
 
   function _createCategoryLink($cat_id) {
 
         global $manager;
 
         $url = '';
 
         $manager->notify('CreateCategoryLink',array('cat_id' => $cat_id, 'url' => &$url));
 
         if ($url == '' )
               $url = createCategoryLink($cat_id,'');
 
         return $url;
 
   }
 
   function doTemplateVar(&$item) {
 
                 $query = "SELECT categories FROM ".$this->table_multiple_categories." WHERE item_id=".intval($item->itemid);
 
        $result = sql_query($query);
       $num = mysql_num_rows($result);
 
       $url = $this->_createCategoryLink($item->catid);
 
                 $cat_string = '<a href='.$url.'>'.$this->_getCatNameFromID($item->catid).'</a>';
 
       if ( $num == 0) {
 
                 }
       else {
           $mc_item = mysql_fetch_object($result);
 
           $data = explode(",",$mc_item->categories);
 
           $mc_flag = 0;
           foreach ($data as $aCat)
           {
              if ( intval($aCat) != intval($item->catid) )
               $cat_string .= ' , <a href='.$this->_createCategoryLink($aCat).'>'.$this->_getCatNameFromID($aCat).'</a>';
           }
 
       }
 
       echo $cat_string;
 
        }   
}       
 
?>
multiplecategoriescode.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