1<?php
2
3if(!defined('DOKU_INC')) die();
4
5class action_plugin_approve_revisions extends DokuWiki_Action_Plugin {
6
7    function register(Doku_Event_Handler $controller) {
8		$controller->register_hook('HTML_REVISIONSFORM_OUTPUT', 'BEFORE', $this, 'handle_revisions', array());
9	}
10
11	function handle_revisions(Doku_Event $event, $param) {
12		global $INFO;
13
14        try {
15            /** @var \helper_plugin_approve_db $db_helper */
16            $db_helper = plugin_load('helper', 'approve_db');
17            $sqlite = $db_helper->getDB();
18        } catch (Exception $e) {
19            msg($e->getMessage(), -1);
20            return;
21        }
22        /** @var helper_plugin_approve $helper */
23        $helper = plugin_load('helper', 'approve');
24
25        if (!$helper->use_approve_here($sqlite, $INFO['id'])) return;
26
27        $res = $sqlite->query('SELECT rev, approved, ready_for_approval
28                                FROM revision
29                                WHERE page=?', $INFO['id']);
30        $approveRevisions = $sqlite->res2arr($res);
31        $approveRevisions = array_combine(array_column($approveRevisions, 'rev'), $approveRevisions);
32
33		$member = null;
34		foreach ($event->data->_content as $key => $ref) {
35            if(isset($ref['_elem']) && $ref['_elem'] == 'opentag' && $ref['_tag'] == 'div' && $ref['class'] == 'li') {
36                $member = $key;
37            }
38
39            if ($member && $ref['_elem'] == 'tag' &&
40                $ref['_tag'] == 'input' && $ref['name'] == 'rev2[]'){
41                $revision = $ref['value'];
42                if ($revision == 'current') {
43                    $revision = $INFO['meta']['date']['modified'];
44                }
45                if (!isset($approveRevisions[$revision])) {
46                    $class =  'plugin__approve_red';
47                } elseif ($approveRevisions[$revision]['approved']) {
48                    $class =  'plugin__approve_green';
49                } elseif ($this->getConf('ready_for_approval') && $approveRevisions[$revision]['ready_for_approval']) {
50                    $class =  'plugin__approve_ready';
51                } else {
52                    $class =  'plugin__approve_red';
53                }
54
55                $event->data->_content[$member]['class'] = "li $class";
56
57                $member = null;
58            }
59		}
60	}
61
62}
63