xref: /plugin/struct/meta/Column.php (revision 956594215d863a5a696a39f1928b7c5aef7a11d3)
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        $this->sort = (int) $sort;
41        $this->type = $type;
42        $this->colref = (int) $colref;
43        $this->enabled = (bool) $enabled;
44        $this->table = $table;
45    }
46
47    /**
48     * @return int
49     */
50    public function getSort() {
51        return $this->sort;
52    }
53
54    /**
55     * @return int
56     */
57    public function getTid() {
58        return $this->type->getTid();
59    }
60
61    /**
62     * @return string
63     */
64    public function getLabel() {
65        return $this->type->getLabel();
66    }
67
68    /**
69     * @return string the label prepended with the table name
70     */
71    public function getFullQualifiedLabel() {
72        if(!$this->table) throw new StructException('No table set for this column');
73        return $this->table .'.'. $this->getLabel();
74    }
75
76    /**
77     * @return string
78     */
79    public function getTranslatedLabel() {
80        return $this->type->getTranslatedLabel();
81    }
82
83    /**
84     * @return string
85     */
86    public function getTranslatedHint() {
87        return $this->type->getTranslatedHint();
88    }
89
90    /**
91     * @return AbstractBaseType
92     */
93    public function getType() {
94        return $this->type;
95    }
96
97    /**
98     * @return int
99     */
100    public function getColref() {
101        return $this->colref;
102    }
103
104    /**
105     * Returns the column name (without a table)
106     *
107     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
108     * @return string
109     */
110    public function getColName($enforceSingleColumn = true) {
111        if($enforceSingleColumn && $this->isMulti()) throw new StructException('Calling getColName on a multi value column makes no sense.');
112        return 'col'.$this->colref;
113    }
114
115    /**
116     * Returns the full column name. When table is set, prefixed by the table name
117     *
118     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
119     * @return string
120     */
121    public function getFullColName($enforceSingleColumn = true) {
122        $col = $this->getColName($enforceSingleColumn);
123        if($this->table) $col = 'data_'.$this->table.'.'.$col;
124        return $col;
125    }
126
127    /**
128     * @return boolean
129     */
130    public function isEnabled() {
131        return $this->enabled;
132    }
133
134    /**
135     * @return string
136     */
137    public function getTable() {
138        return $this->table;
139    }
140
141    /**
142     * @return bool
143     */
144    public function isMulti() {
145        return $this->type->isMulti();
146    }
147
148    /**
149     * @return bool
150     */
151    public function isVisibleInEditor() {
152        return $this->getType()->isVisibleInEditor();
153    }
154
155    /**
156     * @return bool
157     */
158    public function isVisibleInPage() {
159        return $this->getType()->isVisibleInPage();
160    }
161
162    /**
163     * Returns a list of all available types and their class names
164     *
165     * @param bool $reload forces reloading the types
166     * @return array
167     */
168    static public function allTypes($reload=false) {
169        static $map = null;
170        if(!is_null($map) && !$reload) return $map;
171
172        // get our own types
173        $map = array();
174        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
175        foreach($files as $file) {
176            $file = basename($file, '.php');
177            if(substr($file, 0, 8) == 'Abstract') continue;
178            $map[$file] = 'dokuwiki\\plugin\\struct\\types\\' .$file;
179        }
180
181        // let plugins add their own
182        trigger_event('PLUGIN_STRUCT_TYPECLASS_INIT', $map);
183
184        ksort($map);
185        return $map;
186    }
187
188}
189