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
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()) 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