Advertisement
Guest User

Untitled

a guest
Jan 7th, 2012
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.86 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
  4.  * @author     Esther Brunner <wikidesign@gmail.com>
  5.  * @author     Gina Häu�^�ge <osd@foosel.net>
  6.  */
  7.  
  8. // must be run within Dokuwiki
  9. if (!defined('DOKU_INC')) die();
  10.  
  11. if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
  12. if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
  13. if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
  14.  
  15. class helper_plugin_pagelist extends DokuWiki_Plugin {
  16.  
  17.     /* public */
  18.  
  19.     var $page       = NULL;    // associative array for page to list
  20.     // must contain a value to key 'id'
  21.     // can contain: 'title', 'date', 'user', 'desc', 'comments',
  22.     // 'tags', 'status' and 'priority'
  23.  
  24.     var $style      = '';      // table style: 'default', 'table', 'list'
  25.     var $showheader = false;   // show a heading line
  26.     var $column     = array(); // which columns to show
  27.     var $header     = array(); // language strings for table headers
  28.         var $sort       = false;   // alphabetical sort of pages by pagename
  29.         var $rsort      = false;   // reverse alphabetical sort of pages by pagename
  30.  
  31.     var $plugins    = array(); // array of plugins to extend the pagelist
  32.     var $discussion = NULL;    // discussion class object
  33.     var $tag        = NULL;    // tag class object
  34.  
  35.     var $doc        = '';      // the final output XHTML string
  36.  
  37.     /* private */
  38.  
  39.     var $_meta      = NULL;    // metadata array for page  
  40.  
  41.     /**
  42.      * Constructor gets default preferences
  43.      *
  44.      * These can be overriden by plugins using this class
  45.      */
  46.     function helper_plugin_pagelist() {
  47.         $this->style       = $this->getConf('style');
  48.         $this->showheader  = $this->getConf('showheader');
  49.         $this->showfirsthl = $this->getConf('showfirsthl');
  50.                 $this->sort        = $this->getConf('sort');
  51.                 $this->rsort       = $this->getConf('rsort');
  52.  
  53.         $this->column = array(
  54.                 'page'     => true,
  55.                 'date'     => $this->getConf('showdate'),
  56.                 'user'     => $this->getConf('showuser'),
  57.                 'desc'     => $this->getConf('showdesc'),
  58.                 'comments' => $this->getConf('showcomments'),
  59.                 'linkbacks'=> $this->getConf('showlinkbacks'),
  60.                 'tags'     => $this->getConf('showtags'),
  61.                 );
  62.  
  63.         $this->plugins = array(
  64.                 'discussion' => 'comments',
  65.                 'linkback'   => 'linkbacks',
  66.                 'tag'        => 'tags',
  67.                 );
  68.     }
  69.  
  70.     function getMethods() {
  71.         $result = array();
  72.         $result[] = array(
  73.                 'name'   => 'addColumn',
  74.                 'desc'   => 'adds an extra column for plugin data',
  75.  
  76.                 'name'   => 'addColumn',
  77.                 'desc'   => 'adds an extra column for plugin data',
  78.                 'params' => array(
  79.                     'plugin name' => 'string',
  80.                     'column key' => 'string'),
  81.                 );
  82.         $result[] = array(
  83.                 'name'   => 'setFlags',
  84.                 'desc'   => 'overrides standard values for showfooter and firstseconly settings',
  85.                 'params' => array('flags' => 'array'),
  86.                 'return' => array('success' => 'boolean'),
  87.                 );
  88.         $result[] = array(
  89.                 'name'   => 'startList',
  90.                 'desc'   => 'prepares the table header for the page list',
  91.                 );
  92.         $result[] = array(
  93.                 'name'   => 'addPage',
  94.                 'desc'   => 'adds a page to the list',
  95.                 'params' => array("page attributes, 'id' required, others optional" => 'array'),
  96.                 );
  97.         $result[] = array(
  98.                 'name'   => 'finishList',
  99.                 'desc'   => 'returns the XHTML output',
  100.                 'return' => array('xhtml' => 'string'),
  101.                 );
  102.         return $result;
  103.     }
  104.  
  105.     /**
  106.      * Adds an extra column for plugins
  107.      */
  108.     function addColumn($plugin, $col) {
  109.         $this->plugins[$plugin] = $col;
  110.         $this->column[$col]     = true;
  111.     }
  112.  
  113.     /**
  114.  
  115.     /**
  116.      * Overrides standard values for style, showheader and show(column) settings
  117.      */
  118.     function setFlags($flags) {
  119.         if (!is_array($flags)) return false;
  120.  
  121.         $columns = array('date', 'user', 'desc', 'comments', 'linkbacks', 'tags');
  122.         foreach ($flags as $flag) {
  123.             switch ($flag) {
  124.                 case 'default':
  125.                     $this->style = 'default';
  126.                     break;
  127.                 case 'table':
  128.                     $this->style = 'table';
  129.                     break;
  130.                 case 'list':
  131.                     $this->style = 'list';
  132.                     break;
  133.                 case 'header':
  134.                     $this->showheader = true;
  135.                     break;
  136.                 case 'noheader':
  137.                     $this->showheader = false;
  138.                     break;
  139.                 case 'firsthl':
  140.                     $this->showfirsthl = true;
  141.                     break;
  142.                 case 'nofirsthl':
  143.                     $this->showfirsthl = false;
  144.                     break;
  145.                 case 'sort':
  146.                         $this->sort = true;
  147.                         break;
  148.                 case 'rsort':
  149.                     $this->rsort = true;
  150.                     break;
  151.                 case 'nosort':
  152.  
  153.                     break;
  154.                 case 'nosort':
  155.                         $this->sort = false;
  156.                         break;
  157.             }
  158.  
  159.             if (substr($flag, 0, 2) == 'no') {
  160.                 $value = false;
  161.                 $flag  = substr($flag, 2);
  162.             } else {
  163.                 $value = true;
  164.             }
  165.  
  166.             if (in_array($flag, $columns)) $this->column[$flag] = $value;
  167.         }
  168.         return true;
  169.     }
  170.  
  171.     /**
  172.      * Sets the list header  
  173.      */
  174.     function startList() {
  175.  
  176.         // table style
  177.         switch ($this->style) {
  178.             case 'table':
  179.                 $class = 'inline';
  180.                 break;
  181.             case 'list':
  182.                 $class = 'ul';
  183.                 break;
  184.             default:
  185.                 $class = 'pagelist';
  186.         }
  187.         if ($this->style == 'table') $this->doc = '<table class="'.$class.'">'.DOKU_LF;
  188.         else $this->doc = '<ul>'.DOKU_LF;
  189.         $this->page = NULL;
  190.  
  191.         $this->page = NULL;
  192.  
  193.         // check if some plugins are available - if yes, load them!
  194.         foreach ($this->plugins as $plug => $col) {
  195.             if (!$this->column[$col]) continue;
  196.             if (plugin_isdisabled($plug) || (!$this->$plug = plugin_load('helper', $plug)))
  197.                 $this->column[$col] = false;  
  198.         }
  199.  
  200.         // header row
  201.         if ($this->showheader) {
  202.             $this->doc .= DOKU_TAB.'<tr>'.DOKU_LF.DOKU_TAB.DOKU_TAB;
  203.             $columns = array('page', 'date', 'user', 'desc');
  204.             foreach ($columns as $col) {
  205.                 if ($this->column[$col]) {
  206.                     if (!$this->header[$col]) $this->header[$col] = hsc($this->getLang($col));
  207.                     $this->doc .= '<th class="'.$col.'">'.$this->header[$col].'</th>';
  208.                 }
  209.             }
  210.             foreach ($this->plugins as $plug => $col) {
  211.                 if ($this->column[$col]) {
  212.                     if (!$this->header[$col]) $this->header[$col] = hsc($this->$plug->th());
  213.                     $this->doc .= '<th class="'.$col.'">'.$this->header[$col].'</th>';
  214.                 }
  215.             }
  216.             $this->doc .= DOKU_LF.DOKU_TAB.'</tr>'.DOKU_LF;
  217.         }
  218.         return true;
  219.     }
  220.  
  221.     /**
  222.      * Sets a list row
  223.      */
  224.     function addPage($page) {
  225.  
  226.         $id = $page['id'];
  227.         if (!$id) return false;
  228.         $this->page = $page;
  229.  
  230.         if (!$id) return false;
  231.         $this->page = $page;
  232.         $this->_meta = NULL;
  233.  
  234.         // priority and draft
  235.         if (!isset($this->page['draft'])) {
  236.             $this->page['draft'] = ($this->_getMeta('type') == 'draft');
  237.         }
  238.         $class = '';
  239.         if (isset($this->page['priority'])) $class .= 'priority'.$this->page['priority']. ' ';
  240.         if ($this->page['draft']) $class .= 'draft ';
  241.         if ($this->page['class']) $class .= $this->page['class'];
  242.         if(!empty($class)) $class = ' class="' . $class . '"';
  243.  
  244.         if ($this->style == 'table') $this->doc .= DOKU_TAB.'<tr'.$class.'>'.DOKU_LF;
  245.         else $this->doc .= DOKU_TAB.DOKU_LF;
  246.  
  247.         $this->_pageCell($id);
  248.         if ($this->column['date']) $this->_dateCell();
  249.         if ($this->column['user']) $this->_userCell();
  250.         if ($this->column['desc']) $this->_descCell();
  251.         foreach ($this->plugins as $plug => $col) {
  252.             if ($this->column[$col]) $this->_pluginCell($plug, $col, $id);
  253.         }
  254.  
  255.         if ($this->style == 'table') $this->doc .= DOKU_TAB.'</tr>'.DOKU_LF;
  256.         return true;
  257.     }
  258.  
  259.     /**
  260.      * Sets the list footer
  261.      */
  262.     function finishList() {
  263.         if (!isset($this->page)) $this->doc = '';
  264.         else if ($this->style == table) $this->doc .= '</table>'.DOKU_LF;
  265.              else $this->doc .= '</ul>'.DOKU_LF;
  266.  
  267.         // reset defaults  
  268.  
  269.         $this->helper_plugin_pagelist();
  270.  
  271.         return $this->doc;  
  272.     }
  273.  
  274.     /* ---------- Private Methods ---------- */
  275.  
  276.     /**
  277.      * Page title / link to page
  278.      */
  279.     function _pageCell($id) {
  280.  
  281.         // check for page existence
  282.         if (!isset($this->page['exists'])) {
  283.             if (!isset($this->page['file'])) $this->page['file'] = wikiFN($id);
  284.             $this->page['exists'] = @file_exists($this->page['file']);
  285.         }
  286.         if ($this->page['exists']) $class = 'wikilink1';
  287.         else $class = 'wikilink2';
  288.  
  289.         // handle image and text titles
  290.         if ($this->page['image']) {
  291.             $title = '<img src="'.ml($this->page['image']).'" class="media"';
  292.             if ($this->page['title']) $title .= ' title="'.hsc($this->page['title']).'"'.
  293.                 ' alt="'.hsc($this->page['title']).'"';
  294.             $title .= ' />';
  295.         } else {
  296.             if($this->showfirsthl) {
  297.                 $this->page['title'] = $this->_getMeta('title');
  298.             } else {
  299.                 $this->page['title'] = $this->id;
  300.             }
  301.  
  302.             if (!$this->page['title']) $this->page['title'] = str_replace('_', ' ', noNS($id));
  303.             $title = hsc($this->page['title']);
  304.         }
  305.  
  306.         }
  307.  
  308.         // produce output
  309.         $content = '<a href="'.wl($id).($this->page['section'] ? '#'.$this->page['section'] : '').
  310.             '" class="'.$class.'" title="'.$id.'">'.$title.'</a>';
  311.         if ($this->style == 'list') $content = '<li>'.$content.'</li>';
  312.         return $this->_printCell('page', $content);
  313.     }
  314.  
  315.     /**
  316.      * Date - creation or last modification date if not set otherwise
  317.      */
  318.     function _dateCell() {
  319.         global $conf;
  320.  
  321.         if($this->column['date'] == 2) {
  322.             $this->page['date'] = $this->_getMeta(array('date', 'modified'));
  323.         } elseif(!$this->page['date'] && $this->page['exists']) {
  324.             $this->page['date'] = $this->_getMeta(array('date', 'created'));
  325.         }
  326.  
  327.         if ((!$this->page['date']) || (!$this->page['exists'])) {
  328.             return $this->_printCell('date', '');
  329.         } else {
  330.             return $this->_printCell('date', dformat($this->page['date'], $conf['dformat']));
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * User - page creator or contributors if not set otherwise
  336.      */
  337.     function _userCell() {
  338.         if (!array_key_exists('user', $this->page)) {
  339.             if ($this->column['user'] == 2) {
  340.                 $users = $this->_getMeta('contributor');
  341.                 if (is_array($users)) $this->page['user'] = join(', ', $users);
  342.             } else {
  343.  
  344.                 $this->page['user'] = $this->_getMeta('creator');
  345.             }
  346.         }
  347.         return $this->_printCell('user', hsc($this->page['user']));
  348.     }
  349.  
  350.     /**
  351.      * Description - (truncated) auto abstract if not set otherwise
  352.      */
  353.     function _descCell() {
  354.         if (array_key_exists('desc', $this->page)) {
  355.             $desc = $this->page['desc'];
  356.         } elseif (strlen($this->page['description']) > 0) {
  357.             // This condition will become true, when a page-description is given
  358.             // inside the syntax-block
  359.             $desc = $this->page['description'];
  360.         } else {
  361.             $desc = $this->_getMeta(array('description', 'abstract'));
  362.         }
  363.  
  364.         $max = $this->column['desc'];
  365.         if (($max > 1) && (utf8_strlen($desc) > $max)) $desc = utf8_substr($desc, 0, $max).'�^�';
  366.         return $this->_printCell('desc', hsc($desc));
  367.     }
  368.  
  369.     /**
  370.      * Plugins - respective plugins must be installed!
  371.      */
  372.     function _pluginCell($plug, $col, $id) {
  373.         if (!isset($this->page[$col])) $this->page[$col] = $this->$plug->td($id);
  374.         return $this->_printCell($col, $this->page[$col]);
  375.     }
  376.  
  377.     /**
  378.      * Produce XHTML cell output
  379.      */
  380.  
  381.     function _printCell($class, $content) {
  382.         if (!$content) {
  383.             $content = '&nbsp;';
  384.             $empty   = true;
  385.         } else {
  386.             $empty   = false;
  387.         }
  388.         if ($this->style != 'list') {
  389.             $this->doc .= DOKU_TAB.DOKU_TAB.'<td class="'.$class.'">'.$content.'</td>'.DOKU_LF;
  390.         } else {
  391.             $this->doc .= DOKU_TAB.DOKU_TAB.$content.''.DOKU_LF;
  392.         }
  393.         return (!$empty);
  394.     }
  395.  
  396.  
  397.     /**
  398.      * Get default value for an unset element
  399.      */  
  400.     function _getMeta($key) {
  401.         if (!$this->page['exists']) return false;
  402.         if (!isset($this->_meta)) $this->_meta = p_get_metadata($this->page['id']);
  403.         if (is_array($key)) return $this->_meta[$key[0]][$key[1]];
  404.         else return $this->_meta[$key];
  405.     }
  406.  
  407. }
  408. // vim:ts=4:sw=4:et:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement