forked from romansklenar/nette-datagrid
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NumericColumn.php
86 lines (71 loc) · 1.92 KB
/
NumericColumn.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
<?php
namespace DataGrid\Columns;
/**
* Representation of numeric data grid column.
*
* @author Roman Sklenář
* @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
* @license New BSD License
* @example http://addons.nette.org/datagrid
* @package Nette\Extras\DataGrid
*/
class NumericColumn extends Column
{
/** @var int */
public $precision;
/**
* Checkbox column constructor.
* @param string column's textual caption
* @param string number of digits after the decimal point
* @return void
*/
public function __construct($caption = NULL, $precision = 2)
{
parent::__construct($caption);
$this->precision = $precision;
}
/**
* Formats cell's content.
* @param mixed
* @param \DibiRow|array
* @return string
*/
public function formatContent($value, $data = NULL)
{
if (is_array($this->replacement) && !empty($this->replacement)) {
if (in_array($value, array_keys($this->replacement))) {
$value = $this->replacement[$value];
}
}
foreach ($this->formatCallback as $callback) {
if (is_callable($callback)) {
$value = call_user_func($callback, $value, $data);
}
}
return round($value, $this->precision);
}
/**
* Filter data source
*
* @param mixed
* @return void
*/
public function applyFilter($value)
{
if (!$this->hasFilter()) return;
$dataGrid = $this->getDataGrid(TRUE);
if ($value === 'NULL' || $value === 'NOT NULL') {
$dataGrid->getDataSource()->filter($this->name, "IS $value");
} else {
$operator = '=';
$v = str_replace(',', '.', $value);
if (preg_match('/^(?<operator>\>|\>\=|\<|\<\=|\=|\<\>)?(?<value>[\.|\d]+)$/', $v, $matches)) {
if (isset($matches['operator']) && !empty($matches['operator'])) {
$operator = $matches['operator'];
}
$value = $matches['value'];
}
$dataGrid->getDataSource()->filter($this->name, $operator, (float) $value); //or skip converting?
}
}
}