/
controller.php
67 lines (59 loc) · 2.2 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
<?php
namespace Concrete\Attribute\Email;
use Concrete\Core\Attribute\DefaultController;
use Concrete\Core\Attribute\FontAwesomeIconFormatter;
use Concrete\Core\Error\ErrorList\Error\Error;
use Concrete\Core\Error\ErrorList\Error\FieldNotPresentError;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\Error\ErrorList\Field\AttributeField;
use Concrete\Core\Validator\String\EmailValidator;
class Controller extends DefaultController
{
public $helpers = ['form'];
public function form()
{
$value = null;
if (is_object($this->attributeValue)) {
$value = $this->app->make('helper/text')->entities($this->getAttributeValue()->getValue());
}
$this->set('value', $value);
}
public function getIconFormatter()
{
return new FontAwesomeIconFormatter('envelope');
}
public function validateForm($data)
{
if (!$data['value']) {
return new FieldNotPresentError(new AttributeField($this->getAttributeKey()));
} else {
$e = $this->app->make('error');
if (!$this->app->make(EmailValidator::class)->isValid($data['value'], $e)) {
return new Error($e->toText(), new AttributeField($this->getAttributeKey()));
} else {
return true;
}
}
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\SimpleTextExportableAttributeInterface::updateAttributeValueFromTextRepresentation()
*/
public function updateAttributeValueFromTextRepresentation($textRepresentation, ErrorList $warnings)
{
$good = true;
if ($textRepresentation !== '') {
if (!$this->app->make(EmailValidator::class)->isValid($textRepresentation)) {
$good = false;
$warnings->add(t('"%1$s" is not a valid email address for the attribute with handle %2$s', $textRepresentation, $this->attributeKey->getAttributeKeyHandle()));
}
}
if ($good) {
$value = parent::updateAttributeValueFromTextRepresentation($textRepresentation, $warnings);
} else {
$value = $this->getAttributeValueObject();
}
return $value;
}
}