1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\Form\TagOpenElement;
7use dokuwiki\Form\CheckableElement;
8
9class action_plugin_approve_revisions extends ActionPlugin {
10
11    function register(EventHandler $controller) {
12		$controller->register_hook('FORM_REVISIONS_OUTPUT', 'BEFORE', $this, 'handle_revisions', array());
13	}
14
15	function handle_revisions(Event $event, $param) {
16		global $INFO;
17
18        /** @var helper_plugin_approve_db $db */
19        $db = $this->loadHelper('approve_db');
20        /** @var helper_plugin_approve_acl $acl */
21        $acl = $this->loadHelper('approve_acl');
22
23        if (!$acl->useApproveHere($INFO['id'])) return;
24
25        $approve_revisions = $db->getPageRevisions($INFO['id']);
26        $last_approved_rev = $db->getLastDbRev($INFO['id'], 'approved');
27
28        $approve_revisions = array_combine(array_column($approve_revisions, 'rev'), $approve_revisions);
29
30		$parent_div_position = -1;
31		for ($i = 0; $i < $event->data->elementCount(); $i++) {
32            $element = $event->data->getElementAt($i);
33            if ($element instanceof TagOpenElement && $element->val() == 'div'
34                && $element->attr('class') == 'li') {
35                $parent_div_position = $i;
36            } elseif ($parent_div_position > 0 && $element instanceof CheckableElement &&
37                $element->attr('name') == 'rev2[]') {
38                $revision = $element->attr('value');
39                if ($revision == 'current') {
40                    $revision = $INFO['meta']['date']['modified'];
41                }
42                if (!isset($approve_revisions[$revision])) {
43                    $class =  'plugin__approve_draft';
44                } elseif ($approve_revisions[$revision]['status'] == 'approved' && $revision == $last_approved_rev) {
45                    $class =  'plugin__approve_approved';
46                } elseif ($approve_revisions[$revision]['status'] == 'approved') {
47                    $class =  'plugin__approve_old_approved';
48                } elseif ($this->getConf('ready_for_approval') && $approve_revisions[$revision]['status'] == 'ready_for_approval') {
49                    $class =  'plugin__approve_ready';
50                } else {
51                    $class =  'plugin__approve_draft';
52                }
53
54                $parent_div = $event->data->getElementAt($parent_div_position);
55                $parent_div->addClass($class);
56                $parent_div_position = -1;
57            }
58		}
59	}
60
61}
62