xref: /plugin/struct/meta/Column.php (revision 9e9bee91898d9164e48494e09d8110b0d9ef2e99)
11c502704SAndreas Gohr<?php
21c502704SAndreas Gohr
31c502704SAndreas Gohrnamespace plugin\struct\meta;
41c502704SAndreas Gohr
51c502704SAndreas Gohruse 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 *
161c502704SAndreas Gohr * @package 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    /**
69*9e9bee91SAndreas Gohr     * @return string
70*9e9bee91SAndreas Gohr     */
71*9e9bee91SAndreas Gohr    public function getTranslatedLabel() {
72*9e9bee91SAndreas Gohr        return $this->type->getTranslatedLabel();
73*9e9bee91SAndreas Gohr    }
74*9e9bee91SAndreas Gohr
75*9e9bee91SAndreas Gohr    /**
761c502704SAndreas Gohr     * @return AbstractBaseType
771c502704SAndreas Gohr     */
781c502704SAndreas Gohr    public function getType() {
791c502704SAndreas Gohr        return $this->type;
801c502704SAndreas Gohr    }
811c502704SAndreas Gohr
821c502704SAndreas Gohr    /**
831c502704SAndreas Gohr     * @return int
841c502704SAndreas Gohr     */
851c502704SAndreas Gohr    public function getColref() {
861c502704SAndreas Gohr        return $this->colref;
871c502704SAndreas Gohr    }
881c502704SAndreas Gohr
891c502704SAndreas Gohr    /**
90d1b04e89SAndreas Gohr     * Returns the full column name. When table is set, prefixed by the table name
91d1b04e89SAndreas Gohr     *
92d1b04e89SAndreas Gohr     * @return string
93d1b04e89SAndreas Gohr     */
94d1b04e89SAndreas Gohr    public function getColName() {
955b2d2a8cSAndreas Gohr        if($this->isMulti()) throw new StructException('Calling getColName on a multi value column makes no sense.');
965b2d2a8cSAndreas Gohr
97d1b04e89SAndreas Gohr        $col = 'col'.$this->colref;
985b2d2a8cSAndreas Gohr        if($this->table) $col = 'data_'.$this->table.'.'.$col;
99d1b04e89SAndreas Gohr        return $col;
100d1b04e89SAndreas Gohr    }
101d1b04e89SAndreas Gohr
102d1b04e89SAndreas Gohr    /**
1031c502704SAndreas Gohr     * @return boolean
1041c502704SAndreas Gohr     */
1051c502704SAndreas Gohr    public function isEnabled() {
1061c502704SAndreas Gohr        return $this->enabled;
1071c502704SAndreas Gohr    }
1081c502704SAndreas Gohr
109ae697e1fSAndreas Gohr    /**
11063d51bbfSAndreas Gohr     * @return string
11163d51bbfSAndreas Gohr     */
11263d51bbfSAndreas Gohr    public function getTable() {
11363d51bbfSAndreas Gohr        return $this->table;
11463d51bbfSAndreas Gohr    }
11563d51bbfSAndreas Gohr
11663d51bbfSAndreas Gohr    /**
11790ae6c2dSAndreas Gohr     * @return bool
11890ae6c2dSAndreas Gohr     */
11990ae6c2dSAndreas Gohr    public function isMulti() {
12090ae6c2dSAndreas Gohr        return $this->type->isMulti();
12190ae6c2dSAndreas Gohr    }
12290ae6c2dSAndreas Gohr
12390ae6c2dSAndreas Gohr    /**
124ae697e1fSAndreas Gohr     * Returns a list of all available types
125ae697e1fSAndreas Gohr     *
126ae697e1fSAndreas Gohr     * @return array
127ae697e1fSAndreas Gohr     */
128ae697e1fSAndreas Gohr    static public function allTypes() {
129ae697e1fSAndreas Gohr        $types = array();
130ae697e1fSAndreas Gohr        $files = glob(DOKU_PLUGIN . 'struct/types/*.php');
131ae697e1fSAndreas Gohr        foreach($files as $file) {
132ae697e1fSAndreas Gohr            $file = basename($file, '.php');
133ae697e1fSAndreas Gohr            if(substr($file, 0, 8) == 'Abstract') continue;
134ae697e1fSAndreas Gohr            $types[] = $file;
135ae697e1fSAndreas Gohr        }
136ae697e1fSAndreas Gohr        sort($types);
1371c502704SAndreas Gohr
138ae697e1fSAndreas Gohr        return $types;
139ae697e1fSAndreas Gohr    }
1401c502704SAndreas Gohr
1411c502704SAndreas Gohr}
142