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