1
|
|
|
<?php namespace PascalKleindienst\FormListGenerator\Data; |
2
|
|
|
|
3
|
|
|
use PascalKleindienst\FormListGenerator\Support\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Columns for list generator |
7
|
|
|
* @package \PascalKleindienst\FormListGenerator\Data |
8
|
|
|
*/ |
9
|
|
|
class Column |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string List column name. |
13
|
|
|
*/ |
14
|
|
|
public $columnName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string List column label. |
18
|
|
|
*/ |
19
|
|
|
public $label; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string Display mode. Text, number |
23
|
|
|
*/ |
24
|
|
|
public $type = 'text'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var boolean Specifies whether a column is clickable or not |
28
|
|
|
*/ |
29
|
|
|
public $clickable = true; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string Specifies a default value when value is empty. |
33
|
|
|
*/ |
34
|
|
|
public $default; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string Specify a CSS class to attach to the list cell element. |
38
|
|
|
*/ |
39
|
|
|
public $cssClass; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string Specify a format or style for the column value, such as a Date. |
43
|
|
|
*/ |
44
|
|
|
public $format; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string Specifies a path for partial-type fields. |
48
|
|
|
*/ |
49
|
|
|
public $path; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array Raw field configuration. |
53
|
|
|
*/ |
54
|
|
|
public $config; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string Record Url |
58
|
|
|
*/ |
59
|
|
|
protected $recordUrl; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
* @param string $name |
64
|
|
|
* @param array $config |
65
|
|
|
* @param string $recordUrl |
66
|
|
|
*/ |
67
|
36 |
|
public function __construct($name, array $config, $recordUrl) |
68
|
|
|
{ |
69
|
36 |
|
$this->columnName = $name; |
70
|
36 |
|
$this->config = $config; |
71
|
36 |
|
$this->recordUrl = $recordUrl; |
72
|
36 |
|
$this->setup(); |
73
|
36 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Setup the column properties |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
36 |
|
protected function setup() |
81
|
|
|
{ |
82
|
|
|
// type |
83
|
36 |
|
$this->type = isset($this->config['type']) ? strtolower($this->config['type']) : 'text'; |
84
|
|
|
|
85
|
|
|
// save value of properties if they exist |
86
|
36 |
|
$configKeys = ['cssClass', 'default', 'format', 'path', 'label', 'clickable']; |
87
|
|
|
|
88
|
36 |
View Code Duplication |
foreach ($configKeys as $key) { |
|
|
|
|
89
|
36 |
|
if (isset($this->config[$key])) { |
90
|
18 |
|
$this->{$key} = $this->config[$key]; |
91
|
18 |
|
} |
92
|
36 |
|
} |
93
|
|
|
|
94
|
|
|
// save path |
95
|
36 |
|
$this->path = str_replace('~', Config::get('root'), $this->path); |
96
|
36 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the value for the column from the record |
100
|
|
|
* |
101
|
|
|
* @param array $record |
102
|
|
|
* @return void|string |
103
|
|
|
*/ |
104
|
21 |
|
public function getValue(array $record) |
105
|
|
|
{ |
106
|
|
|
// get value or default value if record does not exist |
107
|
21 |
|
$value = $this->default; |
108
|
21 |
|
if (array_key_exists($this->columnName, $record)) { |
109
|
18 |
|
$value = $record[$this->columnName]; |
110
|
18 |
|
} |
111
|
|
|
|
112
|
|
|
// format date |
113
|
21 |
|
if ($this->type === 'date') { |
114
|
9 |
|
$value = date($this->format, $value); |
115
|
9 |
|
} |
116
|
|
|
|
117
|
|
|
// format number |
118
|
21 |
|
if ($this->type === 'number') { |
119
|
9 |
|
$value = '<div class="text-right">' . $value . '</div>'; |
120
|
9 |
|
} |
121
|
|
|
|
122
|
|
|
// load partial |
123
|
21 |
|
if ($this->type === 'partial' && file_exists($this->path)) { |
124
|
3 |
|
include($this->path); |
125
|
3 |
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
18 |
|
return $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the record url |
133
|
|
|
* |
134
|
|
|
* @param array $record |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
9 |
|
public function getRecordUrl(array $record) |
138
|
|
|
{ |
139
|
|
|
// search patterns ala :id basend on record fields |
140
|
9 |
|
$search = array_map( |
141
|
9 |
|
function ($value) { |
142
|
9 |
|
return ":$value"; |
143
|
9 |
|
}, |
144
|
9 |
|
array_keys($record) |
145
|
9 |
|
); |
146
|
|
|
|
147
|
9 |
|
return Config::get('baseUrl', '') . str_replace($search, $record, $this->recordUrl); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.