changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / mpi / mpi_tableeditor.php
1 <?php
2
3 /*
4    <?plugin TableEditor
5     |   aaaa   |   bbbb   |   cccc   |   eeee   |
6     |   1...   |   2...   |   3...   |   4...   |
7     |   ...    |   ...    |   ...    |   ...    |
8    ?>
9
10    Will render the table as usual, but provide a [TableEditor] button,
11    which rerenders the table cells with lots of <textarea>s and another
12    [Save] button for easier editing.
13
14    While this feature was meant for large tables, it may not work out
15    that well for some browsers - because they often aren't prepared for
16    more than a few <textarea> and other <form> <input> fields.
17 */
18
19
20 $ewiki_plugins["mpi"]["tableeditor"] = "ewiki_mpi_tableeditor";
21 function ewiki_mpi_tableeditor($action, $args, &$iii, &$s) {
22
23    global $ewiki_id, $ewiki_data;
24
25    #-- config
26    $rel = 5/2;  // favoured ratio width to height
27    $SEP = "|";  // table cell separator ("|", or "||" for other Wikis);
28    $add_empty_row = 1;
29    $w_min = 7;
30    $w_stretch = 1.17;   // + 17%
31    $w_max = 35;
32    $h_min = 2;
33    $h_max = 12;
34
35    #-- analyze current table for cell sizes
36    $t = array();
37    foreach (explode("\n", trim($args["_"])) as $row) {
38       $t[] = explode($SEP, trim(trim($row), $SEP));
39    }
40    $t_widths = array();
41    $t_heights = array();
42    $x = count($t[0]);
43    $y = count($t);
44    for ($row=0; $row<$y; $row++) {
45       for ($col=0; $col<$x; $col++) {
46          $len = strlen($t[$row][$col]);
47
48          $w = sqrt($rel*$len);
49          if ($w < $w_min) { 
50             $w = $w_min;
51          }
52          $h = max((int) ($len/$w), $h_min);
53          $w = (int) ($w * $w_stretch);
54          $h = min($h, $h_max);
55          $w = min($w, $w_max);
56
57          $t_widths[$col] = max($t_widths[$col], $w);
58          $t_heights[$row] = max($t_heights[$row], $h);
59       }
60    }
61
62
63    #-- store -----------------------------------------------------------
64    $o = '<div class="mpi TableEditor">';
65    if ($_REQUEST["te_save"]) {
66
67       $data = ewiki_db::GET($ewiki_id);
68       if ($data && ($_REQUEST["te_d_ver"] == $data["version"])) {
69
70          if (!preg_match_all('/<\?plugin:?\s*TableEditor/i', $data["content"], $uu)) {
71             $o .= "Could not detect the exact position of the TableEditor inside the page. Not saved.<br />";
72          }
73          elseif (count($uu[0]) >= 2) {
74             $o .= "There can only be <b>one</b> TableEditor call in a page!<br />";
75          }
76          else {
77             $src = "";
78             $t = $_REQUEST["te_d"];
79             foreach ($t as $y=>$row) {
80                $empty = 1;
81                foreach ($row as $x=>$cell) {
82                   $t[$y][$x] = trim(strtr($cell, "\r\n\t\f", "    "));
83                   $empty = $empty && empty($t[$y][$x]);
84                }
85                if ($empty) {
86                   unset($t[$y]);
87                   continue;
88                }
89                $src .= "$SEP " . implode(" $SEP ", $t[$y]) . " $SEP\n";
90             }
91
92             $data["content"] = preg_replace(
93                '/<\?plugin:?\s*TableEditor.+?\?>/is',
94                "<?plugin TableEditor\n\n$src\n?>",
95                $data["content"]
96             );
97             ewiki_data_update($data);
98             $data["version"]++;
99
100             ewiki_db::WRITE($data);
101          }
102
103       }
104       else {
105          $o .= ewiki_t("ERRVERSIONSAVE") . "<br />\n";
106       }
107    }
108
109
110    #-- output start ----------------------------------------------------
111    $o .= '<form action="'.$_SERVER["REQUEST_URI"].'" method="POST" enctype="multipart/form-data">'
112        . '<input type="hidden" name="te_d_ver" value="'.($ewiki_data["version"]).'">'
113        . '<input type="hidden" name="id" value="'.htmlentities($ewiki_id).'">';
114
115    #-- print <textarea> table variant
116    if ($_REQUEST["te_load"]) {
117       $o .= '<input type="submit" name="te_save" value="SaveTable"><br />';
118       $o .= '<table border="1" cellspacing="1" cellpadding="2">';
119       if ($add_empty_row) {
120          $y++;
121       }
122       for ($row=0; $row<$y; $row++) {
123          for ($col=0; $col<$x; $col++) {
124             $t[$row][$col] = "<textarea style=\"border:none;background:transparent;\" name=\"te_d[$row][$col]\" cols=\"$t_widths[$col]\" rows=\"$t_heights[$row]\" wrap=\"soft\">"
125                            . htmlentities(trim($t[$row][$col]))
126                            . "</textarea>";
127          }
128          $o .= '<tr><td>' . implode('</td><td>', $t[$row]) . '</td></tr>' . "\n";
129       }
130       $o .= "</table>\n";
131    }
132
133    #-- reconvert into WikiMarkup, and insert into $iii
134    else {
135       #-- insert <html> form at current position
136       $o .= '<input type="submit" name="te_load" value="TableEditor"><br />';
137       $in = $s["in"];
138       $iii[$in][0] = "WILL BE REPLACED with \$o...";
139
140       #-- mk table markup, insert into $iii
141       $src = "\n\n";
142       foreach ($t as $row) {
143          $src .= "$SEP " . implode(" $SEP ", $row) . " $SEP\n";
144       }
145       $src .= "\n";
146       $iii = array_merge(
147          array_slice($iii, 0, $in+1),
148          array(array($src, 0x137F, "core")),
149          array_slice($iii, $in+1)
150       );
151
152       // the following return($o); will insert the <form> into
153       // the current input buffer $iii[$in][0] later
154    }
155
156    $o .= "</form></div>\n";
157    return($o);
158 }
159
160 ?>