xref: /plugin/struct/meta/Column.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
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 */
18*d6d97f60SAnna Dabrowskaclass Column
19*d6d97f60SAnna Dabrowska{
201c502704SAndreas Gohr
211c502704SAndreas Gohr    /** @var int fields are sorted by this value */
221c502704SAndreas Gohr    protected $sort;
231c502704SAndreas Gohr    /** @var AbstractBaseType the type of this column */
241c502704SAndreas Gohr    protected $type;
251c502704SAndreas Gohr    /** @var int the column in the datatable. columns count from 1 */
261c502704SAndreas Gohr    protected $colref;
271c502704SAndreas Gohr    /** @var bool is this column still enabled? */
281c502704SAndreas Gohr    protected $enabled = true;
2963d51bbfSAndreas Gohr    /** @var  string backreference to the table this column is part of */
3063d51bbfSAndreas Gohr    protected $table;
311c502704SAndreas Gohr
321c502704SAndreas Gohr    /**
331c502704SAndreas Gohr     * Column constructor.
341c502704SAndreas Gohr     * @param int $sort
351c502704SAndreas Gohr     * @param AbstractBaseType $type
361c502704SAndreas Gohr     * @param int $colref
371c502704SAndreas Gohr     * @param bool $enabled
3863d51bbfSAndreas Gohr     * @param string $table
391c502704SAndreas Gohr     */
40*d6d97f60SAnna Dabrowska    public function __construct($sort, AbstractBaseType $type, $colref = 0, $enabled = true, $table = '')
41*d6d97f60SAnna Dabrowska    {
421c502704SAndreas Gohr        $this->sort = (int) $sort;
431c502704SAndreas Gohr        $this->type = $type;
441c502704SAndreas Gohr        $this->colref = (int) $colref;
451c502704SAndreas Gohr        $this->enabled = (bool) $enabled;
4663d51bbfSAndreas Gohr        $this->table = $table;
471c502704SAndreas Gohr    }
481c502704SAndreas Gohr
491c502704SAndreas Gohr    /**
501c502704SAndreas Gohr     * @return int
511c502704SAndreas Gohr     */
52*d6d97f60SAnna Dabrowska    public function getSort()
53*d6d97f60SAnna Dabrowska    {
541c502704SAndreas Gohr        return $this->sort;
551c502704SAndreas Gohr    }
561c502704SAndreas Gohr
571c502704SAndreas Gohr    /**
581c502704SAndreas Gohr     * @return int
591c502704SAndreas Gohr     */
60*d6d97f60SAnna Dabrowska    public function getTid()
61*d6d97f60SAnna Dabrowska    {
6204eb61a6SAndreas Gohr        return $this->type->getTid();
631c502704SAndreas Gohr    }
641c502704SAndreas Gohr
651c502704SAndreas Gohr    /**
6690ae6c2dSAndreas Gohr     * @return string
6790ae6c2dSAndreas Gohr     */
68*d6d97f60SAnna Dabrowska    public function getLabel()
69*d6d97f60SAnna Dabrowska    {
7090ae6c2dSAndreas Gohr        return $this->type->getLabel();
7190ae6c2dSAndreas Gohr    }
7290ae6c2dSAndreas Gohr
7390ae6c2dSAndreas Gohr    /**
7400f6af48SAndreas Gohr     * @return string the label prepended with the table name
7500f6af48SAndreas Gohr     */
76*d6d97f60SAnna Dabrowska    public function getFullQualifiedLabel()
77*d6d97f60SAnna Dabrowska    {
7800f6af48SAndreas Gohr        if (!$this->table) throw new StructException('No table set for this column');
7900f6af48SAndreas Gohr        return $this->table . '.' . $this->getLabel();
8000f6af48SAndreas Gohr    }
8100f6af48SAndreas Gohr
8200f6af48SAndreas Gohr    /**
839e9bee91SAndreas Gohr     * @return string
849e9bee91SAndreas Gohr     */
85*d6d97f60SAnna Dabrowska    public function getTranslatedLabel()
86*d6d97f60SAnna Dabrowska    {
879e9bee91SAndreas Gohr        return $this->type->getTranslatedLabel();
889e9bee91SAndreas Gohr    }
899e9bee91SAndreas Gohr
909e9bee91SAndreas Gohr    /**
913a717675SAndreas Gohr     * @return string
923a717675SAndreas Gohr     */
93*d6d97f60SAnna Dabrowska    public function getTranslatedHint()
94*d6d97f60SAnna Dabrowska    {
953a717675SAndreas Gohr        return $this->type->getTranslatedHint();
963a717675SAndreas Gohr    }
973a717675SAndreas Gohr
983a717675SAndreas Gohr    /**
991c502704SAndreas Gohr     * @return AbstractBaseType
1001c502704SAndreas Gohr     */
101*d6d97f60SAnna Dabrowska    public function getType()
102*d6d97f60SAnna Dabrowska    {
1031c502704SAndreas Gohr        return $this->type;
1041c502704SAndreas Gohr    }
1051c502704SAndreas Gohr
1061c502704SAndreas Gohr    /**
1071c502704SAndreas Gohr     * @return int
1081c502704SAndreas Gohr     */
109*d6d97f60SAnna Dabrowska    public function getColref()
110*d6d97f60SAnna Dabrowska    {
1111c502704SAndreas Gohr        return $this->colref;
1121c502704SAndreas Gohr    }
1131c502704SAndreas Gohr
1141c502704SAndreas Gohr    /**
11527f6439fSAndreas Gohr     * Returns the column name (without a table)
116d1b04e89SAndreas Gohr     *
1175a11127fSAndreas Gohr     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
118d1b04e89SAndreas Gohr     * @return string
119d1b04e89SAndreas Gohr     */
120*d6d97f60SAnna Dabrowska    public function getColName($enforceSingleColumn = true)
121*d6d97f60SAnna Dabrowska    {
1225a11127fSAndreas Gohr        if ($enforceSingleColumn && $this->isMulti()) 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     */
132*d6d97f60SAnna Dabrowska    public function getFullColName($enforceSingleColumn = true)
133*d6d97f60SAnna 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     */
142*d6d97f60SAnna Dabrowska    public function isEnabled()
143*d6d97f60SAnna Dabrowska    {
1441c502704SAndreas Gohr        return $this->enabled;
1451c502704SAndreas Gohr    }
1461c502704SAndreas Gohr
147ae697e1fSAndreas Gohr    /**
14863d51bbfSAndreas Gohr     * @return string
14963d51bbfSAndreas Gohr     */
150*d6d97f60SAnna Dabrowska    public function getTable()
151*d6d97f60SAnna Dabrowska    {
15263d51bbfSAndreas Gohr        return $this->table;
15363d51bbfSAndreas Gohr    }
15463d51bbfSAndreas Gohr
15563d51bbfSAndreas Gohr    /**
15690ae6c2dSAndreas Gohr     * @return bool
15790ae6c2dSAndreas Gohr     */
158*d6d97f60SAnna Dabrowska    public function isMulti()
159*d6d97f60SAnna Dabrowska    {
16090ae6c2dSAndreas Gohr        return $this->type->isMulti();
16190ae6c2dSAndreas Gohr    }
16290ae6c2dSAndreas Gohr
16390ae6c2dSAndreas Gohr    /**
1642350b48eSAndreas Gohr     * @return bool
1652350b48eSAndreas Gohr     */
166*d6d97f60SAnna Dabrowska    public function isVisibleInEditor()
167*d6d97f60SAnna Dabrowska    {
1682350b48eSAndreas Gohr        return $this->getType()->isVisibleInEditor();
1692350b48eSAndreas Gohr    }
1702350b48eSAndreas Gohr
1712350b48eSAndreas Gohr    /**
1722350b48eSAndreas Gohr     * @return bool
1732350b48eSAndreas Gohr     */
174*d6d97f60SAnna Dabrowska    public function isVisibleInPage()
175*d6d97f60SAnna 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     */
185*d6d97f60SAnna Dabrowska    public static function allTypes($reload = false)
186*d6d97f60SAnna 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