1<?php
2/**
3 * DokuWiki Plugin structupdate (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Szymon Olewniczak <it@rid.pl>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14use dokuwiki\plugin\struct\meta\AccessTable;
15use dokuwiki\plugin\struct\meta\AccessTableLookup;
16use dokuwiki\plugin\struct\meta\Assignments;
17use dokuwiki\plugin\struct\meta\Schema;
18use dokuwiki\plugin\struct\meta\StructException;
19
20/**
21 * Update struct date using bureaucracy forms
22 *
23 */
24class helper_plugin_structupdate_update extends helper_plugin_bureaucracy_action
25{
26    /**
27     * Performs struct_lookup action
28     *
29     * @param helper_plugin_bureaucracy_field[] $fields  array with form fields
30     * @param string $thanks  thanks message
31     * @param array  $argv    array with entries: pageid/rowid
32     * @return array|mixed
33     *
34     * @throws Exception
35     */
36    public function run($fields, $thanks, $argv) {
37        global $ID;
38
39        list($page_row_id) = $argv;
40        $page_row_id = trim($page_row_id);
41        if (!$page_row_id) {
42            $page_row_id = $ID;
43        } else {
44            // perform replacements
45            $this->prepareFieldReplacements($fields);
46            $page_row_id = $this->replace($page_row_id);
47        }
48
49        // get all struct values and their associated schemas
50        $tosave = [];
51        foreach($fields as $field) {
52            if(!is_a($field, 'helper_plugin_struct_field')) continue;
53            /** @var helper_plugin_struct_field $field */
54            $tbl = $field->column->getTable();
55            $lbl = $field->column->getLabel();
56            if(!isset($tosave[$tbl])) $tosave[$tbl] = [];
57            $tosave[$tbl][$lbl] = $field->getParam('value');
58        }
59
60        /** @var \helper_plugin_struct $helper */
61        $helper = plugin_load('helper', 'struct');
62        $page = cleanID($page_row_id);
63
64        try {
65            if (page_exists($page)) {
66                $assignments = Assignments::getInstance();
67                $tables = $assignments->getPageAssignments($ID);
68
69                $schemadata = [];
70                foreach($tables as $table) {
71                    $schema = AccessTable::getPageAccess($table, $page);
72                    if(!$schema->getSchema()->isEditable()) {
73                        throw new Exception("Schema $table is not editable");
74                    }
75                    $schemadata[$table] = [];
76                    foreach ($schema->getData() as $col => $value) {
77                        $schemadata[$table][$col] = $value->getRawValue();
78                    }
79                }
80
81                foreach ($schemadata as $table => $cols) {
82                    if (isset($tosave[$table])) {
83                        $schemadata[$table] = array_replace($schemadata[$table], $tosave[$table]);
84                    }
85                }
86                $helper->saveData($page, $schemadata);
87            }
88        } catch(Exception $e) {
89            msg($e->getMessage(), -1);
90            return false;
91        }
92
93        // set thank you message
94        if(!$thanks) {
95            $thanks = sprintf($this->getLang('bureaucracy_action_struct_update_thanks'), wl($ID));
96        } else {
97            $thanks = hsc($thanks);
98        }
99
100        return $thanks;
101    }
102}
103
104