xref: /plugin/structpublish/action/publish.php (revision bcee0e7291f5711195e679503d4d3a3c36e03575)
1<?php
2
3use dokuwiki\plugin\structpublish\meta\Constants;
4use dokuwiki\plugin\structpublish\meta\Revision;
5
6class action_plugin_structpublish_publish extends DokuWiki_Action_Plugin
7{
8    /** @var \helper_plugin_structpublish_db */
9    protected $dbHelper;
10
11    /**
12     * @inheritDoc
13     */
14    public function register(Doku_Event_Handler $controller)
15    {
16        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleApprove');
17        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handlePublish');
18    }
19
20    public function handlePublish(Doku_Event $event)
21    {
22        if ($event->data != 'show') return;
23
24        $this->dbHelper = plugin_load('helper', 'structpublish_db');
25
26        global $INPUT;
27        $in = $INPUT->arr('structpublish');
28        if (!$in || !isset($in[Constants::ACTION_PUBLISH])) {
29            return;
30        }
31
32        $this->saveRevision(Constants::STATUS_PUBLISHED);
33
34        $this->updateSchemaData();
35
36    }
37
38    public function handleApprove(Doku_Event $event)
39    {
40        if ($event->data != 'show') return;
41
42        $this->dbHelper = plugin_load('helper', 'structpublish_db');
43
44        global $INPUT;
45        $in = $INPUT->arr('structpublish');
46        if (!$in || !isset($in[Constants::ACTION_APPROVE])) {
47            return;
48        }
49
50        $this->saveRevision(Constants::STATUS_APPROVED);
51    }
52
53    /**
54     * Save publish data
55     *
56     * @param string $status
57     * @return void
58     */
59    protected function saveRevision($status)
60    {
61        global $ID;
62        global $INFO;
63
64        // FIXME prevent bumping an already published revision
65        $sqlite = $this->dbHelper->getDB();
66        $revision = new Revision($sqlite, $ID, $INFO['currentrev']);
67
68        // TODO do not autoincrement version, make it a string
69        if ($status === Constants::STATUS_PUBLISHED) {
70            $revision->setVersion($revision->getVersion() + 1);
71        }
72        $revision->setUser($_SERVER['REMOTE_USER']);
73        $revision->setStatus($status);
74        $revision->setDate(time());
75        $revision->save();
76    }
77
78    /**
79     * Set "published" status in all assigned schemas
80     *
81     * @return void
82     */
83    protected function updateSchemaData()
84    {
85        global $ID;
86        global $INFO;
87
88        $schemaAssignments = \dokuwiki\plugin\struct\meta\Assignments::getInstance();
89        $tables = $schemaAssignments->getPageAssignments($ID);
90
91        if (empty($tables)) return;
92
93        $sqlite = $this->dbHelper->getDB();
94
95        foreach ($tables as $table) {
96            // TODO unpublish earlier revisions
97            $sqlite->query( "UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?", [$ID, $INFO['currentrev']]);
98            $sqlite->query( "UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?", [$ID, $INFO['currentrev']]);
99        }
100    }
101}
102