xref: /plugin/publish/action/approve.php (revision 18515038deca146ef80752542486449b47995cd9)
1<?php
2
3if(!defined('DOKU_INC')) die();
4
5class action_plugin_publish_approve extends DokuWiki_Action_Plugin {
6
7    private $helper;
8
9    function __construct() {
10        $this->helper = plugin_load('helper', 'publish');
11    }
12
13    function register(&$controller) {
14        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_io_write', array());
15    }
16
17    function handle_io_write(&$event, $param) {
18        # This is the only hook I could find which runs on save,
19        # but late enough to have lastmod set (ACTION_ACT_PREPROCESS
20        # is too early)
21        global $ACT;
22
23        if ($ACT != 'show') {
24            return;
25        }
26
27        if (!isset($_REQUEST['publish_approve'])) {
28            return;
29        }
30
31        if (!$this->helper->canApprove()) {
32            msg($this->getLang('wrong permissions to approve'), -1);
33            return;
34        }
35
36        $this->addApproval();
37        return;
38    }
39
40    function addApproval() {
41        global $USERINFO;
42        global $ID;
43        global $INFO;
44        global $REV;
45
46        if (!$INFO['exists']) {
47            msg($this->getLang('cannot approve a non-existing revision'), -1);
48            return;
49        }
50
51        $approvalRevision = $this->helper->getRevision();
52        $approvals = $this->helper->getApprovals();
53
54        if (!isset($approvals[$approvalRevision])) {
55            $approvals[$approvalRevision] = array();
56        }
57
58        $approvals[$approvalRevision][$INFO['client']] = array(
59            $INFO['client'],
60            $_SERVER['REMOTE_USER'],
61            $USERINFO['mail'],
62            time()
63        );
64
65        $success = p_set_metadata($ID, array('approval' => $approvals), true, true);
66        if ($success) {
67            msg($this->getLang('version approved'), 1);
68        } else {
69            msg($this->getLang('cannot approve error'), -1);
70        }
71
72        send_redirect(wl($ID, array('rev' => $this->helper->getRevision()), true, '&'));
73    }
74
75}
76