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