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