1<?php
2
3if(!defined('DOKU_INC')) die();
4
5class action_plugin_publish_approve extends DokuWiki_Action_Plugin {
6
7    /**
8     * @var helper_plugin_publish
9     */
10    private $helper;
11
12    function __construct() {
13        $this->helper = plugin_load('helper', 'publish');
14    }
15
16    function register(Doku_Event_Handler $controller) {
17        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_io_write', array());
18        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'approveNS', array());
19    }
20
21    function approveNS(Doku_Event &$event, $param) {
22        if ($event->data !== 'plugin_publish_approveNS') {
23            return;
24        }
25        //no other ajax call handlers needed
26        $event->stopPropagation();
27        $event->preventDefault();
28
29        //e.g. access additional request variables
30        global $INPUT; //available since release 2012-10-13 "Adora Belle"
31        $namespace = $INPUT->str('namespace');
32        $pages = $this->helper->getPagesFromNamespace($namespace);
33        $pages = $this->helper->removeSubnamespacePages($pages, $namespace);
34
35        global $ID, $INFO;
36        $original_id = $ID;
37        foreach ($pages as $page) {
38            $ID = $page[0];
39            $INFO = pageinfo();
40            if (!$this->helper->canApprove()) {
41                continue;
42            }
43            $this->addApproval();
44        }
45        $ID = $original_id;
46    }
47
48    function handle_io_write(Doku_Event &$event, $param) {
49        # This is the only hook I could find which runs on save,
50        # but late enough to have lastmod set (ACTION_ACT_PREPROCESS
51        # is too early)
52        global $ACT;
53        global $INPUT;
54        global $ID;
55
56        if ($ACT != 'show') {
57            return;
58        }
59
60        if (!$INPUT->has('publish_approve')) {
61            return;
62        }
63
64        if (!$this->helper->canApprove()) {
65            msg($this->getLang('wrong permissions to approve'), -1);
66            return;
67        }
68
69        $this->addApproval();
70        send_redirect(wl($ID, array('rev' => $this->helper->getRevision()), true, '&'));
71    }
72
73    function addApproval() {
74        global $USERINFO;
75        global $ID;
76        global $INFO;
77
78        if (!$INFO['exists']) {
79            msg($this->getLang('cannot approve a non-existing revision'), -1);
80            return;
81        }
82
83        $approvalRevision = $this->helper->getRevision();
84        $approvals = $this->helper->getApprovals();
85
86        if (!isset($approvals[$approvalRevision])) {
87            $approvals[$approvalRevision] = array();
88        }
89
90        $approvals[$approvalRevision][$INFO['client']] = array(
91            $INFO['client'],
92            $_SERVER['REMOTE_USER'],
93            $USERINFO['mail'],
94            time()
95        );
96
97        $success = p_set_metadata($ID, array('approval' => $approvals), true, true);
98        if ($success) {
99            msg($this->getLang('version approved'), 1);
100
101            $data = array();
102            $data['rev'] = $approvalRevision;
103            $data['id'] = $ID;
104            $data['approver'] = $_SERVER['REMOTE_USER'];
105            $data['approver_info'] = $USERINFO;
106            if ($this->getConf('send_mail_on_approve') && $this->helper->isRevisionApproved($approvalRevision)) {
107                /** @var action_plugin_publish_mail $mail */
108                $mail = plugin_load('action','publish_mail');
109                $mail->send_approve_mail();
110            }
111            trigger_event('PLUGIN_PUBLISH_APPROVE', $data);
112        } else {
113            msg($this->getLang('cannot approve error'), -1);
114        }
115
116    }
117
118}
119