1<?php
2
3use dokuwiki\plugin\struct\meta\AccessTable;
4use dokuwiki\plugin\struct\meta\AccessTableGlobal;
5use dokuwiki\plugin\struct\meta\StructException;
6
7/**
8 * Allows adding a lookup schema as a bureaucracy action
9 *
10 */
11class helper_plugin_struct_lookup extends helper_plugin_bureaucracy_action
12{
13
14    /**
15     * Performs struct_lookup action
16     *
17     * @param helper_plugin_bureaucracy_field[] $fields  array with form fields
18     * @param string $thanks  thanks message
19     * @param array  $argv    array with entries: template, pagename, separator
20     * @return array|mixed
21     *
22     * @throws Exception
23     */
24    public function run($fields, $thanks, $argv)
25    {
26        global $ID;
27
28        // get all struct values and their associated schemas
29        $tosave = array();
30        foreach ($fields as $field) {
31            if (!is_a($field, 'helper_plugin_struct_field')) continue;
32            /** @var helper_plugin_struct_field $field */
33            $tbl = $field->column->getTable();
34            $lbl = $field->column->getLabel();
35            if (!isset($tosave[$tbl])) $tosave[$tbl] = array();
36            $tosave[$tbl][$lbl] = $field->getParam('value');
37        }
38
39        foreach ($tosave as $table => $data) {
40            $access = AccessTable::getGlobalAccess($table);
41            if (!$access instanceof AccessTableGlobal) continue;
42
43            if (!$access->getSchema()->isEditable()) {
44                msg('lookup save error: no permission for schema', -1);
45                return false;
46            }
47            $validator = $access->getValidator($data);
48            if ($validator->validate()) {
49                $validator->saveData();
50            }
51        }
52
53        // set thank you message
54        if (!$thanks) {
55            $thanks = sprintf($this->getLang('bureaucracy_action_struct_lookup_thanks'), wl($ID));
56        } else {
57            $thanks = hsc($thanks);
58        }
59
60        return $thanks;
61    }
62}
63