xref: /plugin/sortablejs/syntax.php (revision e23d2af10a1d64a4d89078c01fd8644e87862f9a)
1<?php
2/**
3 * Sortablejs: Javascript for Sortable table
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Otto Vainio
7 * version 1.1 Fixed javascript error in sorttable js
8 * version 2.0 Added <div> to sort any table
9 * version 2.1 Changed script to allow multiple sortable tables in one page
10 * version 2.2 A table can now be sorted by one column by default.
11 * version 2.2a css+js compress broke this script. Now fixed some jslint complains.
12 * version 2.3 Added support for odt plugin. (Aurélien Bompard)
13 * version 2.3a Fixed default sort with aligned text (Andre Rauschenbach)
14 * version 2.4 Added options to set manual override options for column sort. (nosort, numeric, alpha, ddmm, mmdd)
15 * version 2.5 Fixed problems with secionediting, footnotes and edittable
16 * version 2.6 Added support for jQuery and dokuwiki Weatherwax ->
17 * version 2.7 Fixed problem with first row not getting sorted
18 * version 2.8 Fixed problem with first row not getting sorted in default sort. Added option "sumrow" to prevent sum line sort.
19 * version 2.9 fixed problem with header row being sorted in earlier versions of dokuwiki.
20 * version 2.10 fixed odt export (LarsGit223)
21 * version 2.11 Added ip address sort. Thanks Chefkeks
22 */
23// must be run within Dokuwiki
24if (!defined('DOKU_INC')) die();
25if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
26require_once(DOKU_PLUGIN.'syntax.php');
27
28class syntax_plugin_sortablejs extends DokuWiki_Syntax_Plugin {
29
30
31  function getType() { return 'container';}
32  function getPType(){ return 'block';}
33  function getSort() { return 371; }
34  function getAllowedTypes() {return array('container','formatting','substition');}
35	function connectTo($mode) {
36    $this->Lexer->addEntryPattern('<sortable[^>]*>(?=.*?</sortable>)',$mode,'plugin_sortablejs');
37//    $this->Lexer->addEntryPattern('\x3Csortable.*?\x3E',$mode,'plugin_sortablejs');
38//    $this->Lexer->addEntryPattern('<sortable>',$mode,'plugin_sortablejs');
39  }
40  function postConnect() {
41    $this->Lexer->addExitPattern('</sortable>','plugin_sortablejs');
42  }
43  function handle($match, $state, $pos, &$handler){
44
45    switch ($state) {
46      case DOKU_LEXER_ENTER :
47        $match = substr($match,9,-1);
48        $match=trim($match);
49        $scl="";
50        if (strlen($match)>0) {
51          $scl=$this->__validateOptions($match);
52        }
53        return array($state, $scl);
54        break;
55      case DOKU_LEXER_UNMATCHED :
56//        return p_render('xhtml',p_get_instructions($match),$info);
57        return array($state, $match);
58        break;
59      case DOKU_LEXER_EXIT :
60//        return "</div>";
61        return array($state, "");
62        break;
63    }
64    return array();
65  }
66
67  function render($mode, &$renderer, $data) {
68    list($state,$match) = $data;
69    if ($mode == 'xhtml'){
70      switch ($state) {
71        case DOKU_LEXER_ENTER :
72          $renderer->doc .= "<div class=\"sortable$match\">";
73          break;
74        case DOKU_LEXER_UNMATCHED :
75//          $dbgr = p_render('xhtml',p_get_instructions($match),$info);
76//          $renderer->doc .= p_render('xhtml',p_get_instructions($match),$info);
77//          $renderer->doc .= $match;
78//          $instructions = array_slice(p_get_instructions($match), 1, -1);
79          $instructions = p_get_instructions($match);
80          foreach ($instructions as $instruction) {
81            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
82          }
83
84          break;
85        case DOKU_LEXER_EXIT :
86          $renderer->doc .=  "</div>";
87          break;
88      }
89      return true;
90    } else if($mode == 'odt'){
91      switch ($state) {
92        case DOKU_LEXER_ENTER :
93          // In ODT, tables must not be inside a paragraph. Make sure we
94          // closed any opened paragraph
95          $renderer->p_close();
96          break;
97        case DOKU_LEXER_UNMATCHED :
98          $instructions = array_slice(p_get_instructions($match), 1, -1);
99          foreach ($instructions as $instruction) {
100            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
101          }
102          break;
103        case DOKU_LEXER_EXIT :
104          //$renderer->p_open();
105          // DO NOT re-open the paragraph, it would cause an error if the table is the last content on a page
106          break;
107      }
108      return true;
109    }
110    return false;
111  }
112
113  function __validateOptions($opts) {
114    $oa = split(" ", $opts);
115    $ret = "";
116    foreach($oa as $opt) {
117      list($c,$v) = split("=",$opt);
118      if ($c=="sumrow") {
119        $c=$v;
120        $v="sumrow";
121        if ($c=="") {
122          $c="1";
123        }
124      } else if ($c=="3phase") {
125        $v=$c;
126        $c="";
127      }
128      if ($v!=null) {
129        $cmpr=$v;
130      } else {
131        if (preg_match('/r?\d*/', $c, $matches)) {
132          $cmpr='sort';
133        }
134      }
135      switch ($cmpr) {
136        case '3phase':
137            $ret .= " threephase";
138            break;
139        case 'nosort':
140            $ret .= " col_" . $c . "_nosort";
141            break;
142        case 'numeric':
143            $ret .= " col_" . $c . "_numeric";
144            break;
145        case 'ddmm':
146            $ret .= " col_" . $c . "_ddmm";
147            break;
148        case 'mmdd':
149            $ret .= " col_" . $c . "_mmdd";
150            break;
151        case 'alpha':
152        case 'text':
153            $ret .= " col_" . $c . "_alpha";
154            break;
155        case 'sort':
156            $ret .= ' sort' . $opt;
157            break;
158        case 'sumrow':
159            $ret .= ' sortbottom_' . $c;
160            //$ret = ' sortbottom' . $ret;
161            break;
162      }
163    }
164    return $ret;
165  }
166
167}
168?>
169