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