pierovdfn

BBList Base

Jul 9th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: BBList
  4. Version: 1.0
  5. Plugin URI: http://www.vdfn.altervista.org/redirect/plugin_bblist.html
  6. Description: This plugin add the [bblist] tag to make the list of the entries. It needs BBCode.
  7. Author: Piero VDFN
  8. Author URI: http://www.vdfn.altervista.org/
  9. */
  10.  
  11. /**
  12.  * If a page is often visited it's better making a cache.
  13.  */
  14. define('BBLIST_CACHEFILE', CACHE_DIR.'plugin_bblist_tag.txt');
  15.  
  16. /**
  17.  * This class is the main class of the BBList plugin.
  18.  *
  19.  * @see The description of the plugin.
  20.  */
  21. class plugin_bblist {
  22.  
  23.     /**
  24.      * This function is the constructor.
  25.      */
  26.     function plugin_bblist() {
  27.         add_filter('bbcode_init', array(&$this, 'hook'));
  28.         add_filter('publish_post', array(&$this, 'delete'), 10, 1);
  29.         add_filter('delete_post', array(&$this, 'delete'), 10, 1);
  30.     }
  31.  
  32.     /**
  33.      * Delete cache. It's used as callback.
  34.      *
  35.      * @return boolean: True
  36.      */
  37.     function delete() {
  38.         if(file_exists(BBLIST_CACHEFILE)) {
  39.             @unlink(BBLIST_CACHEFILE);
  40.         }
  41.  
  42.         return true;
  43.     }
  44.  
  45.     /**
  46.      * This function get the list of entries.
  47.      *
  48.      * @return array: The list
  49.      */
  50.     function getEntriesList() {
  51.         if(file_exists(BBLIST_CACHEFILE)) {
  52.             include BBLIST_CACHEFILE;
  53.             return $list;
  54.         }
  55.  
  56.         // Make the list
  57.         $list=array();
  58.  
  59.         $query=new FPDB_Query(array('count'=>-1, 'fullparse'=>false), null);
  60.         while($query->hasMore()) {
  61.             list($id, $entry)=$query->getEntry();
  62.             $date=date_from_id($id);
  63.             $list[$date['y']][$date['m']][$date['d']][$id]=$entry['subject'];
  64.         }
  65.  
  66.         system_save(BBLIST_CACHEFILE, array('list'=>$list));
  67.         return $list;
  68.     }
  69.  
  70.     /**
  71.      * This function manages the bblist tag.
  72.      *
  73.      * @param string $action: The action
  74.      * @param array $attributes: The attributes
  75.      * @param string $content: The content of the tag
  76.      * @params array $params: The parameters
  77.      * @param mixed $node_object: Not known
  78.      * @return string: The replacement
  79.      */
  80.     function tag($action, $attributes, $content, $params, $node_object) {
  81.         if($action=='validate') {
  82.             return true;
  83.         }
  84.  
  85.         $list=(array)$this->getEntriesList();  
  86.  
  87.         // Just an year or a month or a day
  88.         if(isset($attributes['y'])) {
  89.             $y=$attributes['y'];
  90.             $list=(array)@$list[$y];
  91.             if(isset($attributes['m'])) {
  92.                 $m=$attributes['m'];
  93.                 if(strlen($m)==1) {
  94.                     $m='0'.$m;
  95.                 }
  96.                 $list=(array)@$list[$m];
  97.                 if(isset($attributes['d'])) {
  98.                     $d=$attributes['d'];
  99.                     if(strlen($d)==1) {
  100.                         $d='0'.$d;
  101.                     }
  102.                     $list=(array)@$list[$d];
  103.                 }
  104.             }
  105.         }
  106.  
  107.         // If list is empty
  108.         if(!empty($attributes['noentries']) && !count($list)) {
  109.             return $attributes['noentries'];
  110.         } elseif(!count($list)) {
  111.             return 'There aren\'t entries.';
  112.         }
  113.  
  114.         // Make the link?
  115.         $link=true;
  116.         if(isset($attributes['link'])) {
  117.             // Force boolean
  118.             $link=true && $attributes['link'];
  119.         }
  120.  
  121.         // Sort the list
  122.         $sort='asort';
  123.         if(@$attributes['sort']=='desc') {
  124.             $sort='arsort';
  125.         }
  126.         call_user_func($sort, &$list);
  127.  
  128.         // Finally make the list
  129.         $return='';
  130.  
  131.         return $return;
  132.     }
  133.  
  134.     /**
  135.      * This function adds the bblist tag to the BBCode plugin.
  136.      *
  137.      * @param object $bbcode: The bbcode instance
  138.      * @return object: $bbcode modified
  139.      */
  140.     function hook($bbcode) {
  141.         $bbcode->addCode(
  142.             'bblist',
  143.             'callback_replace_single',
  144.             array(&$this, 'tag'),
  145.             array(
  146.                 'usecontent_param'=>array('default')
  147.             ),
  148.             'list',
  149.             array('block', 'listitem'),
  150.             array()
  151.         );
  152.         return $bbcode;
  153.     }
  154.  
  155. }
  156.  
  157. new plugin_bblist();
Advertisement
Add Comment
Please, Sign In to add comment