256 lines
13 KiB
PHP
256 lines
13 KiB
PHP
<?
|
||
|
||
namespace core\db\structure;
|
||
|
||
use ui\form\structure\eInputType;
|
||
use ui\input\Text;
|
||
|
||
class Column {
|
||
|
||
const NAME = 1;
|
||
const TYPE = 2;
|
||
const AUTO_INCREMENT = 3;
|
||
const DEFAULT = 4;
|
||
const LENGTH = 5;
|
||
const NULL = 6;
|
||
const PRIMARY = 7;
|
||
const COL_DEFAULT = 8;
|
||
const VAR_NAME = 9;
|
||
const UNIQUE = 10;
|
||
const TH = 11;
|
||
const FUNC_VALUE = 12;
|
||
const HIDDEN = 13; // внутренний столбец false | true, типа позиция, или указание на другую сущность
|
||
const REAL_NAME = 14; // Нужно для сортировки при leftjoin и др случаях
|
||
const REAL_TYPE = 15; // Нужно для фильтров
|
||
const NO_SORT = 16; // По умолчанию по столбцу можно сортировать. Для некоторых это надо выключать
|
||
const FILTER_INPUT = 17; // поле ввода для фильтрации для таблиц
|
||
const FILTER_COLUMNS = 18; // столбцы по которым на самом деле должен искать фильтр
|
||
const FUNC_FILTER_QUERY = 19; // функция подзапроса для фильтра, результат функции желательно должен быть Where::inQueryExecute
|
||
const FUNC_VALUE_DATA = 20; // функция для возвращения только даты для отрисовки на фронтенде
|
||
const TD_CLASS = 21; // дополнительные классы для ячейки
|
||
const FUNC_TD_INPUT = 22; // функция предоставления данных о поле ввода для ячейки
|
||
const TOTAL_FUNCTION = 23; // функция для подсчёта "итого" по столбцу
|
||
const FILTER_TYPE = 24; // Тип фильтра
|
||
const FUNC_FILTER_SELECT_OPTIONS = 25; //
|
||
const EXPORT = 26; // Можно ли экспортировать этот столбец
|
||
const TH_CLASS = 27; // Класс для TH
|
||
const FUNC_EXPORT_DATA = 28; // Функция для экспорта данных
|
||
//const TO_TABLE_FILTER = 20;
|
||
|
||
protected $name = '';
|
||
protected $real_name = null;
|
||
protected $type = null;
|
||
protected $real_type = null;
|
||
protected $length = null;
|
||
protected $null = false;
|
||
protected $default = null;
|
||
protected $col_default = null;
|
||
protected $primary = false;
|
||
protected $auto_increment = false;
|
||
protected $unique = false;
|
||
protected $th = '';
|
||
protected $options = [];
|
||
protected $func_value = null;
|
||
protected $func_value_data = null;
|
||
protected $func_export_data = null;
|
||
protected $hidden = false;
|
||
protected $is_sort = true;
|
||
protected $filter_input = null;
|
||
protected $filter_columns = null;
|
||
protected $func_filter_query = null;
|
||
protected $td_class = null;
|
||
protected $th_class = null;
|
||
protected $func_td_input = null;
|
||
protected $filter_type = null;
|
||
protected $func_filter_select_options = null;
|
||
public $total_function = null;
|
||
public $export = true;
|
||
|
||
|
||
function __construct( array $options ) {
|
||
$this->name = isset($options[$c = self::NAME]) ? (string)$options[$c] : '';
|
||
$this->real_name = isset($options[$c = self::REAL_NAME]) ? $options[$c] : null;
|
||
$this->type = isset($options[$c = self::TYPE]) ? (int)$options[$c] : '';
|
||
$this->real_type = isset($options[$c = self::REAL_TYPE]) ? (int)$options[$c] : '';
|
||
$this->length = isset($options[$c = self::LENGTH]) ? $options[$c] : null;
|
||
$this->null = isset($options[$c = self::NULL]) ? boolval($options[$c]) : '';
|
||
$this->auto_increment = isset($options[$c = self::AUTO_INCREMENT]) ? boolval($options[$c]) : '';
|
||
$this->primary = isset($options[$c = self::PRIMARY]) ? boolval($options[$c]) : '';
|
||
$this->col_default = isset($options[$c = self::COL_DEFAULT]) ? (string)$options[$c] : null;
|
||
$this->default = isset($options[$c = self::DEFAULT]) ? (string)$options[$c] : null;
|
||
$this->unique = isset($options[$c = self::UNIQUE]) ? boolval($options[$c]) : null;
|
||
$this->th = isset($options[$c = self::TH]) ? $options[$c] : null;
|
||
$this->func_value = isset($options[$c = self::FUNC_VALUE]) ? $options[$c] : null;
|
||
$this->func_value_data = isset($options[$c = self::FUNC_VALUE_DATA]) ? $options[$c] : null;
|
||
$this->hidden = isset($options[$c = self::HIDDEN]) ? boolval($options[$c]) : null;
|
||
$this->is_sort = isset($options[$c = self::NO_SORT]) ? !boolval($options[$c]) : false;
|
||
$this->filter_input = isset($options[$c = self::FILTER_INPUT]) ? $options[$c] : 'default';
|
||
$this->filter_columns = isset($options[$c = self::FILTER_COLUMNS]) ? $options[$c] : [];
|
||
$this->func_filter_query = isset($options[$c = self::FUNC_FILTER_QUERY]) ? $options[$c] : null;
|
||
$this->td_class = isset($options[$c = self::TD_CLASS]) ? $options[$c] : null;
|
||
$this->th_class = isset($options[$c = self::TH_CLASS]) ? $options[$c] : null;
|
||
$this->func_td_input = isset($options[$c = self::FUNC_TD_INPUT]) ? $options[$c] : null;
|
||
$this->total_function = isset($options[$c = self::TOTAL_FUNCTION]) ? $options[$c] : null;
|
||
$this->filter_type = isset($options[$c = self::FILTER_TYPE]) ? $options[$c] : null;
|
||
$this->func_filter_select_options = isset($options[$c = self::FUNC_FILTER_SELECT_OPTIONS]) ? $options[$c] : null;
|
||
$this->func_export_data = isset($options[$c = self::FUNC_EXPORT_DATA]) ? $options[$c] : null;
|
||
$this->export = isset($options[$c = self::EXPORT]) ? $options[$c] : true;
|
||
//$this->to_table_filter = isset($options[$c = self::TO_TABLE_FILTER]) ? $options[$c] : null;
|
||
$this->options = $options;
|
||
|
||
if(isset($options[$c = self::VAR_NAME])) {
|
||
$this->setName( (string)$options[$c] );
|
||
}
|
||
}
|
||
|
||
public function getValue($object=null){
|
||
if($this->func_value!=null){
|
||
$x = $this->func_value;
|
||
return $x($object);
|
||
} else { return (h( isset($object[$this->name]) ? $object[$this->name] : '' ));}
|
||
}
|
||
public function getValueData($object=null){
|
||
if($this->func_value_data!=null){
|
||
$x = $this->func_value_data;
|
||
return $x($object);
|
||
} else { return (( isset($object[$this->name]) ? $object[$this->name] : '' ));}
|
||
}
|
||
public function getExportData($object=null){
|
||
if($this->func_export_data!=null){
|
||
$x = $this->func_export_data;
|
||
return $x($object);
|
||
} else {
|
||
return $this->getValueData($object);
|
||
}
|
||
}
|
||
|
||
public function getFilterSelectOptions(){
|
||
if(get_class($this->func_filter_select_options)=="Closure"){
|
||
$x = $this->func_filter_select_options;
|
||
$res = $x();
|
||
} else {
|
||
$res = [];
|
||
}
|
||
return $res;
|
||
}
|
||
public function getFilterType(){return $this->filter_type;}
|
||
public function getType(){return $this->type;}
|
||
public function getName(){return $this->name;}
|
||
public function getRealName(){
|
||
$res = $this->name;
|
||
if(get_class($this->real_name)=="Closure"){
|
||
$x = $this->real_name;
|
||
$res = $x();
|
||
} else if ($c = $this->real_name){
|
||
$res = $c;
|
||
}
|
||
return $res;
|
||
}
|
||
public function getRealType(){return ($c = $this->real_type)?$c:$this->type;}
|
||
public function getFuctInput(){return ($c = $this->filter_input)?$c:$this->filter_input;}
|
||
public function getTableFilter(){return ($c = $this->to_table_filter)?$c:$this->to_table_filter;}
|
||
public function getTdClass(){return ($c = $this->td_class)?$c:'';}
|
||
public function getThClass(){return ($c = $this->th_class)?$c:'';}
|
||
public function getTh(){
|
||
if ($c = $this->th) {
|
||
if(get_class($this->th)=="Closure"){
|
||
$x = $this->th;
|
||
return $x();
|
||
} else {
|
||
return $this->th;
|
||
}
|
||
} else return $this->getName();
|
||
}
|
||
public function isHidden(){return $this->hidden;}
|
||
public function setName(string $name){
|
||
$this->options[self::VAR_NAME] = $name;
|
||
$this->name = $name;
|
||
}
|
||
public function getPrimary(){return $this->primary;}
|
||
public function getDefault(){return $this->default;}
|
||
public function getUnique(){return $this->unique;}
|
||
public function isSort(){return boolval($this->is_sort);}
|
||
public function isNull(){return boolval($this->null);}
|
||
|
||
public function getTdInput($v) {
|
||
if (!$this->func_td_input) return null;
|
||
$x = $this->func_td_input;
|
||
return $x($v);
|
||
}
|
||
public function getFilterQuery($query) {
|
||
if (!$this->func_filter_query) return null;
|
||
$x = $this->func_filter_query;
|
||
return $x($query);
|
||
}
|
||
public function getFilterInputData(){
|
||
if(!$this->filter_input) return null;
|
||
else return $this->filter_input;
|
||
}
|
||
public function getFilterInput($value){
|
||
if(!$this->filter_input) return null;
|
||
if($this->filter_input=='default'){
|
||
$x = $this->filter_columns;
|
||
$cols = 'data-columns="'.($x!=null?implode(",",$x($value)):'').'"';
|
||
switch ($this->getRealType()){
|
||
case eColumnType::DOUBLE:
|
||
case eColumnType::INT:
|
||
case eColumnType::TEXT:
|
||
case eColumnType::VARCHAR:
|
||
return '<input type="text" class="form-control"
|
||
'.$cols.'
|
||
name="'.$this->getRealName().'" placeholder="'.$this->getTh().'" value="'.h($value?$value:'').'">';
|
||
case eColumnType::DATE: // pickadate
|
||
$vv = explode("-",$value);
|
||
$value2 = $value;
|
||
if(count($vv)==2){
|
||
$value = $vv[0];
|
||
$value2 = $vv[1];
|
||
}
|
||
|
||
return '<input type="text" class="dateperiod '.(($this->type==eColumnType::INT)?'_to_int':'').'" autocomplete="off"
|
||
'.$cols.'
|
||
id="'.$this->getRealName().'"
|
||
name="'.$this->getRealName().'" placeholder="'.$this->getTh().'" value="'.h($value).'" style="width:1px;">'.
|
||
'<input id="'.$this->getRealName().'_end" placeholder="'.$this->getTh().'" value="'.h($value2).'" style="width:1px;">';
|
||
}
|
||
|
||
} else {
|
||
$x = $this->filter_input;
|
||
return $x($value);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
function toStringForCreateTable(){
|
||
|
||
$types = eColumnType::getInfo();
|
||
$type = $types[$this->type]['name'];
|
||
|
||
switch ($this->type) {
|
||
case eColumnType::DOUBLE:
|
||
if($this->length!=null) {
|
||
$type .= '(' . $this->length . ')';
|
||
}
|
||
break;
|
||
case eColumnType::INT:
|
||
case eColumnType::VARCHAR:
|
||
case eColumnType::TINYINT:
|
||
if($this->length===null) {
|
||
$this->length = $types[$this->type]['default_length'];
|
||
}
|
||
$type .= '('.$this->length.')';
|
||
break;
|
||
default:;
|
||
}
|
||
|
||
return "`".$this->name."` ".
|
||
$type." ".
|
||
($this->null ? '' : ' NOT ')." NULL ".
|
||
($this->auto_increment ? ' AUTO_INCREMENT ':'').
|
||
($this->col_default !== null ? "DEFAULT '".(string)$this->col_default."'" : '')
|
||
;
|
||
}
|
||
|
||
}
|