-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModel.php
101 lines (86 loc) · 2.76 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Copyright (c) 2018. Alexandr Kosarev, @kosarev.by
*/
namespace Joomplace\X;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Input\Input;
use Joomplace\X\Helper\ACL\Dummy;
class Model extends BaseModel
{
use Dummy;
public $timestamps = false;
protected $defaults = [];
protected $placeholders = [];
protected $labels = [];
public function __construct(array $attributes = [])
{
Loader::bootDatabase();
parent::__construct($attributes);
}
public function getTable()
{
if (isset($this->table)) {
return str_replace('#__', '', $this->table);
} else {
return parent::getTable();
}
}
public function getName(){
return (new \ReflectionClass($this))->getShortName();
}
public function getDefaults(){
if(!$this->defaults){
/** @var \Illuminate\Database\Connection $connection */
$connection = $this->getConnection();
$table = $connection->getTablePrefix() . $this->getTable();
/** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $schema */
$schema = $connection->getDoctrineSchemaManager();
$columns = $schema->listTableColumns($table);
foreach ($this->fillable as $fillable){
$this->defaults[$fillable] = $columns[$fillable]->getDefault();
}
}
return $this->defaults;
}
public function getDefaultFor($column){
return $this->getDefaults()[$column];
}
public function getPlaceholders(){
if(!$this->placeholders){
$table = strtoupper($this->getTable());
foreach ($this->getDefaults() as $c => $default){
$this->placeholders[$c] = Text::_('MODEL.'.$table.'_PLACEHOLDER_'.strtoupper($c));
}
}
return $this->placeholders;
}
public function getPlaceholderFor($column){
return $this->getPlaceholders()[$column];
}
public function getLabels(){
if(!$this->labels){
$table = strtoupper($this->getTable());
foreach ($this->getDefaults() as $c => $default){
$this->labels[$c] = Text::_('MODEL.'.$table.'_LABEL_'.strtoupper($c));
}
}
return $this->labels;
}
public function getLabelFor($column){
return $this->getLabels()[$column];
}
public static function getFillFromInput(Input $input){
$model = new static();
$input = $input->getArray();
$fill = [];
foreach ($input as $key => $value){
if($model->isFillable($key)){
$fill[$key] = $input[$key];
}
}
return $fill;
}
}