-
Notifications
You must be signed in to change notification settings - Fork 454
/
controller.php
145 lines (124 loc) · 4.13 KB
/
controller.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Concrete\Attribute\Number;
use Concrete\Core\Attribute\Controller as AttributeTypeController;
use Concrete\Core\Attribute\FontAwesomeIconFormatter;
use Concrete\Core\Attribute\SimpleTextExportableAttributeInterface;
use Concrete\Core\Entity\Attribute\Value\Value\NumberValue;
use Concrete\Core\Error\ErrorList\ErrorList;
class Controller extends AttributeTypeController implements SimpleTextExportableAttributeInterface
{
protected $searchIndexFieldDefinition = [
'type' => 'decimal',
'options' => ['precision' => 14, 'scale' => 4, 'default' => 0, 'notnull' => false],
];
public function getIconFormatter()
{
return new FontAwesomeIconFormatter('hashtag');
}
/**
* {@inheritDoc}
*
* @see \Concrete\Core\Attribute\AttributeInterface::getDisplayValue()
*/
public function getDisplayValue()
{
$value = $this->attributeValue->getValue();
return $value === null ? '' : (float) $value;
}
public function getAttributeValueClass()
{
return NumberValue::class;
}
public function searchForm($list)
{
$numFrom = (int) ($this->request('from'));
$numTo = (int) ($this->request('to'));
if ($numFrom) {
$list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $numFrom, '>=');
}
if ($numTo) {
$list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $numTo, '<=');
}
return $list;
}
public function search()
{
$f = $this->app->make('helper/form');
$html = $f->number($this->field('from'), $this->request('from'));
$html .= ' ' . t('to') . ' ';
$html .= $f->number($this->field('to'), $this->request('to'));
echo $html;
}
public function form()
{
if (is_object($this->attributeValue)) {
$value = $this->getAttributeValue()->getValue();
} else {
$value = null;
}
$this->set('form', $this->app->make('helper/form'));
$this->set('value', $value);
}
public function validateForm($p)
{
return $p['value'] != false;
}
public function validateValue()
{
$val = $this->getAttributeValue()->getValue();
return $val !== null && $val !== false;
}
// run when we call setAttribute(), instead of saving through the UI
public function createAttributeValue($value)
{
$av = new NumberValue();
$av->setValue($value);
return $av;
}
public function createAttributeValueFromRequest()
{
$data = $this->post();
if (isset($data['value'])) {
return $this->createAttributeValue($data['value']);
}
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\SimpleTextExportableAttributeInterface::getAttributeValueTextRepresentation()
*/
public function getAttributeValueTextRepresentation()
{
$value = $this->getAttributeValueObject();
if ($value === null) {
$result = '';
} else {
$result = (string) $value->getValue();
}
return $result;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\SimpleTextExportableAttributeInterface::updateAttributeValueFromTextRepresentation()
*/
public function updateAttributeValueFromTextRepresentation($textRepresentation, ErrorList $warnings)
{
$value = $this->getAttributeValueObject();
$textRepresentation = trim($textRepresentation);
if ($textRepresentation === '') {
if ($value !== null) {
$value->setValue(null);
}
} elseif (is_numeric($textRepresentation)) {
if ($value === null) {
$value = $this->createAttributeValue($textRepresentation);
} else {
$value->setValue($textRepresentation);
}
} else {
$warnings->add(t('"%1$s" is not a valid number for the attribute with handle %2$s', $textRepresentation, $this->attributeKey->getAttributeKeyHandle()));
}
return $value;
}
}