forked from gkrid/dokuwiki-plugin-dtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
299 lines (236 loc) · 8.28 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Plugin Now: Inserts a timestamp.
*
* @license GPL 3 (http://www.gnu.org/licenses/gpl.html)
* @author Szymon Olewniczak <szymon.olewniczak@rid.pl>
*/
// must be run within DokuWiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'syntax.php';
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class action_plugin_dtable extends DokuWiki_Action_Plugin {
function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'add_php_data');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
$controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'AFTER', $this, 'parser_preprocess_handler');
$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
}
public function insert_button (Doku_Event $event, $param) {
$event->data[] = array (
'type' => 'format',
'title' => $this->getLang('toolbar_insert_button'),
'icon' => '../../plugins/dtable/images/add_table.png',
'open' => '<dtable>',
'close' => '</dtable>',
'sample' => "\n^ ^ ^\n| | |\n| | |\n| | |\n"
);
}
function parser_preprocess_handler(&$event, $parm)
{
global $ID, $INFO;
$lines = explode("\n", $event->data);
$new_lines = array();
//determine dtable page
//only 100 dtables per page
$i = 0;
$dtable_pages = array();
foreach($lines as $line)
{
if(strpos($line, '<dtable>') === 0) {
$new_lines[] = '<dtab'.( $i < 10 ? '0'.$i : $i ).'>';
$dtable_pages[$i] = $ID;
$i++;
} else
{
$new_lines[] = $line;
}
}
//it will make include plugin behaves correctly
p_set_metadata($INFO['id'], array('dtable_pages' => $dtable_pages), false, false);
//mark dtables
//it will not work becouse section editing in dokuwiki needs no modified content.
if($this->getConf('all_tables'))
{
$new_lines = array();
$in_tab = 0;
$in_dtable_tag = 0;
foreach($lines as $line)
{
if(strpos($line, '<dtable>') === 0)
$in_dtable_tag = 1;
if(strpos($line, '</dtable>') === 0)
$in_dtable_tag = 0;
if(strpos($line, '|') !== 0 && $in_tab == 1 && $in_dtable_tag == 0)
{
$new_lines[] = '</dtable>';
$in_tab = 0;
}
if(strpos($line, '^') === 0 && $in_tab == 0 && $in_dtable_tag == 0)
{
$new_lines[] = '<dtable>';
$in_tab = 1;
}
$new_lines[] = $line;
}
$lines = $new_lines;
}
$event->data = implode("\n", $new_lines);
}
function add_php_data(&$event, $param) {
global $JSINFO, $ID;
if (auth_quickaclcheck($ID) >= AUTH_EDIT)
$JSINFO['write'] = true;
else
$JSINFO['write'] = false;
$JSINFO['disabled'] = explode(',', $this->getConf('disabled'));
$JSINFO['lang']['insert_before'] = $this->getLang('insert_before');
$JSINFO['lang']['insert_after'] = $this->getLang('insert_after');
$JSINFO['lang']['edit'] = $this->getLang('edit');
$JSINFO['lang']['remove'] = $this->getLang('remove');
$JSINFO['lang']['insert_col_left'] = $this->getLang('insert_col_left');
$JSINFO['lang']['insert_col_right'] = $this->getLang('insert_col_right');
$JSINFO['lang']['mark_row_as_header'] = $this->getLang('mark_row_as_header');
$JSINFO['lang']['mark_col_as_header'] = $this->getLang('mark_col_as_header');
$JSINFO['lang']['mark_cell_as_header'] = $this->getLang('mark_cell_as_header');
$JSINFO['lang']['mark_row_as_cell'] = $this->getLang('mark_row_as_cell');
$JSINFO['lang']['mark_col_as_cell'] = $this->getLang('mark_col_as_cell');
$JSINFO['lang']['mark_cell_as_cell'] = $this->getLang('mark_cell_as_cell');
$JSINFO['lang']['show_merged_rows'] = $this->getLang('show_merged_rows');
$JSINFO['lang']['lock_notify'] = str_replace(
array('%u', '%t'),
array('<span class="who"></span>', '<span class="time_left"></span>'),
$this->getLang('lock_notify'));
$JSINFO['lang']['unlock_notify'] = $this->getLang('unlock_notify');
}
function handle_ajax(&$event, $param)
{
global $conf;
switch($event->data)
{
case 'dtable':
$event->preventDefault();
$event->stopPropagation();
$json = new JSON();
list($dtable_start_line, $dtable_page_id) = explode('_', $_POST['table'], 2);
$file = wikiFN( $dtable_page_id );
if( ! @file_exists( $file ) )
{
echo $json->encode( array('type' => 'error', 'msg' => 'This page does not exist.') );
exit(0);
}
$dtable =& plugin_load('helper', 'dtable');
$page_lines = explode( "\n", io_readFile( $file ) );
if(isset($_POST['remove']))
{
$scope = $json->decode($_POST['remove']);
$lines_to_remove = array();
for ($i = $scope[0]; $i <= $scope[1]; $i++)
$lines_to_remove[] = $i;
$removed_line = '';
foreach ($lines_to_remove as $line) {
$removed_line .= $page_lines[ $line ]." ";
}
array_splice($page_lines, $scope[0], $scope[1] - $scope[0] + 1);
$new_cont = implode( "\n", $page_lines );
saveWikiText($dtable_page_id, $new_cont, $this->getLang('summary_remove').' '.$removed_line);
echo $json->encode( array('type' => 'success', 'spans' =>
$dtable->get_spans($dtable_start_line, $page_lines, $dtable_page_id) ) );
} else
{
$cols = array();
$new_table_line = array();
foreach( $_POST as $k => $v )
{
if( strpos( $k, 'col' ) === 0)
{
//remove col from col12, col1 etc. to be 12 1
$cols[(int)substr($k, 3)] = $json->decode($v);
}
}
ksort($cols);
//reset index
$cols = array_values($cols);
$j = 0;
for($i = 0; $i < count($cols); $i++) {
$class = $cols[$i][0];
$value = $cols[$i][1];
if ($value == '' && $j >= 1)
$new_table_line[$j-1][1]++;
else {
$type = $class == 'tablecell_open' ? '|' : '^';
$new_table_line[$j] = array(1, 1, $type, $value);
$j++;
}
}
$new_line = $dtable->format_row($cols);
if( isset( $_POST['add'] ) )
{
$action = 'add';
/*$table_line = (int) $_POST['add'] + 1;
$line_to_add = $dtable_start_line + $table_line;*/
$line_to_add = (int) $_POST['add'] + 1;
array_splice($page_lines, $line_to_add, 0, $new_line );
$line_nr = $line_to_add;
$info = $this->getLang('summary_add').' '.$new_line;
} elseif( isset( $_POST['edit'] ) )
{
$action = 'edit';
$scope = $json->decode($_POST['edit']);
$lines_to_change = array();
for ($i = $scope[0]; $i <= $scope[1]; $i++)
$lines_to_change[] = $i;
$old_line= '';
foreach ($lines_to_change as $line) {
$old_line .= $page_lines[ $line ]." ";
}
//$old_line = $page_lines[ $line_to_change ];
array_splice($page_lines, $scope[0], $scope[1] - $scope[0] + 1, $new_line);
$line_nr = $scope[0];
$new_cont = implode( "\n", $page_lines );
$info = str_replace( array('%o', '%n'), array($old_line, $new_line), $this->getLang('summary_edit') );
}
$new_cont = implode( "\n", $page_lines );
saveWikiText($dtable_page_id, $new_cont, $info);
echo $json->encode( array('type' => 'success', 'action' => $action, 'new_row' => $dtable->parse_line($new_line, $dtable_page_id), 'raw_row' => array($new_table_line, array($line_nr, $line_nr)), 'spans' => $dtable->get_spans($dtable_start_line, $page_lines, $dtable_page_id) ) );
}
break;
case 'dtable_page_lock':
$event->preventDefault();
$event->stopPropagation();
$ID = $_POST['page'];
lock($ID);
break;
case 'dtable_page_unlock':
$event->preventDefault();
$event->stopPropagation();
$ID = $_POST['page'];
unlock($ID);
break;
case 'dtable_is_page_locked':
$event->preventDefault();
$event->stopPropagation();
$ID = $_POST['page'];
$checklock = checklock($ID);
//check when lock expire
$lock_file = wikiLockFN($ID);
if(file_exists($lock_file))
{
$locktime = filemtime(wikiLockFN($ID));
//dokuwiki uses dformat here but we will use raw unix timesamp
$expire = $locktime + $conf['locktime'] - time();
} else
$expire = $conf['locktime'];
$json = new JSON();
if($checklock === false)
echo $json->encode(array('locked' => 0, 'time_left' => $expire));
else
echo $json->encode(array('locked' => 1, 'who' => $checklock, 'time_left' => $expire));
break;
}
}
}