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