xref: /plugin/publish/action/banner.php (revision d0a0ba4345a10b5cdb08a4a2686299303f9af076)
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            if ($this->getConf('hide drafts')) {
61                if (auth_quickaclcheck($ID) < AUTH_EDIT) {
62                    return;
63                }
64            }
65            $class = 'approved_no';
66        }
67
68        printf('<div class="approval %s">', $class);
69        $this->showLatestDraftIfNewer();
70        $this->showLatestApprovedVersion();
71        $this->showDraft();
72        $this->showApproved();
73        $this->showPreviousApproved();
74
75        $this->showApproveAction();
76
77        echo '</div>';
78    }
79
80    function showLatestDraftIfNewer() {
81        global $ID;
82        $revision = $this->hlp->getRevision();
83        $latestRevision = $this->hlp->getLastestRevision();
84
85        if ($revision >= $latestRevision) {
86            return;
87        }
88        if ($this->hlp->isRevisionApproved($latestRevision)) {
89            return;
90        }
91
92        echo '<span class="approval_latest_draft">';
93        printf($this->getLang('apr_recent_draft'), wl($ID, 'force_rev=1'));
94        echo $this->difflink($ID, null, $revision) . '</span>';
95    }
96
97    function showLatestApprovedVersion() {
98        global $ID;
99        $revision = $this->hlp->getRevision();
100        $latestApprovedRevision = $this->hlp->getLatestApprovedRevision();
101
102        if ($latestApprovedRevision <= $revision) {
103            return;
104        }
105
106        $latestRevision = $this->hlp->getLastestRevision();
107        if ($latestApprovedRevision == $latestRevision) {
108            //$latestApprovedRevision = '';
109        }
110        echo '<span class="approval_outdated">';
111        printf($this->getLang('apr_outdated'), wl($ID, 'rev=' . $latestApprovedRevision));
112        echo $this->difflink($ID, $latestApprovedRevision, $revision) . '</span>';
113    }
114
115    function showDraft() {
116        $revision = $this->hlp->getRevision();
117
118        if ($this->hlp->isCurrentRevisionApproved()) {
119            return;
120        }
121
122        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
123        $approvalCount = count($approvals);
124
125        echo '<span class="approval_draft">';
126        printf($this->getLang('apr_draft'), '<span class="approval_date">' . dformat($revision) . '</span>');
127        echo '<br />';
128        printf(' ' . $this->getLang('approvals'), $approvalCount, $this->getConf('number_of_approved'));
129        if ($approvalCount != 0) {
130            printf(' ' . $this->getLang('approved by'), implode(', ', $this->hlp->getApprovers()));
131        }
132        echo '</span>';
133    }
134
135    function showApproved() {
136        if (!$this->hlp->isCurrentRevisionApproved()) {
137            return;
138        }
139
140        echo '<span class="approval_approved">';
141        printf($this->getLang('apr_approved'),
142            '<span class="approval_date">' . dformat($this->hlp->getApprovalDate()) . '</span>',
143            implode(', ', $this->hlp->getApprovers()));
144        echo '</span>';
145    }
146
147    function showPreviousApproved() {
148        global $ID;
149        $previousApproved = $this->hlp->getPreviousApprovedRevision();
150        if (!$previousApproved) {
151            return;
152        }
153        echo '<span class="approval_previous">';
154        printf($this->getLang('apr_previous'),
155            wl($ID, 'rev=' . $previousApproved),
156            dformat($previousApproved));
157        echo $this->difflink($ID, $previousApproved, $this->hlp->getRevision()) . '</span>';
158    }
159
160    private function showApproveAction() {
161        global $ID;
162        global $REV;
163        global $USERINFO;
164        if (!$this->hlp->canApprove()) {
165            return;
166        }
167
168        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
169        foreach ($approvals as $approve) {
170            if ($approve[1] == $_SERVER['REMOTE_USER']) {
171                return;
172            }
173            if ($approve[1] == $USERINFO['mail']) {
174                return;
175            }
176        }
177
178        echo '<span class="approval_action">';
179        echo '<a href="' . wl($ID, array('rev' => $REV, 'publish_approve'=>1)) . '">';
180        echo $this->getLang('approve action');
181        echo '</a>';
182        echo '</span> ';
183    }
184}
185