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        global $INFO;
89        if ($this->getConf('apr_mail_receiver') !== '' && $INFO['isadmin']) {
90            $addr = $this->getConf('apr_mail_receiver');
91            if(!mail_isvalid($addr)) {
92                msg(sprintf($this->getLang('mail_invalid'),htmlspecialchars($addr)),-1);
93            }
94
95        }
96    }
97
98    function showInternalNote() {
99        $note = trim($this->getConf('internal note'));
100        if ($note === '') {
101            return;
102        }
103        if (!$this->hlp->isHidden()) {
104            return;
105        }
106
107        printf('<span>%s</span>', hsc($note));
108    }
109
110    function showLatestDraftIfNewer() {
111        global $ID;
112        $revision = $this->hlp->getRevision();
113        $latestRevision = $this->hlp->getLastestRevision();
114
115        if ($revision >= $latestRevision) {
116            return;
117        }
118        if ($this->hlp->isRevisionApproved($latestRevision)) {
119            return;
120        }
121
122        echo '<span class="approval_latest_draft">';
123        printf($this->getLang('apr_recent_draft'), wl($ID, 'force_rev=1'));
124        echo $this->difflink($ID, null, $revision) . '</span>';
125    }
126
127    function showLatestApprovedVersion() {
128        global $ID;
129        $revision = $this->hlp->getRevision();
130        $latestApprovedRevision = $this->hlp->getLatestApprovedRevision();
131
132        if ($latestApprovedRevision <= $revision) {
133            return;
134        }
135
136        $latestRevision = $this->hlp->getLastestRevision();
137        if ($latestApprovedRevision == $latestRevision) {
138            //$latestApprovedRevision = '';
139        }
140        echo '<span class="approval_outdated">';
141        printf($this->getLang('apr_outdated'), wl($ID, 'rev=' . $latestApprovedRevision));
142        echo $this->difflink($ID, $latestApprovedRevision, $revision) . '</span>';
143    }
144
145    function showDraft() {
146        $revision = $this->hlp->getRevision();
147
148        if ($this->hlp->isCurrentRevisionApproved()) {
149            return;
150        }
151
152        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
153        $approvalCount = count($approvals);
154
155        echo '<span class="approval_draft">';
156        printf($this->getLang('apr_draft'), '<span class="approval_date">' . dformat($revision) . '</span>');
157        echo '<br />';
158        printf(' ' . $this->getLang('approvals'), $approvalCount, $this->getConf('number_of_approved'));
159        if ($approvalCount != 0) {
160            printf(' ' . $this->getLang('approved by'), implode(', ', $this->hlp->getApprovers()));
161        }
162        echo '</span>';
163    }
164
165    function showApproved() {
166        if (!$this->hlp->isCurrentRevisionApproved()) {
167            return;
168        }
169
170        echo '<span class="approval_approved">';
171        printf($this->getLang('apr_approved'),
172            '<span class="approval_date">' . dformat($this->hlp->getApprovalDate()) . '</span>',
173            implode(', ', $this->hlp->getApprovers()));
174        echo '</span>';
175    }
176
177    function showPreviousApproved() {
178        global $ID;
179        $previousApproved = $this->hlp->getPreviousApprovedRevision();
180        if (!$previousApproved) {
181            return;
182        }
183        echo '<span class="approval_previous">';
184        printf($this->getLang('apr_previous'),
185            wl($ID, 'rev=' . $previousApproved),
186            dformat($previousApproved));
187        echo $this->difflink($ID, $previousApproved, $this->hlp->getRevision()) . '</span>';
188    }
189
190    private function showApproveAction() {
191        global $ID;
192        global $REV;
193        global $USERINFO;
194        if (!$this->hlp->canApprove()) {
195            return;
196        }
197
198        $approvals = $this->hlp->getApprovalsOnRevision($this->hlp->getRevision());
199        foreach ($approvals as $approve) {
200            if ($approve[1] == $_SERVER['REMOTE_USER']) {
201                return;
202            }
203            if ($approve[1] == $USERINFO['mail']) {
204                return;
205            }
206        }
207
208        echo '<span class="approval_action">';
209        echo '<a href="' . wl($ID, array('rev' => $REV, 'publish_approve'=>1)) . '">';
210        echo $this->getLang('approve action');
211        echo '</a>';
212        echo '</span> ';
213    }
214}
215