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