/
FilePicker.php
113 lines (101 loc) · 3.49 KB
/
FilePicker.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
<?php
namespace Concrete\Core\Form\Service\DestinationPicker;
use ArrayAccess;
use Concrete\Core\Application\Service\FileManager;
use Concrete\Core\Entity\File\File;
use Concrete\Core\Utility\Service\Validation\Numbers;
use Doctrine\ORM\EntityManagerInterface;
/**
* A picker for DestinationPicker that allows users specify a concrete5 file.
*
* Supported options for the generate method:
* - displayName: the display name of this picker (to be used in the SELECT html element).
* - chooseFileText: the text to be used for the "Choose File" button
* - any other option will be passed to the concrete5 File Manager picker (for example: 'filters' may be useful - @see \Concrete\Core\Application\Service\FileManager::file() )
*
* Supported options for the decode method:
* - none
*/
class FilePicker implements PickerInterface
{
/**
* @var \Concrete\Core\Application\Service\FileManager
*/
protected $fileManager;
/**
* @var \Concrete\Core\Utility\Service\Validation\Numbers
*/
protected $numbers;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $entityManager;
public function __construct(FileManager $fileManager, Numbers $numbers, EntityManagerInterface $entityManager)
{
$this->fileManager = $fileManager;
$this->numbers = $numbers;
$this->entityManager = $entityManager;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Form\Service\DestinationPicker\PickerInterface::getDisplayName()
*/
public function getDisplayName(array $options)
{
return empty($options['displayName']) ? t('File') : $options['displayName'];
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Form\Service\DestinationPicker\PickerInterface::getHeight()
*/
public function getHeight()
{
return 60;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Form\Service\DestinationPicker\PickerInterface::generate()
*/
public function generate($pickerKey, array $options, $selectedValue = null)
{
if (is_object($selectedValue)) {
$fileID = (int) $selectedValue->getFileID();
} elseif (empty($selectedValue)) {
$fileID = 0;
} else {
$fileID = (int) $selectedValue;
}
$args = $options;
unset($args['displayName']);
unset($args['chooseFileText']);
return $this->fileManager->file($pickerKey, $pickerKey, empty($options['chooseFileText']) ? t('Choose File') : $options['chooseFileText'], $fileID, $args);
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Form\Service\DestinationPicker\PickerInterface::decode()
*/
public function decode(array $data, $pickerKey, array $options, ArrayAccess $errors = null, $fieldDisplayName = null)
{
$result = null;
$postValue = array_get($data, $pickerKey);
if ($this->numbers->integer($postValue, 1)) {
$postValue = (int) $postValue;
if ($this->entityManager->find(File::class, $postValue) === null) {
if ($errors !== null) {
if ((string) $fieldDisplayName === '') {
$errors[] = t('Unable to find the specified file.');
} else {
$errors[] = t('Unable to find the file specified for %s.', $fieldDisplayName);
}
}
} else {
$result = $postValue;
}
}
return $result;
}
}