xref: /plugin/struct/meta/Column.php (revision 053212b194c66249cf544d22d6235bed793fcf3f)
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 AbstractBaseType
70     */
71    public function getType() {
72        return $this->type;
73    }
74
75    /**
76     * @return int
77     */
78    public function getColref() {
79        return $this->colref;
80    }
81
82    /**
83     * Returns the full column name. When table is set, prefixed by the table name
84     *
85     * @return string
86     */
87    public function getColName() {
88        if($this->isMulti()) throw new StructException('Calling getColName on a multi value column makes no sense.');
89
90        $col = 'col'.$this->colref;
91        if($this->table) $col = 'data_'.$this->table.'.'.$col;
92        return $col;
93    }
94
95    /**
96     * @return boolean
97     */
98    public function isEnabled() {
99        return $this->enabled;
100    }
101
102    /**
103     * @return string
104     */
105    public function getTable() {
106        return $this->table;
107    }
108
109    /**
110     * @return bool
111     */
112    public function isMulti() {
113        return $this->type->isMulti();
114    }
115
116    /**
117     * Render the values for this column
118     *
119     * @param                $values
120     * @param \Doku_Renderer $R
121     * @param                $mode
122     */
123    public function render($values, \Doku_Renderer $R, $mode) {
124        if ($this->isMulti()) {
125            $this->type->renderMultiValue($values, $R, $mode);
126            return;
127        }
128        $this->type->renderValue($values, $R, $mode);
129    }
130
131    /**
132     * Returns a list of all available types
133     *
134     * @return array
135     */
136    static public function allTypes() {
137        $types = array();
138        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
139        foreach($files as $file) {
140            $file = basename($file, '.php');
141            if(substr($file, 0, 8) == 'Abstract') continue;
142            $types[] = $file;
143        }
144        sort($types);
145
146        return $types;
147    }
148
149}
150