xref: /plugin/struct/helper/lookup.php (revision b193f1ee28c5fa11f136bc19373095ca8f6d073d)
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                throw new StructException('lookup save error: no permission for schema');
42            }
43            $validator = $access->getValidator($data);
44            if($validator->validate()) {
45                $validator->saveData();
46            }
47        }
48
49        // set thank you message
50        if(!$thanks) {
51            $thanks = sprintf($this->getLang('bureaucracy_action_struct_lookup_thanks'), wl($ID));
52        } else {
53            $thanks = hsc($thanks);
54        }
55
56        return $thanks;
57    }
58}