xref: /plugin/struct/meta/Column.php (revision 9ebd2ed6a41b61c3850117c96b169a0b46abd470)
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 */
181c502704SAndreas Gohrclass Column {
191c502704SAndreas Gohr
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     */
3963d51bbfSAndreas Gohr    public function __construct($sort, AbstractBaseType $type, $colref=0, $enabled=true, $table='') {
401c502704SAndreas Gohr        $this->sort = (int) $sort;
411c502704SAndreas Gohr        $this->type = $type;
421c502704SAndreas Gohr        $this->colref = (int) $colref;
431c502704SAndreas Gohr        $this->enabled = (bool) $enabled;
4463d51bbfSAndreas Gohr        $this->table = $table;
451c502704SAndreas Gohr    }
461c502704SAndreas Gohr
471c502704SAndreas Gohr    /**
481c502704SAndreas Gohr     * @return int
491c502704SAndreas Gohr     */
501c502704SAndreas Gohr    public function getSort() {
511c502704SAndreas Gohr        return $this->sort;
521c502704SAndreas Gohr    }
531c502704SAndreas Gohr
541c502704SAndreas Gohr    /**
551c502704SAndreas Gohr     * @return int
561c502704SAndreas Gohr     */
571c502704SAndreas Gohr    public function getTid() {
5804eb61a6SAndreas Gohr        return $this->type->getTid();
591c502704SAndreas Gohr    }
601c502704SAndreas Gohr
611c502704SAndreas Gohr    /**
6290ae6c2dSAndreas Gohr     * @return string
6390ae6c2dSAndreas Gohr     */
6490ae6c2dSAndreas Gohr    public function getLabel() {
6590ae6c2dSAndreas Gohr        return $this->type->getLabel();
6690ae6c2dSAndreas Gohr    }
6790ae6c2dSAndreas Gohr
6890ae6c2dSAndreas Gohr    /**
6900f6af48SAndreas Gohr     * @return string the label prepended with the table name
7000f6af48SAndreas Gohr     */
7100f6af48SAndreas Gohr    public function getFullQualifiedLabel() {
7200f6af48SAndreas Gohr        if(!$this->table) throw new StructException('No table set for this column');
7300f6af48SAndreas Gohr        return $this->table .'.'. $this->getLabel();
7400f6af48SAndreas Gohr    }
7500f6af48SAndreas Gohr
7600f6af48SAndreas Gohr    /**
779e9bee91SAndreas Gohr     * @return string
789e9bee91SAndreas Gohr     */
799e9bee91SAndreas Gohr    public function getTranslatedLabel() {
809e9bee91SAndreas Gohr        return $this->type->getTranslatedLabel();
819e9bee91SAndreas Gohr    }
829e9bee91SAndreas Gohr
839e9bee91SAndreas Gohr    /**
843a717675SAndreas Gohr     * @return string
853a717675SAndreas Gohr     */
863a717675SAndreas Gohr    public function getTranslatedHint() {
873a717675SAndreas Gohr        return $this->type->getTranslatedHint();
883a717675SAndreas Gohr    }
893a717675SAndreas Gohr
903a717675SAndreas Gohr    /**
911c502704SAndreas Gohr     * @return AbstractBaseType
921c502704SAndreas Gohr     */
931c502704SAndreas Gohr    public function getType() {
941c502704SAndreas Gohr        return $this->type;
951c502704SAndreas Gohr    }
961c502704SAndreas Gohr
971c502704SAndreas Gohr    /**
981c502704SAndreas Gohr     * @return int
991c502704SAndreas Gohr     */
1001c502704SAndreas Gohr    public function getColref() {
1011c502704SAndreas Gohr        return $this->colref;
1021c502704SAndreas Gohr    }
1031c502704SAndreas Gohr
1041c502704SAndreas Gohr    /**
10527f6439fSAndreas Gohr     * Returns the column name (without a table)
106d1b04e89SAndreas Gohr     *
1075a11127fSAndreas Gohr     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
108d1b04e89SAndreas Gohr     * @return string
109d1b04e89SAndreas Gohr     */
1105a11127fSAndreas Gohr    public function getColName($enforceSingleColumn = true) {
1115a11127fSAndreas Gohr        if($enforceSingleColumn && $this->isMulti()) throw new StructException('Calling getColName on a multi value column makes no sense.');
11227f6439fSAndreas Gohr        return 'col'.$this->colref;
11327f6439fSAndreas Gohr    }
1145b2d2a8cSAndreas Gohr
11527f6439fSAndreas Gohr    /**
11627f6439fSAndreas Gohr     * Returns the full column name. When table is set, prefixed by the table name
11727f6439fSAndreas Gohr     *
1185a11127fSAndreas Gohr     * @param bool $enforceSingleColumn Throw an exception if $this is a multi column
11927f6439fSAndreas Gohr     * @return string
12027f6439fSAndreas Gohr     */
1215a11127fSAndreas Gohr    public function getFullColName($enforceSingleColumn = true) {
1225a11127fSAndreas Gohr        $col = $this->getColName($enforceSingleColumn);
1235b2d2a8cSAndreas Gohr        if($this->table) $col = 'data_'.$this->table.'.'.$col;
124d1b04e89SAndreas Gohr        return $col;
125d1b04e89SAndreas Gohr    }
126d1b04e89SAndreas Gohr
127d1b04e89SAndreas Gohr    /**
1281c502704SAndreas Gohr     * @return boolean
1291c502704SAndreas Gohr     */
1301c502704SAndreas Gohr    public function isEnabled() {
1311c502704SAndreas Gohr        return $this->enabled;
1321c502704SAndreas Gohr    }
1331c502704SAndreas Gohr
134ae697e1fSAndreas Gohr    /**
13563d51bbfSAndreas Gohr     * @return string
13663d51bbfSAndreas Gohr     */
13763d51bbfSAndreas Gohr    public function getTable() {
13863d51bbfSAndreas Gohr        return $this->table;
13963d51bbfSAndreas Gohr    }
14063d51bbfSAndreas Gohr
14163d51bbfSAndreas Gohr    /**
14290ae6c2dSAndreas Gohr     * @return bool
14390ae6c2dSAndreas Gohr     */
14490ae6c2dSAndreas Gohr    public function isMulti() {
14590ae6c2dSAndreas Gohr        return $this->type->isMulti();
14690ae6c2dSAndreas Gohr    }
14790ae6c2dSAndreas Gohr
14890ae6c2dSAndreas Gohr    /**
1492350b48eSAndreas Gohr     * @return bool
1502350b48eSAndreas Gohr     */
1512350b48eSAndreas Gohr    public function isVisibleInEditor() {
1522350b48eSAndreas Gohr        return $this->getType()->isVisibleInEditor();
1532350b48eSAndreas Gohr    }
1542350b48eSAndreas Gohr
1552350b48eSAndreas Gohr    /**
1562350b48eSAndreas Gohr     * @return bool
1572350b48eSAndreas Gohr     */
1582350b48eSAndreas Gohr    public function isVisibleInPage() {
1592350b48eSAndreas Gohr        return $this->getType()->isVisibleInPage();
1602350b48eSAndreas Gohr    }
1612350b48eSAndreas Gohr
1622350b48eSAndreas Gohr    /**
163636c8abaSAndreas Gohr     * Returns a list of all available types and their class names
164ae697e1fSAndreas Gohr     *
165636c8abaSAndreas Gohr     * @param bool $reload forces reloading the types
166ae697e1fSAndreas Gohr     * @return array
167ae697e1fSAndreas Gohr     */
168636c8abaSAndreas Gohr    static public function allTypes($reload=false) {
169636c8abaSAndreas Gohr        static $map = null;
170636c8abaSAndreas Gohr        if(!is_null($map) && !$reload) return $map;
171636c8abaSAndreas Gohr
172636c8abaSAndreas Gohr        // get our own types
173636c8abaSAndreas Gohr        $map = array();
174ae697e1fSAndreas Gohr        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
175ae697e1fSAndreas Gohr        foreach($files as $file) {
176ae697e1fSAndreas Gohr            $file = basename($file, '.php');
177ae697e1fSAndreas Gohr            if(substr($file, 0, 8) == 'Abstract') continue;
178*9ebd2ed6SAndreas Gohr            if(substr($file, 0, 5) == 'Trait') continue;
179636c8abaSAndreas Gohr            $map[$file] = 'dokuwiki\\plugin\\struct\\types\\' .$file;
180ae697e1fSAndreas Gohr        }
1811c502704SAndreas Gohr
182636c8abaSAndreas Gohr        // let plugins add their own
183636c8abaSAndreas Gohr        trigger_event('PLUGIN_STRUCT_TYPECLASS_INIT', $map);
184636c8abaSAndreas Gohr
185636c8abaSAndreas Gohr        ksort($map);
186636c8abaSAndreas Gohr        return $map;
187ae697e1fSAndreas Gohr    }
1881c502704SAndreas Gohr
1891c502704SAndreas Gohr}
190