/
DefaultController.php
134 lines (114 loc) · 3.63 KB
/
DefaultController.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
<?php
namespace Concrete\Core\Attribute;
use Concrete\Core\Attribute\Controller as AttributeTypeController;
use Concrete\Core\Entity\Attribute\Key\Settings\TextSettings;
use Concrete\Core\Entity\Attribute\Value\Value\TextValue;
use Concrete\Core\Entity\Attribute\Value\Value\Value;
use Concrete\Core\Error\ErrorList\ErrorList;
use Core;
class DefaultController extends AttributeTypeController implements SimpleTextExportableAttributeInterface, FilterableByValueInterface
{
protected $searchIndexFieldDefinition = [
'type' => 'text',
'options' => ['default' => null, 'notnull' => false],
];
public function form()
{
$value = '';
if (is_object($this->attributeValue)) {
$value = Core::make('helper/text')->entities($this->getAttributeValue()->getValue());
}
echo Core::make('helper/form')->textarea($this->field('value'), $value);
}
public function searchForm($list)
{
if ($this->request('value') === '') {
return $list;
}
$list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), '%' . $this->request('value') . '%',
'like');
return $list;
}
public function getDisplayValue()
{
return Core::make('helper/text')->entities($this->attributeValue->getValue());
}
public function getAttributeValueClass()
{
return TextValue::class;
}
/**
* @param \Concrete\Core\Search\ItemList\ItemList $list
* @param TextValue $value
* @return mixed|void
*/
public function filterByExactValue($list, $value)
{
$list->filterByAttribute(
$this->attributeKey->getAttributeKeyHandle(),
$value->getValue(),
'='
);
}
public function search()
{
$f = Core::make('helper/form');
echo $f->text($this->field('value'), $this->request('value'));
}
// run when we call setAttribute(), instead of saving through the UI
public function createAttributeValue($value)
{
$av = new TextValue();
$av->setValue($value);
return $av;
}
public function getAttributeKeySettingsClass()
{
return TextSettings::class;
}
public function createAttributeValueFromRequest()
{
$data = $this->post();
return $this->createAttributeValue(isset($data['value']) ? $data['value'] : null);
}
public function validateValue()
{
return $this->attributeValue->getValue() != '';
}
public function validateForm($data)
{
return isset($data['value']) && $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();
if ($value === null) {
if ($textRepresentation !== '') {
$value = $this->createAttributeValue($textRepresentation);
}
} else {
$value->setValue($textRepresentation);
}
return $value;
}
}