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