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