Completed
Push — master ( 7d5873...47cb7d )
by Nuno
01:12
created

TableColumn::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Jupitern\Table;
3
4
Class TableColumn
5
{
6
	public $value;
7
8
	private $tableInstance;
9
	private $title;
10
	private $attrs;
11
	private $css;
12
	private $filter = false;
13
	private $filterData = null;
14
15
	/**
16
	 * create a new TableColumn instance
17
	 *
18
	 * @param Table $tableInstance
19
	 */
20
	public function __construct(Table &$tableInstance)
21
	{
22
		$this->tableInstance = $tableInstance;
23
		$this->attrs = ['th' => new Properties(), 'tr' => new Properties(), 'td' => new Properties()];
24
		$this->css = ['th' => new Properties(), 'tr' => new Properties(), 'td' => new Properties()];
25
	}
26
27
	public function __get( $prop )
28
    {
29
		return $this->$prop;
30
	}
31
32
	/**
33
	 * set column title
34
	 *
35
	 * @param $title
36
	 * @return $this
37
	 */
38
	public function title($title)
39
	{
40
		$this->title = $title;
41
42
		return $this;
43
	}
44
45
	/**
46
	 * bind colunm value. $value can be:
47
	 * integer index for none associative array or json
48
	 * string index for associative array, json, PDO or ORM result
49
	 * a closure that returns a string
50
	 *
51
	 * @param $value
52
	 * @return $this
53
	 */
54
	public function value($value)
55
	{
56
		$this->value = $value;
57
58
		return $this;
59
	}
60
61
	/**
62
	 * add a attribute to table <td> or <th>
63
	 *
64
	 * @param $elem
65
	 * @param $attr
66
	 * @param $value
67
	 * @return $this
68
	 */
69
	public function attr($elem, $attr, $value)
70
	{
71
		$this->attrs[$elem]->add($attr, $value);
72
73
		return $this;
74
	}
75
76
	/**
77
	 * add css to table <td> or <th>
78
	 *
79
     * @param $elem
80
	 * @param $attr
81
	 * @param $value
82
	 * @return $this
83
	 */
84
	public function css($elem, $attr, $value)
85
	{
86
		$this->css[$elem]->add($attr, $value);
87
88
		return $this;
89
	}
90
91
	/**
92
	 * add a filter to this column.
93
	 * $data can be array (associative or not), json, PDO or ORM result
94
	 *
95
	 * @param null $data
96
	 * @return $this
97
	 */
98
	public function filter($data = null)
99
	{
100
		$this->filterData = Table::isJson($data) ? json_decode($data) : $data;
101
		$this->filter = true;
102
		$this->tableInstance->hasFilters = true;
103
104
		return $this;
105
	}
106
107
	/**
108
	 * add this column to the table
109
	 *
110
	 * @return Table
111
	 */
112
	public function add()
113
	{
114
		return $this->tableInstance;
115
	}
116
117
	/**
118
	 * render column header cell <th>
119
	 *
120
	 * @return mixed
121
	 */
122
	public function renderHeader()
123
	{
124
		if ($this->title === false) {
125
			return "";
126
		}
127
		
128
		if (empty($this->title) && !$this->isCallable($this->value)) {
129
			if ($this->tableInstance->titlesMode == 'underscore') $this->title = $this->underscoreToTitle($this->value);
130
			elseif ($this->tableInstance->titlesMode == 'camelcase') $this->title = $this->camelToTitle($this->value);
131
		}
132
133
		$template = '<th {attrs} style="{css}">{title}</th>';
134
		$attrs = $this->attrs['th']->render('{prop}="{val}" ');
135
		$css = $this->css['th']->render('{prop}:{val}; ');
136
137
		return str_replace(['{attrs}', '{css}', '{title}'], [$attrs, $css, $this->title], $template);
138
	}
139
140
	/**
141
	 * render column filter
142
	 *
143
	 * @return string
144
	 */
145
	public function renderFilter()
146
	{
147
		$html = '';
148
		if ($this->filterData !== null) {
149
			$html .= '<select class="form-control input-sm" style="width: 99%"><option value=""></option>';
150
			foreach ($this->filterData as $option) {
151
				if (is_string($option)) {
152
					$option = [$option, $option];
153
				}
154
				elseif (is_object($option)) {
155
					$option = array_values(get_object_vars($option));
156
				}
157
				$html .= '<option value="'.$option[0].'">'.$option[1].'</option>';
158
			}
159
			$html .= '</select>';
160
		}
161
		elseif ($this->filter) {
162
			$html = '<input type="text" class="form-control input-sm"  style="width: 99%">';
163
		}
164
165
		return '<td>'.$html.'</td>';
166
	}
167
168
	/**
169
	 * render column body cell <td>
170
	 *
171
	 * @param $row
172
	 * @return mixed
173
	 */
174
	public function renderBody( &$row )
175
	{
176
		$template = '<td {attrs} style="{css}">{val}</td>';
177
		$attrs = $this->attrs['td']->render('{prop}="{val}" ');
178
		$css = $this->css['td']->render('{prop}:{val}; ');
179
180
		$val = "";
181
		if ($this->isCallable($this->value)) {
182
			$val = $this->value;
183
			$val = $val($row);
184
		}
185
		elseif (is_object($row)) {
186
			$val = $row->{$this->value};
187
		}
188
		elseif (is_array($row)) {
189
			$val = $this->value !== null ? $row[$this->value] : '';
190
		}
191
192
		return str_replace(['{attrs}','{css}','{title}','{val}'], [$attrs, $css, $this->title, $val], $template);
193
	}
194
195
196
	/**
197
	 * @param string $str
198
	 * @return mixed
199
	 */
200
	private function camelToTitle($str)
201
	{
202
		$intermediate = preg_replace('/(?!^)([[:upper:]][[:lower:]]+)/', ' $0', $str);
203
		$titleStr = preg_replace('/(?!^)([[:lower:]])([[:upper:]])/', '$1 $2', $intermediate);
204
205
		return $titleStr;
206
	}
207
208
209
	/**
210
	 * @param string $str
211
	 * @return string
212
	 */
213
	private function underscoreToTitle($str)
214
	{
215
		$str = ucwords(str_replace("_", " ", $str));
216
217
		return $str;
218
	}
219
    
220
    
221
    /**
222
     * @param string $var
223
     * @return boolean
224
     */
225
    private function isCallable($var)
226
    {
227
        return (!is_string($var) && is_callable($var)) || (is_object($var) && $var instanceof \Closure);
228
    }
229
	
230
}
231