xref: /plugin/publish/action/banner.php (revision 6c4c5b73646e2931f824edbca62bc53eef06c054)
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(Doku_Event_Handler $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 $INFO;
22
23        if (!$this->hlp->isActive()) {
24            return;
25        }
26
27        if ($event->data != 'show') {
28            return;
29        }
30
31        if (!$INFO['exists']) {
32            return;
33        }
34
35        $meta = $INFO['meta'];
36
37        if (!$meta['approval']) {
38            $meta['approval'] = array();
39        }
40
41        if($INFO['perm'] <= AUTH_READ && $this->getConf('hidereaderbanner')){
42            return;
43        }
44
45        if ($this->hlp->isCurrentRevisionApproved() && $this->getConf('hide approved banner')) {
46            return;
47        }
48
49        $this->showBanner();
50        return;
51    }
52
53    function difflink($id, $rev1, $rev2) {
54        if($rev1 == $rev2) {
55            return '';
56        }
57
58        $difflink = $this->hlp->getDifflink($id,$rev1,$rev2);
59
60        return '<a href="' . $difflink .
61            '" class="approved_diff_link">' .
62            '<img src="'.DOKU_BASE.'lib/images/diff.png" class="approved_diff_link" alt="Diff" />' .
63            '</a>';
64    }
65
66    function showBanner() {
67        if ($this->hlp->isCurrentRevisionApproved()) {
68            $class = 'approved_yes';
69        } else {
70            if ($this->hlp->isHiddenForUser()) {
71                return;
72            }
73            $class = 'approved_no';
74        }
75
76        printf('<div class="approval %s">', $class);
77        $this->showLatestDraftIfNewer();
78        $this->showLatestApprovedVersion();
79        $this->showDraft();
80        $this->showApproved();
81        $this->showPreviousApproved();
82
83        $this->showApproveAction();
84        $this->showInternalNote();
85
86        echo '</div>';
87    }
88
89    function showInternalNote() {
90        $note = trim($this->getConf('internal note'));
91        if ($note === '') {
92            return;
93        }
94        if (!$this->hlp->isHidden()) {
95            return;
96        }
97
98        printf('<span>%s</span>', hsc($note));
99    }
100
101    function showLatestDraftIfNewer() {
102        global $ID;
103        $revision = $this->hlp->getRevision();
104        $latestRevision = $this->hlp->getLastestRevision();
105
106        if ($revision >= $latestRevision) {
107            return;
108        }
109        if ($this->hlp->isRevisionApproved($latestRevision)) {
110            return;
111        }
112
113        echo '<span class="approval_latest_draft">';
114        printf($this->getLang('apr_recent_draft'), wl($ID, 'force_rev=1'));
115        echo $this->difflink($ID, null, $revision) . '</span>';
116    }
117
118    function showLatestApprovedVersion() {
119        global $ID;
120        $revision = $this->hlp->getRevision();
121        $latestApprovedRevision = $this->hlp->getLatestApprovedRevision();
122
123        if ($latestApprovedRevision <= $revision) {
124            return;
125        }
126
127        $latestRevision = $this->hlp->getLastestRevision();
128        if ($latestApprovedRevision == $latestRevision) {
129            //$latestApprovedRevision = '';
130        }
131        echo '<span class="approval_outdated">';
132        printf($this->getLang('apr_outdated'), wl($ID, 'rev=' . $latestApprovedRevision));
133        echo $this->difflink($ID, $latestApprovedRevision, $revision) . '</span>';
134    }
135
136    function showDraft() {
137        $revision = $this->hlp->getRevision();
138
139        if ($this->hlp->isCurrentRevisionApproved()) {
140            return;
141        }
142
143        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
144        $approvalCount = count($approvals);
145
146        echo '<span class="approval_draft">';
147        printf($this->getLang('apr_draft'), '<span class="approval_date">' . dformat($revision) . '</span>');
148        echo '<br />';
149        printf(' ' . $this->getLang('approvals'), $approvalCount, $this->getConf('number_of_approved'));
150        if ($approvalCount != 0) {
151            printf(' ' . $this->getLang('approved by'), implode(', ', $this->hlp->getApprovers()));
152        }
153        echo '</span>';
154    }
155
156    function showApproved() {
157        if (!$this->hlp->isCurrentRevisionApproved()) {
158            return;
159        }
160
161        echo '<span class="approval_approved">';
162        printf($this->getLang('apr_approved'),
163            '<span class="approval_date">' . dformat($this->hlp->getApprovalDate()) . '</span>',
164            implode(', ', $this->hlp->getApprovers()));
165        echo '</span>';
166    }
167
168    function showPreviousApproved() {
169        global $ID;
170        $previousApproved = $this->hlp->getPreviousApprovedRevision();
171        if (!$previousApproved) {
172            return;
173        }
174        echo '<span class="approval_previous">';
175        printf($this->getLang('apr_previous'),
176            wl($ID, 'rev=' . $previousApproved),
177            dformat($previousApproved));
178        echo $this->difflink($ID, $previousApproved, $this->hlp->getRevision()) . '</span>';
179    }
180
181    private function showApproveAction() {
182        global $ID;
183        global $REV;
184        global $USERINFO;
185        if (!$this->hlp->canApprove()) {
186            return;
187        }
188
189        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
190        foreach ($approvals as $approve) {
191            if ($approve[1] == $_SERVER['REMOTE_USER']) {
192                return;
193            }
194            if ($approve[1] == $USERINFO['mail']) {
195                return;
196            }
197        }
198
199        echo '<span class="approval_action">';
200        echo '<a href="' . wl($ID, array('rev' => $REV, 'publish_approve'=>1)) . '">';
201        echo $this->getLang('approve action');
202        echo '</a>';
203        echo '</span> ';
204    }
205}
206