xref: /plugin/struct/meta/Column.php (revision ba662a609884bbbecde8cffec91014be306b652b)
11c502704SAndreas Gohr<?php
21c502704SAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
41c502704SAndreas Gohr
5a91bbca2SAndreas Gohruse dokuwiki\Extension\Event;
6ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\AbstractBaseType;
71c502704SAndreas Gohr
81c502704SAndreas Gohr/**
91c502704SAndreas Gohr * Class Column
101c502704SAndreas Gohr *
111c502704SAndreas Gohr * This represents a single column within a schema and contains the configured BaseType as well as the
127182938bSAndreas Gohr * column reference to the data table.
137182938bSAndreas Gohr *
147182938bSAndreas Gohr * It basically combines the information how a column's content behaves (as defines in the BaseType and its
157182938bSAndreas Gohr * configuration) with where to find that content and adds some basic meta data (like sort or enabled)
161c502704SAndreas Gohr *
17ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
181c502704SAndreas Gohr */
19d6d97f60SAnna Dabrowskaclass Column
20d6d97f60SAnna Dabrowska{
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     */
40d6d97f60SAnna Dabrowska    public function __construct($sort, AbstractBaseType $type, $colref = 0, $enabled = true, $table = '')
41d6d97f60SAnna 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     */
52d6d97f60SAnna Dabrowska    public function getSort()
53d6d97f60SAnna Dabrowska    {
541c502704SAndreas Gohr        return $this->sort;
551c502704SAndreas Gohr    }
561c502704SAndreas Gohr
571c502704SAndreas Gohr    /**
581c502704SAndreas Gohr     * @return int
591c502704SAndreas Gohr     */
60d6d97f60SAnna Dabrowska    public function getTid()
61d6d97f60SAnna Dabrowska    {
6204eb61a6SAndreas Gohr        return $this->type->getTid();
631c502704SAndreas Gohr    }
641c502704SAndreas Gohr
651c502704SAndreas Gohr    /**
6690ae6c2dSAndreas Gohr     * @return string
6790ae6c2dSAndreas Gohr     */
68d6d97f60SAnna Dabrowska    public function getLabel()
69d6d97f60SAnna 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     */
76d6d97f60SAnna Dabrowska    public function getFullQualifiedLabel()
77d6d97f60SAnna 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     */
85d6d97f60SAnna Dabrowska    public function getTranslatedLabel()
86d6d97f60SAnna Dabrowska    {
879e9bee91SAndreas Gohr        return $this->type->getTranslatedLabel();
889e9bee91SAndreas Gohr    }
899e9bee91SAndreas Gohr
909e9bee91SAndreas Gohr    /**
913a717675SAndreas Gohr     * @return string
923a717675SAndreas Gohr     */
93d6d97f60SAnna Dabrowska    public function getTranslatedHint()
94d6d97f60SAnna Dabrowska    {
953a717675SAndreas Gohr        return $this->type->getTranslatedHint();
963a717675SAndreas Gohr    }
973a717675SAndreas Gohr
983a717675SAndreas Gohr    /**
991c502704SAndreas Gohr     * @return AbstractBaseType
1001c502704SAndreas Gohr     */
101d6d97f60SAnna Dabrowska    public function getType()
102d6d97f60SAnna Dabrowska    {
1031c502704SAndreas Gohr        return $this->type;
1041c502704SAndreas Gohr    }
1051c502704SAndreas Gohr
1061c502704SAndreas Gohr    /**
1071c502704SAndreas Gohr     * @return int
1081c502704SAndreas Gohr     */
109d6d97f60SAnna Dabrowska    public function getColref()
110d6d97f60SAnna 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     */
120d6d97f60SAnna Dabrowska    public function getColName($enforceSingleColumn = true)
121d6d97f60SAnna Dabrowska    {
12217a3a578SAndreas Gohr        if ($enforceSingleColumn && $this->isMulti())
12317a3a578SAndreas Gohr            throw new StructException('Calling getColName on a multi value column makes no sense.');
12427f6439fSAndreas Gohr        return 'col' . $this->colref;
12527f6439fSAndreas Gohr    }
1265b2d2a8cSAndreas Gohr
12727f6439fSAndreas Gohr    /**
12827f6439fSAndreas Gohr     * Returns the full column name. When table is set, prefixed by the table name
12927f6439fSAndreas Gohr     *
1305a11127fSAndreas Gohr     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
13127f6439fSAndreas Gohr     * @return string
13227f6439fSAndreas Gohr     */
133d6d97f60SAnna Dabrowska    public function getFullColName($enforceSingleColumn = true)
134d6d97f60SAnna Dabrowska    {
1355a11127fSAndreas Gohr        $col = $this->getColName($enforceSingleColumn);
1365b2d2a8cSAndreas Gohr        if ($this->table) $col = 'data_' . $this->table . '.' . $col;
137d1b04e89SAndreas Gohr        return $col;
138d1b04e89SAndreas Gohr    }
139d1b04e89SAndreas Gohr
140d1b04e89SAndreas Gohr    /**
1411c502704SAndreas Gohr     * @return boolean
1421c502704SAndreas Gohr     */
143d6d97f60SAnna Dabrowska    public function isEnabled()
144d6d97f60SAnna Dabrowska    {
1451c502704SAndreas Gohr        return $this->enabled;
1461c502704SAndreas Gohr    }
1471c502704SAndreas Gohr
148ae697e1fSAndreas Gohr    /**
14963d51bbfSAndreas Gohr     * @return string
15063d51bbfSAndreas Gohr     */
151d6d97f60SAnna Dabrowska    public function getTable()
152d6d97f60SAnna Dabrowska    {
15363d51bbfSAndreas Gohr        return $this->table;
15463d51bbfSAndreas Gohr    }
15563d51bbfSAndreas Gohr
15663d51bbfSAndreas Gohr    /**
15790ae6c2dSAndreas Gohr     * @return bool
15890ae6c2dSAndreas Gohr     */
159d6d97f60SAnna Dabrowska    public function isMulti()
160d6d97f60SAnna Dabrowska    {
16190ae6c2dSAndreas Gohr        return $this->type->isMulti();
16290ae6c2dSAndreas Gohr    }
16390ae6c2dSAndreas Gohr
16490ae6c2dSAndreas Gohr    /**
1652350b48eSAndreas Gohr     * @return bool
1662350b48eSAndreas Gohr     */
167d6d97f60SAnna Dabrowska    public function isVisibleInEditor()
168d6d97f60SAnna Dabrowska    {
1692350b48eSAndreas Gohr        return $this->getType()->isVisibleInEditor();
1702350b48eSAndreas Gohr    }
1712350b48eSAndreas Gohr
1722350b48eSAndreas Gohr    /**
1732350b48eSAndreas Gohr     * @return bool
1742350b48eSAndreas Gohr     */
175d6d97f60SAnna Dabrowska    public function isVisibleInPage()
176d6d97f60SAnna Dabrowska    {
1772350b48eSAndreas Gohr        return $this->getType()->isVisibleInPage();
1782350b48eSAndreas Gohr    }
1792350b48eSAndreas Gohr
1802350b48eSAndreas Gohr    /**
181636c8abaSAndreas Gohr     * Returns a list of all available types and their class names
182ae697e1fSAndreas Gohr     *
183636c8abaSAndreas Gohr     * @param bool $reload forces reloading the types
184ae697e1fSAndreas Gohr     * @return array
185ae697e1fSAndreas Gohr     */
186d6d97f60SAnna Dabrowska    public static function allTypes($reload = false)
187d6d97f60SAnna Dabrowska    {
188636c8abaSAndreas Gohr        static $map = null;
189636c8abaSAndreas Gohr        if (!is_null($map) && !$reload) return $map;
190636c8abaSAndreas Gohr
191636c8abaSAndreas Gohr        // get our own types
1927234bfb1Ssplitbrain        $map = [];
193ae697e1fSAndreas Gohr        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
194ae697e1fSAndreas Gohr        foreach ($files as $file) {
195ae697e1fSAndreas Gohr            $file = basename($file, '.php');
196*ba662a60SAndreas Gohr            if (str_starts_with($file, 'Abstract')) continue;
197*ba662a60SAndreas Gohr            if (str_starts_with($file, 'Trait')) continue;
198*ba662a60SAndreas Gohr            if (str_starts_with($file, 'Auto')) continue;
199636c8abaSAndreas Gohr            $map[$file] = 'dokuwiki\\plugin\\struct\\types\\' . $file;
200ae697e1fSAndreas Gohr        }
2011c502704SAndreas Gohr
202636c8abaSAndreas Gohr        // let plugins add their own
203a91bbca2SAndreas Gohr        Event::createAndTrigger('PLUGIN_STRUCT_TYPECLASS_INIT', $map);
204636c8abaSAndreas Gohr
205636c8abaSAndreas Gohr        ksort($map);
206636c8abaSAndreas Gohr        return $map;
207ae697e1fSAndreas Gohr    }
2081c502704SAndreas Gohr}
209