9e6ea335ba2dab8d258306cbeaa0d76d5058cbb2
[atutor.git] / mods / wiki / plugins / feature / appendcomments.php
1 <?php
2
3 /*
4    If you activate this plugin, then additions for pages with the
5    _DB_F_APPENDONLY flag will get saved into a secondary page with a
6    name like "CurrentPage/Comments" if an ordinary user attempts to
7    edit the "CurrentPage" (which remains editable for moderators and
8    admins).
9    This plugin has the drawback, that moderators cannot edit the
10    /Comments page part for brevity - they first needed to log out
11    (a drawback of the _DB_F_PART type setting for the /Comments part).
12
13    Warning: this is hack in progress!
14 */
15
16
17 $ewiki_plugins["edit_hook"][] = "ewiki_edit_hook_appendcomments";
18 $ewiki_plugins["edit_save"][] = "ewiki_edit_save_appendcomments";
19 $ewiki_plugins["handler"][] = "ewiki_aview_appendcomments";
20
21 define("EWIKI_APPENDONLY_COMMENTSPART", "/Comments");
22
23
24
25 /*
26    Add the /Comments entries contents to the current pages
27    $data["content"] for rendering.
28 */
29 function ewiki_aview_appendcomments($id, &$data, $action) {
30
31    if (($action=="view") && ($data["flags"] & EWIKI_DB_F_APPENDONLY)) {
32
33       #-- fetch /Comments part
34       $c_id = $id.EWIKI_APPENDONLY_COMMENTSPART;
35       $row = ewiki_db::GET($c_id);
36
37       #-- add to current pages content
38       if ($row && (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_PART|EWIKI_DB_F_TYPE)) {
39          $data["content"] .= "\n" . $row["content"];
40       }
41    }
42 }
43
44
45 /*
46    swaps currently edit/ed pages content with that from the /Coments
47    page part
48 */
49 function ewiki_edit_hook_appendcomments(&$id, &$data, &$hpdata) {
50
51    global $ewiki_t;
52
53    if ($data["flags"] & EWIKI_DB_F_APPENDONLY) {
54       if (!EWIKI_PROTECTED_MODE || ($ewiki_ring >= 2)) {
55
56          #-- fill edit box with contents from "$id/Content" page
57          $c_id = $id.EWIKI_APPENDONLY_COMMENTSPART;
58          $data = ewiki_db::GET($c_id);
59          if (!$data["version"]) {
60             $data = array(
61                "id" => $c_id,
62                "version" => NULL,
63                "flags" => EWIKI_DB_F_PART|EWIKI_DB_F_TEXT,
64                "created" => time(),
65                "lastmodified" => time(),
66                "hits" => 0,
67                "meta" => array("parent" => $id),
68                "content" => "",
69                "refs" => "",
70             );
71          }
72          $data["flags"] = EWIKI_DB_F_TEXT;   # another ugly workaround
73
74          #-- change "edit" title to "append"
75          foreach (array_keys($ewiki_t) as $LANG) {
76             if ($ewiki_t[$LANG]["APPENDTOPAGE"]) {
77                $ewiki_t[$LANG]["EDITTHISPAGE"] = &$ewiki_t[$LANG]["APPENDTOPAGE"];
78             }
79          }
80
81       }
82    }
83 }
84
85
86 /*
87    manages ewiki_edit_page() to store the currently edited /Comments
88    part back into the correct database entry (and not the main page)
89 */
90 function ewiki_edit_save_appendcomments(&$save, &$old) {
91
92    if ($old["meta"]["parent"] == $save["id"]) {
93       if (!EWIKI_PROTECTED_MODE || ($ewiki_ring >= 2)) {
94
95          #-- transform $save entry into "/Comments" page request
96          $save["id"] = $old["id"];
97          $save["flags"] = EWIKI_DB_F_TEXT|EWIKI_DB_F_PART;
98          $save["created"] = $old["created"];
99          $save["hits"] = $old["hits"];
100          $save["meta"] = $old["meta"];
101          # leaving as is: content, refs, author, lastmodified
102
103       }
104    }
105 }
106
107
108
109 ?>