xref: /plugin/struct/types/AbstractBaseType.php (revision 706e7c4b4fb25490b723f162ecf439f6eea4d948)
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 * This defines also how the content of the coulmn will be entered and formatted.
13 *
14 * @package plugin\struct\types
15 * @see Column
16 */
17abstract class AbstractBaseType {
18
19    /**
20     * @var array current config
21     */
22    protected $config = array();
23
24    /**
25     * @var string label for the field
26     */
27    protected $label = '';
28
29    /**
30     * @var bool is this a multivalue field?
31     */
32    protected $ismulti = false;
33
34    /**
35     * AbstractBaseType constructor.
36     * @param array|null $config The configuration, might be null if nothing saved, yet
37     * @param string $label The label for this field (empty for new definitions=
38     * @param bool $ismulti Should this field accept multiple values?
39     */
40    public function __construct($config = null, $label = '', $ismulti = false) {
41        if(!is_null($config)) $this->config = array_merge($this->config, $config);
42        $this->label = $label;
43        $this->ismulti = (bool) $ismulti;
44    }
45
46    /**
47     * Returns data as associative array
48     *
49     * @return array
50     */
51    public function getAsEntry() {
52        return array(
53            'config' => json_encode($this->config),
54            'label' => $this->label,
55            'ismulti' => $this->ismulti,
56            'class' => $this->getClass()
57        );
58    }
59
60    /**
61     * The class name of this type (no namespace)
62     * @return string
63     */
64    public function getClass() {
65        return substr(get_class($this), 20);
66    }
67
68    /**
69     * Return the current configuration for this type
70     *
71     * @return array
72     */
73    public function getConfig() {
74        return $this->config;
75    }
76
77    /**
78     * @return boolean
79     */
80    public function isMulti() {
81        return $this->ismulti;
82    }
83
84    /**
85     * @return string
86     */
87    public function getLabel() {
88        return $this->label;
89    }
90
91    /**
92     * Return the editor to edit a single value
93     *
94     * @param string $name the form name where this has to be stored
95     * @param string $value the current value
96     * @return string html
97     */
98    abstract public function valueEditor($name, $value);
99
100    /**
101     * Output the stored data
102     *
103     * @param string|int $value the value stored in the database
104     * @return string the HTML to represent this data
105     */
106    abstract public function getDisplayData($value);
107}
108