xref: /plugin/sortablejs/syntax.php (revision 11e1031cee63225d5d8c9cd7fdfa89a2decbf97a)
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 * version 2.12 php 7 compatibility. Cahnged split -> explode
23 */
24// must be run within Dokuwiki
25if (!defined('DOKU_INC')) die();
26if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
27require_once(DOKU_PLUGIN.'syntax.php');
28
29class syntax_plugin_sortablejs extends DokuWiki_Syntax_Plugin {
30
31
32  function getType() { return 'container';}
33  function getPType(){ return 'block';}
34  function getSort() { return 371; }
35  function getAllowedTypes() {return array('container','formatting','substition');}
36	function connectTo($mode) {
37    $this->Lexer->addEntryPattern('<sortable[^>]*>(?=.*?</sortable>)',$mode,'plugin_sortablejs');
38//    $this->Lexer->addEntryPattern('\x3Csortable.*?\x3E',$mode,'plugin_sortablejs');
39//    $this->Lexer->addEntryPattern('<sortable>',$mode,'plugin_sortablejs');
40  }
41  function postConnect() {
42    $this->Lexer->addExitPattern('</sortable>','plugin_sortablejs');
43  }
44  function handle($match, $state, $pos, &$handler){
45
46    switch ($state) {
47      case DOKU_LEXER_ENTER :
48        $match = substr($match,9,-1);
49        $match=trim($match);
50        $scl="";
51        if (strlen($match)>0) {
52          $scl=$this->__validateOptions($match);
53        }
54        return array($state, $scl);
55        break;
56      case DOKU_LEXER_UNMATCHED :
57//        return p_render('xhtml',p_get_instructions($match),$info);
58        return array($state, $match);
59        break;
60      case DOKU_LEXER_EXIT :
61//        return "</div>";
62        return array($state, "");
63        break;
64    }
65    return array();
66  }
67
68  function render($mode, &$renderer, $data) {
69    list($state,$match) = $data;
70    if ($mode == 'xhtml'){
71      switch ($state) {
72        case DOKU_LEXER_ENTER :
73          $renderer->doc .= "<div class=\"sortable$match\">";
74          break;
75        case DOKU_LEXER_UNMATCHED :
76//          $dbgr = p_render('xhtml',p_get_instructions($match),$info);
77//          $renderer->doc .= p_render('xhtml',p_get_instructions($match),$info);
78//          $renderer->doc .= $match;
79//          $instructions = array_slice(p_get_instructions($match), 1, -1);
80          $instructions = p_get_instructions($match);
81          foreach ($instructions as $instruction) {
82            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
83          }
84
85          break;
86        case DOKU_LEXER_EXIT :
87          $renderer->doc .=  "</div>";
88          break;
89      }
90      return true;
91    } else if($mode == 'odt'){
92      switch ($state) {
93        case DOKU_LEXER_ENTER :
94          // In ODT, tables must not be inside a paragraph. Make sure we
95          // closed any opened paragraph
96          $renderer->p_close();
97          break;
98        case DOKU_LEXER_UNMATCHED :
99          $instructions = array_slice(p_get_instructions($match), 1, -1);
100          foreach ($instructions as $instruction) {
101            call_user_func_array(array(&$renderer, $instruction[0]),$instruction[1]);
102          }
103          break;
104        case DOKU_LEXER_EXIT :
105          //$renderer->p_open();
106          // DO NOT re-open the paragraph, it would cause an error if the table is the last content on a page
107          break;
108      }
109      return true;
110    }
111    return false;
112  }
113
114  function __validateOptions($opts) {
115    $oa = explode(" ", $opts);
116    $ret = "";
117    foreach($oa as $opt) {
118      list($c,$v) = explode("=",$opt);
119      if ($c=="sumrow") {
120        $c=$v;
121        $v="sumrow";
122        if ($c=="") {
123          $c="1";
124        }
125      } else if ($c=="3phase") {
126        $v=$c;
127        $c="";
128      }
129      if ($v!=null) {
130        $cmpr=$v;
131      } else {
132        if (preg_match('/r?\d*/', $c, $matches)) {
133          $cmpr='sort';
134        }
135      }
136      switch ($cmpr) {
137        case '3phase':
138            $ret .= " threephase";
139            break;
140        case 'nosort':
141            $ret .= " col_" . $c . "_nosort";
142            break;
143        case 'numeric':
144            $ret .= " col_" . $c . "_numeric";
145            break;
146        case 'ddmm':
147            $ret .= " col_" . $c . "_ddmm";
148            break;
149        case 'mmdd':
150            $ret .= " col_" . $c . "_mmdd";
151            break;
152        case 'alpha':
153        case 'text':
154            $ret .= " col_" . $c . "_alpha";
155            break;
156        case 'sort':
157            $ret .= ' sort' . $opt;
158            break;
159        case 'sumrow':
160            $ret .= ' sortbottom_' . $c;
161            //$ret = ' sortbottom' . $ret;
162            break;
163      }
164    }
165    return $ret;
166  }
167
168}
169?>
170