xref: /plugin/struct/types/AbstractBaseType.php (revision a57a64a5064de0f3bcdccaae3fe663e44d732149)
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     * Adds the admin schema editor to the given form
93     *
94     * @param Form $form
95     * @return void
96     */
97    abstract public function schemaEditor(Form $form);
98
99    /**
100     * Adds the frontend editor to the given form
101     *
102     * @param Form $form
103     * @return void
104     */
105    abstract public function frontendEditor(Form $form);
106
107    /**
108     * Output the stored data
109     *
110     * @param string|int $value the value stored in the database
111     * @return string the HTML to represent this data
112     */
113    abstract public function getDisplayData($value);
114}
115