xref: /plugin/struct/types/AbstractBaseType.php (revision 009f0f28cbb64169e5af7f121b8e72a6b0b18739)
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     * @var int the type ID
36     */
37    protected $tid = 0;
38
39    /**
40     * AbstractBaseType constructor.
41     * @param array|null $config The configuration, might be null if nothing saved, yet
42     * @param string $label The label for this field (empty for new definitions=
43     * @param bool $ismulti Should this field accept multiple values?
44     * @param int $tid The id of this type if it has been saved, yet
45     */
46    public function __construct($config = null, $label = '', $ismulti = false, $tid=0) {
47        if(!is_null($config)) $this->config = array_merge($this->config, $config);
48        $this->label = $label;
49        $this->ismulti = (bool) $ismulti;
50        $this->tid = $tid;
51    }
52
53    /**
54     * Returns data as associative array
55     *
56     * @return array
57     */
58    public function getAsEntry() {
59        return array(
60            'config' => json_encode($this->config),
61            'label' => $this->label,
62            'ismulti' => $this->ismulti,
63            'class' => $this->getClass()
64        );
65    }
66
67    /**
68     * The class name of this type (no namespace)
69     * @return string
70     */
71    public function getClass() {
72        return substr(get_class($this), 20);
73    }
74
75    /**
76     * Return the current configuration for this type
77     *
78     * @return array
79     */
80    public function getConfig() {
81        return $this->config;
82    }
83
84    /**
85     * @return boolean
86     */
87    public function isMulti() {
88        return $this->ismulti;
89    }
90
91    /**
92     * @return string
93     */
94    public function getLabel() {
95        return $this->label;
96    }
97
98    /**
99     * @return int
100     */
101    public function getTid() {
102        return $this->tid;
103    }
104
105    /**
106     * Return the editor to edit a single value
107     *
108     * @param string $name the form name where this has to be stored
109     * @param string $value the current value
110     * @return string html
111     */
112    abstract public function valueEditor($name, $value);
113
114    /**
115     * Output the stored data
116     *
117     * @param string|int $value the value stored in the database
118     * @return string the HTML to represent this data
119     */
120    abstract public function getDisplayData($value);
121}
122