xref: /plugin/struct/types/AbstractBaseType.php (revision 1c502704592431c9b605eb91ad8b3f133892d618)
1<?php
2namespace plugin\struct\types;
3
4use dokuwiki\Form\Form;
5
6/**
7 * Class AbstractBaseType
8 *
9 * This class represents a basic type that can be configured to be used in a Schema. It is the main
10 * part of a column definition as defined in meta\Column
11 *
12 * @package plugin\struct\types
13 */
14abstract class AbstractBaseType {
15
16    /**
17     * @var array current config
18     */
19    protected $config = array();
20
21    /**
22     * @var string label for the field
23     */
24    protected $label = '';
25
26    /**
27     * @var bool is this a multivalue field?
28     */
29    protected $ismulti = false;
30
31    /**
32     * AbstractBaseType constructor.
33     * @param array|null $config The configuration, might be null if nothing saved, yet
34     * @param string $label The label for this field (empty for new definitions=
35     * @param bool $ismulti Should this field accept multiple values?
36     */
37    public function __construct($config = null, $label = '', $ismulti = false) {
38        if(!is_null($config)) $this->config = array_merge($this->config, $config);
39        $this->label = $label;
40        $this->ismulti = (bool) $ismulti;
41    }
42
43    /**
44     * Returns data as associative array
45     *
46     * @return array
47     */
48    public function getAsEntry() {
49        return array(
50            'config' => json_encode($this->config),
51            'label' => $this->label,
52            'ismulti' => $this->ismulti,
53            'class' => $this->getClass()
54        );
55    }
56
57    /**
58     * The class name of this type (no namespace)
59     * @return string
60     */
61    public function getClass() {
62        return substr(get_class($this), 20);
63    }
64
65    /**
66     * Return the current configuration for this type
67     *
68     * @return array
69     */
70    public function getConfig() {
71        return $this->config;
72    }
73
74    /**
75     * @return boolean
76     */
77    public function isMulti() {
78        return $this->ismulti;
79    }
80
81    /**
82     * @return string
83     */
84    public function getLabel() {
85        return $this->label;
86    }
87
88    /**
89     * Adds the admin schema editor to the given form
90     *
91     * @param Form $form
92     * @return void
93     */
94    abstract public function schemaEditor(Form $form);
95
96    /**
97     * Adds the frontend editor to the given form
98     *
99     * @param Form $form
100     * @return void
101     */
102    abstract public function frontendEditor(Form $form);
103
104    /**
105     * Output the stored data
106     *
107     * @param string|int $value the value stored in the database
108     * @return string the HTML to represent this data
109     */
110    abstract public function getDisplayData($value);
111}
112