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())
122            throw new StructException('Calling getColName on a multi value column makes no sense.');
123        return 'col' . $this->colref;
124    }
125
126    /**
127     * Returns the full column name. When table is set, prefixed by the table name
128     *
129     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
130     * @return string
131     */
132    public function getFullColName($enforceSingleColumn = true)
133    {
134        $col = $this->getColName($enforceSingleColumn);
135        if ($this->table) $col = 'data_' . $this->table . '.' . $col;
136        return $col;
137    }
138
139    /**
140     * @return boolean
141     */
142    public function isEnabled()
143    {
144        return $this->enabled;
145    }
146
147    /**
148     * @return string
149     */
150    public function getTable()
151    {
152        return $this->table;
153    }
154
155    /**
156     * @return bool
157     */
158    public function isMulti()
159    {
160        return $this->type->isMulti();
161    }
162
163    /**
164     * @return bool
165     */
166    public function isVisibleInEditor()
167    {
168        return $this->getType()->isVisibleInEditor();
169    }
170
171    /**
172     * @return bool
173     */
174    public function isVisibleInPage()
175    {
176        return $this->getType()->isVisibleInPage();
177    }
178
179    /**
180     * Returns a list of all available types and their class names
181     *
182     * @param bool $reload forces reloading the types
183     * @return array
184     */
185    public static function allTypes($reload = false)
186    {
187        static $map = null;
188        if (!is_null($map) && !$reload) return $map;
189
190        // get our own types
191        $map = array();
192        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
193        foreach ($files as $file) {
194            $file = basename($file, '.php');
195            if (substr($file, 0, 8) == 'Abstract') continue;
196            if (substr($file, 0, 5) == 'Trait') continue;
197            if (substr($file, 0, 4) == 'Auto') continue;
198            $map[$file] = 'dokuwiki\\plugin\\struct\\types\\' . $file;
199        }
200
201        // let plugins add their own
202        trigger_event('PLUGIN_STRUCT_TYPECLASS_INIT', $map);
203
204        ksort($map);
205        return $map;
206    }
207}
208