xref: /plugin/struct/meta/Column.php (revision 0549dcc5bc88d4f9d923acdd09931d8d51be7097)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5use dokuwiki\plugin\struct\types\AbstractBaseType;
6
7/**
8 * Class Column
9 *
10 * This represents a single column within a schema and contains the configured BaseType as well as the
11 * column reference to the data table.
12 *
13 * It basically combines the information how a column's content behaves (as defines in the BaseType and its
14 * configuration) with where to find that content and adds some basic meta data (like sort or enabled)
15 *
16 * @package dokuwiki\plugin\struct\meta
17 */
18class Column
19{
20    /** @var int fields are sorted by this value */
21    protected $sort;
22    /** @var AbstractBaseType the type of this column */
23    protected $type;
24    /** @var int the column in the datatable. columns count from 1 */
25    protected $colref;
26    /** @var bool is this column still enabled? */
27    protected $enabled = true;
28    /** @var  string backreference to the table this column is part of */
29    protected $table;
30
31    /**
32     * Column constructor.
33     * @param int $sort
34     * @param AbstractBaseType $type
35     * @param int $colref
36     * @param bool $enabled
37     * @param string $table
38     */
39    public function __construct($sort, AbstractBaseType $type, $colref = 0, $enabled = true, $table = '')
40    {
41        $this->sort = (int)$sort;
42        $this->type = $type;
43        $this->colref = (int)$colref;
44        $this->enabled = (bool)$enabled;
45        $this->table = $table;
46    }
47
48    /**
49     * @return int
50     */
51    public function getSort()
52    {
53        return $this->sort;
54    }
55
56    /**
57     * @return int
58     */
59    public function getTid()
60    {
61        return $this->type->getTid();
62    }
63
64    /**
65     * @return string
66     */
67    public function getLabel()
68    {
69        return $this->type->getLabel();
70    }
71
72    /**
73     * @return string the label prepended with the table name
74     */
75    public function getFullQualifiedLabel()
76    {
77        if (!$this->table) throw new StructException('No table set for this column');
78        return $this->table . '.' . $this->getLabel();
79    }
80
81    /**
82     * @return string
83     */
84    public function getTranslatedLabel()
85    {
86        return $this->type->getTranslatedLabel();
87    }
88
89    /**
90     * @return string
91     */
92    public function getTranslatedHint()
93    {
94        return $this->type->getTranslatedHint();
95    }
96
97    /**
98     * @return AbstractBaseType
99     */
100    public function getType()
101    {
102        return $this->type;
103    }
104
105    /**
106     * @return int
107     */
108    public function getColref()
109    {
110        return $this->colref;
111    }
112
113    /**
114     * Returns the column name (without a table)
115     *
116     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
117     * @return string
118     */
119    public function getColName($enforceSingleColumn = true)
120    {
121        if ($enforceSingleColumn && $this->isMulti()) throw new StructException('Calling getColName on a multi value column makes no sense.');
122        return 'col' . $this->colref;
123    }
124
125    /**
126     * Returns the full column name. When table is set, prefixed by the table name
127     *
128     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
129     * @return string
130     */
131    public function getFullColName($enforceSingleColumn = true)
132    {
133        $col = $this->getColName($enforceSingleColumn);
134        if ($this->table) $col = 'data_' . $this->table . '.' . $col;
135        return $col;
136    }
137
138    /**
139     * @return boolean
140     */
141    public function isEnabled()
142    {
143        return $this->enabled;
144    }
145
146    /**
147     * @return string
148     */
149    public function getTable()
150    {
151        return $this->table;
152    }
153
154    /**
155     * @return bool
156     */
157    public function isMulti()
158    {
159        return $this->type->isMulti();
160    }
161
162    /**
163     * @return bool
164     */
165    public function isVisibleInEditor()
166    {
167        return $this->getType()->isVisibleInEditor();
168    }
169
170    /**
171     * @return bool
172     */
173    public function isVisibleInPage()
174    {
175        return $this->getType()->isVisibleInPage();
176    }
177
178    /**
179     * Returns a list of all available types and their class names
180     *
181     * @param bool $reload forces reloading the types
182     * @return array
183     */
184    public static function allTypes($reload = false)
185    {
186        static $map = null;
187        if (!is_null($map) && !$reload) return $map;
188
189        // get our own types
190        $map = array();
191        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
192        foreach ($files as $file) {
193            $file = basename($file, '.php');
194            if (substr($file, 0, 8) == 'Abstract') continue;
195            if (substr($file, 0, 5) == 'Trait') continue;
196            if (substr($file, 0, 4) == 'Auto') continue;
197            $map[$file] = 'dokuwiki\\plugin\\struct\\types\\' . $file;
198        }
199
200        // let plugins add their own
201        trigger_event('PLUGIN_STRUCT_TYPECLASS_INIT', $map);
202
203        ksort($map);
204        return $map;
205    }
206}
207