xref: /plugin/struct/meta/Column.php (revision 59062ef63bd4f6ca6b56f27c887575cf47af2d3c)
1<?php
2
3namespace plugin\struct\meta;
4
5use 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 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 full column name. When table is set, prefixed by the table name
106     *
107     * @return string
108     */
109    public function getColName() {
110        if($this->isMulti()) throw new StructException('Calling getColName on a multi value column makes no sense.');
111
112        $col = 'col'.$this->colref;
113        if($this->table) $col = 'data_'.$this->table.'.'.$col;
114        return $col;
115    }
116
117    /**
118     * @return boolean
119     */
120    public function isEnabled() {
121        return $this->enabled;
122    }
123
124    /**
125     * @return string
126     */
127    public function getTable() {
128        return $this->table;
129    }
130
131    /**
132     * @return bool
133     */
134    public function isMulti() {
135        return $this->type->isMulti();
136    }
137
138    /**
139     * @return bool
140     */
141    public function isVisibleInEditor() {
142        return $this->getType()->isVisibleInEditor();
143    }
144
145    /**
146     * @return bool
147     */
148    public function isVisibleInPage() {
149        return $this->getType()->isVisibleInPage();
150    }
151
152    /**
153     * Returns a list of all available types
154     *
155     * @return array
156     */
157    static public function allTypes() {
158        $types = array();
159        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
160        foreach($files as $file) {
161            $file = basename($file, '.php');
162            if(substr($file, 0, 8) == 'Abstract') continue;
163            $types[] = $file;
164        }
165        sort($types);
166
167        return $types;
168    }
169
170}
171