1<?php
2/**
3 * Plugin OpenAs.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author    Myron Turner <turnermm02@shaw.ca>
7 */
8
9// must be run within DokuWiki
10if(!defined('DOKU_INC')) die();
11//ini_set("display_errors","1");
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once DOKU_PLUGIN.'syntax.php';
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_openas extends DokuWiki_Syntax_Plugin {
20    var $labels = array();
21    function getType() { return 'substition'; }
22    function getSort() { return 60; }
23    function getPType() { return 'block'; }
24    function connectTo($mode) {
25
26        $this->Lexer->addEntryPattern('~~OpenAsVarsStart~~', $mode, 'plugin_openas');
27        $this->Lexer->addPattern('~~OpenAsVar>TAreaOpen~~', $mode, 'plugin_openas');
28        $this->Lexer->addPattern('~~OpenAsVar>TAreaClose~~', $mode, 'plugin_openas');
29        $this->Lexer->addPattern('~~OpenAsVAR>.*?~~','plugin_openas');
30        $this->Lexer->addPattern('~~OpenAsNUM>.*?~~','plugin_openas');
31        $this->Lexer->addSpecialPattern('~~SaveAS>.*?~~',$mode,'plugin_openas');
32        $this->Lexer->addSpecialPattern('~~OpenAS>.*?~~',$mode,'plugin_openas');
33        $this->Lexer->addSpecialPattern('~~MoveTO>.*?~~',$mode,'plugin_openas');
34    }
35
36    function postConnect()
37    {
38      $this->Lexer->addExitPattern('~~OpenAsVarsClose~~', 'plugin_openas');
39    }
40
41    function handle($match, $state, $pos, Doku_Handler $handler) {
42         global $ID;
43
44         $actions = array('SaveAS' => 'save', 'MoveTO' => 'delete');
45         $which = array('SaveAS'=>'saved as', 'MoveTO' => 'renamed');
46         $file = wikiFN($ID);
47         list($type,$name,$newpagevars) = explode('>',(trim($match,'~')));
48         $name=trim($name);
49         $labels = $this->getConf('labels');
50         if($labels == 'none') {
51             $this->labels['open'] = "";
52             $this->labels['close'] = "";
53         }
54          else {
55             $labels = trim($labels);
56             $ltype = $labels[0];
57             $this->labels['open'] = "<$ltype>";
58             $this->labels['close'] = "</$ltype>";
59         }
60
61         switch ($state) {
62              case DOKU_LEXER_ENTER : return array($state, '');
63              case DOKU_LEXER_UNMATCHED :  return array($state, $match);
64              case DOKU_LEXER_MATCHED :
65                  if($type == 'OpenAsNUM') {
66                      return array($state,"$type:$name");
67                 }
68                      return array($state, $name);
69              case DOKU_LEXER_EXIT :       return array($state, '');
70              case DOKU_LEXER_SPECIAL :
71         if($name[0] != ':') $name = ":$name";
72
73         if($type == 'SaveAS' || $type == 'MoveTO') {
74           $action = $actions[$type];
75           $newfile = wikiFN($name);
76           $contents = file_get_contents($file);
77           $contents = preg_replace('/~~' . $type .'.*?~~/',"",$contents,1);
78           io_saveFile($newfile,$contents);
79           $wikilink = html_wikilink("$name?saveas_orig=$ID&openas=$action");
80           $msg = "$ID " . $this->getLang('will_save') .   $which[$type] ."  $name.<br />";
81           $match = $msg  .  $this->getLang('open_wl') . "<br /> $wikilink";
82         }
83         else if($type == 'OpenAS') {
84           list($id,$template) = explode('#',$name);
85           $newpagevars = urlencode($newpagevars);
86           $match = $this->getLang('open_page'). '<br />' .
87            html_wikilink("$id?do=edit&rev=&newpagetemplate=$template&newpagevars=$newpagevars");
88         }
89         return array($state,$match);
90    }
91    }
92
93    function render($mode, Doku_Renderer $renderer, $data) {
94        list($state,$match) = $data;
95
96        if($mode == 'xhtml'){
97            switch ($state) {
98            case DOKU_LEXER_ENTER :
99                    $renderer->doc .= '<div id="openasrepl" class="openasrepl"><form id="open_as_var">' ."\n";
100                    break;
101            case DOKU_LEXER_UNMATCHED :
102                   $text = $renderer->_xmlEntities($match);
103                   $text = preg_replace('/\*\*(.*?)\*\*/ms', $this->labels['open'] ."$1" .$this->labels['close'] ,$text);
104                   $text = preg_replace ("#\s*\\\\\s*#",'<br />',$text);
105                   $renderer->doc .=  $text;
106            break;
107            case DOKU_LEXER_MATCHED :
108               if(preg_match('/^TAreaOpen:(.*?)$/',$match,$matches)) {
109                   $renderer->doc .= "<textarea rows = '4' cols = '60' name = '$matches[1]' id = '$matches[1]' class='open_as_repl'>\n";
110               }
111               else if($match == 'TAreaClose') {
112                   $renderer->doc .= "</textarea>\n";
113               }
114               else {
115                   $size = "24";
116                   if(preg_match("/^OpenAsNUM:(.*?)$/",$match,$matches)) {
117                       $match = $matches[1];
118                       $size = "10";
119                   }
120                   $renderer->doc .= "<input type='text' size='$size' name= '$match'  id ='$match' class='open_as_repl'>&nbsp;\n";
121                }
122               break;
123            case DOKU_LEXER_EXIT :  $renderer->doc .= '</form></div>' . "\n";
124                   break;
125            case DOKU_LEXER_SPECIAL :
126            $class= 'save_as';
127                if(preg_match("/SAVEAS_PAGE/i",$match)) {
128                     $id = $this->labels['open'] . $this->getLang('pageid')  .$this->labels['close'] ;
129                 $renderer->doc .= '<div class="save_as_info">' . "\n";
130                     $renderer->doc .= '<form id="save_as_info">' . $id . ' <input type="text" size="24" name="save_as_page" id ="save_as_page">' . "\n";
131                 $renderer->doc .= "</form></div>\n";
132                 $class ='save_as_tmpl';
133            }
134            $renderer->doc .= '<div class="'. $class .'">' . $match . '</div>';
135            return true;
136        }
137    }
138        return false;
139    }
140
141  function  getReplacementAttributes($name) {
142
143  }
144  function write_debug($what) {
145     $handle = fopen("saveas.txt", "a");
146     if(is_array($what)) $what = print_r($what,true);
147     fwrite($handle,"$what\n");
148     fclose($handle);
149  }
150}
151