xref: /plugin/struct/meta/Column.php (revision 17a3a5782666ca8742a2c64cc11565d4f9fe1076)
11c502704SAndreas Gohr<?php
21c502704SAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
41c502704SAndreas Gohr
5ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\AbstractBaseType;
61c502704SAndreas Gohr
71c502704SAndreas Gohr/**
81c502704SAndreas Gohr * Class Column
91c502704SAndreas Gohr *
101c502704SAndreas Gohr * This represents a single column within a schema and contains the configured BaseType as well as the
117182938bSAndreas Gohr * column reference to the data table.
127182938bSAndreas Gohr *
137182938bSAndreas Gohr * It basically combines the information how a column's content behaves (as defines in the BaseType and its
147182938bSAndreas Gohr * configuration) with where to find that content and adds some basic meta data (like sort or enabled)
151c502704SAndreas Gohr *
16ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
171c502704SAndreas Gohr */
18d6d97f60SAnna Dabrowskaclass Column
19d6d97f60SAnna Dabrowska{
201c502704SAndreas Gohr    /** @var int fields are sorted by this value */
211c502704SAndreas Gohr    protected $sort;
221c502704SAndreas Gohr    /** @var AbstractBaseType the type of this column */
231c502704SAndreas Gohr    protected $type;
241c502704SAndreas Gohr    /** @var int the column in the datatable. columns count from 1 */
251c502704SAndreas Gohr    protected $colref;
261c502704SAndreas Gohr    /** @var bool is this column still enabled? */
271c502704SAndreas Gohr    protected $enabled = true;
2863d51bbfSAndreas Gohr    /** @var  string backreference to the table this column is part of */
2963d51bbfSAndreas Gohr    protected $table;
301c502704SAndreas Gohr
311c502704SAndreas Gohr    /**
321c502704SAndreas Gohr     * Column constructor.
331c502704SAndreas Gohr     * @param int $sort
341c502704SAndreas Gohr     * @param AbstractBaseType $type
351c502704SAndreas Gohr     * @param int $colref
361c502704SAndreas Gohr     * @param bool $enabled
3763d51bbfSAndreas Gohr     * @param string $table
381c502704SAndreas Gohr     */
39d6d97f60SAnna Dabrowska    public function __construct($sort, AbstractBaseType $type, $colref = 0, $enabled = true, $table = '')
40d6d97f60SAnna Dabrowska    {
411c502704SAndreas Gohr        $this->sort = (int)$sort;
421c502704SAndreas Gohr        $this->type = $type;
431c502704SAndreas Gohr        $this->colref = (int)$colref;
441c502704SAndreas Gohr        $this->enabled = (bool)$enabled;
4563d51bbfSAndreas Gohr        $this->table = $table;
461c502704SAndreas Gohr    }
471c502704SAndreas Gohr
481c502704SAndreas Gohr    /**
491c502704SAndreas Gohr     * @return int
501c502704SAndreas Gohr     */
51d6d97f60SAnna Dabrowska    public function getSort()
52d6d97f60SAnna Dabrowska    {
531c502704SAndreas Gohr        return $this->sort;
541c502704SAndreas Gohr    }
551c502704SAndreas Gohr
561c502704SAndreas Gohr    /**
571c502704SAndreas Gohr     * @return int
581c502704SAndreas Gohr     */
59d6d97f60SAnna Dabrowska    public function getTid()
60d6d97f60SAnna Dabrowska    {
6104eb61a6SAndreas Gohr        return $this->type->getTid();
621c502704SAndreas Gohr    }
631c502704SAndreas Gohr
641c502704SAndreas Gohr    /**
6590ae6c2dSAndreas Gohr     * @return string
6690ae6c2dSAndreas Gohr     */
67d6d97f60SAnna Dabrowska    public function getLabel()
68d6d97f60SAnna Dabrowska    {
6990ae6c2dSAndreas Gohr        return $this->type->getLabel();
7090ae6c2dSAndreas Gohr    }
7190ae6c2dSAndreas Gohr
7290ae6c2dSAndreas Gohr    /**
7300f6af48SAndreas Gohr     * @return string the label prepended with the table name
7400f6af48SAndreas Gohr     */
75d6d97f60SAnna Dabrowska    public function getFullQualifiedLabel()
76d6d97f60SAnna Dabrowska    {
7700f6af48SAndreas Gohr        if (!$this->table) throw new StructException('No table set for this column');
7800f6af48SAndreas Gohr        return $this->table . '.' . $this->getLabel();
7900f6af48SAndreas Gohr    }
8000f6af48SAndreas Gohr
8100f6af48SAndreas Gohr    /**
829e9bee91SAndreas Gohr     * @return string
839e9bee91SAndreas Gohr     */
84d6d97f60SAnna Dabrowska    public function getTranslatedLabel()
85d6d97f60SAnna Dabrowska    {
869e9bee91SAndreas Gohr        return $this->type->getTranslatedLabel();
879e9bee91SAndreas Gohr    }
889e9bee91SAndreas Gohr
899e9bee91SAndreas Gohr    /**
903a717675SAndreas Gohr     * @return string
913a717675SAndreas Gohr     */
92d6d97f60SAnna Dabrowska    public function getTranslatedHint()
93d6d97f60SAnna Dabrowska    {
943a717675SAndreas Gohr        return $this->type->getTranslatedHint();
953a717675SAndreas Gohr    }
963a717675SAndreas Gohr
973a717675SAndreas Gohr    /**
981c502704SAndreas Gohr     * @return AbstractBaseType
991c502704SAndreas Gohr     */
100d6d97f60SAnna Dabrowska    public function getType()
101d6d97f60SAnna Dabrowska    {
1021c502704SAndreas Gohr        return $this->type;
1031c502704SAndreas Gohr    }
1041c502704SAndreas Gohr
1051c502704SAndreas Gohr    /**
1061c502704SAndreas Gohr     * @return int
1071c502704SAndreas Gohr     */
108d6d97f60SAnna Dabrowska    public function getColref()
109d6d97f60SAnna Dabrowska    {
1101c502704SAndreas Gohr        return $this->colref;
1111c502704SAndreas Gohr    }
1121c502704SAndreas Gohr
1131c502704SAndreas Gohr    /**
11427f6439fSAndreas Gohr     * Returns the column name (without a table)
115d1b04e89SAndreas Gohr     *
1165a11127fSAndreas Gohr     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
117d1b04e89SAndreas Gohr     * @return string
118d1b04e89SAndreas Gohr     */
119d6d97f60SAnna Dabrowska    public function getColName($enforceSingleColumn = true)
120d6d97f60SAnna Dabrowska    {
121*17a3a578SAndreas Gohr        if ($enforceSingleColumn && $this->isMulti())
122*17a3a578SAndreas Gohr            throw new StructException('Calling getColName on a multi value column makes no sense.');
12327f6439fSAndreas Gohr        return 'col' . $this->colref;
12427f6439fSAndreas Gohr    }
1255b2d2a8cSAndreas Gohr
12627f6439fSAndreas Gohr    /**
12727f6439fSAndreas Gohr     * Returns the full column name. When table is set, prefixed by the table name
12827f6439fSAndreas Gohr     *
1295a11127fSAndreas Gohr     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
13027f6439fSAndreas Gohr     * @return string
13127f6439fSAndreas Gohr     */
132d6d97f60SAnna Dabrowska    public function getFullColName($enforceSingleColumn = true)
133d6d97f60SAnna Dabrowska    {
1345a11127fSAndreas Gohr        $col = $this->getColName($enforceSingleColumn);
1355b2d2a8cSAndreas Gohr        if ($this->table) $col = 'data_' . $this->table . '.' . $col;
136d1b04e89SAndreas Gohr        return $col;
137d1b04e89SAndreas Gohr    }
138d1b04e89SAndreas Gohr
139d1b04e89SAndreas Gohr    /**
1401c502704SAndreas Gohr     * @return boolean
1411c502704SAndreas Gohr     */
142d6d97f60SAnna Dabrowska    public function isEnabled()
143d6d97f60SAnna Dabrowska    {
1441c502704SAndreas Gohr        return $this->enabled;
1451c502704SAndreas Gohr    }
1461c502704SAndreas Gohr
147ae697e1fSAndreas Gohr    /**
14863d51bbfSAndreas Gohr     * @return string
14963d51bbfSAndreas Gohr     */
150d6d97f60SAnna Dabrowska    public function getTable()
151d6d97f60SAnna Dabrowska    {
15263d51bbfSAndreas Gohr        return $this->table;
15363d51bbfSAndreas Gohr    }
15463d51bbfSAndreas Gohr
15563d51bbfSAndreas Gohr    /**
15690ae6c2dSAndreas Gohr     * @return bool
15790ae6c2dSAndreas Gohr     */
158d6d97f60SAnna Dabrowska    public function isMulti()
159d6d97f60SAnna Dabrowska    {
16090ae6c2dSAndreas Gohr        return $this->type->isMulti();
16190ae6c2dSAndreas Gohr    }
16290ae6c2dSAndreas Gohr
16390ae6c2dSAndreas Gohr    /**
1642350b48eSAndreas Gohr     * @return bool
1652350b48eSAndreas Gohr     */
166d6d97f60SAnna Dabrowska    public function isVisibleInEditor()
167d6d97f60SAnna Dabrowska    {
1682350b48eSAndreas Gohr        return $this->getType()->isVisibleInEditor();
1692350b48eSAndreas Gohr    }
1702350b48eSAndreas Gohr
1712350b48eSAndreas Gohr    /**
1722350b48eSAndreas Gohr     * @return bool
1732350b48eSAndreas Gohr     */
174d6d97f60SAnna Dabrowska    public function isVisibleInPage()
175d6d97f60SAnna Dabrowska    {
1762350b48eSAndreas Gohr        return $this->getType()->isVisibleInPage();
1772350b48eSAndreas Gohr    }
1782350b48eSAndreas Gohr
1792350b48eSAndreas Gohr    /**
180636c8abaSAndreas Gohr     * Returns a list of all available types and their class names
181ae697e1fSAndreas Gohr     *
182636c8abaSAndreas Gohr     * @param bool $reload forces reloading the types
183ae697e1fSAndreas Gohr     * @return array
184ae697e1fSAndreas Gohr     */
185d6d97f60SAnna Dabrowska    public static function allTypes($reload = false)
186d6d97f60SAnna Dabrowska    {
187636c8abaSAndreas Gohr        static $map = null;
188636c8abaSAndreas Gohr        if (!is_null($map) && !$reload) return $map;
189636c8abaSAndreas Gohr
190636c8abaSAndreas Gohr        // get our own types
191636c8abaSAndreas Gohr        $map = array();
192ae697e1fSAndreas Gohr        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
193ae697e1fSAndreas Gohr        foreach ($files as $file) {
194ae697e1fSAndreas Gohr            $file = basename($file, '.php');
195ae697e1fSAndreas Gohr            if (substr($file, 0, 8) == 'Abstract') continue;
1969ebd2ed6SAndreas Gohr            if (substr($file, 0, 5) == 'Trait') continue;
1976781c68dSSzymon Olewniczak            if (substr($file, 0, 4) == 'Auto') continue;
198636c8abaSAndreas Gohr            $map[$file] = 'dokuwiki\\plugin\\struct\\types\\' . $file;
199ae697e1fSAndreas Gohr        }
2001c502704SAndreas Gohr
201636c8abaSAndreas Gohr        // let plugins add their own
202636c8abaSAndreas Gohr        trigger_event('PLUGIN_STRUCT_TYPECLASS_INIT', $map);
203636c8abaSAndreas Gohr
204636c8abaSAndreas Gohr        ksort($map);
205636c8abaSAndreas Gohr        return $map;
206ae697e1fSAndreas Gohr    }
2071c502704SAndreas Gohr}
208