xref: /plugin/struct/types/AbstractBaseType.php (revision 0fe33e720e6122782050fa864daf1ac30d713126)
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     * Split a single value into multiple values
107     *
108     * This function is called on saving data when only a single value instead of an array
109     * was submitted.
110     *
111     * Types implementing their own @see multiValueEditor() will probably want to override this
112     *
113     * @param string $value
114     * @return array
115     */
116    public function splitValues($value) {
117        return array_map('trim', explode(',', $value));
118    }
119
120    /**
121     * Return the editor to edit multiple values
122     *
123     * Types can override this to provide a better alternative than multiple entry fields
124     *
125     * @param string $name the form base name where this has to be stored
126     * @param string[] $values the current values
127     * @return string html
128     */
129    public function multiValueEditor($name, $values) {
130        $html = '';
131        foreach($values as $value) {
132            $html .= $this->valueEditor($name . '[]', $value);
133        }
134        // empty field to add
135        $html .= $this->valueEditor($name . '[]', '');
136
137        return $html;
138    }
139
140    /**
141     * Return the editor to edit a single value
142     *
143     * @param string $name the form name where this has to be stored
144     * @param string $value the current value
145     * @return string html
146     */
147    abstract public function valueEditor($name, $value);
148
149    /**
150     * Output the stored data
151     *
152     * @param string|int $value the value stored in the database
153     * @return string the HTML to represent this data
154     */
155    abstract public function getDisplayData($value);
156
157    /**
158     * This function builds a where clause for this column, comparing
159     * the current value stored in $column with $value. Types can use it to do
160     * clever things with the comparison.
161     *
162     * This default implementation is probably good enough for most basic types
163     *
164     * @param string $column The column name to us in the SQL
165     * @param string $comp The comparator @see Search::$COMPARATORS
166     * @param string $value
167     * @return array Tuple with the SQL and parameter array
168     */
169    public function compare($column, $comp, $value) {
170        if($comp == '~') {
171            $sql = "$column LIKE ?";
172            $opt = array('%' . $value . '%');
173        } else if($comp == '!~') {
174            $sql = "$column NOT LIKE ?";
175            $opt = array('%' . $value . '%');
176        } else {
177            $sql = "$column $comp ?";
178            $opt = array($value);
179        }
180
181        return array($sql, $opt);
182    }
183}
184