xref: /plugin/struct/helper/lookup.php (revision 987ccc7f0328ab5d1bee7848402f7803f7b62014)
1<?php
2use dokuwiki\plugin\struct\meta\AccessTable;
3use dokuwiki\plugin\struct\meta\AccessTableLookup;
4use dokuwiki\plugin\struct\meta\StructException;
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        global $ID;
24
25        // get all struct values and their associated schemas
26        $tosave = array();
27        foreach($fields as $field) {
28            if(!is_a($field, 'helper_plugin_struct_field')) continue;
29            /** @var helper_plugin_struct_field $field */
30            $tbl = $field->column->getTable();
31            $lbl = $field->column->getLabel();
32            if(!isset($tosave[$tbl])) $tosave[$tbl] = array();
33            $tosave[$tbl][$lbl] = $field->getParam('value');
34        }
35
36        foreach($tosave as $table => $data) {
37            $access = AccessTable::byTableName($table, 0, 0);
38            if (!$access instanceof AccessTableLookup) continue;
39
40            if(!$access->getSchema()->isEditable()) {
41                msg('lookup save error: no permission for schema', -1);
42                return false;
43            }
44            $validator = $access->getValidator($data);
45            if($validator->validate()) {
46                $validator->saveData();
47            }
48        }
49
50        // set thank you message
51        if(!$thanks) {
52            $thanks = sprintf($this->getLang('bureaucracy_action_struct_lookup_thanks'), wl($ID));
53        } else {
54            $thanks = hsc($thanks);
55        }
56
57        return $thanks;
58    }
59}