xref: /plugin/publish/action/banner.php (revision 17d0ef8f18ced1cfc00a443d7593f5e47897a3f4)
1<?php
2
3if(!defined('DOKU_INC')) die();
4
5class action_plugin_publish_banner extends DokuWiki_Action_Plugin {
6
7    /**
8     * @var helper_plugin_publish
9     */
10    private $hlp;
11
12    function __construct() {
13        $this->hlp = plugin_load('helper','publish');
14    }
15
16    function register(&$controller) {
17        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_display_banner', array());
18    }
19
20    function handle_display_banner(&$event, $param) {
21        global $ID;
22        global $REV;
23        global $INFO;
24
25        if (!$this->hlp->in_namespace($this->getConf('apr_namespaces'), $ID)) {
26            return;
27        }
28
29        if ($event->data != 'show') {
30            return;
31        }
32
33        if (!$INFO['exists']) {
34            return;
35        }
36
37        $meta = $INFO['meta'];
38
39        if (!$meta['approval']) {
40            $meta['approval'] = array();
41        }
42
43
44        $this->showBanner();
45        return;
46    }
47
48    function difflink($id, $rev1, $rev2) {
49        if($rev1 == $rev2) { return ''; }
50        return '<a href="' . wl($id, 'rev2[]=' . $rev1 . '&rev2[]=' . $rev2 . '&do[diff]=1') .
51            '" class="approved_diff_link">' .
52            '<img src="'.DOKU_BASE.'lib/images/diff.png" class="approved_diff_link" alt="Diff" />' .
53            '</a>';
54    }
55
56    function showBanner() {
57        if ($this->hlp->isCurrentRevisionApproved()) {
58            $class = 'approved_yes';
59        } else {
60            $class = 'approved_no';
61        }
62
63        printf('<div class="approval %s">', $class);
64        $this->showLatestDraftIfNewer();
65        $this->showLatestApprovedVersion();
66        $this->showDraft();
67        $this->showApproved();
68        $this->showPreviousApproved();
69
70        $this->showApproveAction();
71
72        echo '</div>';
73    }
74
75    function showLatestDraftIfNewer() {
76        global $ID;
77        $revision = $this->hlp->getRevision();
78        $latestRevision = $this->hlp->getLastestRevision();
79
80        if ($revision >= $latestRevision) {
81            return;
82        }
83        if ($this->hlp->isRevisionApproved($latestRevision)) {
84            return;
85        }
86
87        echo '<span class="approval_latest_draft">';
88        printf($this->getLang('apr_recent_draft'), wl($ID, 'force_rev=1'));
89        echo $this->difflink($ID, null, $revision) . '</span>';
90    }
91
92    function showLatestApprovedVersion() {
93        global $ID;
94        $revision = $this->hlp->getRevision();
95        $latestApprovedRevision = $this->hlp->getLatestApprovedRevision();
96
97        if ($latestApprovedRevision <= $revision) {
98            return;
99        }
100
101        $latestRevision = $this->hlp->getLastestRevision();
102        if ($latestApprovedRevision == $latestRevision) {
103            //$latestApprovedRevision = '';
104        }
105        echo '<span class="approval_outdated">';
106        printf($this->getLang('apr_outdated'), wl($ID, 'rev=' . $latestApprovedRevision));
107        echo $this->difflink($ID, $latestApprovedRevision, $revision) . '</span>';
108    }
109
110    function showDraft() {
111        $revision = $this->hlp->getRevision();
112
113        if ($this->hlp->isCurrentRevisionApproved()) {
114            return;
115        }
116
117        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
118        $approvalCount = count($approvals);
119
120        echo '<span class="approval_draft">';
121        printf($this->getLang('apr_draft'), '<span class="approval_date">' . dformat($revision) . '</span>');
122        echo '<br />';
123        printf(' ' . $this->getLang('approvals'), $approvalCount, $this->getConf('number_of_approved'));
124        if ($approvalCount != 0) {
125            printf(' ' . $this->getLang('approved by'), implode(', ', $this->hlp->getApprovers()));
126        }
127        echo '</span>';
128    }
129
130    function showApproved() {
131        if (!$this->hlp->isCurrentRevisionApproved()) {
132            return;
133        }
134
135        echo '<span class="approval_approved">';
136        printf($this->getLang('apr_approved'),
137            '<span class="approval_date">' . dformat($this->hlp->getApprovalDate()) . '</span>',
138            implode(', ', $this->hlp->getApprovers()));
139        echo '</span>';
140    }
141
142    function showPreviousApproved() {
143        global $ID;
144        $previousApproved = $this->hlp->getPreviousApprovedRevision();
145        if (!$previousApproved) {
146            return;
147        }
148        echo '<span class="approval_previous">';
149        printf($this->getLang('apr_previous'),
150            wl($ID, 'rev=' . $previousApproved),
151            dformat($previousApproved));
152        echo $this->difflink($ID, $previousApproved, $this->hlp->getRevision()) . '</span>';
153    }
154
155    private function showApproveAction() {
156        global $ID;
157        global $REV;
158        global $USERINFO;
159        if (!$this->hlp->canApprove()) {
160            return;
161        }
162
163        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
164        foreach ($approvals as $approve) {
165            if ($approve[1] == $_SERVER['REMOTE_USER']) {
166                return;
167            }
168            if ($approve[1] == $USERINFO['mail']) {
169                return;
170            }
171        }
172
173        echo '<span class="approval_action">';
174        echo '<a href="' . wl($ID, array('rev' => $REV, 'publish_approve'=>1)) . '">';
175        echo $this->getLang('approve action');
176        echo '</a>';
177        echo '</span> ';
178    }
179}
180